blob: 7698785876d716aaa71ea159dc8f111b0420653b [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 Westphala2817d82017-02-07 15:00:17 +010048static struct xfrm_policy_afinfo __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
119static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
120{
121 rcu_read_unlock();
122}
123
David Ahern42a7b322015-08-10 16:58:11 -0600124static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
125 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500126 const xfrm_address_t *saddr,
127 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900128 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800130 struct xfrm_policy_afinfo *afinfo;
131 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Herbert Xu25ee3282007-12-11 09:32:34 -0800133 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800135 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David Ahern42a7b322015-08-10 16:58:11 -0600137 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140
141 return dst;
142}
143
David Ahern42a7b322015-08-10 16:58:11 -0600144static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
145 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900146 xfrm_address_t *prev_saddr,
147 xfrm_address_t *prev_daddr,
148 int family)
149{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800150 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900151 xfrm_address_t *saddr = &x->props.saddr;
152 xfrm_address_t *daddr = &x->id.daddr;
153 struct dst_entry *dst;
154
155 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
156 saddr = x->coaddr;
157 daddr = prev_daddr;
158 }
159 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
160 saddr = prev_saddr;
161 daddr = x->coaddr;
162 }
163
David Ahern42a7b322015-08-10 16:58:11 -0600164 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900165
166 if (!IS_ERR(dst)) {
167 if (prev_saddr != saddr)
168 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
169 if (prev_daddr != daddr)
170 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
171 }
172
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800173 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static inline unsigned long make_jiffies(long secs)
177{
178 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
179 return MAX_SCHEDULE_TIMEOUT-1;
180 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900181 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static void xfrm_policy_timer(unsigned long data)
185{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800186 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800187 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 long next = LONG_MAX;
189 int warn = 0;
190 int dir;
191
192 read_lock(&xp->lock);
193
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000194 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700197 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (xp->lft.hard_add_expires_seconds) {
200 long tmo = xp->lft.hard_add_expires_seconds +
201 xp->curlft.add_time - now;
202 if (tmo <= 0)
203 goto expired;
204 if (tmo < next)
205 next = tmo;
206 }
207 if (xp->lft.hard_use_expires_seconds) {
208 long tmo = xp->lft.hard_use_expires_seconds +
209 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
210 if (tmo <= 0)
211 goto expired;
212 if (tmo < next)
213 next = tmo;
214 }
215 if (xp->lft.soft_add_expires_seconds) {
216 long tmo = xp->lft.soft_add_expires_seconds +
217 xp->curlft.add_time - now;
218 if (tmo <= 0) {
219 warn = 1;
220 tmo = XFRM_KM_TIMEOUT;
221 }
222 if (tmo < next)
223 next = tmo;
224 }
225 if (xp->lft.soft_use_expires_seconds) {
226 long tmo = xp->lft.soft_use_expires_seconds +
227 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
228 if (tmo <= 0) {
229 warn = 1;
230 tmo = XFRM_KM_TIMEOUT;
231 }
232 if (tmo < next)
233 next = tmo;
234 }
235
236 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800237 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (next != LONG_MAX &&
239 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
240 xfrm_pol_hold(xp);
241
242out:
243 read_unlock(&xp->lock);
244 xfrm_pol_put(xp);
245 return;
246
247expired:
248 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700249 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800250 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfrm_pol_put(xp);
252}
253
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000254static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
255{
256 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
257
258 if (unlikely(pol->walk.dead))
259 flo = NULL;
260 else
261 xfrm_pol_hold(pol);
262
263 return flo;
264}
265
266static int xfrm_policy_flo_check(struct flow_cache_object *flo)
267{
268 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
269
270 return !pol->walk.dead;
271}
272
273static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
274{
275 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
276}
277
278static const struct flow_cache_ops xfrm_policy_fc_ops = {
279 .get = xfrm_policy_flo_get,
280 .check = xfrm_policy_flo_check,
281 .delete = xfrm_policy_flo_delete,
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
285 * SPD calls.
286 */
287
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800288struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct xfrm_policy *policy;
291
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700292 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800295 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700296 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700297 INIT_HLIST_NODE(&policy->bydst);
298 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700300 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100301 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800302 setup_timer(&policy->timer, xfrm_policy_timer,
303 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100304 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
305 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000306 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 return policy;
309}
310EXPORT_SYMBOL(xfrm_policy_alloc);
311
Eric Dumazet56f04732015-12-08 07:22:01 -0800312static void xfrm_policy_destroy_rcu(struct rcu_head *head)
313{
314 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
315
316 security_xfrm_policy_free(policy->security);
317 kfree(policy);
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Destroy xfrm_policy: descendant resources must be released to this moment. */
321
WANG Cong64c31b32008-01-07 22:34:29 -0800322void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Herbert Xu12a169e2008-10-01 07:03:24 -0700324 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Fan Du0659eea2013-08-01 18:08:36 +0800326 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 BUG();
328
Eric Dumazet56f04732015-12-08 07:22:01 -0800329 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
WANG Cong64c31b32008-01-07 22:34:29 -0800331EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100333/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * entry dead. The rule must be unlinked from lists to the moment.
335 */
336
337static void xfrm_policy_kill(struct xfrm_policy *policy)
338{
Herbert Xu12a169e2008-10-01 07:03:24 -0700339 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Timo Teräs285ead12010-04-07 00:30:06 +0000341 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200343 if (del_timer(&policy->polq.hold_timer))
344 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800345 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100346
Timo Teräs285ead12010-04-07 00:30:06 +0000347 if (del_timer(&policy->timer))
348 xfrm_pol_put(policy);
349
350 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
David S. Miller2518c7c2006-08-24 04:45:07 -0700353static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
354
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800355static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700356{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800357 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700358}
359
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200360/* calculate policy hash thresholds */
361static void __get_hash_thresh(struct net *net,
362 unsigned short family, int dir,
363 u8 *dbits, u8 *sbits)
364{
365 switch (family) {
366 case AF_INET:
367 *dbits = net->xfrm.policy_bydst[dir].dbits4;
368 *sbits = net->xfrm.policy_bydst[dir].sbits4;
369 break;
370
371 case AF_INET6:
372 *dbits = net->xfrm.policy_bydst[dir].dbits6;
373 *sbits = net->xfrm.policy_bydst[dir].sbits6;
374 break;
375
376 default:
377 *dbits = 0;
378 *sbits = 0;
379 }
380}
381
David S. Miller5f803b52011-02-24 00:33:19 -0500382static struct hlist_head *policy_hash_bysel(struct net *net,
383 const struct xfrm_selector *sel,
384 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700385{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800386 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200387 unsigned int hash;
388 u8 dbits;
389 u8 sbits;
390
391 __get_hash_thresh(net, family, dir, &dbits, &sbits);
392 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700393
Florian Westphale1e551b2016-08-11 15:17:53 +0200394 if (hash == hmask + 1)
395 return &net->xfrm.policy_inexact[dir];
396
397 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
398 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700399}
400
David S. Miller5f803b52011-02-24 00:33:19 -0500401static struct hlist_head *policy_hash_direct(struct net *net,
402 const xfrm_address_t *daddr,
403 const xfrm_address_t *saddr,
404 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700405{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800406 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200407 unsigned int hash;
408 u8 dbits;
409 u8 sbits;
410
411 __get_hash_thresh(net, family, dir, &dbits, &sbits);
412 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700413
Florian Westphale1e551b2016-08-11 15:17:53 +0200414 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
415 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416}
417
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418static void xfrm_dst_hash_transfer(struct net *net,
419 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200421 unsigned int nhashmask,
422 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700423{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800424 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700425 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800426 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200427 u8 dbits;
428 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700429
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800431 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700432 unsigned int h;
433
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200434 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700435 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200436 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200438 hlist_del_rcu(&pol->bydst);
439 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800440 h0 = h;
441 } else {
442 if (h != h0)
443 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200444 hlist_del_rcu(&pol->bydst);
445 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800446 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800447 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800448 }
449 if (!hlist_empty(list)) {
450 entry0 = NULL;
451 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700452 }
453}
454
455static void xfrm_idx_hash_transfer(struct hlist_head *list,
456 struct hlist_head *nidxtable,
457 unsigned int nhashmask)
458{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 struct xfrm_policy *pol;
461
Sasha Levinb67bfe02013-02-27 17:06:00 -0800462 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700463 unsigned int h;
464
465 h = __idx_hash(pol->index, nhashmask);
466 hlist_add_head(&pol->byidx, nidxtable+h);
467 }
468}
469
470static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
471{
472 return ((old_hmask + 1) << 1) - 1;
473}
474
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800475static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700476{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800477 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
479 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700480 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200481 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700482 int i;
483
484 if (!ndst)
485 return;
486
Florian Westphal9d0380d2016-08-11 15:17:59 +0200487 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200488 write_seqcount_begin(&xfrm_policy_hash_generation);
489
490 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
491 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700492
Florian Westphale1e551b2016-08-11 15:17:53 +0200493 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
494 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
495
David S. Miller2518c7c2006-08-24 04:45:07 -0700496 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200497 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498
Florian Westphale1e551b2016-08-11 15:17:53 +0200499 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800500 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700501
Florian Westphal30846092016-08-11 15:17:54 +0200502 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200503 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504
Florian Westphale1e551b2016-08-11 15:17:53 +0200505 synchronize_rcu();
506
David S. Miller44e36b42006-08-24 04:50:50 -0700507 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700508}
509
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700511{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800512 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700513 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
514 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800515 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700516 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700517 int i;
518
519 if (!nidx)
520 return;
521
Florian Westphal9d0380d2016-08-11 15:17:59 +0200522 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700523
524 for (i = hmask; i >= 0; i--)
525 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
526
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800527 net->xfrm.policy_byidx = nidx;
528 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700529
Florian Westphal9d0380d2016-08-11 15:17:59 +0200530 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700531
David S. Miller44e36b42006-08-24 04:50:50 -0700532 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700533}
534
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800535static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700536{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800537 unsigned int cnt = net->xfrm.policy_count[dir];
538 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700539
540 if (total)
541 *total += cnt;
542
543 if ((hmask + 1) < xfrm_policy_hashmax &&
544 cnt > hmask)
545 return 1;
546
547 return 0;
548}
549
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800550static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700551{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800552 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700553
554 if ((hmask + 1) < xfrm_policy_hashmax &&
555 total > hmask)
556 return 1;
557
558 return 0;
559}
560
Alexey Dobriyane0710412010-01-23 13:37:10 +0000561void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700562{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000563 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
564 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
565 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
566 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
567 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
568 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
569 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700570 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700571}
572EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700573
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700574static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800575static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700576{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800577 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700578 int dir, total;
579
580 mutex_lock(&hash_resize_mutex);
581
582 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800583 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800584 if (xfrm_bydst_should_resize(net, dir, &total))
585 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700586 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800587 if (xfrm_byidx_should_resize(net, total))
588 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700589
590 mutex_unlock(&hash_resize_mutex);
591}
592
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200593static void xfrm_hash_rebuild(struct work_struct *work)
594{
595 struct net *net = container_of(work, struct net,
596 xfrm.policy_hthresh.work);
597 unsigned int hmask;
598 struct xfrm_policy *pol;
599 struct xfrm_policy *policy;
600 struct hlist_head *chain;
601 struct hlist_head *odst;
602 struct hlist_node *newpos;
603 int i;
604 int dir;
605 unsigned seq;
606 u8 lbits4, rbits4, lbits6, rbits6;
607
608 mutex_lock(&hash_resize_mutex);
609
610 /* read selector prefixlen thresholds */
611 do {
612 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
613
614 lbits4 = net->xfrm.policy_hthresh.lbits4;
615 rbits4 = net->xfrm.policy_hthresh.rbits4;
616 lbits6 = net->xfrm.policy_hthresh.lbits6;
617 rbits6 = net->xfrm.policy_hthresh.rbits6;
618 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
619
Florian Westphal9d0380d2016-08-11 15:17:59 +0200620 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200621
622 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800623 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200624 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
625 hmask = net->xfrm.policy_bydst[dir].hmask;
626 odst = net->xfrm.policy_bydst[dir].table;
627 for (i = hmask; i >= 0; i--)
628 INIT_HLIST_HEAD(odst + i);
629 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
630 /* dir out => dst = remote, src = local */
631 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
632 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
633 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
634 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
635 } else {
636 /* dir in/fwd => dst = local, src = remote */
637 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
638 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
639 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
640 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
641 }
642 }
643
644 /* re-insert all policies by order of creation */
645 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200646 if (xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
647 /* skip socket policies */
648 continue;
649 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200650 newpos = NULL;
651 chain = policy_hash_bysel(net, &policy->selector,
652 policy->family,
653 xfrm_policy_id2dir(policy->index));
654 hlist_for_each_entry(pol, chain, bydst) {
655 if (policy->priority >= pol->priority)
656 newpos = &pol->bydst;
657 else
658 break;
659 }
660 if (newpos)
661 hlist_add_behind(&policy->bydst, newpos);
662 else
663 hlist_add_head(&policy->bydst, chain);
664 }
665
Florian Westphal9d0380d2016-08-11 15:17:59 +0200666 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200667
668 mutex_unlock(&hash_resize_mutex);
669}
670
671void xfrm_policy_hash_rebuild(struct net *net)
672{
673 schedule_work(&net->xfrm.policy_hthresh.work);
674}
675EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/* Generate new index... KAME seems to generate them ordered by cost
678 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800679static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 static u32 idx_generator;
682
683 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700684 struct hlist_head *list;
685 struct xfrm_policy *p;
686 u32 idx;
687 int found;
688
Fan Due682adf02013-11-07 17:47:48 +0800689 if (!index) {
690 idx = (idx_generator | dir);
691 idx_generator += 8;
692 } else {
693 idx = index;
694 index = 0;
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (idx == 0)
698 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800699 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800701 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700702 if (p->index == idx) {
703 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700707 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return idx;
709 }
710}
711
David S. Miller2518c7c2006-08-24 04:45:07 -0700712static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
713{
714 u32 *p1 = (u32 *) s1;
715 u32 *p2 = (u32 *) s2;
716 int len = sizeof(struct xfrm_selector) / sizeof(u32);
717 int i;
718
719 for (i = 0; i < len; i++) {
720 if (p1[i] != p2[i])
721 return 1;
722 }
723
724 return 0;
725}
726
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100727static void xfrm_policy_requeue(struct xfrm_policy *old,
728 struct xfrm_policy *new)
729{
730 struct xfrm_policy_queue *pq = &old->polq;
731 struct sk_buff_head list;
732
Li RongQingde2ad482015-04-30 17:25:19 +0800733 if (skb_queue_empty(&pq->hold_queue))
734 return;
735
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100736 __skb_queue_head_init(&list);
737
738 spin_lock_bh(&pq->hold_queue.lock);
739 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200740 if (del_timer(&pq->hold_timer))
741 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100742 spin_unlock_bh(&pq->hold_queue.lock);
743
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100744 pq = &new->polq;
745
746 spin_lock_bh(&pq->hold_queue.lock);
747 skb_queue_splice(&list, &pq->hold_queue);
748 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200749 if (!mod_timer(&pq->hold_timer, jiffies))
750 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100751 spin_unlock_bh(&pq->hold_queue.lock);
752}
753
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100754static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
755 struct xfrm_policy *pol)
756{
757 u32 mark = policy->mark.v & policy->mark.m;
758
759 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
760 return true;
761
762 if ((mark & pol->mark.m) == pol->mark.v &&
763 policy->priority == pol->priority)
764 return true;
765
766 return false;
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
770{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800771 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700772 struct xfrm_policy *pol;
773 struct xfrm_policy *delpol;
774 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800775 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Florian Westphal9d0380d2016-08-11 15:17:59 +0200777 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800778 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700779 delpol = NULL;
780 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800781 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800782 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700783 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100784 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800785 xfrm_sec_ctx_match(pol->security, policy->security) &&
786 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200788 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EEXIST;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 delpol = pol;
792 if (policy->priority > pol->priority)
793 continue;
794 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800795 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 continue;
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (delpol)
799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700802 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700803 else
804 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800805 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800806 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800807
808 /* After previous checking, family can either be AF_INET or AF_INET6 */
809 if (policy->family == AF_INET)
810 rt_genid_bump_ipv4(net);
811 else
812 rt_genid_bump_ipv6(net);
813
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100814 if (delpol) {
815 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800816 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100817 }
Fan Due682adf02013-11-07 17:47:48 +0800818 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800819 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800820 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 policy->curlft.use_time = 0;
822 if (!mod_timer(&policy->timer, jiffies + HZ))
823 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200824 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David S. Miller9b78a822005-12-22 07:39:48 -0800826 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800828 else if (xfrm_bydst_should_resize(net, dir, NULL))
829 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
832}
833EXPORT_SYMBOL(xfrm_policy_insert);
834
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000835struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
836 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800837 struct xfrm_sec_ctx *ctx, int delete,
838 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 struct xfrm_policy *pol, *ret;
841 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Eric Parisef41aaa2007-03-07 15:37:58 -0800843 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200844 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800845 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800847 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000849 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700850 !selector_cmp(sel, &pol->selector) &&
851 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700853 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700854 *err = security_xfrm_policy_delete(
855 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800856 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200857 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800858 return pol;
859 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800860 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700861 }
862 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864 }
865 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200866 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000868 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700869 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Trent Jaegerdf718372005-12-13 23:12:27 -0800872EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000874struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
875 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
David S. Miller2518c7c2006-08-24 04:45:07 -0700877 struct xfrm_policy *pol, *ret;
878 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Herbert Xub5505c62007-05-14 02:15:47 -0700880 *err = -ENOENT;
881 if (xfrm_policy_id2dir(id) != dir)
882 return NULL;
883
Eric Parisef41aaa2007-03-07 15:37:58 -0800884 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200885 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800886 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700887 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800888 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000889 if (pol->type == type && pol->index == id &&
890 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700892 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700893 *err = security_xfrm_policy_delete(
894 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800895 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200896 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800897 return pol;
898 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800899 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700900 }
901 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903 }
904 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200905 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000907 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700908 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911EXPORT_SYMBOL(xfrm_policy_byid);
912
Joy Latten4aa2e622007-06-04 19:05:57 -0400913#ifdef CONFIG_SECURITY_NETWORK_XFRM
914static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900915xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Joy Latten4aa2e622007-06-04 19:05:57 -0400917 int dir, err = 0;
918
919 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
920 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400921 int i;
922
Sasha Levinb67bfe02013-02-27 17:06:00 -0800923 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800924 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400925 if (pol->type != type)
926 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700927 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400928 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900929 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400930 return err;
931 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900932 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800933 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800934 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800935 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400936 bydst) {
937 if (pol->type != type)
938 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700939 err = security_xfrm_policy_delete(
940 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400941 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700942 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900943 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400944 return err;
945 }
946 }
947 }
948 }
949 return err;
950}
951#else
952static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900953xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400954{
955 return 0;
956}
957#endif
958
Tetsuo Handa2e710292014-04-22 21:48:30 +0900959int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400960{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000961 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Florian Westphal9d0380d2016-08-11 15:17:59 +0200963 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400964
Tetsuo Handa2e710292014-04-22 21:48:30 +0900965 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400966 if (err)
967 goto out;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700970 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800971 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700972
973 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800974 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800975 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700976 if (pol->type != type)
977 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000978 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200979 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000980 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Tetsuo Handa2e710292014-04-22 21:48:30 +0900982 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600983
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Steffen Klassert4141b362016-08-24 13:08:40 +0200986 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700987 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700989
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800990 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700991 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800992 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800993 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700994 bydst) {
995 if (pol->type != type)
996 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000997 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200998 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000999 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -07001000
Tetsuo Handa2e710292014-04-22 21:48:30 +09001001 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -07001002 xfrm_policy_kill(pol);
1003
Florian Westphal9d0380d2016-08-11 15:17:59 +02001004 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001005 goto again2;
1006 }
1007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001010 if (!cnt)
1011 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001012out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001013 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001014 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016EXPORT_SYMBOL(xfrm_policy_flush);
1017
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001018int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001019 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 void *data)
1021{
Herbert Xu12a169e2008-10-01 07:03:24 -07001022 struct xfrm_policy *pol;
1023 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001024 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Timo Teras4c563f72008-02-28 21:31:08 -08001026 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1027 walk->type != XFRM_POLICY_TYPE_ANY)
1028 return -EINVAL;
1029
Herbert Xu12a169e2008-10-01 07:03:24 -07001030 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001031 return 0;
1032
Florian Westphal9d0380d2016-08-11 15:17:59 +02001033 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001034 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001035 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001036 else
Li RongQing80077702015-04-22 17:09:54 +08001037 x = list_first_entry(&walk->walk.all,
1038 struct xfrm_policy_walk_entry, all);
1039
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001040 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001041 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001042 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001043 pol = container_of(x, struct xfrm_policy, walk);
1044 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1045 walk->type != pol->type)
1046 continue;
1047 error = func(pol, xfrm_policy_id2dir(pol->index),
1048 walk->seq, data);
1049 if (error) {
1050 list_move_tail(&walk->walk.all, &x->all);
1051 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001052 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001053 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001055 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001056 error = -ENOENT;
1057 goto out;
1058 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001059 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001061 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return error;
1063}
1064EXPORT_SYMBOL(xfrm_policy_walk);
1065
Herbert Xu12a169e2008-10-01 07:03:24 -07001066void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1067{
1068 INIT_LIST_HEAD(&walk->walk.all);
1069 walk->walk.dead = 1;
1070 walk->type = type;
1071 walk->seq = 0;
1072}
1073EXPORT_SYMBOL(xfrm_policy_walk_init);
1074
Fan Du283bc9f2013-11-07 17:47:50 +08001075void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001076{
1077 if (list_empty(&walk->walk.all))
1078 return;
1079
Florian Westphal9d0380d2016-08-11 15:17:59 +02001080 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001081 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001082 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001083}
1084EXPORT_SYMBOL(xfrm_policy_walk_done);
1085
James Morris134b0fc2006-10-05 15:42:27 -05001086/*
1087 * Find policy to apply to this flow.
1088 *
1089 * Returns 0 if policy found, else an -errno.
1090 */
David S. Millerf299d552011-02-24 01:23:30 -05001091static int xfrm_policy_match(const struct xfrm_policy *pol,
1092 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001093 u8 type, u16 family, int dir)
1094{
David S. Millerf299d552011-02-24 01:23:30 -05001095 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001096 int ret = -ESRCH;
1097 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001098
1099 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001100 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001101 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001102 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001103
1104 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001105 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001106 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001107 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001108
James Morris134b0fc2006-10-05 15:42:27 -05001109 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001110}
1111
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001112static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001113 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001114 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
James Morris134b0fc2006-10-05 15:42:27 -05001116 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001117 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001118 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001119 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001120 unsigned int sequence;
1121 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001122
1123 daddr = xfrm_flowi_daddr(fl, family);
1124 saddr = xfrm_flowi_saddr(fl, family);
1125 if (unlikely(!daddr || !saddr))
1126 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Florian Westphala7c442472016-08-11 15:17:56 +02001128 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001129 retry:
1130 do {
1131 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1132 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1133 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1134
1135 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001136 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001137 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001138 err = xfrm_policy_match(pol, fl, type, family, dir);
1139 if (err) {
1140 if (err == -ESRCH)
1141 continue;
1142 else {
1143 ret = ERR_PTR(err);
1144 goto fail;
1145 }
1146 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001147 ret = pol;
1148 priority = ret->priority;
1149 break;
1150 }
1151 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001152 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001153 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001154 if ((pol->priority >= priority) && ret)
1155 break;
1156
James Morris134b0fc2006-10-05 15:42:27 -05001157 err = xfrm_policy_match(pol, fl, type, family, dir);
1158 if (err) {
1159 if (err == -ESRCH)
1160 continue;
1161 else {
1162 ret = ERR_PTR(err);
1163 goto fail;
1164 }
Li RongQing8faf4912015-05-14 11:16:59 +08001165 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001166 ret = pol;
1167 break;
1168 }
1169 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001170
Florian Westphal30846092016-08-11 15:17:54 +02001171 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1172 goto retry;
1173
Florian Westphale37cc8a2016-08-11 15:17:55 +02001174 if (ret && !xfrm_pol_hold_rcu(ret))
1175 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001176fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001177 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001178
David S. Miller2518c7c2006-08-24 04:45:07 -07001179 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001180}
1181
Timo Teräs80c802f2010-04-07 00:30:05 +00001182static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001183__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001184{
1185#ifdef CONFIG_XFRM_SUB_POLICY
1186 struct xfrm_policy *pol;
1187
1188 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1189 if (pol != NULL)
1190 return pol;
1191#endif
1192 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1193}
1194
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001195static int flow_to_policy_dir(int dir)
1196{
1197 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1198 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1199 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1200 return dir;
1201
1202 switch (dir) {
1203 default:
1204 case FLOW_DIR_IN:
1205 return XFRM_POLICY_IN;
1206 case FLOW_DIR_OUT:
1207 return XFRM_POLICY_OUT;
1208 case FLOW_DIR_FWD:
1209 return XFRM_POLICY_FWD;
1210 }
1211}
1212
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001213static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001214xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001215 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001216{
1217 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001218
1219 if (old_obj)
1220 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001221
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001222 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001223 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001224 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001225
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001226 /* Resolver returns two references:
1227 * one for cache and one for caller of flow_cache_lookup() */
1228 xfrm_pol_hold(pol);
1229
1230 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Trent Jaegerdf718372005-12-13 23:12:27 -08001233static inline int policy_to_flow_dir(int dir)
1234{
1235 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001236 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1237 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1238 return dir;
1239 switch (dir) {
1240 default:
1241 case XFRM_POLICY_IN:
1242 return FLOW_DIR_IN;
1243 case XFRM_POLICY_OUT:
1244 return FLOW_DIR_OUT;
1245 case XFRM_POLICY_FWD:
1246 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001247 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001248}
1249
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001250static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001251 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct xfrm_policy *pol;
1254
Eric Dumazetd188ba82015-12-08 07:22:02 -08001255 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001256 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001257 pol = rcu_dereference(sk->sk_policy[dir]);
1258 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001259 bool match = xfrm_selector_match(&pol->selector, fl,
1260 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001261 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001262
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001263 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001264 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1265 pol = NULL;
1266 goto out;
1267 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001268 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001269 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001270 policy_to_flow_dir(dir));
Florian Westphal330e8322016-11-17 13:21:46 +01001271 if (!err) {
1272 if (!xfrm_pol_hold_rcu(pol))
1273 goto again;
1274 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001275 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001276 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001277 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001278 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001279 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 pol = NULL;
1281 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001282out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001283 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return pol;
1285}
1286
1287static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1288{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001289 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001290
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001291 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001292 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 xfrm_pol_hold(pol);
1294}
1295
1296static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1297 int dir)
1298{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001299 struct net *net = xp_net(pol);
1300
Herbert Xu53c2e282014-11-13 17:09:49 +08001301 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001302 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Herbert Xu53c2e282014-11-13 17:09:49 +08001304 /* Socket policies are not hashed. */
1305 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001306 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001307 hlist_del(&pol->byidx);
1308 }
1309
1310 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001311 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001312
1313 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Herbert Xu53c2e282014-11-13 17:09:49 +08001316static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1317{
1318 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1319}
1320
1321static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1322{
1323 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1324}
1325
Herbert Xu4666faa2005-06-18 22:43:22 -07001326int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Fan Du283bc9f2013-11-07 17:47:50 +08001328 struct net *net = xp_net(pol);
1329
Florian Westphal9d0380d2016-08-11 15:17:59 +02001330 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001332 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001337 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
David S. Millera70fcb02006-03-20 19:18:52 -08001339EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1342{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001343 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct xfrm_policy *old_pol;
1345
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001346#ifdef CONFIG_XFRM_SUB_POLICY
1347 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1348 return -EINVAL;
1349#endif
1350
Florian Westphal9d0380d2016-08-11 15:17:59 +02001351 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001352 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1353 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001355 pol->curlft.add_time = get_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001356 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001357 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001359 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001360 if (old_pol) {
1361 if (pol)
1362 xfrm_policy_requeue(old_pol, pol);
1363
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001364 /* Unlinking succeeds always. This is the only function
1365 * allowed to delete or replace socket policy.
1366 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001367 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001368 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001369 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (old_pol) {
1372 xfrm_policy_kill(old_pol);
1373 }
1374 return 0;
1375}
1376
David S. Millerd3e40a92011-02-24 01:25:41 -05001377static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001379 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001380 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 if (newp) {
1383 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001384 if (security_xfrm_policy_clone(old->security,
1385 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001386 kfree(newp);
1387 return NULL; /* ENOMEM */
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 newp->lft = old->lft;
1390 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001391 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 newp->action = old->action;
1393 newp->flags = old->flags;
1394 newp->xfrm_nr = old->xfrm_nr;
1395 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001396 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 memcpy(newp->xfrm_vec, old->xfrm_vec,
1398 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001399 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001400 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001401 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 xfrm_pol_put(newp);
1403 }
1404 return newp;
1405}
1406
Eric Dumazetd188ba82015-12-08 07:22:02 -08001407int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001409 const struct xfrm_policy *p;
1410 struct xfrm_policy *np;
1411 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Eric Dumazetd188ba82015-12-08 07:22:02 -08001413 rcu_read_lock();
1414 for (i = 0; i < 2; i++) {
1415 p = rcu_dereference(osk->sk_policy[i]);
1416 if (p) {
1417 np = clone_policy(p, i);
1418 if (unlikely(!np)) {
1419 ret = -ENOMEM;
1420 break;
1421 }
1422 rcu_assign_pointer(sk->sk_policy[i], np);
1423 }
1424 }
1425 rcu_read_unlock();
1426 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001429static int
David Ahern42a7b322015-08-10 16:58:11 -06001430xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1431 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001432{
1433 int err;
1434 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1435
1436 if (unlikely(afinfo == NULL))
1437 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001438 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001439 xfrm_policy_put_afinfo(afinfo);
1440 return err;
1441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/* Resolve list of templates for the flow, given policy. */
1444
1445static int
David S. Millera6c2e612011-02-22 18:35:39 -08001446xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1447 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001449 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int nx;
1451 int i, error;
1452 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1453 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001454 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Weilong Chen9b7a7872013-12-24 09:43:46 +08001456 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct xfrm_state *x;
1458 xfrm_address_t *remote = daddr;
1459 xfrm_address_t *local = saddr;
1460 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1461
Joakim Koskela48b8d782007-07-26 00:08:42 -07001462 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1463 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 remote = &tmpl->id.daddr;
1465 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001466 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001467 error = xfrm_get_saddr(net, fl->flowi_oif,
1468 &tmp, remote,
1469 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001470 if (error)
1471 goto fail;
1472 local = &tmp;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
1476 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1477
1478 if (x && x->km.state == XFRM_STATE_VALID) {
1479 xfrm[nx++] = x;
1480 daddr = remote;
1481 saddr = local;
1482 continue;
1483 }
1484 if (x) {
1485 error = (x->km.state == XFRM_STATE_ERROR ?
1486 -EINVAL : -EAGAIN);
1487 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001488 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001489 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (!tmpl->optional)
1493 goto fail;
1494 }
1495 return nx;
1496
1497fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001498 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 xfrm_state_put(xfrm[nx]);
1500 return error;
1501}
1502
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001503static int
David S. Millera6c2e612011-02-22 18:35:39 -08001504xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1505 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001506{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001507 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1508 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001509 int cnx = 0;
1510 int error;
1511 int ret;
1512 int i;
1513
1514 for (i = 0; i < npols; i++) {
1515 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1516 error = -ENOBUFS;
1517 goto fail;
1518 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001519
1520 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001521 if (ret < 0) {
1522 error = ret;
1523 goto fail;
1524 } else
1525 cnx += ret;
1526 }
1527
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001528 /* found states are sorted for outbound processing */
1529 if (npols > 1)
1530 xfrm_state_sort(xfrm, tpp, cnx, family);
1531
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001532 return cnx;
1533
1534 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001535 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001536 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001537 return error;
1538
1539}
1540
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001541static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001542{
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001543 struct xfrm_policy_afinfo *afinfo;
1544 int tos = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001545
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001546 afinfo = xfrm_policy_get_afinfo(family);
1547 tos = afinfo ? afinfo->get_tos(fl) : 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001548
1549 xfrm_policy_put_afinfo(afinfo);
1550
1551 return tos;
1552}
1553
Timo Teräs80c802f2010-04-07 00:30:05 +00001554static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1555{
1556 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1557 struct dst_entry *dst = &xdst->u.dst;
1558
1559 if (xdst->route == NULL) {
1560 /* Dummy bundle - if it has xfrms we were not
1561 * able to build bundle as template resolution failed.
1562 * It means we need to try again resolving. */
1563 if (xdst->num_xfrms > 0)
1564 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001565 } else if (dst->flags & DST_XFRM_QUEUE) {
1566 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001567 } else {
1568 /* Real bundle */
1569 if (stale_bundle(dst))
1570 return NULL;
1571 }
1572
1573 dst_hold(dst);
1574 return flo;
1575}
1576
1577static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1578{
1579 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1580 struct dst_entry *dst = &xdst->u.dst;
1581
1582 if (!xdst->route)
1583 return 0;
1584 if (stale_bundle(dst))
1585 return 0;
1586
1587 return 1;
1588}
1589
1590static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1591{
1592 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1593 struct dst_entry *dst = &xdst->u.dst;
1594
1595 dst_free(dst);
1596}
1597
1598static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1599 .get = xfrm_bundle_flo_get,
1600 .check = xfrm_bundle_flo_check,
1601 .delete = xfrm_bundle_flo_delete,
1602};
1603
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001604static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001605{
1606 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001607 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001608 struct xfrm_dst *xdst;
1609
1610 if (!afinfo)
1611 return ERR_PTR(-EINVAL);
1612
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001613 switch (family) {
1614 case AF_INET:
1615 dst_ops = &net->xfrm.xfrm4_dst_ops;
1616 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001617#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001618 case AF_INET6:
1619 dst_ops = &net->xfrm.xfrm6_dst_ops;
1620 break;
1621#endif
1622 default:
1623 BUG();
1624 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001625 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001626
Madalin Bucurd4cae562011-09-26 07:04:36 +00001627 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001628 struct dst_entry *dst = &xdst->u.dst;
1629
1630 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001631 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001632 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001633 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001634
Madalin Bucurd4cae562011-09-26 07:04:36 +00001635 xfrm_policy_put_afinfo(afinfo);
1636
Herbert Xu25ee3282007-12-11 09:32:34 -08001637 return xdst;
1638}
1639
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001640static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1641 int nfheader_len)
1642{
1643 struct xfrm_policy_afinfo *afinfo =
1644 xfrm_policy_get_afinfo(dst->ops->family);
1645 int err;
1646
1647 if (!afinfo)
1648 return -EINVAL;
1649
1650 err = afinfo->init_path(path, dst, nfheader_len);
1651
1652 xfrm_policy_put_afinfo(afinfo);
1653
1654 return err;
1655}
1656
Herbert Xu87c1e122010-03-02 02:51:56 +00001657static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001658 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001659{
1660 struct xfrm_policy_afinfo *afinfo =
1661 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1662 int err;
1663
1664 if (!afinfo)
1665 return -EINVAL;
1666
Herbert Xu87c1e122010-03-02 02:51:56 +00001667 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001668
1669 xfrm_policy_put_afinfo(afinfo);
1670
1671 return err;
1672}
1673
Timo Teräs80c802f2010-04-07 00:30:05 +00001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1676 * all the metrics... Shortly, bundle a bundle.
1677 */
1678
Herbert Xu25ee3282007-12-11 09:32:34 -08001679static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1680 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001681 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001682 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001684 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001685 unsigned long now = jiffies;
1686 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001687 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001688 struct dst_entry *dst_prev = NULL;
1689 struct dst_entry *dst0 = NULL;
1690 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001692 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001693 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001694 int trailer_len = 0;
1695 int tos;
1696 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001697 xfrm_address_t saddr, daddr;
1698
1699 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001700
1701 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001702
1703 dst_hold(dst);
1704
1705 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001706 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001707 struct dst_entry *dst1 = &xdst->u.dst;
1708
1709 err = PTR_ERR(xdst);
1710 if (IS_ERR(xdst)) {
1711 dst_release(dst);
1712 goto put_states;
1713 }
1714
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001715 if (xfrm[i]->sel.family == AF_UNSPEC) {
1716 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1717 xfrm_af2proto(family));
1718 if (!inner_mode) {
1719 err = -EAFNOSUPPORT;
1720 dst_release(dst);
1721 goto put_states;
1722 }
1723 } else
1724 inner_mode = xfrm[i]->inner_mode;
1725
Herbert Xu25ee3282007-12-11 09:32:34 -08001726 if (!dst_prev)
1727 dst0 = dst1;
1728 else {
1729 dst_prev->child = dst_clone(dst1);
1730 dst1->flags |= DST_NOHASH;
1731 }
1732
1733 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001734 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001735
1736 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1737 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001738 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1739 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001740 err = PTR_ERR(dst);
1741 if (IS_ERR(dst))
1742 goto put_states;
1743 } else
1744 dst_hold(dst);
1745
1746 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001747 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001748
David S. Millerf5b0a872012-07-19 12:31:33 -07001749 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001750 dst1->flags |= DST_HOST;
1751 dst1->lastuse = now;
1752
1753 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001754 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001755
1756 dst1->next = dst_prev;
1757 dst_prev = dst1;
1758
1759 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001760 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1761 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001762 trailer_len += xfrm[i]->props.trailer_len;
1763 }
1764
1765 dst_prev->child = dst;
1766 dst0->path = dst;
1767
1768 err = -ENODEV;
1769 dev = dst->dev;
1770 if (!dev)
1771 goto free_dst;
1772
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001773 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001774 xfrm_init_pmtu(dst_prev);
1775
1776 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1777 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1778
Herbert Xu87c1e122010-03-02 02:51:56 +00001779 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001780 if (err)
1781 goto free_dst;
1782
1783 dst_prev->header_len = header_len;
1784 dst_prev->trailer_len = trailer_len;
1785 header_len -= xdst->u.dst.xfrm->props.header_len;
1786 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1787 }
1788
1789out:
1790 return dst0;
1791
1792put_states:
1793 for (; i < nx; i++)
1794 xfrm_state_put(xfrm[i]);
1795free_dst:
1796 if (dst0)
1797 dst_free(dst0);
1798 dst0 = ERR_PTR(err);
1799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Ying Xueda7c2242014-01-08 10:26:39 +08001802#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001803static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001804{
1805 if (!*target) {
1806 *target = kmalloc(size, GFP_ATOMIC);
1807 if (!*target)
1808 return -ENOMEM;
1809 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001810
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001811 memcpy(*target, src, size);
1812 return 0;
1813}
Ying Xueda7c2242014-01-08 10:26:39 +08001814#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001815
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001816static int xfrm_dst_update_parent(struct dst_entry *dst,
1817 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001818{
1819#ifdef CONFIG_XFRM_SUB_POLICY
1820 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1821 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1822 sel, sizeof(*sel));
1823#else
1824 return 0;
1825#endif
1826}
1827
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001828static int xfrm_dst_update_origin(struct dst_entry *dst,
1829 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001830{
1831#ifdef CONFIG_XFRM_SUB_POLICY
1832 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1833 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1834#else
1835 return 0;
1836#endif
1837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
David S. Miller73ff93c2011-02-22 18:33:42 -08001839static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001840 struct xfrm_policy **pols,
1841 int *num_pols, int *num_xfrms)
1842{
1843 int i;
1844
1845 if (*num_pols == 0 || !pols[0]) {
1846 *num_pols = 0;
1847 *num_xfrms = 0;
1848 return 0;
1849 }
1850 if (IS_ERR(pols[0]))
1851 return PTR_ERR(pols[0]);
1852
1853 *num_xfrms = pols[0]->xfrm_nr;
1854
1855#ifdef CONFIG_XFRM_SUB_POLICY
1856 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1857 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1858 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1859 XFRM_POLICY_TYPE_MAIN,
1860 fl, family,
1861 XFRM_POLICY_OUT);
1862 if (pols[1]) {
1863 if (IS_ERR(pols[1])) {
1864 xfrm_pols_put(pols, *num_pols);
1865 return PTR_ERR(pols[1]);
1866 }
Weilong Chen02d08922013-12-24 09:43:48 +08001867 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001868 (*num_xfrms) += pols[1]->xfrm_nr;
1869 }
1870 }
1871#endif
1872 for (i = 0; i < *num_pols; i++) {
1873 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1874 *num_xfrms = -1;
1875 break;
1876 }
1877 }
1878
1879 return 0;
1880
1881}
1882
1883static struct xfrm_dst *
1884xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001885 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001886 struct dst_entry *dst_orig)
1887{
1888 struct net *net = xp_net(pols[0]);
1889 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1890 struct dst_entry *dst;
1891 struct xfrm_dst *xdst;
1892 int err;
1893
1894 /* Try to instantiate a bundle */
1895 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001896 if (err <= 0) {
1897 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001898 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1899 return ERR_PTR(err);
1900 }
1901
1902 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1903 if (IS_ERR(dst)) {
1904 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1905 return ERR_CAST(dst);
1906 }
1907
1908 xdst = (struct xfrm_dst *)dst;
1909 xdst->num_xfrms = err;
1910 if (num_pols > 1)
1911 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1912 else
1913 err = xfrm_dst_update_origin(dst, fl);
1914 if (unlikely(err)) {
1915 dst_free(dst);
1916 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1917 return ERR_PTR(err);
1918 }
1919
1920 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001921 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001922 xdst->policy_genid = atomic_read(&pols[0]->genid);
1923
1924 return xdst;
1925}
1926
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001927static void xfrm_policy_queue_process(unsigned long arg)
1928{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001929 struct sk_buff *skb;
1930 struct sock *sk;
1931 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001932 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001933 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001934 struct xfrm_policy_queue *pq = &pol->polq;
1935 struct flowi fl;
1936 struct sk_buff_head list;
1937
1938 spin_lock(&pq->hold_queue.lock);
1939 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001940 if (!skb) {
1941 spin_unlock(&pq->hold_queue.lock);
1942 goto out;
1943 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001944 dst = skb_dst(skb);
1945 sk = skb->sk;
1946 xfrm_decode_session(skb, &fl, dst->ops->family);
1947 spin_unlock(&pq->hold_queue.lock);
1948
1949 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001950 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001951 if (IS_ERR(dst))
1952 goto purge_queue;
1953
1954 if (dst->flags & DST_XFRM_QUEUE) {
1955 dst_release(dst);
1956
1957 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1958 goto purge_queue;
1959
1960 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001961 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1962 xfrm_pol_hold(pol);
1963 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001964 }
1965
1966 dst_release(dst);
1967
1968 __skb_queue_head_init(&list);
1969
1970 spin_lock(&pq->hold_queue.lock);
1971 pq->timeout = 0;
1972 skb_queue_splice_init(&pq->hold_queue, &list);
1973 spin_unlock(&pq->hold_queue.lock);
1974
1975 while (!skb_queue_empty(&list)) {
1976 skb = __skb_dequeue(&list);
1977
1978 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1979 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001980 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001981 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001982 kfree_skb(skb);
1983 continue;
1984 }
1985
1986 nf_reset(skb);
1987 skb_dst_drop(skb);
1988 skb_dst_set(skb, dst);
1989
Eric W. Biederman13206b62015-10-07 16:48:35 -05001990 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001991 }
1992
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001993out:
1994 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001995 return;
1996
1997purge_queue:
1998 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001999 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002000 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002001}
2002
Eric W. Biedermanede20592015-10-07 16:48:47 -05002003static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002004{
2005 unsigned long sched_next;
2006 struct dst_entry *dst = skb_dst(skb);
2007 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002008 struct xfrm_policy *pol = xdst->pols[0];
2009 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002010
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002011 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002012 kfree_skb(skb);
2013 return 0;
2014 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002015
2016 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2017 kfree_skb(skb);
2018 return -EAGAIN;
2019 }
2020
2021 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002022
2023 spin_lock_bh(&pq->hold_queue.lock);
2024
2025 if (!pq->timeout)
2026 pq->timeout = XFRM_QUEUE_TMO_MIN;
2027
2028 sched_next = jiffies + pq->timeout;
2029
2030 if (del_timer(&pq->hold_timer)) {
2031 if (time_before(pq->hold_timer.expires, sched_next))
2032 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002033 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002034 }
2035
2036 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002037 if (!mod_timer(&pq->hold_timer, sched_next))
2038 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002039
2040 spin_unlock_bh(&pq->hold_queue.lock);
2041
2042 return 0;
2043}
2044
2045static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002046 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002047 const struct flowi *fl,
2048 int num_xfrms,
2049 u16 family)
2050{
2051 int err;
2052 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002053 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002054 struct dst_entry *dst1;
2055 struct xfrm_dst *xdst;
2056
2057 xdst = xfrm_alloc_dst(net, family);
2058 if (IS_ERR(xdst))
2059 return xdst;
2060
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002061 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2062 net->xfrm.sysctl_larval_drop ||
2063 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002064 return xdst;
2065
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002066 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002067 dst1 = &xdst->u.dst;
2068 dst_hold(dst);
2069 xdst->route = dst;
2070
2071 dst_copy_metrics(dst1, dst);
2072
2073 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2074 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2075 dst1->lastuse = jiffies;
2076
2077 dst1->input = dst_discard;
2078 dst1->output = xdst_queue_output;
2079
2080 dst_hold(dst);
2081 dst1->child = dst;
2082 dst1->path = dst;
2083
2084 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2085
2086 err = -ENODEV;
2087 dev = dst->dev;
2088 if (!dev)
2089 goto free_dst;
2090
2091 err = xfrm_fill_dst(xdst, dev, fl);
2092 if (err)
2093 goto free_dst;
2094
2095out:
2096 return xdst;
2097
2098free_dst:
2099 dst_release(dst1);
2100 xdst = ERR_PTR(err);
2101 goto out;
2102}
2103
Timo Teräs80c802f2010-04-07 00:30:05 +00002104static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002105xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002106 struct flow_cache_object *oldflo, void *ctx)
2107{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002108 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002109 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2110 struct xfrm_dst *xdst, *new_xdst;
2111 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2112
2113 /* Check if the policies from old bundle are usable */
2114 xdst = NULL;
2115 if (oldflo) {
2116 xdst = container_of(oldflo, struct xfrm_dst, flo);
2117 num_pols = xdst->num_pols;
2118 num_xfrms = xdst->num_xfrms;
2119 pol_dead = 0;
2120 for (i = 0; i < num_pols; i++) {
2121 pols[i] = xdst->pols[i];
2122 pol_dead |= pols[i]->walk.dead;
2123 }
2124 if (pol_dead) {
2125 dst_free(&xdst->u.dst);
2126 xdst = NULL;
2127 num_pols = 0;
2128 num_xfrms = 0;
2129 oldflo = NULL;
2130 }
2131 }
2132
2133 /* Resolve policies to use if we couldn't get them from
2134 * previous cache entry */
2135 if (xdst == NULL) {
2136 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002137 pols[0] = __xfrm_policy_lookup(net, fl, family,
2138 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002139 err = xfrm_expand_policies(fl, family, pols,
2140 &num_pols, &num_xfrms);
2141 if (err < 0)
2142 goto inc_error;
2143 if (num_pols == 0)
2144 return NULL;
2145 if (num_xfrms <= 0)
2146 goto make_dummy_bundle;
2147 }
2148
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002149 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2150 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002151 if (IS_ERR(new_xdst)) {
2152 err = PTR_ERR(new_xdst);
2153 if (err != -EAGAIN)
2154 goto error;
2155 if (oldflo == NULL)
2156 goto make_dummy_bundle;
2157 dst_hold(&xdst->u.dst);
2158 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002159 } else if (new_xdst == NULL) {
2160 num_xfrms = 0;
2161 if (oldflo == NULL)
2162 goto make_dummy_bundle;
2163 xdst->num_xfrms = 0;
2164 dst_hold(&xdst->u.dst);
2165 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002166 }
2167
2168 /* Kill the previous bundle */
2169 if (xdst) {
2170 /* The policies were stolen for newly generated bundle */
2171 xdst->num_pols = 0;
2172 dst_free(&xdst->u.dst);
2173 }
2174
2175 /* Flow cache does not have reference, it dst_free()'s,
2176 * but we do need to return one reference for original caller */
2177 dst_hold(&new_xdst->u.dst);
2178 return &new_xdst->flo;
2179
2180make_dummy_bundle:
2181 /* We found policies, but there's no bundles to instantiate:
2182 * either because the policy blocks, has no transformations or
2183 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002184 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002185 if (IS_ERR(xdst)) {
2186 xfrm_pols_put(pols, num_pols);
2187 return ERR_CAST(xdst);
2188 }
2189 xdst->num_pols = num_pols;
2190 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002191 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002192
2193 dst_hold(&xdst->u.dst);
2194 return &xdst->flo;
2195
2196inc_error:
2197 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2198error:
2199 if (xdst != NULL)
2200 dst_free(&xdst->u.dst);
2201 else
2202 xfrm_pols_put(pols, num_pols);
2203 return ERR_PTR(err);
2204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
David S. Miller2774c132011-03-01 14:59:04 -08002206static struct dst_entry *make_blackhole(struct net *net, u16 family,
2207 struct dst_entry *dst_orig)
2208{
2209 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2210 struct dst_entry *ret;
2211
2212 if (!afinfo) {
2213 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002214 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002215 } else {
2216 ret = afinfo->blackhole_route(net, dst_orig);
2217 }
2218 xfrm_policy_put_afinfo(afinfo);
2219
2220 return ret;
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223/* Main function: finds/creates a bundle for given flow.
2224 *
2225 * At the moment we eat a raw IP route. Mostly to speed up lookups
2226 * on interfaces with disabled IPsec.
2227 */
David S. Miller452edd52011-03-02 13:27:41 -08002228struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2229 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002230 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002232 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002233 struct flow_cache_object *flo;
2234 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002235 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002236 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002237 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002238 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002239
Timo Teräs80c802f2010-04-07 00:30:05 +00002240 dst = NULL;
2241 xdst = NULL;
2242 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002243
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002244 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002245 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002246 num_pols = 1;
2247 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2248 err = xfrm_expand_policies(fl, family, pols,
2249 &num_pols, &num_xfrms);
2250 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002251 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002252
2253 if (num_pols) {
2254 if (num_xfrms <= 0) {
2255 drop_pols = num_pols;
2256 goto no_transform;
2257 }
2258
2259 xdst = xfrm_resolve_and_create_bundle(
2260 pols, num_pols, fl,
2261 family, dst_orig);
2262 if (IS_ERR(xdst)) {
2263 xfrm_pols_put(pols, num_pols);
2264 err = PTR_ERR(xdst);
2265 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002266 } else if (xdst == NULL) {
2267 num_xfrms = 0;
2268 drop_pols = num_pols;
2269 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002270 }
2271
Steffen Klassertb7eea452014-06-18 12:34:21 +02002272 dst_hold(&xdst->u.dst);
2273 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002274 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002275 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Timo Teräs80c802f2010-04-07 00:30:05 +00002278 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002279 struct xfrm_flo xflo;
2280
2281 xflo.dst_orig = dst_orig;
2282 xflo.flags = flags;
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002285 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002286 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002287 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Timo Teräs80c802f2010-04-07 00:30:05 +00002289 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002290 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002291 if (flo == NULL)
2292 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002293 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002294 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002295 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002296 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002297 xdst = container_of(flo, struct xfrm_dst, flo);
2298
2299 num_pols = xdst->num_pols;
2300 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002301 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002302 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304
Timo Teräs80c802f2010-04-07 00:30:05 +00002305 dst = &xdst->u.dst;
2306 if (route == NULL && num_xfrms > 0) {
2307 /* The only case when xfrm_bundle_lookup() returns a
2308 * bundle with null route, is when the template could
2309 * not be resolved. It means policies are there, but
2310 * bundle could not be created, since we don't yet
2311 * have the xfrm_state's. We need to wait for KM to
2312 * negotiate new SA's or bail out with error.*/
2313 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002314 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002315 err = -EREMOTE;
2316 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002317 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002318
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002319 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002320
2321 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2322 goto error;
2323 }
2324
2325no_transform:
2326 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002327 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Timo Teräs80c802f2010-04-07 00:30:05 +00002329 if ((flags & XFRM_LOOKUP_ICMP) &&
2330 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2331 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002332 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002333 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002334
Timo Teräs80c802f2010-04-07 00:30:05 +00002335 for (i = 0; i < num_pols; i++)
2336 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002337
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002340 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002341 err = -EPERM;
2342 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002343 } else if (num_xfrms > 0) {
2344 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002345 dst_release(dst_orig);
2346 } else {
2347 /* Flow passes untransformed */
2348 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002349 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002351ok:
2352 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002353 if (dst && dst->xfrm &&
2354 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2355 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002356 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Timo Teräs80c802f2010-04-07 00:30:05 +00002358nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002359 if (!(flags & XFRM_LOOKUP_ICMP)) {
2360 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002361 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002362 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002363 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002365 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002366dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002367 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2368 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002369 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002370 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372EXPORT_SYMBOL(xfrm_lookup);
2373
Steffen Klassertf92ee612014-09-16 10:08:40 +02002374/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2375 * Otherwise we may send out blackholed packets.
2376 */
2377struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2378 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002379 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002380{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002381 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002382 flags | XFRM_LOOKUP_QUEUE |
2383 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002384
2385 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2386 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2387
2388 return dst;
2389}
2390EXPORT_SYMBOL(xfrm_lookup_route);
2391
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002392static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002393xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002394{
2395 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002396
2397 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2398 return 0;
2399 x = skb->sp->xvec[idx];
2400 if (!x->type->reject)
2401 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002402 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405/* When skb is transformed back to its "native" form, we have to
2406 * check policy restrictions. At the moment we make this in maximally
2407 * stupid way. Shame on me. :-) Of course, connected sockets must
2408 * have policy cached at them.
2409 */
2410
2411static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002412xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 unsigned short family)
2414{
2415 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002416 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return x->id.proto == tmpl->id.proto &&
2418 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2419 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2420 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002421 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002422 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002423 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2424 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002427/*
2428 * 0 or more than 0 is returned when validation is succeeded (either bypass
2429 * because of optional transport mode, or next index of the mathced secpath
2430 * state with the template.
2431 * -1 is returned when no matching template is found.
2432 * Otherwise "-2 - errored_index" is returned.
2433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002435xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 unsigned short family)
2437{
2438 int idx = start;
2439
2440 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002441 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return start;
2443 } else
2444 start = -1;
2445 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002446 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002448 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2449 if (start == -1)
2450 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 return start;
2455}
2456
Herbert Xud5422ef2007-12-12 10:44:16 -08002457int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2458 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002461 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 if (unlikely(afinfo == NULL))
2464 return -EAFNOSUPPORT;
2465
Herbert Xud5422ef2007-12-12 10:44:16 -08002466 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002467 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
Herbert Xud5422ef2007-12-12 10:44:16 -08002471EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David S. Miller9a7386e2011-02-24 01:44:12 -05002473static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002476 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002477 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481
2482 return 0;
2483}
2484
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002485int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 unsigned short family)
2487{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002488 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002490 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2491 int npols = 0;
2492 int xfrm_nr;
2493 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002494 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002496 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002497 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Herbert Xud5422ef2007-12-12 10:44:16 -08002499 reverse = dir & ~XFRM_POLICY_MASK;
2500 dir &= XFRM_POLICY_MASK;
2501 fl_dir = policy_to_flow_dir(dir);
2502
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002503 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002504 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002506 }
2507
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002508 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 /* First, check used SA against their selectors. */
2511 if (skb->sp) {
2512 int i;
2513
Weilong Chen9b7a7872013-12-24 09:43:46 +08002514 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002515 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002516 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002517 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521 }
2522
2523 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002524 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002525 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002526 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002527 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002528 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002529 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002530 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002533 if (!pol) {
2534 struct flow_cache_object *flo;
2535
2536 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2537 xfrm_policy_lookup, NULL);
2538 if (IS_ERR_OR_NULL(flo))
2539 pol = ERR_CAST(flo);
2540 else
2541 pol = container_of(flo, struct xfrm_policy, flo);
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002544 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002545 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002546 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002547 }
James Morris134b0fc2006-10-05 15:42:27 -05002548
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002549 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002550 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002551 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002552 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002553 return 0;
2554 }
2555 return 1;
2556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
James Morris9d729f72007-03-04 16:12:44 -08002558 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002560 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002561 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002562#ifdef CONFIG_XFRM_SUB_POLICY
2563 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002564 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002565 &fl, family,
2566 XFRM_POLICY_IN);
2567 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002568 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002569 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002570 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002571 }
James Morris9d729f72007-03-04 16:12:44 -08002572 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002573 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002574 }
2575 }
2576#endif
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (pol->action == XFRM_POLICY_ALLOW) {
2579 struct sec_path *sp;
2580 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002581 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002582 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002583 struct xfrm_tmpl **tpp = tp;
2584 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 int i, k;
2586
2587 if ((sp = skb->sp) == NULL)
2588 sp = &dummy;
2589
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002590 for (pi = 0; pi < npols; pi++) {
2591 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002592 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002593 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002594 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002595 }
2596 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002597 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002598 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002599 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002600 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2601 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2602 }
2603 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002604 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002605 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002606 tpp = stp;
2607 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /* For each tunnel xfrm, find the first matching tmpl.
2610 * For each tmpl before that, find corresponding xfrm.
2611 * Order is _important_. Later we will implement
2612 * some barriers, but at the moment barriers
2613 * are implied between each two transformations.
2614 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002615 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2616 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002617 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002618 if (k < -1)
2619 /* "-2 - errored_index" returned */
2620 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002621 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
2625
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002626 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002627 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002631 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return 1;
2633 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002634 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002637 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002638reject_error:
2639 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return 0;
2641}
2642EXPORT_SYMBOL(__xfrm_policy_check);
2643
2644int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2645{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002646 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002648 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002649 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002651 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002652 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002656 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002657
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002658 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002659 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002660 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002661 dst = NULL;
2662 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002663 skb_dst_set(skb, dst);
2664 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666EXPORT_SYMBOL(__xfrm_route_forward);
2667
David S. Millerd49c73c2006-08-13 18:55:53 -07002668/* Optimize later using cookies and generation ids. */
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2671{
David S. Millerd49c73c2006-08-13 18:55:53 -07002672 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002673 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2674 * get validated by dst_ops->check on every use. We do this
2675 * because when a normal route referenced by an XFRM dst is
2676 * obsoleted we do not go looking around for all parent
2677 * referencing XFRM dsts so that we can invalidate them. It
2678 * is just too much work. Instead we make the checks here on
2679 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002680 *
2681 * XFRM dst A --> IPv4 dst X
2682 *
2683 * X is the "xdst->route" of A (X is also the "dst->path" of A
2684 * in this example). If X is marked obsolete, "A" will not
2685 * notice. That's what we are validating here via the
2686 * stale_bundle() check.
2687 *
2688 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002689 * dst which causes it's ->obsolete field to be set to
2690 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2691 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002692 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002693 if (dst->obsolete < 0 && !stale_bundle(dst))
2694 return dst;
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return NULL;
2697}
2698
2699static int stale_bundle(struct dst_entry *dst)
2700{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002701 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Herbert Xuaabc9762005-05-03 16:27:10 -07002704void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002707 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002708 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 dev_put(dev);
2710 }
2711}
Herbert Xuaabc9762005-05-03 16:27:10 -07002712EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714static void xfrm_link_failure(struct sk_buff *skb)
2715{
2716 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
2719static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2720{
2721 if (dst) {
2722 if (dst->obsolete) {
2723 dst_release(dst);
2724 dst = NULL;
2725 }
2726 }
2727 return dst;
2728}
2729
Paul Mooree4c17212013-05-29 07:36:25 +00002730void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002731{
Fan Duca925cf2014-01-18 09:55:27 +08002732 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002733}
Paul Mooree4c17212013-05-29 07:36:25 +00002734EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002735
Florian Westphal3d7d25a2017-02-07 15:00:16 +01002736void xfrm_garbage_collect_deferred(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002737{
Fan Duca925cf2014-01-18 09:55:27 +08002738 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002739}
Florian Westphal3d7d25a2017-02-07 15:00:16 +01002740EXPORT_SYMBOL(xfrm_garbage_collect_deferred);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002741
Herbert Xu25ee3282007-12-11 09:32:34 -08002742static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 do {
2745 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2746 u32 pmtu, route_mtu_cached;
2747
2748 pmtu = dst_mtu(dst->child);
2749 xdst->child_mtu_cached = pmtu;
2750
2751 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2752
2753 route_mtu_cached = dst_mtu(xdst->route);
2754 xdst->route_mtu_cached = route_mtu_cached;
2755
2756 if (pmtu > route_mtu_cached)
2757 pmtu = route_mtu_cached;
2758
David S. Millerdefb3512010-12-08 21:16:57 -08002759 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 } while ((dst = dst->next));
2761}
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763/* Check that the bundle accepts the flow and its components are
2764 * still valid.
2765 */
2766
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002767static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct dst_entry *dst = &first->u.dst;
2770 struct xfrm_dst *last;
2771 u32 mtu;
2772
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002773 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 (dst->dev && !netif_running(dst->dev)))
2775 return 0;
2776
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002777 if (dst->flags & DST_XFRM_QUEUE)
2778 return 1;
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 last = NULL;
2781
2782 do {
2783 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2786 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002787 if (xdst->xfrm_genid != dst->xfrm->genid)
2788 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002789 if (xdst->num_pols > 0 &&
2790 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 mtu = dst_mtu(dst->child);
2794 if (xdst->child_mtu_cached != mtu) {
2795 last = xdst;
2796 xdst->child_mtu_cached = mtu;
2797 }
2798
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002799 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
2801 mtu = dst_mtu(xdst->route);
2802 if (xdst->route_mtu_cached != mtu) {
2803 last = xdst;
2804 xdst->route_mtu_cached = mtu;
2805 }
2806
2807 dst = dst->child;
2808 } while (dst->xfrm);
2809
2810 if (likely(!last))
2811 return 1;
2812
2813 mtu = last->child_mtu_cached;
2814 for (;;) {
2815 dst = &last->u.dst;
2816
2817 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2818 if (mtu > last->route_mtu_cached)
2819 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002820 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 if (last == first)
2823 break;
2824
Patrick McHardybd0bf072007-07-18 01:55:52 -07002825 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 last->child_mtu_cached = mtu;
2827 }
2828
2829 return 1;
2830}
2831
David S. Miller0dbaee32010-12-13 12:52:14 -08002832static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2833{
2834 return dst_metric_advmss(dst->path);
2835}
2836
Steffen Klassertebb762f2011-11-23 02:12:51 +00002837static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002838{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002839 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2840
2841 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002842}
2843
David S. Millerf894cbf2012-07-02 21:52:24 -07002844static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2845 struct sk_buff *skb,
2846 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002847{
David S. Millerf894cbf2012-07-02 21:52:24 -07002848 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002849}
2850
Florian Westphala2817d82017-02-07 15:00:17 +01002851int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01002854
2855 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01002857
Eric Dumazetef8531b2012-08-19 12:31:48 +02002858 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01002859 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002860 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 else {
2862 struct dst_ops *dst_ops = afinfo->dst_ops;
2863 if (likely(dst_ops->kmem_cachep == NULL))
2864 dst_ops->kmem_cachep = xfrm_dst_cache;
2865 if (likely(dst_ops->check == NULL))
2866 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002867 if (likely(dst_ops->default_advmss == NULL))
2868 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002869 if (likely(dst_ops->mtu == NULL))
2870 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (likely(dst_ops->negative_advice == NULL))
2872 dst_ops->negative_advice = xfrm_negative_advice;
2873 if (likely(dst_ops->link_failure == NULL))
2874 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002875 if (likely(dst_ops->neigh_lookup == NULL))
2876 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Florian Westphala2817d82017-02-07 15:00:17 +01002877 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002879 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return err;
2882}
2883EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2884
Florian Westphala2817d82017-02-07 15:00:17 +01002885void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
Florian Westphal2b619972017-02-07 15:00:15 +01002887 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01002888 int i;
Florian Westphal2b619972017-02-07 15:00:15 +01002889
Florian Westphala2817d82017-02-07 15:00:17 +01002890 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
2891 if (xfrm_policy_afinfo[i] != afinfo)
2892 continue;
2893 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
2894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002896
Florian Westphal2b619972017-02-07 15:00:15 +01002897 synchronize_rcu();
Eric Dumazetef8531b2012-08-19 12:31:48 +02002898
Florian Westphal2b619972017-02-07 15:00:15 +01002899 dst_ops->kmem_cachep = NULL;
2900 dst_ops->check = NULL;
2901 dst_ops->negative_advice = NULL;
2902 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2907{
Jiri Pirko351638e2013-05-28 01:30:21 +00002908 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 switch (event) {
2911 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002912 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 return NOTIFY_DONE;
2915}
2916
2917static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002918 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919};
2920
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002921#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002922static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002923{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002924 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002925 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2926 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002927 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002928 rv = xfrm_proc_init(net);
2929 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002930 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002931 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002932}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002933
2934static void xfrm_statistics_fini(struct net *net)
2935{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002936 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002937 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002938}
2939#else
2940static int __net_init xfrm_statistics_init(struct net *net)
2941{
2942 return 0;
2943}
2944
2945static void xfrm_statistics_fini(struct net *net)
2946{
2947}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002948#endif
2949
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002950static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
David S. Miller2518c7c2006-08-24 04:45:07 -07002952 unsigned int hmask, sz;
2953 int dir;
2954
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002955 if (net_eq(net, &init_net))
2956 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002958 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002959 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
David S. Miller2518c7c2006-08-24 04:45:07 -07002961 hmask = 8 - 1;
2962 sz = (hmask+1) * sizeof(struct hlist_head);
2963
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002964 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2965 if (!net->xfrm.policy_byidx)
2966 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002967 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002968
Herbert Xu53c2e282014-11-13 17:09:49 +08002969 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002970 struct xfrm_policy_hash *htab;
2971
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002972 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002973 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002974 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002975
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002976 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002977 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002978 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002979 goto out_bydst;
2980 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002981 htab->dbits4 = 32;
2982 htab->sbits4 = 32;
2983 htab->dbits6 = 128;
2984 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002985 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002986 net->xfrm.policy_hthresh.lbits4 = 32;
2987 net->xfrm.policy_hthresh.rbits4 = 32;
2988 net->xfrm.policy_hthresh.lbits6 = 128;
2989 net->xfrm.policy_hthresh.rbits6 = 128;
2990
2991 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002992
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002993 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002994 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002995 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002996 if (net_eq(net, &init_net))
2997 register_netdevice_notifier(&xfrm_dev_notifier);
2998 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002999
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003000out_bydst:
3001 for (dir--; dir >= 0; dir--) {
3002 struct xfrm_policy_hash *htab;
3003
3004 htab = &net->xfrm.policy_bydst[dir];
3005 xfrm_hash_free(htab->table, sz);
3006 }
3007 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003008out_byidx:
3009 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003012static void xfrm_policy_fini(struct net *net)
3013{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003014 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003015 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003016
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003017 flush_work(&net->xfrm.policy_hash_work);
3018#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003019 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003020#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003021 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003022
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003023 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003024
Herbert Xu53c2e282014-11-13 17:09:49 +08003025 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003026 struct xfrm_policy_hash *htab;
3027
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003028 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003029
3030 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003031 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003032 WARN_ON(!hlist_empty(htab->table));
3033 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003034 }
3035
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003036 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003037 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3038 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003039}
3040
3041static int __net_init xfrm_net_init(struct net *net)
3042{
3043 int rv;
3044
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003045 rv = xfrm_statistics_init(net);
3046 if (rv < 0)
3047 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003048 rv = xfrm_state_init(net);
3049 if (rv < 0)
3050 goto out_state;
3051 rv = xfrm_policy_init(net);
3052 if (rv < 0)
3053 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003054 rv = xfrm_sysctl_init(net);
3055 if (rv < 0)
3056 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003057 rv = flow_cache_init(net);
3058 if (rv < 0)
3059 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003060
3061 /* Initialize the per-net locks here */
3062 spin_lock_init(&net->xfrm.xfrm_state_lock);
Florian Westphal9d0380d2016-08-11 15:17:59 +02003063 spin_lock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003064 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3065
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003066 return 0;
3067
Steffen Klassert4a93f502014-03-12 09:43:17 +01003068out:
3069 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003070out_sysctl:
3071 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003072out_policy:
3073 xfrm_state_fini(net);
3074out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003075 xfrm_statistics_fini(net);
3076out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003077 return rv;
3078}
3079
3080static void __net_exit xfrm_net_exit(struct net *net)
3081{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003082 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003083 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003084 xfrm_policy_fini(net);
3085 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003086 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003087}
3088
3089static struct pernet_operations __net_initdata xfrm_net_ops = {
3090 .init = xfrm_net_init,
3091 .exit = xfrm_net_exit,
3092};
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094void __init xfrm_init(void)
3095{
Sebastian Andrzej Siewiora4fc1bf2016-11-03 15:50:05 +01003096 flow_cache_hp_init();
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003097 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003098 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 xfrm_input_init();
3100}
3101
Joy Lattenab5f5e82007-09-17 11:51:22 -07003102#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003103static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3104 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003105{
Paul Moore875179f2007-12-01 23:27:18 +11003106 struct xfrm_sec_ctx *ctx = xp->security;
3107 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003108
Paul Moore875179f2007-12-01 23:27:18 +11003109 if (ctx)
3110 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3111 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3112
Weilong Chen9b7a7872013-12-24 09:43:46 +08003113 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003114 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003115 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003116 if (sel->prefixlen_s != 32)
3117 audit_log_format(audit_buf, " src_prefixlen=%d",
3118 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003119 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003120 if (sel->prefixlen_d != 32)
3121 audit_log_format(audit_buf, " dst_prefixlen=%d",
3122 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003123 break;
3124 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003125 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003126 if (sel->prefixlen_s != 128)
3127 audit_log_format(audit_buf, " src_prefixlen=%d",
3128 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003129 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003130 if (sel->prefixlen_d != 128)
3131 audit_log_format(audit_buf, " dst_prefixlen=%d",
3132 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003133 break;
3134 }
3135}
3136
Tetsuo Handa2e710292014-04-22 21:48:30 +09003137void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003138{
3139 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003140
Paul Mooreafeb14b2007-12-21 14:58:11 -08003141 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003142 if (audit_buf == NULL)
3143 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003144 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003145 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003146 xfrm_audit_common_policyinfo(xp, audit_buf);
3147 audit_log_end(audit_buf);
3148}
3149EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3150
Paul Moore68277ac2007-12-20 20:49:33 -08003151void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003152 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153{
3154 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003155
Paul Mooreafeb14b2007-12-21 14:58:11 -08003156 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003157 if (audit_buf == NULL)
3158 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003159 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003160 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003161 xfrm_audit_common_policyinfo(xp, audit_buf);
3162 audit_log_end(audit_buf);
3163}
3164EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3165#endif
3166
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003167#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003168static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3169 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003170{
3171 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3172 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003173 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3174 sel_cmp->family) &&
3175 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3176 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003177 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3178 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003179 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003180 }
3181 } else {
3182 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003183 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003184 }
3185 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003186 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003187}
3188
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003189static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3190 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003191{
3192 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003193 struct hlist_head *chain;
3194 u32 priority = ~0U;
3195
Florian Westphal9d0380d2016-08-11 15:17:59 +02003196 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003197 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003198 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003199 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3200 pol->type == type) {
3201 ret = pol;
3202 priority = ret->priority;
3203 break;
3204 }
3205 }
Fan Du8d549c42013-11-07 17:47:49 +08003206 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003207 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003208 if ((pol->priority >= priority) && ret)
3209 break;
3210
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003211 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003212 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003213 ret = pol;
3214 break;
3215 }
3216 }
3217
Li RongQing586f2eb2015-04-30 17:13:41 +08003218 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003219
Florian Westphal9d0380d2016-08-11 15:17:59 +02003220 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003221
3222 return ret;
3223}
3224
David S. Millerdd701752011-02-24 00:21:08 -05003225static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003226{
3227 int match = 0;
3228
3229 if (t->mode == m->mode && t->id.proto == m->proto &&
3230 (m->reqid == 0 || t->reqid == m->reqid)) {
3231 switch (t->mode) {
3232 case XFRM_MODE_TUNNEL:
3233 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003234 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3235 m->old_family) &&
3236 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3237 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003238 match = 1;
3239 }
3240 break;
3241 case XFRM_MODE_TRANSPORT:
3242 /* in case of transport mode, template does not store
3243 any IP addresses, hence we just compare mode and
3244 protocol */
3245 match = 1;
3246 break;
3247 default:
3248 break;
3249 }
3250 }
3251 return match;
3252}
3253
3254/* update endpoint address(es) of template(s) */
3255static int xfrm_policy_migrate(struct xfrm_policy *pol,
3256 struct xfrm_migrate *m, int num_migrate)
3257{
3258 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003259 int i, j, n = 0;
3260
3261 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003262 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003263 /* target policy has been deleted */
3264 write_unlock_bh(&pol->lock);
3265 return -ENOENT;
3266 }
3267
3268 for (i = 0; i < pol->xfrm_nr; i++) {
3269 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3270 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3271 continue;
3272 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003273 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3274 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003275 continue;
3276 /* update endpoints */
3277 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3278 sizeof(pol->xfrm_vec[i].id.daddr));
3279 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3280 sizeof(pol->xfrm_vec[i].saddr));
3281 pol->xfrm_vec[i].encap_family = mp->new_family;
3282 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003283 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003284 }
3285 }
3286
3287 write_unlock_bh(&pol->lock);
3288
3289 if (!n)
3290 return -ENODATA;
3291
3292 return 0;
3293}
3294
David S. Millerdd701752011-02-24 00:21:08 -05003295static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003296{
3297 int i, j;
3298
3299 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3300 return -EINVAL;
3301
3302 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003303 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3304 m[i].old_family) &&
3305 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3306 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003307 return -EINVAL;
3308 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3309 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3310 return -EINVAL;
3311
3312 /* check if there is any duplicated entry */
3313 for (j = i + 1; j < num_migrate; j++) {
3314 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3315 sizeof(m[i].old_daddr)) &&
3316 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3317 sizeof(m[i].old_saddr)) &&
3318 m[i].proto == m[j].proto &&
3319 m[i].mode == m[j].mode &&
3320 m[i].reqid == m[j].reqid &&
3321 m[i].old_family == m[j].old_family)
3322 return -EINVAL;
3323 }
3324 }
3325
3326 return 0;
3327}
3328
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003329int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003330 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003331 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003332{
3333 int i, err, nx_cur = 0, nx_new = 0;
3334 struct xfrm_policy *pol = NULL;
3335 struct xfrm_state *x, *xc;
3336 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3337 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3338 struct xfrm_migrate *mp;
3339
3340 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3341 goto out;
3342
3343 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003344 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003345 err = -ENOENT;
3346 goto out;
3347 }
3348
3349 /* Stage 2 - find and update state(s) */
3350 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003351 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003352 x_cur[nx_cur] = x;
3353 nx_cur++;
3354 if ((xc = xfrm_state_migrate(x, mp))) {
3355 x_new[nx_new] = xc;
3356 nx_new++;
3357 } else {
3358 err = -ENODATA;
3359 goto restore_state;
3360 }
3361 }
3362 }
3363
3364 /* Stage 3 - update policy */
3365 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3366 goto restore_state;
3367
3368 /* Stage 4 - delete old state(s) */
3369 if (nx_cur) {
3370 xfrm_states_put(x_cur, nx_cur);
3371 xfrm_states_delete(x_cur, nx_cur);
3372 }
3373
3374 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003375 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003376
3377 xfrm_pol_put(pol);
3378
3379 return 0;
3380out:
3381 return err;
3382
3383restore_state:
3384 if (pol)
3385 xfrm_pol_put(pol);
3386 if (nx_cur)
3387 xfrm_states_put(x_cur, nx_cur);
3388 if (nx_new)
3389 xfrm_states_delete(x_new, nx_new);
3390
3391 return err;
3392}
David S. Millere610e672007-02-08 13:29:15 -08003393EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003394#endif