blob: 04ed1a1ae0198f1f82368cc73b7811826fe33cfb [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Steffen Klassertb8c203b2014-09-16 10:08:49 +020042struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
Priyanka Jain418a99a2012-08-12 21:22:29 +000047static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +010048static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +000049 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *xfrm_dst_cache __read_mostly;
Florian Westphal30846092016-08-11 15:17:54 +020052static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Herbert Xu25ee3282007-12-11 09:32:34 -080054static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000055static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000056static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010057static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Herbert Xu12bfa8b2014-11-13 17:09:50 +080059static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080060static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
Florian Westphale37cc8a2016-08-11 15:17:55 +020063static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
64{
65 return atomic_inc_not_zero(&policy->refcnt);
66}
67
David S. Millerbc9b35a2012-05-15 15:04:57 -040068static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050069__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080070{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050071 const struct flowi4 *fl4 = &fl->u.ip4;
72
Alexey Dobriyan26bff942011-11-22 06:46:02 +000073 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
74 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050075 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
76 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
77 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
78 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080079}
80
David S. Millerbc9b35a2012-05-15 15:04:57 -040081static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050082__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080083{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050084 const struct flowi6 *fl6 = &fl->u.ip6;
85
86 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
87 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
88 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
89 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
90 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
91 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080092}
93
David S. Millerbc9b35a2012-05-15 15:04:57 -040094bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
95 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080096{
97 switch (family) {
98 case AF_INET:
99 return __xfrm4_selector_match(sel, fl);
100 case AF_INET6:
101 return __xfrm6_selector_match(sel, fl);
102 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400103 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800104}
105
Florian Westphala2817d82017-02-07 15:00:17 +0100106static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200107{
Florian Westphala2817d82017-02-07 15:00:17 +0100108 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200109
Florian Westphala2817d82017-02-07 15:00:17 +0100110 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200111 return NULL;
112 rcu_read_lock();
113 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
114 if (unlikely(!afinfo))
115 rcu_read_unlock();
116 return afinfo;
117}
118
David Ahern42a7b322015-08-10 16:58:11 -0600119static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
120 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500121 const xfrm_address_t *saddr,
122 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900123 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Florian Westphal37b10382017-02-07 15:00:19 +0100125 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800126 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Herbert Xu25ee3282007-12-11 09:32:34 -0800128 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800130 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Ahern42a7b322015-08-10 16:58:11 -0600132 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900133
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100134 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900135
136 return dst;
137}
138
David Ahern42a7b322015-08-10 16:58:11 -0600139static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
140 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900141 xfrm_address_t *prev_saddr,
142 xfrm_address_t *prev_daddr,
143 int family)
144{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800145 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900146 xfrm_address_t *saddr = &x->props.saddr;
147 xfrm_address_t *daddr = &x->id.daddr;
148 struct dst_entry *dst;
149
150 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
151 saddr = x->coaddr;
152 daddr = prev_daddr;
153 }
154 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
155 saddr = prev_saddr;
156 daddr = x->coaddr;
157 }
158
David Ahern42a7b322015-08-10 16:58:11 -0600159 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900160
161 if (!IS_ERR(dst)) {
162 if (prev_saddr != saddr)
163 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
164 if (prev_daddr != daddr)
165 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
166 }
167
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800168 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static inline unsigned long make_jiffies(long secs)
172{
173 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
174 return MAX_SCHEDULE_TIMEOUT-1;
175 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900176 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static void xfrm_policy_timer(unsigned long data)
180{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800181 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800182 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 long next = LONG_MAX;
184 int warn = 0;
185 int dir;
186
187 read_lock(&xp->lock);
188
Timo Teräsea2dea92010-03-31 00:17:05 +0000189 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto out;
191
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700192 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (xp->lft.hard_add_expires_seconds) {
195 long tmo = xp->lft.hard_add_expires_seconds +
196 xp->curlft.add_time - now;
197 if (tmo <= 0)
198 goto expired;
199 if (tmo < next)
200 next = tmo;
201 }
202 if (xp->lft.hard_use_expires_seconds) {
203 long tmo = xp->lft.hard_use_expires_seconds +
204 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
205 if (tmo <= 0)
206 goto expired;
207 if (tmo < next)
208 next = tmo;
209 }
210 if (xp->lft.soft_add_expires_seconds) {
211 long tmo = xp->lft.soft_add_expires_seconds +
212 xp->curlft.add_time - now;
213 if (tmo <= 0) {
214 warn = 1;
215 tmo = XFRM_KM_TIMEOUT;
216 }
217 if (tmo < next)
218 next = tmo;
219 }
220 if (xp->lft.soft_use_expires_seconds) {
221 long tmo = xp->lft.soft_use_expires_seconds +
222 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
223 if (tmo <= 0) {
224 warn = 1;
225 tmo = XFRM_KM_TIMEOUT;
226 }
227 if (tmo < next)
228 next = tmo;
229 }
230
231 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800232 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (next != LONG_MAX &&
234 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
235 xfrm_pol_hold(xp);
236
237out:
238 read_unlock(&xp->lock);
239 xfrm_pol_put(xp);
240 return;
241
242expired:
243 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700244 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800245 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfrm_pol_put(xp);
247}
248
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000249static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
250{
251 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
252
253 if (unlikely(pol->walk.dead))
254 flo = NULL;
255 else
256 xfrm_pol_hold(pol);
257
258 return flo;
259}
260
261static int xfrm_policy_flo_check(struct flow_cache_object *flo)
262{
263 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
264
265 return !pol->walk.dead;
266}
267
268static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
269{
270 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
271}
272
273static const struct flow_cache_ops xfrm_policy_fc_ops = {
274 .get = xfrm_policy_flo_get,
275 .check = xfrm_policy_flo_check,
276 .delete = xfrm_policy_flo_delete,
277};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
280 * SPD calls.
281 */
282
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800283struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct xfrm_policy *policy;
286
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700287 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800290 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700291 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700292 INIT_HLIST_NODE(&policy->bydst);
293 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700295 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100296 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800297 setup_timer(&policy->timer, xfrm_policy_timer,
298 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100299 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
300 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000301 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 return policy;
304}
305EXPORT_SYMBOL(xfrm_policy_alloc);
306
Eric Dumazet56f04732015-12-08 07:22:01 -0800307static void xfrm_policy_destroy_rcu(struct rcu_head *head)
308{
309 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
310
311 security_xfrm_policy_free(policy->security);
312 kfree(policy);
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* Destroy xfrm_policy: descendant resources must be released to this moment. */
316
WANG Cong64c31b32008-01-07 22:34:29 -0800317void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Herbert Xu12a169e2008-10-01 07:03:24 -0700319 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Fan Du0659eea2013-08-01 18:08:36 +0800321 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 BUG();
323
Eric Dumazet56f04732015-12-08 07:22:01 -0800324 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
WANG Cong64c31b32008-01-07 22:34:29 -0800326EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100328/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * entry dead. The rule must be unlinked from lists to the moment.
330 */
331
332static void xfrm_policy_kill(struct xfrm_policy *policy)
333{
Herbert Xu12a169e2008-10-01 07:03:24 -0700334 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Timo Teräs285ead12010-04-07 00:30:06 +0000336 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200338 if (del_timer(&policy->polq.hold_timer))
339 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800340 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100341
Timo Teräs285ead12010-04-07 00:30:06 +0000342 if (del_timer(&policy->timer))
343 xfrm_pol_put(policy);
344
345 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
David S. Miller2518c7c2006-08-24 04:45:07 -0700348static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
349
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800350static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700351{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800352 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700353}
354
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200355/* calculate policy hash thresholds */
356static void __get_hash_thresh(struct net *net,
357 unsigned short family, int dir,
358 u8 *dbits, u8 *sbits)
359{
360 switch (family) {
361 case AF_INET:
362 *dbits = net->xfrm.policy_bydst[dir].dbits4;
363 *sbits = net->xfrm.policy_bydst[dir].sbits4;
364 break;
365
366 case AF_INET6:
367 *dbits = net->xfrm.policy_bydst[dir].dbits6;
368 *sbits = net->xfrm.policy_bydst[dir].sbits6;
369 break;
370
371 default:
372 *dbits = 0;
373 *sbits = 0;
374 }
375}
376
David S. Miller5f803b52011-02-24 00:33:19 -0500377static struct hlist_head *policy_hash_bysel(struct net *net,
378 const struct xfrm_selector *sel,
379 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700380{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800381 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200382 unsigned int hash;
383 u8 dbits;
384 u8 sbits;
385
386 __get_hash_thresh(net, family, dir, &dbits, &sbits);
387 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700388
Florian Westphale1e551b2016-08-11 15:17:53 +0200389 if (hash == hmask + 1)
390 return &net->xfrm.policy_inexact[dir];
391
392 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
393 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700394}
395
David S. Miller5f803b52011-02-24 00:33:19 -0500396static struct hlist_head *policy_hash_direct(struct net *net,
397 const xfrm_address_t *daddr,
398 const xfrm_address_t *saddr,
399 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700400{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800401 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200402 unsigned int hash;
403 u8 dbits;
404 u8 sbits;
405
406 __get_hash_thresh(net, family, dir, &dbits, &sbits);
407 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700408
Florian Westphale1e551b2016-08-11 15:17:53 +0200409 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
410 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700411}
412
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200413static void xfrm_dst_hash_transfer(struct net *net,
414 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700415 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200416 unsigned int nhashmask,
417 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700418{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800419 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200422 u8 dbits;
423 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700424
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800425redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800426 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700427 unsigned int h;
428
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200429 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700430 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200431 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800432 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200433 hlist_del_rcu(&pol->bydst);
434 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800435 h0 = h;
436 } else {
437 if (h != h0)
438 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200439 hlist_del_rcu(&pol->bydst);
440 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800441 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800442 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800443 }
444 if (!hlist_empty(list)) {
445 entry0 = NULL;
446 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700447 }
448}
449
450static void xfrm_idx_hash_transfer(struct hlist_head *list,
451 struct hlist_head *nidxtable,
452 unsigned int nhashmask)
453{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800454 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700455 struct xfrm_policy *pol;
456
Sasha Levinb67bfe02013-02-27 17:06:00 -0800457 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700458 unsigned int h;
459
460 h = __idx_hash(pol->index, nhashmask);
461 hlist_add_head(&pol->byidx, nidxtable+h);
462 }
463}
464
465static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
466{
467 return ((old_hmask + 1) << 1) - 1;
468}
469
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800470static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700471{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800472 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700473 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
474 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700475 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200476 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700477 int i;
478
479 if (!ndst)
480 return;
481
Florian Westphal9d0380d2016-08-11 15:17:59 +0200482 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200483 write_seqcount_begin(&xfrm_policy_hash_generation);
484
485 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
486 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700487
Florian Westphale1e551b2016-08-11 15:17:53 +0200488 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
489 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
490
David S. Miller2518c7c2006-08-24 04:45:07 -0700491 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200492 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700493
Florian Westphale1e551b2016-08-11 15:17:53 +0200494 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800495 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700496
Florian Westphal30846092016-08-11 15:17:54 +0200497 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200498 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700499
Florian Westphale1e551b2016-08-11 15:17:53 +0200500 synchronize_rcu();
501
David S. Miller44e36b42006-08-24 04:50:50 -0700502 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700503}
504
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800505static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700506{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800507 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700508 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
509 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700511 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512 int i;
513
514 if (!nidx)
515 return;
516
Florian Westphal9d0380d2016-08-11 15:17:59 +0200517 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700518
519 for (i = hmask; i >= 0; i--)
520 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
521
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800522 net->xfrm.policy_byidx = nidx;
523 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700524
Florian Westphal9d0380d2016-08-11 15:17:59 +0200525 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700526
David S. Miller44e36b42006-08-24 04:50:50 -0700527 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700528}
529
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800530static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700531{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800532 unsigned int cnt = net->xfrm.policy_count[dir];
533 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700534
535 if (total)
536 *total += cnt;
537
538 if ((hmask + 1) < xfrm_policy_hashmax &&
539 cnt > hmask)
540 return 1;
541
542 return 0;
543}
544
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800545static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700546{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800547 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700548
549 if ((hmask + 1) < xfrm_policy_hashmax &&
550 total > hmask)
551 return 1;
552
553 return 0;
554}
555
Alexey Dobriyane0710412010-01-23 13:37:10 +0000556void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700557{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000558 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
559 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
560 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
561 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
562 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
563 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
564 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700565 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700566}
567EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700568
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700569static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800570static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700571{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800572 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700573 int dir, total;
574
575 mutex_lock(&hash_resize_mutex);
576
577 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800578 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800579 if (xfrm_bydst_should_resize(net, dir, &total))
580 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700581 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800582 if (xfrm_byidx_should_resize(net, total))
583 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700584
585 mutex_unlock(&hash_resize_mutex);
586}
587
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200588static void xfrm_hash_rebuild(struct work_struct *work)
589{
590 struct net *net = container_of(work, struct net,
591 xfrm.policy_hthresh.work);
592 unsigned int hmask;
593 struct xfrm_policy *pol;
594 struct xfrm_policy *policy;
595 struct hlist_head *chain;
596 struct hlist_head *odst;
597 struct hlist_node *newpos;
598 int i;
599 int dir;
600 unsigned seq;
601 u8 lbits4, rbits4, lbits6, rbits6;
602
603 mutex_lock(&hash_resize_mutex);
604
605 /* read selector prefixlen thresholds */
606 do {
607 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
608
609 lbits4 = net->xfrm.policy_hthresh.lbits4;
610 rbits4 = net->xfrm.policy_hthresh.rbits4;
611 lbits6 = net->xfrm.policy_hthresh.lbits6;
612 rbits6 = net->xfrm.policy_hthresh.rbits6;
613 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
614
Florian Westphal9d0380d2016-08-11 15:17:59 +0200615 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200616
617 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800618 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200619 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
620 hmask = net->xfrm.policy_bydst[dir].hmask;
621 odst = net->xfrm.policy_bydst[dir].table;
622 for (i = hmask; i >= 0; i--)
623 INIT_HLIST_HEAD(odst + i);
624 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
625 /* dir out => dst = remote, src = local */
626 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
627 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
628 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
629 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
630 } else {
631 /* dir in/fwd => dst = local, src = remote */
632 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
633 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
634 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
635 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
636 }
637 }
638
639 /* re-insert all policies by order of creation */
640 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200641 if (xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
642 /* skip socket policies */
643 continue;
644 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200645 newpos = NULL;
646 chain = policy_hash_bysel(net, &policy->selector,
647 policy->family,
648 xfrm_policy_id2dir(policy->index));
649 hlist_for_each_entry(pol, chain, bydst) {
650 if (policy->priority >= pol->priority)
651 newpos = &pol->bydst;
652 else
653 break;
654 }
655 if (newpos)
656 hlist_add_behind(&policy->bydst, newpos);
657 else
658 hlist_add_head(&policy->bydst, chain);
659 }
660
Florian Westphal9d0380d2016-08-11 15:17:59 +0200661 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200662
663 mutex_unlock(&hash_resize_mutex);
664}
665
666void xfrm_policy_hash_rebuild(struct net *net)
667{
668 schedule_work(&net->xfrm.policy_hthresh.work);
669}
670EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/* Generate new index... KAME seems to generate them ordered by cost
673 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800674static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 static u32 idx_generator;
677
678 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 struct hlist_head *list;
680 struct xfrm_policy *p;
681 u32 idx;
682 int found;
683
Fan Due682adf2013-11-07 17:47:48 +0800684 if (!index) {
685 idx = (idx_generator | dir);
686 idx_generator += 8;
687 } else {
688 idx = index;
689 index = 0;
690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (idx == 0)
693 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800694 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700695 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800696 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700697 if (p->index == idx) {
698 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700702 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return idx;
704 }
705}
706
David S. Miller2518c7c2006-08-24 04:45:07 -0700707static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
708{
709 u32 *p1 = (u32 *) s1;
710 u32 *p2 = (u32 *) s2;
711 int len = sizeof(struct xfrm_selector) / sizeof(u32);
712 int i;
713
714 for (i = 0; i < len; i++) {
715 if (p1[i] != p2[i])
716 return 1;
717 }
718
719 return 0;
720}
721
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100722static void xfrm_policy_requeue(struct xfrm_policy *old,
723 struct xfrm_policy *new)
724{
725 struct xfrm_policy_queue *pq = &old->polq;
726 struct sk_buff_head list;
727
Li RongQingde2ad482015-04-30 17:25:19 +0800728 if (skb_queue_empty(&pq->hold_queue))
729 return;
730
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100731 __skb_queue_head_init(&list);
732
733 spin_lock_bh(&pq->hold_queue.lock);
734 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200735 if (del_timer(&pq->hold_timer))
736 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100737 spin_unlock_bh(&pq->hold_queue.lock);
738
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100739 pq = &new->polq;
740
741 spin_lock_bh(&pq->hold_queue.lock);
742 skb_queue_splice(&list, &pq->hold_queue);
743 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200744 if (!mod_timer(&pq->hold_timer, jiffies))
745 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100746 spin_unlock_bh(&pq->hold_queue.lock);
747}
748
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100749static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
750 struct xfrm_policy *pol)
751{
752 u32 mark = policy->mark.v & policy->mark.m;
753
754 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
755 return true;
756
757 if ((mark & pol->mark.m) == pol->mark.v &&
758 policy->priority == pol->priority)
759 return true;
760
761 return false;
762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
765{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800766 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 struct xfrm_policy *pol;
768 struct xfrm_policy *delpol;
769 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800770 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Florian Westphal9d0380d2016-08-11 15:17:59 +0200772 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800773 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700774 delpol = NULL;
775 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800776 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800777 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700778 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100779 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800780 xfrm_sec_ctx_match(pol->security, policy->security) &&
781 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200783 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EEXIST;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 delpol = pol;
787 if (policy->priority > pol->priority)
788 continue;
789 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800790 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 continue;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (delpol)
794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700797 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700798 else
799 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800800 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800801 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800802
803 /* After previous checking, family can either be AF_INET or AF_INET6 */
804 if (policy->family == AF_INET)
805 rt_genid_bump_ipv4(net);
806 else
807 rt_genid_bump_ipv6(net);
808
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100809 if (delpol) {
810 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800811 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100812 }
Fan Due682adf2013-11-07 17:47:48 +0800813 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800814 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800815 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 policy->curlft.use_time = 0;
817 if (!mod_timer(&policy->timer, jiffies + HZ))
818 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200819 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David S. Miller9b78a822005-12-22 07:39:48 -0800821 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800823 else if (xfrm_bydst_should_resize(net, dir, NULL))
824 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827}
828EXPORT_SYMBOL(xfrm_policy_insert);
829
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000830struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
831 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800832 struct xfrm_sec_ctx *ctx, int delete,
833 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
David S. Miller2518c7c2006-08-24 04:45:07 -0700835 struct xfrm_policy *pol, *ret;
836 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Eric Parisef41aaa2007-03-07 15:37:58 -0800838 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200839 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800840 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800842 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700843 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000844 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700845 !selector_cmp(sel, &pol->selector) &&
846 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700849 *err = security_xfrm_policy_delete(
850 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800851 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200852 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800853 return pol;
854 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800855 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 }
857 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 }
860 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200861 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000863 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700865 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
Trent Jaegerdf718372005-12-13 23:12:27 -0800867EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000869struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
870 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
David S. Miller2518c7c2006-08-24 04:45:07 -0700872 struct xfrm_policy *pol, *ret;
873 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Herbert Xub5505c62007-05-14 02:15:47 -0700875 *err = -ENOENT;
876 if (xfrm_policy_id2dir(id) != dir)
877 return NULL;
878
Eric Parisef41aaa2007-03-07 15:37:58 -0800879 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200880 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800881 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700882 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800883 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000884 if (pol->type == type && pol->index == id &&
885 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700887 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700888 *err = security_xfrm_policy_delete(
889 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800890 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200891 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800892 return pol;
893 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800894 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700895 }
896 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 }
899 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200900 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000902 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700903 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700904 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906EXPORT_SYMBOL(xfrm_policy_byid);
907
Joy Latten4aa2e622007-06-04 19:05:57 -0400908#ifdef CONFIG_SECURITY_NETWORK_XFRM
909static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900910xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Joy Latten4aa2e622007-06-04 19:05:57 -0400912 int dir, err = 0;
913
914 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
915 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400916 int i;
917
Sasha Levinb67bfe02013-02-27 17:06:00 -0800918 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800919 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400920 if (pol->type != type)
921 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700922 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400923 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900924 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400925 return err;
926 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900927 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800928 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800929 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800930 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400931 bydst) {
932 if (pol->type != type)
933 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700934 err = security_xfrm_policy_delete(
935 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400936 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700937 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900938 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400939 return err;
940 }
941 }
942 }
943 }
944 return err;
945}
946#else
947static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900948xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400949{
950 return 0;
951}
952#endif
953
Tetsuo Handa2e710292014-04-22 21:48:30 +0900954int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400955{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000956 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Florian Westphal9d0380d2016-08-11 15:17:59 +0200958 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400959
Tetsuo Handa2e710292014-04-22 21:48:30 +0900960 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400961 if (err)
962 goto out;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700965 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800966 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700967
968 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800969 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800970 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700971 if (pol->type != type)
972 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000973 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200974 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000975 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Tetsuo Handa2e710292014-04-22 21:48:30 +0900977 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600978
David S. Miller2518c7c2006-08-24 04:45:07 -0700979 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Steffen Klassert4141b362016-08-24 13:08:40 +0200981 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700982 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700984
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800985 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700986 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800987 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800988 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700989 bydst) {
990 if (pol->type != type)
991 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000992 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200993 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000994 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700995
Tetsuo Handa2e710292014-04-22 21:48:30 +0900996 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700997 xfrm_policy_kill(pol);
998
Florian Westphal9d0380d2016-08-11 15:17:59 +0200999 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001000 goto again2;
1001 }
1002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001005 if (!cnt)
1006 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001007out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001008 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011EXPORT_SYMBOL(xfrm_policy_flush);
1012
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001013int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001014 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 void *data)
1016{
Herbert Xu12a169e2008-10-01 07:03:24 -07001017 struct xfrm_policy *pol;
1018 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001019 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Timo Teras4c563f72008-02-28 21:31:08 -08001021 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1022 walk->type != XFRM_POLICY_TYPE_ANY)
1023 return -EINVAL;
1024
Herbert Xu12a169e2008-10-01 07:03:24 -07001025 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001026 return 0;
1027
Florian Westphal9d0380d2016-08-11 15:17:59 +02001028 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001029 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001030 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001031 else
Li RongQing80077702015-04-22 17:09:54 +08001032 x = list_first_entry(&walk->walk.all,
1033 struct xfrm_policy_walk_entry, all);
1034
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001035 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001036 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001037 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001038 pol = container_of(x, struct xfrm_policy, walk);
1039 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1040 walk->type != pol->type)
1041 continue;
1042 error = func(pol, xfrm_policy_id2dir(pol->index),
1043 walk->seq, data);
1044 if (error) {
1045 list_move_tail(&walk->walk.all, &x->all);
1046 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001047 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001048 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001050 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001051 error = -ENOENT;
1052 goto out;
1053 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001054 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001056 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return error;
1058}
1059EXPORT_SYMBOL(xfrm_policy_walk);
1060
Herbert Xu12a169e2008-10-01 07:03:24 -07001061void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1062{
1063 INIT_LIST_HEAD(&walk->walk.all);
1064 walk->walk.dead = 1;
1065 walk->type = type;
1066 walk->seq = 0;
1067}
1068EXPORT_SYMBOL(xfrm_policy_walk_init);
1069
Fan Du283bc9f2013-11-07 17:47:50 +08001070void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001071{
1072 if (list_empty(&walk->walk.all))
1073 return;
1074
Florian Westphal9d0380d2016-08-11 15:17:59 +02001075 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001076 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001077 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001078}
1079EXPORT_SYMBOL(xfrm_policy_walk_done);
1080
James Morris134b0fc2006-10-05 15:42:27 -05001081/*
1082 * Find policy to apply to this flow.
1083 *
1084 * Returns 0 if policy found, else an -errno.
1085 */
David S. Millerf299d552011-02-24 01:23:30 -05001086static int xfrm_policy_match(const struct xfrm_policy *pol,
1087 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001088 u8 type, u16 family, int dir)
1089{
David S. Millerf299d552011-02-24 01:23:30 -05001090 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001091 int ret = -ESRCH;
1092 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001093
1094 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001095 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001096 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001097 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001098
1099 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001100 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001101 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001102 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001103
James Morris134b0fc2006-10-05 15:42:27 -05001104 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001105}
1106
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001107static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001108 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001109 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
James Morris134b0fc2006-10-05 15:42:27 -05001111 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001112 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001113 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001114 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001115 unsigned int sequence;
1116 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001117
1118 daddr = xfrm_flowi_daddr(fl, family);
1119 saddr = xfrm_flowi_saddr(fl, family);
1120 if (unlikely(!daddr || !saddr))
1121 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Florian Westphala7c442472016-08-11 15:17:56 +02001123 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001124 retry:
1125 do {
1126 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1127 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1128 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1129
1130 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001131 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001132 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001133 err = xfrm_policy_match(pol, fl, type, family, dir);
1134 if (err) {
1135 if (err == -ESRCH)
1136 continue;
1137 else {
1138 ret = ERR_PTR(err);
1139 goto fail;
1140 }
1141 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001142 ret = pol;
1143 priority = ret->priority;
1144 break;
1145 }
1146 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001147 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001148 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001149 if ((pol->priority >= priority) && ret)
1150 break;
1151
James Morris134b0fc2006-10-05 15:42:27 -05001152 err = xfrm_policy_match(pol, fl, type, family, dir);
1153 if (err) {
1154 if (err == -ESRCH)
1155 continue;
1156 else {
1157 ret = ERR_PTR(err);
1158 goto fail;
1159 }
Li RongQing8faf4912015-05-14 11:16:59 +08001160 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001161 ret = pol;
1162 break;
1163 }
1164 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001165
Florian Westphal30846092016-08-11 15:17:54 +02001166 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1167 goto retry;
1168
Florian Westphale37cc8a2016-08-11 15:17:55 +02001169 if (ret && !xfrm_pol_hold_rcu(ret))
1170 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001171fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001172 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001173
David S. Miller2518c7c2006-08-24 04:45:07 -07001174 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001175}
1176
Timo Teräs80c802f2010-04-07 00:30:05 +00001177static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001178__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001179{
1180#ifdef CONFIG_XFRM_SUB_POLICY
1181 struct xfrm_policy *pol;
1182
1183 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1184 if (pol != NULL)
1185 return pol;
1186#endif
1187 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1188}
1189
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001190static int flow_to_policy_dir(int dir)
1191{
1192 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1193 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1194 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1195 return dir;
1196
1197 switch (dir) {
1198 default:
1199 case FLOW_DIR_IN:
1200 return XFRM_POLICY_IN;
1201 case FLOW_DIR_OUT:
1202 return XFRM_POLICY_OUT;
1203 case FLOW_DIR_FWD:
1204 return XFRM_POLICY_FWD;
1205 }
1206}
1207
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001208static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001209xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001210 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001211{
1212 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001213
1214 if (old_obj)
1215 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001216
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001217 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001218 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001219 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001220
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001221 /* Resolver returns two references:
1222 * one for cache and one for caller of flow_cache_lookup() */
1223 xfrm_pol_hold(pol);
1224
1225 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Trent Jaegerdf718372005-12-13 23:12:27 -08001228static inline int policy_to_flow_dir(int dir)
1229{
1230 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001231 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1232 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1233 return dir;
1234 switch (dir) {
1235 default:
1236 case XFRM_POLICY_IN:
1237 return FLOW_DIR_IN;
1238 case XFRM_POLICY_OUT:
1239 return FLOW_DIR_OUT;
1240 case XFRM_POLICY_FWD:
1241 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001242 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001243}
1244
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001245static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001246 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct xfrm_policy *pol;
1249
Eric Dumazetd188ba82015-12-08 07:22:02 -08001250 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001251 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001252 pol = rcu_dereference(sk->sk_policy[dir]);
1253 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001254 bool match = xfrm_selector_match(&pol->selector, fl,
1255 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001256 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001257
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001258 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001259 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1260 pol = NULL;
1261 goto out;
1262 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001263 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001264 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001265 policy_to_flow_dir(dir));
Florian Westphal330e8322016-11-17 13:21:46 +01001266 if (!err) {
1267 if (!xfrm_pol_hold_rcu(pol))
1268 goto again;
1269 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001270 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001271 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001272 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001273 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001274 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 pol = NULL;
1276 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001277out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001278 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return pol;
1280}
1281
1282static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1283{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001284 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001285
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001286 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001287 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 xfrm_pol_hold(pol);
1289}
1290
1291static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1292 int dir)
1293{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001294 struct net *net = xp_net(pol);
1295
Herbert Xu53c2e282014-11-13 17:09:49 +08001296 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001297 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Herbert Xu53c2e282014-11-13 17:09:49 +08001299 /* Socket policies are not hashed. */
1300 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001301 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001302 hlist_del(&pol->byidx);
1303 }
1304
1305 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001306 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001307
1308 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Herbert Xu53c2e282014-11-13 17:09:49 +08001311static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1312{
1313 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1314}
1315
1316static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1317{
1318 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1319}
1320
Herbert Xu4666faa2005-06-18 22:43:22 -07001321int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Fan Du283bc9f2013-11-07 17:47:50 +08001323 struct net *net = xp_net(pol);
1324
Florian Westphal9d0380d2016-08-11 15:17:59 +02001325 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001327 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001332 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
David S. Millera70fcb02006-03-20 19:18:52 -08001334EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1337{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001338 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct xfrm_policy *old_pol;
1340
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001341#ifdef CONFIG_XFRM_SUB_POLICY
1342 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1343 return -EINVAL;
1344#endif
1345
Florian Westphal9d0380d2016-08-11 15:17:59 +02001346 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001347 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1348 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001350 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001351 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001352 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001354 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001355 if (old_pol) {
1356 if (pol)
1357 xfrm_policy_requeue(old_pol, pol);
1358
Timo Teräsea2dea92010-03-31 00:17:05 +00001359 /* Unlinking succeeds always. This is the only function
1360 * allowed to delete or replace socket policy.
1361 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001362 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001363 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001364 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 if (old_pol) {
1367 xfrm_policy_kill(old_pol);
1368 }
1369 return 0;
1370}
1371
David S. Millerd3e40a92011-02-24 01:25:41 -05001372static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001374 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001375 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 if (newp) {
1378 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001379 if (security_xfrm_policy_clone(old->security,
1380 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001381 kfree(newp);
1382 return NULL; /* ENOMEM */
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 newp->lft = old->lft;
1385 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001386 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 newp->action = old->action;
1388 newp->flags = old->flags;
1389 newp->xfrm_nr = old->xfrm_nr;
1390 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001391 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 memcpy(newp->xfrm_vec, old->xfrm_vec,
1393 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001394 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001395 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001396 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 xfrm_pol_put(newp);
1398 }
1399 return newp;
1400}
1401
Eric Dumazetd188ba82015-12-08 07:22:02 -08001402int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001404 const struct xfrm_policy *p;
1405 struct xfrm_policy *np;
1406 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Eric Dumazetd188ba82015-12-08 07:22:02 -08001408 rcu_read_lock();
1409 for (i = 0; i < 2; i++) {
1410 p = rcu_dereference(osk->sk_policy[i]);
1411 if (p) {
1412 np = clone_policy(p, i);
1413 if (unlikely(!np)) {
1414 ret = -ENOMEM;
1415 break;
1416 }
1417 rcu_assign_pointer(sk->sk_policy[i], np);
1418 }
1419 }
1420 rcu_read_unlock();
1421 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001424static int
David Ahern42a7b322015-08-10 16:58:11 -06001425xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1426 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001427{
1428 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01001429 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001430
1431 if (unlikely(afinfo == NULL))
1432 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001433 err = afinfo->get_saddr(net, oif, local, remote);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001434 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001435 return err;
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/* Resolve list of templates for the flow, given policy. */
1439
1440static int
David S. Millera6c2e612011-02-22 18:35:39 -08001441xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1442 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001444 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int nx;
1446 int i, error;
1447 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1448 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001449 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Weilong Chen9b7a7872013-12-24 09:43:46 +08001451 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct xfrm_state *x;
1453 xfrm_address_t *remote = daddr;
1454 xfrm_address_t *local = saddr;
1455 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1456
Joakim Koskela48b8d782007-07-26 00:08:42 -07001457 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1458 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 remote = &tmpl->id.daddr;
1460 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001461 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001462 error = xfrm_get_saddr(net, fl->flowi_oif,
1463 &tmp, remote,
1464 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001465 if (error)
1466 goto fail;
1467 local = &tmp;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1472
1473 if (x && x->km.state == XFRM_STATE_VALID) {
1474 xfrm[nx++] = x;
1475 daddr = remote;
1476 saddr = local;
1477 continue;
1478 }
1479 if (x) {
1480 error = (x->km.state == XFRM_STATE_ERROR ?
1481 -EINVAL : -EAGAIN);
1482 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001483 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001484 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (!tmpl->optional)
1488 goto fail;
1489 }
1490 return nx;
1491
1492fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001493 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 xfrm_state_put(xfrm[nx]);
1495 return error;
1496}
1497
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001498static int
David S. Millera6c2e612011-02-22 18:35:39 -08001499xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1500 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001501{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001502 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1503 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001504 int cnx = 0;
1505 int error;
1506 int ret;
1507 int i;
1508
1509 for (i = 0; i < npols; i++) {
1510 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1511 error = -ENOBUFS;
1512 goto fail;
1513 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001514
1515 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001516 if (ret < 0) {
1517 error = ret;
1518 goto fail;
1519 } else
1520 cnx += ret;
1521 }
1522
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001523 /* found states are sorted for outbound processing */
1524 if (npols > 1)
1525 xfrm_state_sort(xfrm, tpp, cnx, family);
1526
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001527 return cnx;
1528
1529 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001530 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001531 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001532 return error;
1533
1534}
1535
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001536static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001537{
Florian Westphal37b10382017-02-07 15:00:19 +01001538 const struct xfrm_policy_afinfo *afinfo;
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001539 int tos = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001540
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001541 afinfo = xfrm_policy_get_afinfo(family);
1542 tos = afinfo ? afinfo->get_tos(fl) : 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001543
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001544 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001545
1546 return tos;
1547}
1548
Timo Teräs80c802f2010-04-07 00:30:05 +00001549static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1550{
1551 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1552 struct dst_entry *dst = &xdst->u.dst;
1553
1554 if (xdst->route == NULL) {
1555 /* Dummy bundle - if it has xfrms we were not
1556 * able to build bundle as template resolution failed.
1557 * It means we need to try again resolving. */
1558 if (xdst->num_xfrms > 0)
1559 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001560 } else if (dst->flags & DST_XFRM_QUEUE) {
1561 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001562 } else {
1563 /* Real bundle */
1564 if (stale_bundle(dst))
1565 return NULL;
1566 }
1567
1568 dst_hold(dst);
1569 return flo;
1570}
1571
1572static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1573{
1574 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1575 struct dst_entry *dst = &xdst->u.dst;
1576
1577 if (!xdst->route)
1578 return 0;
1579 if (stale_bundle(dst))
1580 return 0;
1581
1582 return 1;
1583}
1584
1585static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1586{
1587 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1588 struct dst_entry *dst = &xdst->u.dst;
1589
1590 dst_free(dst);
1591}
1592
1593static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1594 .get = xfrm_bundle_flo_get,
1595 .check = xfrm_bundle_flo_check,
1596 .delete = xfrm_bundle_flo_delete,
1597};
1598
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001599static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001600{
Florian Westphal37b10382017-02-07 15:00:19 +01001601 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001602 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001603 struct xfrm_dst *xdst;
1604
1605 if (!afinfo)
1606 return ERR_PTR(-EINVAL);
1607
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001608 switch (family) {
1609 case AF_INET:
1610 dst_ops = &net->xfrm.xfrm4_dst_ops;
1611 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001612#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001613 case AF_INET6:
1614 dst_ops = &net->xfrm.xfrm6_dst_ops;
1615 break;
1616#endif
1617 default:
1618 BUG();
1619 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001620 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001621
Madalin Bucurd4cae562011-09-26 07:04:36 +00001622 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001623 struct dst_entry *dst = &xdst->u.dst;
1624
1625 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001626 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001627 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001628 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001629
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001630 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00001631
Herbert Xu25ee3282007-12-11 09:32:34 -08001632 return xdst;
1633}
1634
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001635static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1636 int nfheader_len)
1637{
Florian Westphal37b10382017-02-07 15:00:19 +01001638 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001639 xfrm_policy_get_afinfo(dst->ops->family);
1640 int err;
1641
1642 if (!afinfo)
1643 return -EINVAL;
1644
1645 err = afinfo->init_path(path, dst, nfheader_len);
1646
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001647 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001648
1649 return err;
1650}
1651
Herbert Xu87c1e122010-03-02 02:51:56 +00001652static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001653 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001654{
Florian Westphal37b10382017-02-07 15:00:19 +01001655 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08001656 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1657 int err;
1658
1659 if (!afinfo)
1660 return -EINVAL;
1661
Herbert Xu87c1e122010-03-02 02:51:56 +00001662 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001663
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001664 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001665
1666 return err;
1667}
1668
Timo Teräs80c802f2010-04-07 00:30:05 +00001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1671 * all the metrics... Shortly, bundle a bundle.
1672 */
1673
Herbert Xu25ee3282007-12-11 09:32:34 -08001674static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1675 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001676 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001677 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001679 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001680 unsigned long now = jiffies;
1681 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001682 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001683 struct dst_entry *dst_prev = NULL;
1684 struct dst_entry *dst0 = NULL;
1685 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001687 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001688 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001689 int trailer_len = 0;
1690 int tos;
1691 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001692 xfrm_address_t saddr, daddr;
1693
1694 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001695
1696 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001697
1698 dst_hold(dst);
1699
1700 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001701 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001702 struct dst_entry *dst1 = &xdst->u.dst;
1703
1704 err = PTR_ERR(xdst);
1705 if (IS_ERR(xdst)) {
1706 dst_release(dst);
1707 goto put_states;
1708 }
1709
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001710 if (xfrm[i]->sel.family == AF_UNSPEC) {
1711 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1712 xfrm_af2proto(family));
1713 if (!inner_mode) {
1714 err = -EAFNOSUPPORT;
1715 dst_release(dst);
1716 goto put_states;
1717 }
1718 } else
1719 inner_mode = xfrm[i]->inner_mode;
1720
Herbert Xu25ee3282007-12-11 09:32:34 -08001721 if (!dst_prev)
1722 dst0 = dst1;
1723 else {
1724 dst_prev->child = dst_clone(dst1);
1725 dst1->flags |= DST_NOHASH;
1726 }
1727
1728 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001729 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001730
1731 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1732 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001733 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1734 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001735 err = PTR_ERR(dst);
1736 if (IS_ERR(dst))
1737 goto put_states;
1738 } else
1739 dst_hold(dst);
1740
1741 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001742 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001743
David S. Millerf5b0a872012-07-19 12:31:33 -07001744 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001745 dst1->flags |= DST_HOST;
1746 dst1->lastuse = now;
1747
1748 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001749 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001750
1751 dst1->next = dst_prev;
1752 dst_prev = dst1;
1753
1754 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001755 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1756 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001757 trailer_len += xfrm[i]->props.trailer_len;
1758 }
1759
1760 dst_prev->child = dst;
1761 dst0->path = dst;
1762
1763 err = -ENODEV;
1764 dev = dst->dev;
1765 if (!dev)
1766 goto free_dst;
1767
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001768 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001769 xfrm_init_pmtu(dst_prev);
1770
1771 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1772 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1773
Herbert Xu87c1e122010-03-02 02:51:56 +00001774 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001775 if (err)
1776 goto free_dst;
1777
1778 dst_prev->header_len = header_len;
1779 dst_prev->trailer_len = trailer_len;
1780 header_len -= xdst->u.dst.xfrm->props.header_len;
1781 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1782 }
1783
1784out:
1785 return dst0;
1786
1787put_states:
1788 for (; i < nx; i++)
1789 xfrm_state_put(xfrm[i]);
1790free_dst:
1791 if (dst0)
1792 dst_free(dst0);
1793 dst0 = ERR_PTR(err);
1794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Ying Xueda7c2242014-01-08 10:26:39 +08001797#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001798static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001799{
1800 if (!*target) {
1801 *target = kmalloc(size, GFP_ATOMIC);
1802 if (!*target)
1803 return -ENOMEM;
1804 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001805
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001806 memcpy(*target, src, size);
1807 return 0;
1808}
Ying Xueda7c2242014-01-08 10:26:39 +08001809#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001810
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001811static int xfrm_dst_update_parent(struct dst_entry *dst,
1812 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001813{
1814#ifdef CONFIG_XFRM_SUB_POLICY
1815 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1816 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1817 sel, sizeof(*sel));
1818#else
1819 return 0;
1820#endif
1821}
1822
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001823static int xfrm_dst_update_origin(struct dst_entry *dst,
1824 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001825{
1826#ifdef CONFIG_XFRM_SUB_POLICY
1827 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1828 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1829#else
1830 return 0;
1831#endif
1832}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
David S. Miller73ff93c2011-02-22 18:33:42 -08001834static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001835 struct xfrm_policy **pols,
1836 int *num_pols, int *num_xfrms)
1837{
1838 int i;
1839
1840 if (*num_pols == 0 || !pols[0]) {
1841 *num_pols = 0;
1842 *num_xfrms = 0;
1843 return 0;
1844 }
1845 if (IS_ERR(pols[0]))
1846 return PTR_ERR(pols[0]);
1847
1848 *num_xfrms = pols[0]->xfrm_nr;
1849
1850#ifdef CONFIG_XFRM_SUB_POLICY
1851 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1852 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1853 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1854 XFRM_POLICY_TYPE_MAIN,
1855 fl, family,
1856 XFRM_POLICY_OUT);
1857 if (pols[1]) {
1858 if (IS_ERR(pols[1])) {
1859 xfrm_pols_put(pols, *num_pols);
1860 return PTR_ERR(pols[1]);
1861 }
Weilong Chen02d08922013-12-24 09:43:48 +08001862 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001863 (*num_xfrms) += pols[1]->xfrm_nr;
1864 }
1865 }
1866#endif
1867 for (i = 0; i < *num_pols; i++) {
1868 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1869 *num_xfrms = -1;
1870 break;
1871 }
1872 }
1873
1874 return 0;
1875
1876}
1877
1878static struct xfrm_dst *
1879xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001880 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001881 struct dst_entry *dst_orig)
1882{
1883 struct net *net = xp_net(pols[0]);
1884 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1885 struct dst_entry *dst;
1886 struct xfrm_dst *xdst;
1887 int err;
1888
1889 /* Try to instantiate a bundle */
1890 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001891 if (err <= 0) {
1892 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001893 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1894 return ERR_PTR(err);
1895 }
1896
1897 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1898 if (IS_ERR(dst)) {
1899 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1900 return ERR_CAST(dst);
1901 }
1902
1903 xdst = (struct xfrm_dst *)dst;
1904 xdst->num_xfrms = err;
1905 if (num_pols > 1)
1906 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1907 else
1908 err = xfrm_dst_update_origin(dst, fl);
1909 if (unlikely(err)) {
1910 dst_free(dst);
1911 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1912 return ERR_PTR(err);
1913 }
1914
1915 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001916 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001917 xdst->policy_genid = atomic_read(&pols[0]->genid);
1918
1919 return xdst;
1920}
1921
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001922static void xfrm_policy_queue_process(unsigned long arg)
1923{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001924 struct sk_buff *skb;
1925 struct sock *sk;
1926 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001927 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001928 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001929 struct xfrm_policy_queue *pq = &pol->polq;
1930 struct flowi fl;
1931 struct sk_buff_head list;
1932
1933 spin_lock(&pq->hold_queue.lock);
1934 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001935 if (!skb) {
1936 spin_unlock(&pq->hold_queue.lock);
1937 goto out;
1938 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001939 dst = skb_dst(skb);
1940 sk = skb->sk;
1941 xfrm_decode_session(skb, &fl, dst->ops->family);
1942 spin_unlock(&pq->hold_queue.lock);
1943
1944 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001945 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001946 if (IS_ERR(dst))
1947 goto purge_queue;
1948
1949 if (dst->flags & DST_XFRM_QUEUE) {
1950 dst_release(dst);
1951
1952 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1953 goto purge_queue;
1954
1955 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001956 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1957 xfrm_pol_hold(pol);
1958 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001959 }
1960
1961 dst_release(dst);
1962
1963 __skb_queue_head_init(&list);
1964
1965 spin_lock(&pq->hold_queue.lock);
1966 pq->timeout = 0;
1967 skb_queue_splice_init(&pq->hold_queue, &list);
1968 spin_unlock(&pq->hold_queue.lock);
1969
1970 while (!skb_queue_empty(&list)) {
1971 skb = __skb_dequeue(&list);
1972
1973 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1974 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001975 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001976 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001977 kfree_skb(skb);
1978 continue;
1979 }
1980
1981 nf_reset(skb);
1982 skb_dst_drop(skb);
1983 skb_dst_set(skb, dst);
1984
Eric W. Biederman13206b62015-10-07 16:48:35 -05001985 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001986 }
1987
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001988out:
1989 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001990 return;
1991
1992purge_queue:
1993 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001994 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001995 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001996}
1997
Eric W. Biedermanede20592015-10-07 16:48:47 -05001998static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001999{
2000 unsigned long sched_next;
2001 struct dst_entry *dst = skb_dst(skb);
2002 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002003 struct xfrm_policy *pol = xdst->pols[0];
2004 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002005
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002006 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002007 kfree_skb(skb);
2008 return 0;
2009 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002010
2011 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2012 kfree_skb(skb);
2013 return -EAGAIN;
2014 }
2015
2016 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002017
2018 spin_lock_bh(&pq->hold_queue.lock);
2019
2020 if (!pq->timeout)
2021 pq->timeout = XFRM_QUEUE_TMO_MIN;
2022
2023 sched_next = jiffies + pq->timeout;
2024
2025 if (del_timer(&pq->hold_timer)) {
2026 if (time_before(pq->hold_timer.expires, sched_next))
2027 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002028 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002029 }
2030
2031 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002032 if (!mod_timer(&pq->hold_timer, sched_next))
2033 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002034
2035 spin_unlock_bh(&pq->hold_queue.lock);
2036
2037 return 0;
2038}
2039
2040static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002041 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002042 const struct flowi *fl,
2043 int num_xfrms,
2044 u16 family)
2045{
2046 int err;
2047 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002048 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002049 struct dst_entry *dst1;
2050 struct xfrm_dst *xdst;
2051
2052 xdst = xfrm_alloc_dst(net, family);
2053 if (IS_ERR(xdst))
2054 return xdst;
2055
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002056 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2057 net->xfrm.sysctl_larval_drop ||
2058 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002059 return xdst;
2060
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002061 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002062 dst1 = &xdst->u.dst;
2063 dst_hold(dst);
2064 xdst->route = dst;
2065
2066 dst_copy_metrics(dst1, dst);
2067
2068 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2069 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2070 dst1->lastuse = jiffies;
2071
2072 dst1->input = dst_discard;
2073 dst1->output = xdst_queue_output;
2074
2075 dst_hold(dst);
2076 dst1->child = dst;
2077 dst1->path = dst;
2078
2079 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2080
2081 err = -ENODEV;
2082 dev = dst->dev;
2083 if (!dev)
2084 goto free_dst;
2085
2086 err = xfrm_fill_dst(xdst, dev, fl);
2087 if (err)
2088 goto free_dst;
2089
2090out:
2091 return xdst;
2092
2093free_dst:
2094 dst_release(dst1);
2095 xdst = ERR_PTR(err);
2096 goto out;
2097}
2098
Timo Teräs80c802f2010-04-07 00:30:05 +00002099static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002100xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002101 struct flow_cache_object *oldflo, void *ctx)
2102{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002103 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002104 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2105 struct xfrm_dst *xdst, *new_xdst;
2106 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2107
2108 /* Check if the policies from old bundle are usable */
2109 xdst = NULL;
2110 if (oldflo) {
2111 xdst = container_of(oldflo, struct xfrm_dst, flo);
2112 num_pols = xdst->num_pols;
2113 num_xfrms = xdst->num_xfrms;
2114 pol_dead = 0;
2115 for (i = 0; i < num_pols; i++) {
2116 pols[i] = xdst->pols[i];
2117 pol_dead |= pols[i]->walk.dead;
2118 }
2119 if (pol_dead) {
2120 dst_free(&xdst->u.dst);
2121 xdst = NULL;
2122 num_pols = 0;
2123 num_xfrms = 0;
2124 oldflo = NULL;
2125 }
2126 }
2127
2128 /* Resolve policies to use if we couldn't get them from
2129 * previous cache entry */
2130 if (xdst == NULL) {
2131 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002132 pols[0] = __xfrm_policy_lookup(net, fl, family,
2133 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002134 err = xfrm_expand_policies(fl, family, pols,
2135 &num_pols, &num_xfrms);
2136 if (err < 0)
2137 goto inc_error;
2138 if (num_pols == 0)
2139 return NULL;
2140 if (num_xfrms <= 0)
2141 goto make_dummy_bundle;
2142 }
2143
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002144 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2145 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002146 if (IS_ERR(new_xdst)) {
2147 err = PTR_ERR(new_xdst);
2148 if (err != -EAGAIN)
2149 goto error;
2150 if (oldflo == NULL)
2151 goto make_dummy_bundle;
2152 dst_hold(&xdst->u.dst);
2153 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002154 } else if (new_xdst == NULL) {
2155 num_xfrms = 0;
2156 if (oldflo == NULL)
2157 goto make_dummy_bundle;
2158 xdst->num_xfrms = 0;
2159 dst_hold(&xdst->u.dst);
2160 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002161 }
2162
2163 /* Kill the previous bundle */
2164 if (xdst) {
2165 /* The policies were stolen for newly generated bundle */
2166 xdst->num_pols = 0;
2167 dst_free(&xdst->u.dst);
2168 }
2169
2170 /* Flow cache does not have reference, it dst_free()'s,
2171 * but we do need to return one reference for original caller */
2172 dst_hold(&new_xdst->u.dst);
2173 return &new_xdst->flo;
2174
2175make_dummy_bundle:
2176 /* We found policies, but there's no bundles to instantiate:
2177 * either because the policy blocks, has no transformations or
2178 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002179 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002180 if (IS_ERR(xdst)) {
2181 xfrm_pols_put(pols, num_pols);
2182 return ERR_CAST(xdst);
2183 }
2184 xdst->num_pols = num_pols;
2185 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002186 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002187
2188 dst_hold(&xdst->u.dst);
2189 return &xdst->flo;
2190
2191inc_error:
2192 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2193error:
2194 if (xdst != NULL)
2195 dst_free(&xdst->u.dst);
2196 else
2197 xfrm_pols_put(pols, num_pols);
2198 return ERR_PTR(err);
2199}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
David S. Miller2774c132011-03-01 14:59:04 -08002201static struct dst_entry *make_blackhole(struct net *net, u16 family,
2202 struct dst_entry *dst_orig)
2203{
Florian Westphal37b10382017-02-07 15:00:19 +01002204 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002205 struct dst_entry *ret;
2206
2207 if (!afinfo) {
2208 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002209 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002210 } else {
2211 ret = afinfo->blackhole_route(net, dst_orig);
2212 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002213 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08002214
2215 return ret;
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218/* Main function: finds/creates a bundle for given flow.
2219 *
2220 * At the moment we eat a raw IP route. Mostly to speed up lookups
2221 * on interfaces with disabled IPsec.
2222 */
David S. Miller452edd52011-03-02 13:27:41 -08002223struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2224 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002225 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002227 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002228 struct flow_cache_object *flo;
2229 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002230 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002231 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002232 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002233 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002234
Timo Teräs80c802f2010-04-07 00:30:05 +00002235 dst = NULL;
2236 xdst = NULL;
2237 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002238
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002239 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002240 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002241 num_pols = 1;
2242 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2243 err = xfrm_expand_policies(fl, family, pols,
2244 &num_pols, &num_xfrms);
2245 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002246 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002247
2248 if (num_pols) {
2249 if (num_xfrms <= 0) {
2250 drop_pols = num_pols;
2251 goto no_transform;
2252 }
2253
2254 xdst = xfrm_resolve_and_create_bundle(
2255 pols, num_pols, fl,
2256 family, dst_orig);
2257 if (IS_ERR(xdst)) {
2258 xfrm_pols_put(pols, num_pols);
2259 err = PTR_ERR(xdst);
2260 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002261 } else if (xdst == NULL) {
2262 num_xfrms = 0;
2263 drop_pols = num_pols;
2264 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002265 }
2266
Steffen Klassertb7eea452014-06-18 12:34:21 +02002267 dst_hold(&xdst->u.dst);
2268 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002269 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002270 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Timo Teräs80c802f2010-04-07 00:30:05 +00002273 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002274 struct xfrm_flo xflo;
2275
2276 xflo.dst_orig = dst_orig;
2277 xflo.flags = flags;
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002280 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002281 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002282 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Timo Teräs80c802f2010-04-07 00:30:05 +00002284 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002285 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002286 if (flo == NULL)
2287 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002288 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002289 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002290 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002291 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002292 xdst = container_of(flo, struct xfrm_dst, flo);
2293
2294 num_pols = xdst->num_pols;
2295 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002296 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002297 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299
Timo Teräs80c802f2010-04-07 00:30:05 +00002300 dst = &xdst->u.dst;
2301 if (route == NULL && num_xfrms > 0) {
2302 /* The only case when xfrm_bundle_lookup() returns a
2303 * bundle with null route, is when the template could
2304 * not be resolved. It means policies are there, but
2305 * bundle could not be created, since we don't yet
2306 * have the xfrm_state's. We need to wait for KM to
2307 * negotiate new SA's or bail out with error.*/
2308 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002309 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002310 err = -EREMOTE;
2311 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002312 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002313
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002314 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002315
2316 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2317 goto error;
2318 }
2319
2320no_transform:
2321 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002322 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Timo Teräs80c802f2010-04-07 00:30:05 +00002324 if ((flags & XFRM_LOOKUP_ICMP) &&
2325 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2326 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002327 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002328 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002329
Timo Teräs80c802f2010-04-07 00:30:05 +00002330 for (i = 0; i < num_pols; i++)
2331 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002332
Timo Teräs80c802f2010-04-07 00:30:05 +00002333 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002335 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002336 err = -EPERM;
2337 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 } else if (num_xfrms > 0) {
2339 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002340 dst_release(dst_orig);
2341 } else {
2342 /* Flow passes untransformed */
2343 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002344 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002346ok:
2347 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002348 if (dst && dst->xfrm &&
2349 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2350 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002351 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Timo Teräs80c802f2010-04-07 00:30:05 +00002353nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002354 if (!(flags & XFRM_LOOKUP_ICMP)) {
2355 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002356 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002357 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002358 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002360 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002361dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002362 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2363 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002364 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002365 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367EXPORT_SYMBOL(xfrm_lookup);
2368
Steffen Klassertf92ee612014-09-16 10:08:40 +02002369/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2370 * Otherwise we may send out blackholed packets.
2371 */
2372struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2373 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002374 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002375{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002376 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002377 flags | XFRM_LOOKUP_QUEUE |
2378 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002379
2380 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2381 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2382
2383 return dst;
2384}
2385EXPORT_SYMBOL(xfrm_lookup_route);
2386
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002387static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002388xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002389{
2390 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002391
2392 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2393 return 0;
2394 x = skb->sp->xvec[idx];
2395 if (!x->type->reject)
2396 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002397 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002398}
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400/* When skb is transformed back to its "native" form, we have to
2401 * check policy restrictions. At the moment we make this in maximally
2402 * stupid way. Shame on me. :-) Of course, connected sockets must
2403 * have policy cached at them.
2404 */
2405
2406static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002407xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 unsigned short family)
2409{
2410 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002411 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return x->id.proto == tmpl->id.proto &&
2413 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2414 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2415 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002416 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002417 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002418 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2419 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002422/*
2423 * 0 or more than 0 is returned when validation is succeeded (either bypass
2424 * because of optional transport mode, or next index of the mathced secpath
2425 * state with the template.
2426 * -1 is returned when no matching template is found.
2427 * Otherwise "-2 - errored_index" is returned.
2428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002430xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 unsigned short family)
2432{
2433 int idx = start;
2434
2435 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002436 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return start;
2438 } else
2439 start = -1;
2440 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002441 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002443 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2444 if (start == -1)
2445 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449 return start;
2450}
2451
Herbert Xud5422ef2007-12-12 10:44:16 -08002452int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2453 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Florian Westphal37b10382017-02-07 15:00:19 +01002455 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002456 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 if (unlikely(afinfo == NULL))
2459 return -EAFNOSUPPORT;
2460
Herbert Xud5422ef2007-12-12 10:44:16 -08002461 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002462 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002463 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002464 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
Herbert Xud5422ef2007-12-12 10:44:16 -08002466EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
David S. Miller9a7386e2011-02-24 01:44:12 -05002468static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002471 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002472 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
2477 return 0;
2478}
2479
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002480int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 unsigned short family)
2482{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002483 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002485 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2486 int npols = 0;
2487 int xfrm_nr;
2488 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002489 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002491 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002492 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Herbert Xud5422ef2007-12-12 10:44:16 -08002494 reverse = dir & ~XFRM_POLICY_MASK;
2495 dir &= XFRM_POLICY_MASK;
2496 fl_dir = policy_to_flow_dir(dir);
2497
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002498 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002499 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002501 }
2502
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002503 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 /* First, check used SA against their selectors. */
2506 if (skb->sp) {
2507 int i;
2508
Weilong Chen9b7a7872013-12-24 09:43:46 +08002509 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002510 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002511 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002512 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 }
2516 }
2517
2518 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002519 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002520 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002521 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002522 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002523 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002524 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002525 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002528 if (!pol) {
2529 struct flow_cache_object *flo;
2530
2531 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2532 xfrm_policy_lookup, NULL);
2533 if (IS_ERR_OR_NULL(flo))
2534 pol = ERR_CAST(flo);
2535 else
2536 pol = container_of(flo, struct xfrm_policy, flo);
2537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002539 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002540 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002541 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002542 }
James Morris134b0fc2006-10-05 15:42:27 -05002543
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002544 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002545 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002546 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002547 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002548 return 0;
2549 }
2550 return 1;
2551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
James Morris9d729f72007-03-04 16:12:44 -08002553 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002555 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002556 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002557#ifdef CONFIG_XFRM_SUB_POLICY
2558 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002559 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002560 &fl, family,
2561 XFRM_POLICY_IN);
2562 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002563 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002564 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002565 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002566 }
James Morris9d729f72007-03-04 16:12:44 -08002567 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002568 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002569 }
2570 }
2571#endif
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (pol->action == XFRM_POLICY_ALLOW) {
2574 struct sec_path *sp;
2575 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002576 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002577 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002578 struct xfrm_tmpl **tpp = tp;
2579 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 int i, k;
2581
2582 if ((sp = skb->sp) == NULL)
2583 sp = &dummy;
2584
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002585 for (pi = 0; pi < npols; pi++) {
2586 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002587 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002588 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002589 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002590 }
2591 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002592 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002593 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002594 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002595 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2596 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2597 }
2598 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002599 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002600 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002601 tpp = stp;
2602 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* For each tunnel xfrm, find the first matching tmpl.
2605 * For each tmpl before that, find corresponding xfrm.
2606 * Order is _important_. Later we will implement
2607 * some barriers, but at the moment barriers
2608 * are implied between each two transformations.
2609 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002610 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2611 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002612 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002613 if (k < -1)
2614 /* "-2 - errored_index" returned */
2615 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002616 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002621 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002622 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002626 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return 1;
2628 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002629 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002632 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002633reject_error:
2634 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 0;
2636}
2637EXPORT_SYMBOL(__xfrm_policy_check);
2638
2639int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2640{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002641 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002643 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002644 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002646 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002647 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002651 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002652
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002653 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002654 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002655 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002656 dst = NULL;
2657 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002658 skb_dst_set(skb, dst);
2659 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661EXPORT_SYMBOL(__xfrm_route_forward);
2662
David S. Millerd49c73c2006-08-13 18:55:53 -07002663/* Optimize later using cookies and generation ids. */
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2666{
David S. Millerd49c73c2006-08-13 18:55:53 -07002667 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002668 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2669 * get validated by dst_ops->check on every use. We do this
2670 * because when a normal route referenced by an XFRM dst is
2671 * obsoleted we do not go looking around for all parent
2672 * referencing XFRM dsts so that we can invalidate them. It
2673 * is just too much work. Instead we make the checks here on
2674 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002675 *
2676 * XFRM dst A --> IPv4 dst X
2677 *
2678 * X is the "xdst->route" of A (X is also the "dst->path" of A
2679 * in this example). If X is marked obsolete, "A" will not
2680 * notice. That's what we are validating here via the
2681 * stale_bundle() check.
2682 *
2683 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002684 * dst which causes it's ->obsolete field to be set to
2685 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2686 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002687 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002688 if (dst->obsolete < 0 && !stale_bundle(dst))
2689 return dst;
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return NULL;
2692}
2693
2694static int stale_bundle(struct dst_entry *dst)
2695{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002696 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Herbert Xuaabc9762005-05-03 16:27:10 -07002699void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002702 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002703 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 dev_put(dev);
2705 }
2706}
Herbert Xuaabc9762005-05-03 16:27:10 -07002707EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709static void xfrm_link_failure(struct sk_buff *skb)
2710{
2711 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
2714static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2715{
2716 if (dst) {
2717 if (dst->obsolete) {
2718 dst_release(dst);
2719 dst = NULL;
2720 }
2721 }
2722 return dst;
2723}
2724
Paul Mooree4c17212013-05-29 07:36:25 +00002725void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002726{
Fan Duca925cf2014-01-18 09:55:27 +08002727 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002728}
Paul Mooree4c17212013-05-29 07:36:25 +00002729EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002730
Florian Westphal3d7d25a2017-02-07 15:00:16 +01002731void xfrm_garbage_collect_deferred(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002732{
Fan Duca925cf2014-01-18 09:55:27 +08002733 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002734}
Florian Westphal3d7d25a2017-02-07 15:00:16 +01002735EXPORT_SYMBOL(xfrm_garbage_collect_deferred);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002736
Herbert Xu25ee3282007-12-11 09:32:34 -08002737static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 do {
2740 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2741 u32 pmtu, route_mtu_cached;
2742
2743 pmtu = dst_mtu(dst->child);
2744 xdst->child_mtu_cached = pmtu;
2745
2746 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2747
2748 route_mtu_cached = dst_mtu(xdst->route);
2749 xdst->route_mtu_cached = route_mtu_cached;
2750
2751 if (pmtu > route_mtu_cached)
2752 pmtu = route_mtu_cached;
2753
David S. Millerdefb3512010-12-08 21:16:57 -08002754 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 } while ((dst = dst->next));
2756}
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758/* Check that the bundle accepts the flow and its components are
2759 * still valid.
2760 */
2761
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002762static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 struct dst_entry *dst = &first->u.dst;
2765 struct xfrm_dst *last;
2766 u32 mtu;
2767
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002768 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 (dst->dev && !netif_running(dst->dev)))
2770 return 0;
2771
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002772 if (dst->flags & DST_XFRM_QUEUE)
2773 return 1;
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 last = NULL;
2776
2777 do {
2778 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2781 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002782 if (xdst->xfrm_genid != dst->xfrm->genid)
2783 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002784 if (xdst->num_pols > 0 &&
2785 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 mtu = dst_mtu(dst->child);
2789 if (xdst->child_mtu_cached != mtu) {
2790 last = xdst;
2791 xdst->child_mtu_cached = mtu;
2792 }
2793
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002794 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return 0;
2796 mtu = dst_mtu(xdst->route);
2797 if (xdst->route_mtu_cached != mtu) {
2798 last = xdst;
2799 xdst->route_mtu_cached = mtu;
2800 }
2801
2802 dst = dst->child;
2803 } while (dst->xfrm);
2804
2805 if (likely(!last))
2806 return 1;
2807
2808 mtu = last->child_mtu_cached;
2809 for (;;) {
2810 dst = &last->u.dst;
2811
2812 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2813 if (mtu > last->route_mtu_cached)
2814 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002815 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 if (last == first)
2818 break;
2819
Patrick McHardybd0bf072007-07-18 01:55:52 -07002820 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 last->child_mtu_cached = mtu;
2822 }
2823
2824 return 1;
2825}
2826
David S. Miller0dbaee32010-12-13 12:52:14 -08002827static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2828{
2829 return dst_metric_advmss(dst->path);
2830}
2831
Steffen Klassertebb762f2011-11-23 02:12:51 +00002832static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002833{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002834 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2835
2836 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002837}
2838
David S. Millerf894cbf2012-07-02 21:52:24 -07002839static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2840 struct sk_buff *skb,
2841 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002842{
David S. Millerf894cbf2012-07-02 21:52:24 -07002843 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002844}
2845
Florian Westphala2817d82017-02-07 15:00:17 +01002846int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01002849
2850 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01002852
Eric Dumazetef8531b2012-08-19 12:31:48 +02002853 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01002854 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002855 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 else {
2857 struct dst_ops *dst_ops = afinfo->dst_ops;
2858 if (likely(dst_ops->kmem_cachep == NULL))
2859 dst_ops->kmem_cachep = xfrm_dst_cache;
2860 if (likely(dst_ops->check == NULL))
2861 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002862 if (likely(dst_ops->default_advmss == NULL))
2863 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002864 if (likely(dst_ops->mtu == NULL))
2865 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (likely(dst_ops->negative_advice == NULL))
2867 dst_ops->negative_advice = xfrm_negative_advice;
2868 if (likely(dst_ops->link_failure == NULL))
2869 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002870 if (likely(dst_ops->neigh_lookup == NULL))
2871 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Florian Westphala2817d82017-02-07 15:00:17 +01002872 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002874 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return err;
2877}
2878EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2879
Florian Westphala2817d82017-02-07 15:00:17 +01002880void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Florian Westphal2b619972017-02-07 15:00:15 +01002882 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01002883 int i;
Florian Westphal2b619972017-02-07 15:00:15 +01002884
Florian Westphala2817d82017-02-07 15:00:17 +01002885 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
2886 if (xfrm_policy_afinfo[i] != afinfo)
2887 continue;
2888 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
2889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002891
Florian Westphal2b619972017-02-07 15:00:15 +01002892 synchronize_rcu();
Eric Dumazetef8531b2012-08-19 12:31:48 +02002893
Florian Westphal2b619972017-02-07 15:00:15 +01002894 dst_ops->kmem_cachep = NULL;
2895 dst_ops->check = NULL;
2896 dst_ops->negative_advice = NULL;
2897 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2902{
Jiri Pirko351638e2013-05-28 01:30:21 +00002903 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 switch (event) {
2906 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002907 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
2909 return NOTIFY_DONE;
2910}
2911
2912static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002913 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914};
2915
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002916#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002917static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002918{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002919 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002920 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2921 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002922 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002923 rv = xfrm_proc_init(net);
2924 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002925 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002926 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002927}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002928
2929static void xfrm_statistics_fini(struct net *net)
2930{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002931 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002932 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002933}
2934#else
2935static int __net_init xfrm_statistics_init(struct net *net)
2936{
2937 return 0;
2938}
2939
2940static void xfrm_statistics_fini(struct net *net)
2941{
2942}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002943#endif
2944
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002945static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
David S. Miller2518c7c2006-08-24 04:45:07 -07002947 unsigned int hmask, sz;
2948 int dir;
2949
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002950 if (net_eq(net, &init_net))
2951 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002953 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002954 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
David S. Miller2518c7c2006-08-24 04:45:07 -07002956 hmask = 8 - 1;
2957 sz = (hmask+1) * sizeof(struct hlist_head);
2958
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002959 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2960 if (!net->xfrm.policy_byidx)
2961 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002962 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002963
Herbert Xu53c2e282014-11-13 17:09:49 +08002964 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002965 struct xfrm_policy_hash *htab;
2966
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002967 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002968 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002969 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002970
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002971 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002972 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002973 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002974 goto out_bydst;
2975 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002976 htab->dbits4 = 32;
2977 htab->sbits4 = 32;
2978 htab->dbits6 = 128;
2979 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002980 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002981 net->xfrm.policy_hthresh.lbits4 = 32;
2982 net->xfrm.policy_hthresh.rbits4 = 32;
2983 net->xfrm.policy_hthresh.lbits6 = 128;
2984 net->xfrm.policy_hthresh.rbits6 = 128;
2985
2986 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002987
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002988 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002989 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002990 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002991 if (net_eq(net, &init_net))
2992 register_netdevice_notifier(&xfrm_dev_notifier);
2993 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002994
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002995out_bydst:
2996 for (dir--; dir >= 0; dir--) {
2997 struct xfrm_policy_hash *htab;
2998
2999 htab = &net->xfrm.policy_bydst[dir];
3000 xfrm_hash_free(htab->table, sz);
3001 }
3002 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003003out_byidx:
3004 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003007static void xfrm_policy_fini(struct net *net)
3008{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003009 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003010 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003011
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003012 flush_work(&net->xfrm.policy_hash_work);
3013#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003014 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003015#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003016 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003017
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003018 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003019
Herbert Xu53c2e282014-11-13 17:09:49 +08003020 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003021 struct xfrm_policy_hash *htab;
3022
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003023 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003024
3025 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003026 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003027 WARN_ON(!hlist_empty(htab->table));
3028 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003029 }
3030
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003031 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003032 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3033 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003034}
3035
3036static int __net_init xfrm_net_init(struct net *net)
3037{
3038 int rv;
3039
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003040 rv = xfrm_statistics_init(net);
3041 if (rv < 0)
3042 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003043 rv = xfrm_state_init(net);
3044 if (rv < 0)
3045 goto out_state;
3046 rv = xfrm_policy_init(net);
3047 if (rv < 0)
3048 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003049 rv = xfrm_sysctl_init(net);
3050 if (rv < 0)
3051 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003052 rv = flow_cache_init(net);
3053 if (rv < 0)
3054 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003055
3056 /* Initialize the per-net locks here */
3057 spin_lock_init(&net->xfrm.xfrm_state_lock);
Florian Westphal9d0380d2016-08-11 15:17:59 +02003058 spin_lock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003059 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3060
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003061 return 0;
3062
Steffen Klassert4a93f502014-03-12 09:43:17 +01003063out:
3064 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003065out_sysctl:
3066 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003067out_policy:
3068 xfrm_state_fini(net);
3069out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003070 xfrm_statistics_fini(net);
3071out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003072 return rv;
3073}
3074
3075static void __net_exit xfrm_net_exit(struct net *net)
3076{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003077 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003078 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003079 xfrm_policy_fini(net);
3080 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003081 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003082}
3083
3084static struct pernet_operations __net_initdata xfrm_net_ops = {
3085 .init = xfrm_net_init,
3086 .exit = xfrm_net_exit,
3087};
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089void __init xfrm_init(void)
3090{
Sebastian Andrzej Siewiora4fc1bf2016-11-03 15:50:05 +01003091 flow_cache_hp_init();
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003092 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003093 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 xfrm_input_init();
3095}
3096
Joy Lattenab5f5e82007-09-17 11:51:22 -07003097#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003098static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3099 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003100{
Paul Moore875179f2007-12-01 23:27:18 +11003101 struct xfrm_sec_ctx *ctx = xp->security;
3102 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003103
Paul Moore875179f2007-12-01 23:27:18 +11003104 if (ctx)
3105 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3106 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3107
Weilong Chen9b7a7872013-12-24 09:43:46 +08003108 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003109 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003110 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003111 if (sel->prefixlen_s != 32)
3112 audit_log_format(audit_buf, " src_prefixlen=%d",
3113 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003114 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003115 if (sel->prefixlen_d != 32)
3116 audit_log_format(audit_buf, " dst_prefixlen=%d",
3117 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003118 break;
3119 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003120 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003121 if (sel->prefixlen_s != 128)
3122 audit_log_format(audit_buf, " src_prefixlen=%d",
3123 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003124 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003125 if (sel->prefixlen_d != 128)
3126 audit_log_format(audit_buf, " dst_prefixlen=%d",
3127 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003128 break;
3129 }
3130}
3131
Tetsuo Handa2e710292014-04-22 21:48:30 +09003132void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003133{
3134 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003135
Paul Mooreafeb14b2007-12-21 14:58:11 -08003136 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003137 if (audit_buf == NULL)
3138 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003139 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003140 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003141 xfrm_audit_common_policyinfo(xp, audit_buf);
3142 audit_log_end(audit_buf);
3143}
3144EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3145
Paul Moore68277ac2007-12-20 20:49:33 -08003146void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003147 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003148{
3149 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003150
Paul Mooreafeb14b2007-12-21 14:58:11 -08003151 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003152 if (audit_buf == NULL)
3153 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003154 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003155 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003156 xfrm_audit_common_policyinfo(xp, audit_buf);
3157 audit_log_end(audit_buf);
3158}
3159EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3160#endif
3161
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003162#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003163static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3164 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003165{
3166 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3167 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003168 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3169 sel_cmp->family) &&
3170 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3171 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003172 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3173 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003174 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003175 }
3176 } else {
3177 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003178 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003179 }
3180 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003181 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003182}
3183
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003184static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3185 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003186{
3187 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003188 struct hlist_head *chain;
3189 u32 priority = ~0U;
3190
Florian Westphal9d0380d2016-08-11 15:17:59 +02003191 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003192 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003193 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003194 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3195 pol->type == type) {
3196 ret = pol;
3197 priority = ret->priority;
3198 break;
3199 }
3200 }
Fan Du8d549c42013-11-07 17:47:49 +08003201 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003202 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003203 if ((pol->priority >= priority) && ret)
3204 break;
3205
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003206 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003207 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003208 ret = pol;
3209 break;
3210 }
3211 }
3212
Li RongQing586f2eb2015-04-30 17:13:41 +08003213 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003214
Florian Westphal9d0380d2016-08-11 15:17:59 +02003215 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003216
3217 return ret;
3218}
3219
David S. Millerdd701752011-02-24 00:21:08 -05003220static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003221{
3222 int match = 0;
3223
3224 if (t->mode == m->mode && t->id.proto == m->proto &&
3225 (m->reqid == 0 || t->reqid == m->reqid)) {
3226 switch (t->mode) {
3227 case XFRM_MODE_TUNNEL:
3228 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003229 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3230 m->old_family) &&
3231 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3232 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003233 match = 1;
3234 }
3235 break;
3236 case XFRM_MODE_TRANSPORT:
3237 /* in case of transport mode, template does not store
3238 any IP addresses, hence we just compare mode and
3239 protocol */
3240 match = 1;
3241 break;
3242 default:
3243 break;
3244 }
3245 }
3246 return match;
3247}
3248
3249/* update endpoint address(es) of template(s) */
3250static int xfrm_policy_migrate(struct xfrm_policy *pol,
3251 struct xfrm_migrate *m, int num_migrate)
3252{
3253 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003254 int i, j, n = 0;
3255
3256 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003257 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003258 /* target policy has been deleted */
3259 write_unlock_bh(&pol->lock);
3260 return -ENOENT;
3261 }
3262
3263 for (i = 0; i < pol->xfrm_nr; i++) {
3264 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3265 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3266 continue;
3267 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003268 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3269 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003270 continue;
3271 /* update endpoints */
3272 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3273 sizeof(pol->xfrm_vec[i].id.daddr));
3274 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3275 sizeof(pol->xfrm_vec[i].saddr));
3276 pol->xfrm_vec[i].encap_family = mp->new_family;
3277 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003278 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003279 }
3280 }
3281
3282 write_unlock_bh(&pol->lock);
3283
3284 if (!n)
3285 return -ENODATA;
3286
3287 return 0;
3288}
3289
David S. Millerdd701752011-02-24 00:21:08 -05003290static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003291{
3292 int i, j;
3293
3294 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3295 return -EINVAL;
3296
3297 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003298 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3299 m[i].old_family) &&
3300 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3301 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003302 return -EINVAL;
3303 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3304 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3305 return -EINVAL;
3306
3307 /* check if there is any duplicated entry */
3308 for (j = i + 1; j < num_migrate; j++) {
3309 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3310 sizeof(m[i].old_daddr)) &&
3311 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3312 sizeof(m[i].old_saddr)) &&
3313 m[i].proto == m[j].proto &&
3314 m[i].mode == m[j].mode &&
3315 m[i].reqid == m[j].reqid &&
3316 m[i].old_family == m[j].old_family)
3317 return -EINVAL;
3318 }
3319 }
3320
3321 return 0;
3322}
3323
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003324int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003325 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003326 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003327{
3328 int i, err, nx_cur = 0, nx_new = 0;
3329 struct xfrm_policy *pol = NULL;
3330 struct xfrm_state *x, *xc;
3331 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3332 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3333 struct xfrm_migrate *mp;
3334
3335 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3336 goto out;
3337
3338 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003339 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003340 err = -ENOENT;
3341 goto out;
3342 }
3343
3344 /* Stage 2 - find and update state(s) */
3345 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003346 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003347 x_cur[nx_cur] = x;
3348 nx_cur++;
3349 if ((xc = xfrm_state_migrate(x, mp))) {
3350 x_new[nx_new] = xc;
3351 nx_new++;
3352 } else {
3353 err = -ENODATA;
3354 goto restore_state;
3355 }
3356 }
3357 }
3358
3359 /* Stage 3 - update policy */
3360 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3361 goto restore_state;
3362
3363 /* Stage 4 - delete old state(s) */
3364 if (nx_cur) {
3365 xfrm_states_put(x_cur, nx_cur);
3366 xfrm_states_delete(x_cur, nx_cur);
3367 }
3368
3369 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003370 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003371
3372 xfrm_pol_put(pol);
3373
3374 return 0;
3375out:
3376 return err;
3377
3378restore_state:
3379 if (pol)
3380 xfrm_pol_put(pol);
3381 if (nx_cur)
3382 xfrm_states_put(x_cur, nx_cur);
3383 if (nx_new)
3384 xfrm_states_delete(x_new, nx_new);
3385
3386 return err;
3387}
David S. Millere610e672007-02-08 13:29:15 -08003388EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003389#endif