blob: 61e6d033c6c85da009cb23e79e8d1a40af5a6929 [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
Steffen Klassertf5bb9e92018-06-12 14:07:12 +020047static DEFINE_SPINLOCK(xfrm_if_cb_lock);
48static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
49
Priyanka Jain418a99a2012-08-12 21:22:29 +000050static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
51static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
52 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Christoph Lametere18b8902006-12-06 20:33:20 -080054static struct kmem_cache *xfrm_dst_cache __read_mostly;
Florian Westphal30846092016-08-11 15:17:54 +020055static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Herbert Xu25ee3282007-12-11 09:32:34 -080057static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000058static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000059static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010060static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Herbert Xu12bfa8b2014-11-13 17:09:50 +080062static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080063static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
64 int dir);
65
Florian Westphale37cc8a2016-08-11 15:17:55 +020066static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
67{
68 return atomic_inc_not_zero(&policy->refcnt);
69}
70
David S. Millerbc9b35a2012-05-15 15:04:57 -040071static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050072__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080073{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050074 const struct flowi4 *fl4 = &fl->u.ip4;
75
Alexey Dobriyan26bff942011-11-22 06:46:02 +000076 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
77 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050078 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
79 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
80 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
81 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080082}
83
David S. Millerbc9b35a2012-05-15 15:04:57 -040084static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050085__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080086{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050087 const struct flowi6 *fl6 = &fl->u.ip6;
88
89 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
90 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
91 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
92 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
93 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
94 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080095}
96
David S. Millerbc9b35a2012-05-15 15:04:57 -040097bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
98 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080099{
100 switch (family) {
101 case AF_INET:
102 return __xfrm4_selector_match(sel, fl);
103 case AF_INET6:
104 return __xfrm6_selector_match(sel, fl);
105 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400106 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800107}
108
Eric Dumazetef8531b2012-08-19 12:31:48 +0200109static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
110{
111 struct xfrm_policy_afinfo *afinfo;
112
113 if (unlikely(family >= NPROTO))
114 return NULL;
115 rcu_read_lock();
116 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
117 if (unlikely(!afinfo))
118 rcu_read_unlock();
119 return afinfo;
120}
121
Steffen Klassertf5bb9e92018-06-12 14:07:12 +0200122/* Called with rcu_read_lock(). */
123static const struct xfrm_if_cb *xfrm_if_get_cb(void)
124{
125 return rcu_dereference(xfrm_if_cb);
126}
127
128static void xfrm_policy_put_afinfo(const struct xfrm_policy_afinfo *afinfo)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200129{
130 rcu_read_unlock();
131}
132
David Ahern42a7b322015-08-10 16:58:11 -0600133static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
134 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500135 const xfrm_address_t *saddr,
136 const xfrm_address_t *daddr,
Lorenzo Colitti34e23de2017-08-11 02:11:33 +0900137 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800139 struct xfrm_policy_afinfo *afinfo;
140 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Herbert Xu25ee3282007-12-11 09:32:34 -0800142 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800144 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Lorenzo Colitti34e23de2017-08-11 02:11:33 +0900146 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900149
150 return dst;
151}
152
David Ahern42a7b322015-08-10 16:58:11 -0600153static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
154 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900155 xfrm_address_t *prev_saddr,
156 xfrm_address_t *prev_daddr,
Lorenzo Colitti34e23de2017-08-11 02:11:33 +0900157 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900158{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800159 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900160 xfrm_address_t *saddr = &x->props.saddr;
161 xfrm_address_t *daddr = &x->id.daddr;
162 struct dst_entry *dst;
163
164 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
165 saddr = x->coaddr;
166 daddr = prev_daddr;
167 }
168 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
169 saddr = prev_saddr;
170 daddr = x->coaddr;
171 }
172
Lorenzo Colitti34e23de2017-08-11 02:11:33 +0900173 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900174
175 if (!IS_ERR(dst)) {
176 if (prev_saddr != saddr)
177 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
178 if (prev_daddr != daddr)
179 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
180 }
181
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800182 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static inline unsigned long make_jiffies(long secs)
186{
187 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
188 return MAX_SCHEDULE_TIMEOUT-1;
189 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900190 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static void xfrm_policy_timer(unsigned long data)
194{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800195 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800196 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 long next = LONG_MAX;
198 int warn = 0;
199 int dir;
200
201 read_lock(&xp->lock);
202
Timo Teräsea2dea92010-03-31 00:17:05 +0000203 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 goto out;
205
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700206 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (xp->lft.hard_add_expires_seconds) {
209 long tmo = xp->lft.hard_add_expires_seconds +
210 xp->curlft.add_time - now;
211 if (tmo <= 0)
212 goto expired;
213 if (tmo < next)
214 next = tmo;
215 }
216 if (xp->lft.hard_use_expires_seconds) {
217 long tmo = xp->lft.hard_use_expires_seconds +
218 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
219 if (tmo <= 0)
220 goto expired;
221 if (tmo < next)
222 next = tmo;
223 }
224 if (xp->lft.soft_add_expires_seconds) {
225 long tmo = xp->lft.soft_add_expires_seconds +
226 xp->curlft.add_time - now;
227 if (tmo <= 0) {
228 warn = 1;
229 tmo = XFRM_KM_TIMEOUT;
230 }
231 if (tmo < next)
232 next = tmo;
233 }
234 if (xp->lft.soft_use_expires_seconds) {
235 long tmo = xp->lft.soft_use_expires_seconds +
236 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
237 if (tmo <= 0) {
238 warn = 1;
239 tmo = XFRM_KM_TIMEOUT;
240 }
241 if (tmo < next)
242 next = tmo;
243 }
244
245 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800246 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (next != LONG_MAX &&
248 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
249 xfrm_pol_hold(xp);
250
251out:
252 read_unlock(&xp->lock);
253 xfrm_pol_put(xp);
254 return;
255
256expired:
257 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700258 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800259 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 xfrm_pol_put(xp);
261}
262
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000263static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
264{
265 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
266
267 if (unlikely(pol->walk.dead))
268 flo = NULL;
269 else
270 xfrm_pol_hold(pol);
271
272 return flo;
273}
274
275static int xfrm_policy_flo_check(struct flow_cache_object *flo)
276{
277 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
278
279 return !pol->walk.dead;
280}
281
282static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
283{
284 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
285}
286
287static const struct flow_cache_ops xfrm_policy_fc_ops = {
288 .get = xfrm_policy_flo_get,
289 .check = xfrm_policy_flo_check,
290 .delete = xfrm_policy_flo_delete,
291};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
294 * SPD calls.
295 */
296
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800297struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct xfrm_policy *policy;
300
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700301 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800304 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700305 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700306 INIT_HLIST_NODE(&policy->bydst);
307 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700309 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100310 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800311 setup_timer(&policy->timer, xfrm_policy_timer,
312 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100313 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
314 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000315 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 return policy;
318}
319EXPORT_SYMBOL(xfrm_policy_alloc);
320
Eric Dumazet56f04732015-12-08 07:22:01 -0800321static void xfrm_policy_destroy_rcu(struct rcu_head *head)
322{
323 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
324
325 security_xfrm_policy_free(policy->security);
326 kfree(policy);
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* Destroy xfrm_policy: descendant resources must be released to this moment. */
330
WANG Cong64c31b32008-01-07 22:34:29 -0800331void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Herbert Xu12a169e2008-10-01 07:03:24 -0700333 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Fan Du0659eea2013-08-01 18:08:36 +0800335 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 BUG();
337
Eric Dumazet56f04732015-12-08 07:22:01 -0800338 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
WANG Cong64c31b32008-01-07 22:34:29 -0800340EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* Rule must be locked. Release descentant resources, announce
343 * entry dead. The rule must be unlinked from lists to the moment.
344 */
345
346static void xfrm_policy_kill(struct xfrm_policy *policy)
347{
Herbert Xu12a169e2008-10-01 07:03:24 -0700348 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Timo Teräs285ead12010-04-07 00:30:06 +0000350 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200352 if (del_timer(&policy->polq.hold_timer))
353 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800354 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100355
Timo Teräs285ead12010-04-07 00:30:06 +0000356 if (del_timer(&policy->timer))
357 xfrm_pol_put(policy);
358
359 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
David S. Miller2518c7c2006-08-24 04:45:07 -0700362static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
363
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800364static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700365{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800366 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700367}
368
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200369/* calculate policy hash thresholds */
370static void __get_hash_thresh(struct net *net,
371 unsigned short family, int dir,
372 u8 *dbits, u8 *sbits)
373{
374 switch (family) {
375 case AF_INET:
376 *dbits = net->xfrm.policy_bydst[dir].dbits4;
377 *sbits = net->xfrm.policy_bydst[dir].sbits4;
378 break;
379
380 case AF_INET6:
381 *dbits = net->xfrm.policy_bydst[dir].dbits6;
382 *sbits = net->xfrm.policy_bydst[dir].sbits6;
383 break;
384
385 default:
386 *dbits = 0;
387 *sbits = 0;
388 }
389}
390
David S. Miller5f803b52011-02-24 00:33:19 -0500391static struct hlist_head *policy_hash_bysel(struct net *net,
392 const struct xfrm_selector *sel,
393 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700394{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800395 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200396 unsigned int hash;
397 u8 dbits;
398 u8 sbits;
399
400 __get_hash_thresh(net, family, dir, &dbits, &sbits);
401 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700402
Florian Westphale1e551b2016-08-11 15:17:53 +0200403 if (hash == hmask + 1)
404 return &net->xfrm.policy_inexact[dir];
405
406 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
407 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700408}
409
David S. Miller5f803b52011-02-24 00:33:19 -0500410static struct hlist_head *policy_hash_direct(struct net *net,
411 const xfrm_address_t *daddr,
412 const xfrm_address_t *saddr,
413 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700414{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800415 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200416 unsigned int hash;
417 u8 dbits;
418 u8 sbits;
419
420 __get_hash_thresh(net, family, dir, &dbits, &sbits);
421 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700422
Florian Westphale1e551b2016-08-11 15:17:53 +0200423 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
424 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700425}
426
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200427static void xfrm_dst_hash_transfer(struct net *net,
428 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700429 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200430 unsigned int nhashmask,
431 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700432{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800433 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700434 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800435 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200436 u8 dbits;
437 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700438
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800439redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800440 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700441 unsigned int h;
442
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200443 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700444 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200445 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800446 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200447 hlist_del_rcu(&pol->bydst);
448 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800449 h0 = h;
450 } else {
451 if (h != h0)
452 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200453 hlist_del_rcu(&pol->bydst);
454 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800455 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800456 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800457 }
458 if (!hlist_empty(list)) {
459 entry0 = NULL;
460 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700461 }
462}
463
464static void xfrm_idx_hash_transfer(struct hlist_head *list,
465 struct hlist_head *nidxtable,
466 unsigned int nhashmask)
467{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800468 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700469 struct xfrm_policy *pol;
470
Sasha Levinb67bfe02013-02-27 17:06:00 -0800471 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700472 unsigned int h;
473
474 h = __idx_hash(pol->index, nhashmask);
475 hlist_add_head(&pol->byidx, nidxtable+h);
476 }
477}
478
479static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
480{
481 return ((old_hmask + 1) << 1) - 1;
482}
483
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800484static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700485{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800486 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700487 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
488 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700489 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200490 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700491 int i;
492
493 if (!ndst)
494 return;
495
Florian Westphal9d0380d2016-08-11 15:17:59 +0200496 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200497 write_seqcount_begin(&xfrm_policy_hash_generation);
498
499 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
500 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700501
Florian Westphale1e551b2016-08-11 15:17:53 +0200502 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
503 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
504
David S. Miller2518c7c2006-08-24 04:45:07 -0700505 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200506 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700507
Florian Westphale1e551b2016-08-11 15:17:53 +0200508 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800509 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700510
Florian Westphal30846092016-08-11 15:17:54 +0200511 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200512 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700513
Florian Westphale1e551b2016-08-11 15:17:53 +0200514 synchronize_rcu();
515
David S. Miller44e36b42006-08-24 04:50:50 -0700516 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700517}
518
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800519static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700520{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800521 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700522 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
523 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700525 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700526 int i;
527
528 if (!nidx)
529 return;
530
Florian Westphal9d0380d2016-08-11 15:17:59 +0200531 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700532
533 for (i = hmask; i >= 0; i--)
534 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
535
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800536 net->xfrm.policy_byidx = nidx;
537 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700538
Florian Westphal9d0380d2016-08-11 15:17:59 +0200539 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700540
David S. Miller44e36b42006-08-24 04:50:50 -0700541 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700542}
543
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800544static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700545{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800546 unsigned int cnt = net->xfrm.policy_count[dir];
547 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700548
549 if (total)
550 *total += cnt;
551
552 if ((hmask + 1) < xfrm_policy_hashmax &&
553 cnt > hmask)
554 return 1;
555
556 return 0;
557}
558
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800559static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700560{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800561 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700562
563 if ((hmask + 1) < xfrm_policy_hashmax &&
564 total > hmask)
565 return 1;
566
567 return 0;
568}
569
Alexey Dobriyane0710412010-01-23 13:37:10 +0000570void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700571{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000572 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
573 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
574 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
575 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
576 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
577 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
578 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700579 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700580}
581EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700582
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700583static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800584static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700585{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800586 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700587 int dir, total;
588
589 mutex_lock(&hash_resize_mutex);
590
591 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800592 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800593 if (xfrm_bydst_should_resize(net, dir, &total))
594 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700595 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800596 if (xfrm_byidx_should_resize(net, total))
597 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700598
599 mutex_unlock(&hash_resize_mutex);
600}
601
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200602static void xfrm_hash_rebuild(struct work_struct *work)
603{
604 struct net *net = container_of(work, struct net,
605 xfrm.policy_hthresh.work);
606 unsigned int hmask;
607 struct xfrm_policy *pol;
608 struct xfrm_policy *policy;
609 struct hlist_head *chain;
610 struct hlist_head *odst;
611 struct hlist_node *newpos;
612 int i;
613 int dir;
614 unsigned seq;
615 u8 lbits4, rbits4, lbits6, rbits6;
616
617 mutex_lock(&hash_resize_mutex);
618
619 /* read selector prefixlen thresholds */
620 do {
621 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
622
623 lbits4 = net->xfrm.policy_hthresh.lbits4;
624 rbits4 = net->xfrm.policy_hthresh.rbits4;
625 lbits6 = net->xfrm.policy_hthresh.lbits6;
626 rbits6 = net->xfrm.policy_hthresh.rbits6;
627 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
628
Florian Westphal9d0380d2016-08-11 15:17:59 +0200629 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200630
631 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800632 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200633 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
634 hmask = net->xfrm.policy_bydst[dir].hmask;
635 odst = net->xfrm.policy_bydst[dir].table;
636 for (i = hmask; i >= 0; i--)
637 INIT_HLIST_HEAD(odst + i);
638 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
639 /* dir out => dst = remote, src = local */
640 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
641 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
642 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
643 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
644 } else {
645 /* dir in/fwd => dst = local, src = remote */
646 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
647 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
648 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
649 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
650 }
651 }
652
653 /* re-insert all policies by order of creation */
654 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal5d899172017-12-27 23:25:45 +0100655 if (policy->walk.dead ||
656 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200657 /* skip socket policies */
658 continue;
659 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200660 newpos = NULL;
661 chain = policy_hash_bysel(net, &policy->selector,
662 policy->family,
663 xfrm_policy_id2dir(policy->index));
664 hlist_for_each_entry(pol, chain, bydst) {
665 if (policy->priority >= pol->priority)
666 newpos = &pol->bydst;
667 else
668 break;
669 }
670 if (newpos)
Florian Westphal9da255b2018-10-10 18:02:21 +0200671 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200672 else
Florian Westphal9da255b2018-10-10 18:02:21 +0200673 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200674 }
675
Florian Westphal9d0380d2016-08-11 15:17:59 +0200676 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200677
678 mutex_unlock(&hash_resize_mutex);
679}
680
681void xfrm_policy_hash_rebuild(struct net *net)
682{
683 schedule_work(&net->xfrm.policy_hthresh.work);
684}
685EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/* Generate new index... KAME seems to generate them ordered by cost
688 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800689static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 static u32 idx_generator;
692
693 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700694 struct hlist_head *list;
695 struct xfrm_policy *p;
696 u32 idx;
697 int found;
698
Fan Due682adf2013-11-07 17:47:48 +0800699 if (!index) {
700 idx = (idx_generator | dir);
701 idx_generator += 8;
702 } else {
703 idx = index;
704 index = 0;
705 }
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (idx == 0)
708 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800709 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700710 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800711 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700712 if (p->index == idx) {
713 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700717 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return idx;
719 }
720}
721
David S. Miller2518c7c2006-08-24 04:45:07 -0700722static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
723{
724 u32 *p1 = (u32 *) s1;
725 u32 *p2 = (u32 *) s2;
726 int len = sizeof(struct xfrm_selector) / sizeof(u32);
727 int i;
728
729 for (i = 0; i < len; i++) {
730 if (p1[i] != p2[i])
731 return 1;
732 }
733
734 return 0;
735}
736
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100737static void xfrm_policy_requeue(struct xfrm_policy *old,
738 struct xfrm_policy *new)
739{
740 struct xfrm_policy_queue *pq = &old->polq;
741 struct sk_buff_head list;
742
Li RongQingde2ad482015-04-30 17:25:19 +0800743 if (skb_queue_empty(&pq->hold_queue))
744 return;
745
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100746 __skb_queue_head_init(&list);
747
748 spin_lock_bh(&pq->hold_queue.lock);
749 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200750 if (del_timer(&pq->hold_timer))
751 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100752 spin_unlock_bh(&pq->hold_queue.lock);
753
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100754 pq = &new->polq;
755
756 spin_lock_bh(&pq->hold_queue.lock);
757 skb_queue_splice(&list, &pq->hold_queue);
758 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200759 if (!mod_timer(&pq->hold_timer, jiffies))
760 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100761 spin_unlock_bh(&pq->hold_queue.lock);
762}
763
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100764static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
765 struct xfrm_policy *pol)
766{
767 u32 mark = policy->mark.v & policy->mark.m;
768
769 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
770 return true;
771
772 if ((mark & pol->mark.m) == pol->mark.v &&
773 policy->priority == pol->priority)
774 return true;
775
776 return false;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
780{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800781 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700782 struct xfrm_policy *pol;
783 struct xfrm_policy *delpol;
784 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800785 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Florian Westphal9d0380d2016-08-11 15:17:59 +0200787 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800788 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700789 delpol = NULL;
790 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800791 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800792 if (pol->type == policy->type &&
Steffen Klasserta80f5602018-06-12 14:07:07 +0200793 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700794 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100795 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800796 xfrm_sec_ctx_match(pol->security, policy->security) &&
797 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200799 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return -EEXIST;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 delpol = pol;
803 if (policy->priority > pol->priority)
804 continue;
805 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800806 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 continue;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (delpol)
810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 if (newpos)
Florian Westphal9da255b2018-10-10 18:02:21 +0200813 hlist_add_behind_rcu(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700814 else
Florian Westphal9da255b2018-10-10 18:02:21 +0200815 hlist_add_head_rcu(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800816 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800817 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800818
819 /* After previous checking, family can either be AF_INET or AF_INET6 */
820 if (policy->family == AF_INET)
821 rt_genid_bump_ipv4(net);
822 else
823 rt_genid_bump_ipv6(net);
824
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100825 if (delpol) {
826 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800827 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100828 }
Fan Due682adf2013-11-07 17:47:48 +0800829 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800830 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800831 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 policy->curlft.use_time = 0;
833 if (!mod_timer(&policy->timer, jiffies + HZ))
834 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200835 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
David S. Miller9b78a822005-12-22 07:39:48 -0800837 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800839 else if (xfrm_bydst_should_resize(net, dir, NULL))
840 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 0;
843}
844EXPORT_SYMBOL(xfrm_policy_insert);
845
Steffen Klasserta80f5602018-06-12 14:07:07 +0200846struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
847 u8 type, int dir,
848 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800849 struct xfrm_sec_ctx *ctx, int delete,
850 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
David S. Miller2518c7c2006-08-24 04:45:07 -0700852 struct xfrm_policy *pol, *ret;
853 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Eric Parisef41aaa2007-03-07 15:37:58 -0800855 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200856 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800857 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700858 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800859 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700860 if (pol->type == type &&
Steffen Klasserta80f5602018-06-12 14:07:07 +0200861 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000862 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700863 !selector_cmp(sel, &pol->selector) &&
864 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700867 *err = security_xfrm_policy_delete(
868 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800869 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200870 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800871 return pol;
872 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800873 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 }
875 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
877 }
878 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200879 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000881 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700882 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Trent Jaegerdf718372005-12-13 23:12:27 -0800885EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Steffen Klasserta80f5602018-06-12 14:07:07 +0200887struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
888 u8 type, int dir, u32 id, int delete,
889 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
David S. Miller2518c7c2006-08-24 04:45:07 -0700891 struct xfrm_policy *pol, *ret;
892 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Herbert Xub5505c62007-05-14 02:15:47 -0700894 *err = -ENOENT;
895 if (xfrm_policy_id2dir(id) != dir)
896 return NULL;
897
Eric Parisef41aaa2007-03-07 15:37:58 -0800898 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200899 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800900 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700901 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000903 if (pol->type == type && pol->index == id &&
Steffen Klasserta80f5602018-06-12 14:07:07 +0200904 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000905 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700907 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700908 *err = security_xfrm_policy_delete(
909 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800910 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200911 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800912 return pol;
913 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800914 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700915 }
916 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918 }
919 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200920 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000922 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700923 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926EXPORT_SYMBOL(xfrm_policy_byid);
927
Joy Latten4aa2e622007-06-04 19:05:57 -0400928#ifdef CONFIG_SECURITY_NETWORK_XFRM
929static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900930xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Joy Latten4aa2e622007-06-04 19:05:57 -0400932 int dir, err = 0;
933
934 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
935 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400936 int i;
937
Sasha Levinb67bfe02013-02-27 17:06:00 -0800938 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800939 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400940 if (pol->type != type)
941 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700942 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400943 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900944 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400945 return err;
946 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900947 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800948 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800949 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800950 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400951 bydst) {
952 if (pol->type != type)
953 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700954 err = security_xfrm_policy_delete(
955 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400956 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700957 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900958 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400959 return err;
960 }
961 }
962 }
963 }
964 return err;
965}
966#else
967static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900968xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400969{
970 return 0;
971}
972#endif
973
Tetsuo Handa2e710292014-04-22 21:48:30 +0900974int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400975{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000976 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Florian Westphal9d0380d2016-08-11 15:17:59 +0200978 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400979
Tetsuo Handa2e710292014-04-22 21:48:30 +0900980 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400981 if (err)
982 goto out;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700985 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800986 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700987
988 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800989 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800990 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700991 if (pol->type != type)
992 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000993 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200994 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000995 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Tetsuo Handa2e710292014-04-22 21:48:30 +0900997 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600998
David S. Miller2518c7c2006-08-24 04:45:07 -0700999 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Steffen Klassert4141b362016-08-24 13:08:40 +02001001 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001002 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001004
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -08001005 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001006 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001007 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -08001008 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -07001009 bydst) {
1010 if (pol->type != type)
1011 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +00001012 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001013 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +00001014 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -07001015
Tetsuo Handa2e710292014-04-22 21:48:30 +09001016 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -07001017 xfrm_policy_kill(pol);
1018
Florian Westphal9d0380d2016-08-11 15:17:59 +02001019 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001020 goto again2;
1021 }
1022 }
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001025 if (!cnt)
1026 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001027out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001028 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001029 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031EXPORT_SYMBOL(xfrm_policy_flush);
1032
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001033int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001034 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 void *data)
1036{
Herbert Xu12a169e2008-10-01 07:03:24 -07001037 struct xfrm_policy *pol;
1038 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001039 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Timo Teras4c563f72008-02-28 21:31:08 -08001041 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1042 walk->type != XFRM_POLICY_TYPE_ANY)
1043 return -EINVAL;
1044
Herbert Xu12a169e2008-10-01 07:03:24 -07001045 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001046 return 0;
1047
Florian Westphal9d0380d2016-08-11 15:17:59 +02001048 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001049 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001050 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001051 else
Li RongQing80077702015-04-22 17:09:54 +08001052 x = list_first_entry(&walk->walk.all,
1053 struct xfrm_policy_walk_entry, all);
1054
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001055 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001056 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001057 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001058 pol = container_of(x, struct xfrm_policy, walk);
1059 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1060 walk->type != pol->type)
1061 continue;
1062 error = func(pol, xfrm_policy_id2dir(pol->index),
1063 walk->seq, data);
1064 if (error) {
1065 list_move_tail(&walk->walk.all, &x->all);
1066 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001067 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001068 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001070 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001071 error = -ENOENT;
1072 goto out;
1073 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001074 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001076 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return error;
1078}
1079EXPORT_SYMBOL(xfrm_policy_walk);
1080
Herbert Xu12a169e2008-10-01 07:03:24 -07001081void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1082{
1083 INIT_LIST_HEAD(&walk->walk.all);
1084 walk->walk.dead = 1;
1085 walk->type = type;
1086 walk->seq = 0;
1087}
1088EXPORT_SYMBOL(xfrm_policy_walk_init);
1089
Fan Du283bc9f2013-11-07 17:47:50 +08001090void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001091{
1092 if (list_empty(&walk->walk.all))
1093 return;
1094
Florian Westphal9d0380d2016-08-11 15:17:59 +02001095 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001096 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001097 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001098}
1099EXPORT_SYMBOL(xfrm_policy_walk_done);
1100
James Morris134b0fc2006-10-05 15:42:27 -05001101/*
1102 * Find policy to apply to this flow.
1103 *
1104 * Returns 0 if policy found, else an -errno.
1105 */
David S. Millerf299d552011-02-24 01:23:30 -05001106static int xfrm_policy_match(const struct xfrm_policy *pol,
1107 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001108 u8 type, u16 family, int dir)
1109{
David S. Millerf299d552011-02-24 01:23:30 -05001110 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001111 int ret = -ESRCH;
1112 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001113
1114 if (pol->family != family ||
Steffen Klasserta80f5602018-06-12 14:07:07 +02001115 pol->if_id != fl->flowi_xfrm.if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001116 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001117 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001118 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001119
1120 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001121 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001122 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001123 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001124
James Morris134b0fc2006-10-05 15:42:27 -05001125 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001126}
1127
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001128static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001129 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001130 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
James Morris134b0fc2006-10-05 15:42:27 -05001132 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001133 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001134 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001135 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001136 unsigned int sequence;
1137 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001138
1139 daddr = xfrm_flowi_daddr(fl, family);
1140 saddr = xfrm_flowi_saddr(fl, family);
1141 if (unlikely(!daddr || !saddr))
1142 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Florian Westphala7c442472016-08-11 15:17:56 +02001144 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001145 retry:
1146 do {
1147 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1148 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1149 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1150
1151 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001152 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001153 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001154 err = xfrm_policy_match(pol, fl, type, family, dir);
1155 if (err) {
1156 if (err == -ESRCH)
1157 continue;
1158 else {
1159 ret = ERR_PTR(err);
1160 goto fail;
1161 }
1162 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001163 ret = pol;
1164 priority = ret->priority;
1165 break;
1166 }
1167 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001168 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001169 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001170 if ((pol->priority >= priority) && ret)
1171 break;
1172
James Morris134b0fc2006-10-05 15:42:27 -05001173 err = xfrm_policy_match(pol, fl, type, family, dir);
1174 if (err) {
1175 if (err == -ESRCH)
1176 continue;
1177 else {
1178 ret = ERR_PTR(err);
1179 goto fail;
1180 }
Li RongQing8faf4912015-05-14 11:16:59 +08001181 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001182 ret = pol;
1183 break;
1184 }
1185 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001186
Florian Westphal30846092016-08-11 15:17:54 +02001187 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1188 goto retry;
1189
Florian Westphale37cc8a2016-08-11 15:17:55 +02001190 if (ret && !xfrm_pol_hold_rcu(ret))
1191 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001192fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001193 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001194
David S. Miller2518c7c2006-08-24 04:45:07 -07001195 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001196}
1197
Timo Teräs80c802f2010-04-07 00:30:05 +00001198static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001199__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001200{
1201#ifdef CONFIG_XFRM_SUB_POLICY
1202 struct xfrm_policy *pol;
1203
1204 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1205 if (pol != NULL)
1206 return pol;
1207#endif
1208 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1209}
1210
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001211static int flow_to_policy_dir(int dir)
1212{
1213 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1214 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1215 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1216 return dir;
1217
1218 switch (dir) {
1219 default:
1220 case FLOW_DIR_IN:
1221 return XFRM_POLICY_IN;
1222 case FLOW_DIR_OUT:
1223 return XFRM_POLICY_OUT;
1224 case FLOW_DIR_FWD:
1225 return XFRM_POLICY_FWD;
1226 }
1227}
1228
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001229static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001230xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001231 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001232{
1233 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001234
1235 if (old_obj)
1236 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001237
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001238 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001239 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001240 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001241
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001242 /* Resolver returns two references:
1243 * one for cache and one for caller of flow_cache_lookup() */
1244 xfrm_pol_hold(pol);
1245
1246 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Trent Jaegerdf718372005-12-13 23:12:27 -08001249static inline int policy_to_flow_dir(int dir)
1250{
1251 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001252 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1253 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1254 return dir;
1255 switch (dir) {
1256 default:
1257 case XFRM_POLICY_IN:
1258 return FLOW_DIR_IN;
1259 case XFRM_POLICY_OUT:
1260 return FLOW_DIR_OUT;
1261 case XFRM_POLICY_FWD:
1262 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001263 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001264}
1265
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001266static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Steffen Klassert0b865642017-02-14 07:43:56 +01001267 const struct flowi *fl, u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct xfrm_policy *pol;
1270
Eric Dumazetd188ba82015-12-08 07:22:02 -08001271 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001272 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001273 pol = rcu_dereference(sk->sk_policy[dir]);
1274 if (pol != NULL) {
Steffen Klassert46b31712017-11-29 06:53:55 +01001275 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001276 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001277
Steffen Klassert46b31712017-11-29 06:53:55 +01001278 if (pol->family != family) {
1279 pol = NULL;
1280 goto out;
1281 }
1282
1283 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001284 if (match) {
Steffen Klasserta80f5602018-06-12 14:07:07 +02001285 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
1286 pol->if_id != fl->flowi_xfrm.if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001287 pol = NULL;
1288 goto out;
1289 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001290 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001291 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001292 policy_to_flow_dir(dir));
Florian Westphal330e8322016-11-17 13:21:46 +01001293 if (!err) {
1294 if (!xfrm_pol_hold_rcu(pol))
1295 goto again;
1296 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001297 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001298 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001299 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001300 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001301 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 pol = NULL;
1303 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001304out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001305 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return pol;
1307}
1308
1309static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1310{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001311 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001312
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001313 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001314 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 xfrm_pol_hold(pol);
1316}
1317
1318static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1319 int dir)
1320{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001321 struct net *net = xp_net(pol);
1322
Herbert Xu53c2e282014-11-13 17:09:49 +08001323 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001324 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Herbert Xu53c2e282014-11-13 17:09:49 +08001326 /* Socket policies are not hashed. */
1327 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001328 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001329 hlist_del(&pol->byidx);
1330 }
1331
1332 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001333 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001334
1335 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Herbert Xu53c2e282014-11-13 17:09:49 +08001338static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1339{
1340 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1341}
1342
1343static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1344{
1345 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1346}
1347
Herbert Xu4666faa2005-06-18 22:43:22 -07001348int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Fan Du283bc9f2013-11-07 17:47:50 +08001350 struct net *net = xp_net(pol);
1351
Florian Westphal9d0380d2016-08-11 15:17:59 +02001352 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001354 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001359 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
David S. Millera70fcb02006-03-20 19:18:52 -08001361EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1364{
Lorenzo Colitti38884cc2017-11-20 19:26:02 +09001365 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 struct xfrm_policy *old_pol;
1367
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001368#ifdef CONFIG_XFRM_SUB_POLICY
1369 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1370 return -EINVAL;
1371#endif
1372
Florian Westphal9d0380d2016-08-11 15:17:59 +02001373 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001374 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1375 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001377 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001378 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001379 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001381 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001382 if (old_pol) {
1383 if (pol)
1384 xfrm_policy_requeue(old_pol, pol);
1385
Timo Teräsea2dea92010-03-31 00:17:05 +00001386 /* Unlinking succeeds always. This is the only function
1387 * allowed to delete or replace socket policy.
1388 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001389 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001390 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001391 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 if (old_pol) {
1394 xfrm_policy_kill(old_pol);
1395 }
1396 return 0;
1397}
1398
David S. Millerd3e40a92011-02-24 01:25:41 -05001399static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001401 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001402 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (newp) {
1405 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001406 if (security_xfrm_policy_clone(old->security,
1407 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001408 kfree(newp);
1409 return NULL; /* ENOMEM */
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 newp->lft = old->lft;
1412 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001413 newp->mark = old->mark;
Steffen Klasserta80f5602018-06-12 14:07:07 +02001414 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 newp->action = old->action;
1416 newp->flags = old->flags;
1417 newp->xfrm_nr = old->xfrm_nr;
1418 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001419 newp->type = old->type;
Herbert Xu6ceabde2017-11-10 14:14:06 +11001420 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 memcpy(newp->xfrm_vec, old->xfrm_vec,
1422 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001423 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001424 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001425 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 xfrm_pol_put(newp);
1427 }
1428 return newp;
1429}
1430
Eric Dumazetd188ba82015-12-08 07:22:02 -08001431int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001433 const struct xfrm_policy *p;
1434 struct xfrm_policy *np;
1435 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Eric Dumazetd188ba82015-12-08 07:22:02 -08001437 rcu_read_lock();
1438 for (i = 0; i < 2; i++) {
1439 p = rcu_dereference(osk->sk_policy[i]);
1440 if (p) {
1441 np = clone_policy(p, i);
1442 if (unlikely(!np)) {
1443 ret = -ENOMEM;
1444 break;
1445 }
1446 rcu_assign_pointer(sk->sk_policy[i], np);
1447 }
1448 }
1449 rcu_read_unlock();
1450 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001453static int
David Ahern42a7b322015-08-10 16:58:11 -06001454xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti34e23de2017-08-11 02:11:33 +09001455 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001456{
1457 int err;
1458 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1459
1460 if (unlikely(afinfo == NULL))
1461 return -EINVAL;
Lorenzo Colitti34e23de2017-08-11 02:11:33 +09001462 err = afinfo->get_saddr(net, oif, local, remote, mark);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001463 xfrm_policy_put_afinfo(afinfo);
1464 return err;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/* Resolve list of templates for the flow, given policy. */
1468
1469static int
David S. Millera6c2e612011-02-22 18:35:39 -08001470xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1471 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001473 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int nx;
1475 int i, error;
1476 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1477 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001478 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Weilong Chen9b7a7872013-12-24 09:43:46 +08001480 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct xfrm_state *x;
1482 xfrm_address_t *remote = daddr;
1483 xfrm_address_t *local = saddr;
1484 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1485
Joakim Koskela48b8d782007-07-26 00:08:42 -07001486 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1487 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 remote = &tmpl->id.daddr;
1489 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001490 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001491 error = xfrm_get_saddr(net, fl->flowi_oif,
1492 &tmp, remote,
Lorenzo Colitti34e23de2017-08-11 02:11:33 +09001493 tmpl->encap_family, 0);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001494 if (error)
1495 goto fail;
1496 local = &tmp;
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1501
1502 if (x && x->km.state == XFRM_STATE_VALID) {
1503 xfrm[nx++] = x;
1504 daddr = remote;
1505 saddr = local;
1506 continue;
1507 }
1508 if (x) {
1509 error = (x->km.state == XFRM_STATE_ERROR ?
1510 -EINVAL : -EAGAIN);
1511 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001512 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001513 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 if (!tmpl->optional)
1517 goto fail;
1518 }
1519 return nx;
1520
1521fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001522 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 xfrm_state_put(xfrm[nx]);
1524 return error;
1525}
1526
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001527static int
David S. Millera6c2e612011-02-22 18:35:39 -08001528xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1529 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001530{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001531 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1532 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001533 int cnx = 0;
1534 int error;
1535 int ret;
1536 int i;
1537
1538 for (i = 0; i < npols; i++) {
1539 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1540 error = -ENOBUFS;
1541 goto fail;
1542 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001543
1544 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001545 if (ret < 0) {
1546 error = ret;
1547 goto fail;
1548 } else
1549 cnx += ret;
1550 }
1551
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001552 /* found states are sorted for outbound processing */
1553 if (npols > 1)
1554 xfrm_state_sort(xfrm, tpp, cnx, family);
1555
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001556 return cnx;
1557
1558 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001559 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001560 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001561 return error;
1562
1563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/* Check that the bundle accepts the flow and its components are
1566 * still valid.
1567 */
1568
David S. Miller05d84022011-02-22 17:47:10 -08001569static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001570{
1571 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1572 int tos;
1573
1574 if (!afinfo)
1575 return -EINVAL;
1576
1577 tos = afinfo->get_tos(fl);
1578
1579 xfrm_policy_put_afinfo(afinfo);
1580
1581 return tos;
1582}
1583
Timo Teräs80c802f2010-04-07 00:30:05 +00001584static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1585{
1586 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1587 struct dst_entry *dst = &xdst->u.dst;
1588
1589 if (xdst->route == NULL) {
1590 /* Dummy bundle - if it has xfrms we were not
1591 * able to build bundle as template resolution failed.
1592 * It means we need to try again resolving. */
1593 if (xdst->num_xfrms > 0)
1594 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001595 } else if (dst->flags & DST_XFRM_QUEUE) {
1596 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001597 } else {
1598 /* Real bundle */
1599 if (stale_bundle(dst))
1600 return NULL;
1601 }
1602
1603 dst_hold(dst);
1604 return flo;
1605}
1606
1607static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1608{
1609 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1610 struct dst_entry *dst = &xdst->u.dst;
1611
1612 if (!xdst->route)
1613 return 0;
1614 if (stale_bundle(dst))
1615 return 0;
1616
1617 return 1;
1618}
1619
1620static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1621{
1622 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1623 struct dst_entry *dst = &xdst->u.dst;
1624
1625 dst_free(dst);
1626}
1627
1628static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1629 .get = xfrm_bundle_flo_get,
1630 .check = xfrm_bundle_flo_check,
1631 .delete = xfrm_bundle_flo_delete,
1632};
1633
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001634static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001635{
1636 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001637 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001638 struct xfrm_dst *xdst;
1639
1640 if (!afinfo)
1641 return ERR_PTR(-EINVAL);
1642
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001643 switch (family) {
1644 case AF_INET:
1645 dst_ops = &net->xfrm.xfrm4_dst_ops;
1646 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001647#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001648 case AF_INET6:
1649 dst_ops = &net->xfrm.xfrm6_dst_ops;
1650 break;
1651#endif
1652 default:
1653 BUG();
1654 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001655 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001656
Madalin Bucurd4cae562011-09-26 07:04:36 +00001657 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001658 struct dst_entry *dst = &xdst->u.dst;
1659
1660 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001661 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001662 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001663 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001664
Madalin Bucurd4cae562011-09-26 07:04:36 +00001665 xfrm_policy_put_afinfo(afinfo);
1666
Herbert Xu25ee3282007-12-11 09:32:34 -08001667 return xdst;
1668}
1669
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001670static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1671 int nfheader_len)
1672{
1673 struct xfrm_policy_afinfo *afinfo =
1674 xfrm_policy_get_afinfo(dst->ops->family);
1675 int err;
1676
1677 if (!afinfo)
1678 return -EINVAL;
1679
1680 err = afinfo->init_path(path, dst, nfheader_len);
1681
1682 xfrm_policy_put_afinfo(afinfo);
1683
1684 return err;
1685}
1686
Herbert Xu87c1e122010-03-02 02:51:56 +00001687static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001688 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001689{
1690 struct xfrm_policy_afinfo *afinfo =
1691 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1692 int err;
1693
1694 if (!afinfo)
1695 return -EINVAL;
1696
Herbert Xu87c1e122010-03-02 02:51:56 +00001697 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001698
1699 xfrm_policy_put_afinfo(afinfo);
1700
1701 return err;
1702}
1703
Timo Teräs80c802f2010-04-07 00:30:05 +00001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1706 * all the metrics... Shortly, bundle a bundle.
1707 */
1708
Herbert Xu25ee3282007-12-11 09:32:34 -08001709static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1710 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001711 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001712 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001714 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001715 unsigned long now = jiffies;
1716 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001717 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001718 struct dst_entry *dst_prev = NULL;
1719 struct dst_entry *dst0 = NULL;
1720 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001722 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001723 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001724 int trailer_len = 0;
1725 int tos;
1726 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001727 xfrm_address_t saddr, daddr;
1728
1729 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001730
1731 tos = xfrm_get_tos(fl, family);
1732 err = tos;
1733 if (tos < 0)
1734 goto put_states;
1735
1736 dst_hold(dst);
1737
1738 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001739 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001740 struct dst_entry *dst1 = &xdst->u.dst;
1741
1742 err = PTR_ERR(xdst);
1743 if (IS_ERR(xdst)) {
1744 dst_release(dst);
1745 goto put_states;
1746 }
1747
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001748 if (xfrm[i]->sel.family == AF_UNSPEC) {
1749 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1750 xfrm_af2proto(family));
1751 if (!inner_mode) {
1752 err = -EAFNOSUPPORT;
1753 dst_release(dst);
1754 goto put_states;
1755 }
1756 } else
1757 inner_mode = xfrm[i]->inner_mode;
1758
Herbert Xu25ee3282007-12-11 09:32:34 -08001759 if (!dst_prev)
1760 dst0 = dst1;
1761 else {
1762 dst_prev->child = dst_clone(dst1);
1763 dst1->flags |= DST_NOHASH;
1764 }
1765
1766 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001767 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001768
1769 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Benedict Wong6fffec42019-01-14 11:24:38 -08001770 __u32 mark = 0;
1771
1772 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
1773 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
Steffen Klassert78f10c52018-06-12 12:44:26 +02001774
Herbert Xu25ee3282007-12-11 09:32:34 -08001775 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001776 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert78f10c52018-06-12 12:44:26 +02001777 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08001778 err = PTR_ERR(dst);
1779 if (IS_ERR(dst))
1780 goto put_states;
1781 } else
1782 dst_hold(dst);
1783
1784 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001785 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001786
David S. Millerf5b0a872012-07-19 12:31:33 -07001787 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001788 dst1->flags |= DST_HOST;
1789 dst1->lastuse = now;
1790
1791 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001792 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001793
1794 dst1->next = dst_prev;
1795 dst_prev = dst1;
1796
1797 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001798 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1799 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001800 trailer_len += xfrm[i]->props.trailer_len;
1801 }
1802
1803 dst_prev->child = dst;
1804 dst0->path = dst;
1805
1806 err = -ENODEV;
1807 dev = dst->dev;
1808 if (!dev)
1809 goto free_dst;
1810
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001811 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001812 xfrm_init_pmtu(dst_prev);
1813
1814 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1815 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1816
Herbert Xu87c1e122010-03-02 02:51:56 +00001817 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001818 if (err)
1819 goto free_dst;
1820
1821 dst_prev->header_len = header_len;
1822 dst_prev->trailer_len = trailer_len;
1823 header_len -= xdst->u.dst.xfrm->props.header_len;
1824 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1825 }
1826
1827out:
1828 return dst0;
1829
1830put_states:
1831 for (; i < nx; i++)
1832 xfrm_state_put(xfrm[i]);
1833free_dst:
1834 if (dst0)
1835 dst_free(dst0);
1836 dst0 = ERR_PTR(err);
1837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
David S. Miller73ff93c2011-02-22 18:33:42 -08001840static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001841 struct xfrm_policy **pols,
1842 int *num_pols, int *num_xfrms)
1843{
1844 int i;
1845
1846 if (*num_pols == 0 || !pols[0]) {
1847 *num_pols = 0;
1848 *num_xfrms = 0;
1849 return 0;
1850 }
1851 if (IS_ERR(pols[0]))
1852 return PTR_ERR(pols[0]);
1853
1854 *num_xfrms = pols[0]->xfrm_nr;
1855
1856#ifdef CONFIG_XFRM_SUB_POLICY
1857 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1858 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1859 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1860 XFRM_POLICY_TYPE_MAIN,
1861 fl, family,
1862 XFRM_POLICY_OUT);
1863 if (pols[1]) {
1864 if (IS_ERR(pols[1])) {
1865 xfrm_pols_put(pols, *num_pols);
1866 return PTR_ERR(pols[1]);
1867 }
Weilong Chen02d08922013-12-24 09:43:48 +08001868 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001869 (*num_xfrms) += pols[1]->xfrm_nr;
1870 }
1871 }
1872#endif
1873 for (i = 0; i < *num_pols; i++) {
1874 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1875 *num_xfrms = -1;
1876 break;
1877 }
1878 }
1879
1880 return 0;
1881
1882}
1883
1884static struct xfrm_dst *
1885xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001886 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001887 struct dst_entry *dst_orig)
1888{
1889 struct net *net = xp_net(pols[0]);
1890 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1891 struct dst_entry *dst;
1892 struct xfrm_dst *xdst;
1893 int err;
1894
1895 /* Try to instantiate a bundle */
1896 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001897 if (err <= 0) {
YueHaibingaf322ba2018-07-25 16:54:33 +08001898 if (err == 0)
1899 return NULL;
1900
1901 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001902 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1903 return ERR_PTR(err);
1904 }
1905
1906 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1907 if (IS_ERR(dst)) {
1908 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1909 return ERR_CAST(dst);
1910 }
1911
1912 xdst = (struct xfrm_dst *)dst;
1913 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00001914 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001915 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001916 xdst->policy_genid = atomic_read(&pols[0]->genid);
1917
1918 return xdst;
1919}
1920
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001921static void xfrm_policy_queue_process(unsigned long arg)
1922{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001923 struct sk_buff *skb;
1924 struct sock *sk;
1925 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001926 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001927 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001928 struct xfrm_policy_queue *pq = &pol->polq;
1929 struct flowi fl;
1930 struct sk_buff_head list;
1931
1932 spin_lock(&pq->hold_queue.lock);
1933 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001934 if (!skb) {
1935 spin_unlock(&pq->hold_queue.lock);
1936 goto out;
1937 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001938 dst = skb_dst(skb);
1939 sk = skb->sk;
1940 xfrm_decode_session(skb, &fl, dst->ops->family);
1941 spin_unlock(&pq->hold_queue.lock);
1942
1943 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001944 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001945 if (IS_ERR(dst))
1946 goto purge_queue;
1947
1948 if (dst->flags & DST_XFRM_QUEUE) {
1949 dst_release(dst);
1950
1951 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1952 goto purge_queue;
1953
1954 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001955 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1956 xfrm_pol_hold(pol);
1957 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001958 }
1959
1960 dst_release(dst);
1961
1962 __skb_queue_head_init(&list);
1963
1964 spin_lock(&pq->hold_queue.lock);
1965 pq->timeout = 0;
1966 skb_queue_splice_init(&pq->hold_queue, &list);
1967 spin_unlock(&pq->hold_queue.lock);
1968
1969 while (!skb_queue_empty(&list)) {
1970 skb = __skb_dequeue(&list);
1971
1972 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1973 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001974 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001975 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001976 kfree_skb(skb);
1977 continue;
1978 }
1979
1980 nf_reset(skb);
1981 skb_dst_drop(skb);
1982 skb_dst_set(skb, dst);
1983
Eric W. Biederman13206b62015-10-07 16:48:35 -05001984 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001985 }
1986
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001987out:
1988 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001989 return;
1990
1991purge_queue:
1992 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001993 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001994 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001995}
1996
Eric W. Biedermanede20592015-10-07 16:48:47 -05001997static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001998{
1999 unsigned long sched_next;
2000 struct dst_entry *dst = skb_dst(skb);
2001 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002002 struct xfrm_policy *pol = xdst->pols[0];
2003 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002004
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002005 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002006 kfree_skb(skb);
2007 return 0;
2008 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002009
2010 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2011 kfree_skb(skb);
2012 return -EAGAIN;
2013 }
2014
2015 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002016
2017 spin_lock_bh(&pq->hold_queue.lock);
2018
2019 if (!pq->timeout)
2020 pq->timeout = XFRM_QUEUE_TMO_MIN;
2021
2022 sched_next = jiffies + pq->timeout;
2023
2024 if (del_timer(&pq->hold_timer)) {
2025 if (time_before(pq->hold_timer.expires, sched_next))
2026 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002027 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002028 }
2029
2030 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002031 if (!mod_timer(&pq->hold_timer, sched_next))
2032 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002033
2034 spin_unlock_bh(&pq->hold_queue.lock);
2035
2036 return 0;
2037}
2038
2039static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002040 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002041 const struct flowi *fl,
2042 int num_xfrms,
2043 u16 family)
2044{
2045 int err;
2046 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002047 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002048 struct dst_entry *dst1;
2049 struct xfrm_dst *xdst;
2050
2051 xdst = xfrm_alloc_dst(net, family);
2052 if (IS_ERR(xdst))
2053 return xdst;
2054
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002055 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2056 net->xfrm.sysctl_larval_drop ||
2057 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002058 return xdst;
2059
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002060 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002061 dst1 = &xdst->u.dst;
2062 dst_hold(dst);
2063 xdst->route = dst;
2064
2065 dst_copy_metrics(dst1, dst);
2066
2067 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2068 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2069 dst1->lastuse = jiffies;
2070
2071 dst1->input = dst_discard;
2072 dst1->output = xdst_queue_output;
2073
2074 dst_hold(dst);
2075 dst1->child = dst;
2076 dst1->path = dst;
2077
2078 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2079
2080 err = -ENODEV;
2081 dev = dst->dev;
2082 if (!dev)
2083 goto free_dst;
2084
2085 err = xfrm_fill_dst(xdst, dev, fl);
2086 if (err)
2087 goto free_dst;
2088
2089out:
2090 return xdst;
2091
2092free_dst:
2093 dst_release(dst1);
2094 xdst = ERR_PTR(err);
2095 goto out;
2096}
2097
Timo Teräs80c802f2010-04-07 00:30:05 +00002098static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002099xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002100 struct flow_cache_object *oldflo, void *ctx)
2101{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002102 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002103 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2104 struct xfrm_dst *xdst, *new_xdst;
2105 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2106
2107 /* Check if the policies from old bundle are usable */
2108 xdst = NULL;
2109 if (oldflo) {
2110 xdst = container_of(oldflo, struct xfrm_dst, flo);
2111 num_pols = xdst->num_pols;
2112 num_xfrms = xdst->num_xfrms;
2113 pol_dead = 0;
2114 for (i = 0; i < num_pols; i++) {
2115 pols[i] = xdst->pols[i];
2116 pol_dead |= pols[i]->walk.dead;
2117 }
2118 if (pol_dead) {
2119 dst_free(&xdst->u.dst);
2120 xdst = NULL;
2121 num_pols = 0;
2122 num_xfrms = 0;
2123 oldflo = NULL;
2124 }
2125 }
2126
2127 /* Resolve policies to use if we couldn't get them from
2128 * previous cache entry */
2129 if (xdst == NULL) {
2130 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002131 pols[0] = __xfrm_policy_lookup(net, fl, family,
2132 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002133 err = xfrm_expand_policies(fl, family, pols,
2134 &num_pols, &num_xfrms);
2135 if (err < 0)
2136 goto inc_error;
2137 if (num_pols == 0)
2138 return NULL;
2139 if (num_xfrms <= 0)
2140 goto make_dummy_bundle;
2141 }
2142
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002143 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2144 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002145 if (IS_ERR(new_xdst)) {
2146 err = PTR_ERR(new_xdst);
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002147 if (err == -EREMOTE) {
2148 xfrm_pols_put(pols, num_pols);
2149 return NULL;
2150 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002151 if (err != -EAGAIN)
2152 goto error;
2153 if (oldflo == NULL)
2154 goto make_dummy_bundle;
2155 dst_hold(&xdst->u.dst);
2156 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002157 } else if (new_xdst == NULL) {
2158 num_xfrms = 0;
2159 if (oldflo == NULL)
2160 goto make_dummy_bundle;
2161 xdst->num_xfrms = 0;
2162 dst_hold(&xdst->u.dst);
2163 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002164 }
2165
2166 /* Kill the previous bundle */
2167 if (xdst) {
2168 /* The policies were stolen for newly generated bundle */
2169 xdst->num_pols = 0;
2170 dst_free(&xdst->u.dst);
2171 }
2172
2173 /* Flow cache does not have reference, it dst_free()'s,
2174 * but we do need to return one reference for original caller */
2175 dst_hold(&new_xdst->u.dst);
2176 return &new_xdst->flo;
2177
2178make_dummy_bundle:
2179 /* We found policies, but there's no bundles to instantiate:
2180 * either because the policy blocks, has no transformations or
2181 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002182 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002183 if (IS_ERR(xdst)) {
2184 xfrm_pols_put(pols, num_pols);
2185 return ERR_CAST(xdst);
2186 }
2187 xdst->num_pols = num_pols;
2188 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002189 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002190
2191 dst_hold(&xdst->u.dst);
2192 return &xdst->flo;
2193
2194inc_error:
2195 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2196error:
2197 if (xdst != NULL)
2198 dst_free(&xdst->u.dst);
2199 else
2200 xfrm_pols_put(pols, num_pols);
2201 return ERR_PTR(err);
2202}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
David S. Miller2774c132011-03-01 14:59:04 -08002204static struct dst_entry *make_blackhole(struct net *net, u16 family,
2205 struct dst_entry *dst_orig)
2206{
2207 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2208 struct dst_entry *ret;
2209
2210 if (!afinfo) {
2211 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002212 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002213 } else {
2214 ret = afinfo->blackhole_route(net, dst_orig);
2215 }
2216 xfrm_policy_put_afinfo(afinfo);
2217
2218 return ret;
2219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221/* Main function: finds/creates a bundle for given flow.
2222 *
2223 * At the moment we eat a raw IP route. Mostly to speed up lookups
2224 * on interfaces with disabled IPsec.
2225 */
David S. Miller452edd52011-03-02 13:27:41 -08002226struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2227 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002228 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002230 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002231 struct flow_cache_object *flo;
2232 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002233 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002234 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002235 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002236 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002237
Timo Teräs80c802f2010-04-07 00:30:05 +00002238 dst = NULL;
2239 xdst = NULL;
2240 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002241
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002242 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002243 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002244 num_pols = 1;
Steffen Klassert0b865642017-02-14 07:43:56 +01002245 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002246 err = xfrm_expand_policies(fl, family, pols,
2247 &num_pols, &num_xfrms);
2248 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002249 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002250
2251 if (num_pols) {
2252 if (num_xfrms <= 0) {
2253 drop_pols = num_pols;
2254 goto no_transform;
2255 }
2256
2257 xdst = xfrm_resolve_and_create_bundle(
2258 pols, num_pols, fl,
2259 family, dst_orig);
2260 if (IS_ERR(xdst)) {
2261 xfrm_pols_put(pols, num_pols);
2262 err = PTR_ERR(xdst);
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002263 if (err == -EREMOTE)
2264 goto nopol;
2265
Timo Teräs80c802f2010-04-07 00:30:05 +00002266 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002267 } else if (xdst == NULL) {
2268 num_xfrms = 0;
2269 drop_pols = num_pols;
2270 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002271 }
2272
Steffen Klassertb7eea452014-06-18 12:34:21 +02002273 dst_hold(&xdst->u.dst);
2274 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002275 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002276 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Timo Teräs80c802f2010-04-07 00:30:05 +00002279 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002280 struct xfrm_flo xflo;
2281
2282 xflo.dst_orig = dst_orig;
2283 xflo.flags = flags;
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002286 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002287 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002288 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Timo Teräs80c802f2010-04-07 00:30:05 +00002290 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002291 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002292 if (flo == NULL)
2293 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002294 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002295 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002296 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002297 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002298 xdst = container_of(flo, struct xfrm_dst, flo);
2299
2300 num_pols = xdst->num_pols;
2301 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002302 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002303 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305
Timo Teräs80c802f2010-04-07 00:30:05 +00002306 dst = &xdst->u.dst;
2307 if (route == NULL && num_xfrms > 0) {
2308 /* The only case when xfrm_bundle_lookup() returns a
2309 * bundle with null route, is when the template could
2310 * not be resolved. It means policies are there, but
2311 * bundle could not be created, since we don't yet
2312 * have the xfrm_state's. We need to wait for KM to
2313 * negotiate new SA's or bail out with error.*/
2314 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002315 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002316 err = -EREMOTE;
2317 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002318 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002319
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002320 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002321
2322 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2323 goto error;
2324 }
2325
2326no_transform:
2327 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002328 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Timo Teräs80c802f2010-04-07 00:30:05 +00002330 if ((flags & XFRM_LOOKUP_ICMP) &&
2331 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2332 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002333 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002334 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002335
Timo Teräs80c802f2010-04-07 00:30:05 +00002336 for (i = 0; i < num_pols; i++)
2337 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002338
Timo Teräs80c802f2010-04-07 00:30:05 +00002339 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002341 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002342 err = -EPERM;
2343 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002344 } else if (num_xfrms > 0) {
2345 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 dst_release(dst_orig);
2347 } else {
2348 /* Flow passes untransformed */
2349 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002350 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002352ok:
2353 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002354 if (dst && dst->xfrm &&
2355 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2356 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002357 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Timo Teräs80c802f2010-04-07 00:30:05 +00002359nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002360 if (!(flags & XFRM_LOOKUP_ICMP)) {
2361 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002362 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002363 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002364 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002366 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002367dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002368 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2369 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002370 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002371 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373EXPORT_SYMBOL(xfrm_lookup);
2374
Steffen Klassertf92ee612014-09-16 10:08:40 +02002375/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2376 * Otherwise we may send out blackholed packets.
2377 */
2378struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2379 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002380 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002381{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002382 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002383 flags | XFRM_LOOKUP_QUEUE |
2384 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002385
2386 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2387 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2388
Tommi Rantala590f3122018-06-21 09:30:47 +03002389 if (IS_ERR(dst))
2390 dst_release(dst_orig);
2391
Steffen Klassertf92ee612014-09-16 10:08:40 +02002392 return dst;
2393}
2394EXPORT_SYMBOL(xfrm_lookup_route);
2395
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002396static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002397xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002398{
2399 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002400
2401 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2402 return 0;
2403 x = skb->sp->xvec[idx];
2404 if (!x->type->reject)
2405 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002406 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409/* When skb is transformed back to its "native" form, we have to
2410 * check policy restrictions. At the moment we make this in maximally
2411 * stupid way. Shame on me. :-) Of course, connected sockets must
2412 * have policy cached at them.
2413 */
2414
2415static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002416xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 unsigned short family)
2418{
2419 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002420 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return x->id.proto == tmpl->id.proto &&
2422 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2423 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2424 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002425 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002426 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002427 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2428 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002431/*
2432 * 0 or more than 0 is returned when validation is succeeded (either bypass
2433 * because of optional transport mode, or next index of the mathced secpath
2434 * state with the template.
2435 * -1 is returned when no matching template is found.
2436 * Otherwise "-2 - errored_index" is returned.
2437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002439xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 unsigned short family)
2441{
2442 int idx = start;
2443
2444 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002445 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return start;
2447 } else
2448 start = -1;
2449 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002450 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002452 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2453 if (start == -1)
2454 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458 return start;
2459}
2460
Herbert Xud5422ef2007-12-12 10:44:16 -08002461int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2462 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002464 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2465 const struct xfrm_if_cb *ifcb = xfrm_if_get_cb();
2466 struct xfrm_if *xi;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002467 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if (unlikely(afinfo == NULL))
2470 return -EAFNOSUPPORT;
2471
Herbert Xud5422ef2007-12-12 10:44:16 -08002472 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002473 if (ifcb) {
2474 xi = ifcb->decode_session(skb);
2475 if (xi)
2476 fl->flowi_xfrm.if_id = xi->p.if_id;
2477 }
2478
David S. Miller1d28f422011-03-12 00:29:39 -05002479 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002481 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
Herbert Xud5422ef2007-12-12 10:44:16 -08002483EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
David S. Miller9a7386e2011-02-24 01:44:12 -05002485static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002488 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002489 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493
2494 return 0;
2495}
2496
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002497int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 unsigned short family)
2499{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002500 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002502 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2503 int npols = 0;
2504 int xfrm_nr;
2505 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002506 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002508 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002509 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Herbert Xud5422ef2007-12-12 10:44:16 -08002511 reverse = dir & ~XFRM_POLICY_MASK;
2512 dir &= XFRM_POLICY_MASK;
2513 fl_dir = policy_to_flow_dir(dir);
2514
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002515 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002516 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002518 }
2519
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002520 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 /* First, check used SA against their selectors. */
2523 if (skb->sp) {
2524 int i;
2525
Weilong Chen9b7a7872013-12-24 09:43:46 +08002526 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002527 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002528 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002529 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 }
2534
2535 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002536 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002537 if (sk && sk->sk_policy[dir]) {
Steffen Klassert0b865642017-02-14 07:43:56 +01002538 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002539 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002540 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002541 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002542 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002545 if (!pol) {
2546 struct flow_cache_object *flo;
2547
2548 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2549 xfrm_policy_lookup, NULL);
2550 if (IS_ERR_OR_NULL(flo))
2551 pol = ERR_CAST(flo);
2552 else
2553 pol = container_of(flo, struct xfrm_policy, flo);
2554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002556 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002557 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002558 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002559 }
James Morris134b0fc2006-10-05 15:42:27 -05002560
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002561 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002562 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002563 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002564 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002565 return 0;
2566 }
2567 return 1;
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
James Morris9d729f72007-03-04 16:12:44 -08002570 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002572 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002573 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002574#ifdef CONFIG_XFRM_SUB_POLICY
2575 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002576 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002577 &fl, family,
2578 XFRM_POLICY_IN);
2579 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002580 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002581 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002582 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002583 }
James Morris9d729f72007-03-04 16:12:44 -08002584 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002585 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002586 }
2587 }
2588#endif
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (pol->action == XFRM_POLICY_ALLOW) {
2591 struct sec_path *sp;
2592 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002593 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002594 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002595 struct xfrm_tmpl **tpp = tp;
2596 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 int i, k;
2598
2599 if ((sp = skb->sp) == NULL)
2600 sp = &dummy;
2601
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002602 for (pi = 0; pi < npols; pi++) {
2603 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002604 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002605 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002606 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002607 }
2608 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002609 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002610 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002611 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002612 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2613 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2614 }
2615 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002616 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002617 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002618 tpp = stp;
2619 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 /* For each tunnel xfrm, find the first matching tmpl.
2622 * For each tmpl before that, find corresponding xfrm.
2623 * Order is _important_. Later we will implement
2624 * some barriers, but at the moment barriers
2625 * are implied between each two transformations.
2626 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002627 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2628 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002629 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002630 if (k < -1)
2631 /* "-2 - errored_index" returned */
2632 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002633 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002638 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002639 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002643 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return 1;
2645 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002646 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002649 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002650reject_error:
2651 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return 0;
2653}
2654EXPORT_SYMBOL(__xfrm_policy_check);
2655
2656int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2657{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002658 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002660 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002661 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002663 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002664 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002668 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002669
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002670 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002671 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002672 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002673 dst = NULL;
2674 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002675 skb_dst_set(skb, dst);
2676 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678EXPORT_SYMBOL(__xfrm_route_forward);
2679
David S. Millerd49c73c2006-08-13 18:55:53 -07002680/* Optimize later using cookies and generation ids. */
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2683{
David S. Millerd49c73c2006-08-13 18:55:53 -07002684 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002685 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2686 * get validated by dst_ops->check on every use. We do this
2687 * because when a normal route referenced by an XFRM dst is
2688 * obsoleted we do not go looking around for all parent
2689 * referencing XFRM dsts so that we can invalidate them. It
2690 * is just too much work. Instead we make the checks here on
2691 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002692 *
2693 * XFRM dst A --> IPv4 dst X
2694 *
2695 * X is the "xdst->route" of A (X is also the "dst->path" of A
2696 * in this example). If X is marked obsolete, "A" will not
2697 * notice. That's what we are validating here via the
2698 * stale_bundle() check.
2699 *
2700 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002701 * dst which causes it's ->obsolete field to be set to
2702 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2703 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002704 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002705 if (dst->obsolete < 0 && !stale_bundle(dst))
2706 return dst;
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return NULL;
2709}
2710
2711static int stale_bundle(struct dst_entry *dst)
2712{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002713 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Herbert Xuaabc9762005-05-03 16:27:10 -07002716void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002719 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002720 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 dev_put(dev);
2722 }
2723}
Herbert Xuaabc9762005-05-03 16:27:10 -07002724EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726static void xfrm_link_failure(struct sk_buff *skb)
2727{
2728 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
2731static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2732{
2733 if (dst) {
2734 if (dst->obsolete) {
2735 dst_release(dst);
2736 dst = NULL;
2737 }
2738 }
2739 return dst;
2740}
2741
Paul Mooree4c17212013-05-29 07:36:25 +00002742void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002743{
Fan Duca925cf2014-01-18 09:55:27 +08002744 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002745}
Paul Mooree4c17212013-05-29 07:36:25 +00002746EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002747
2748static void xfrm_garbage_collect_deferred(struct net *net)
2749{
Fan Duca925cf2014-01-18 09:55:27 +08002750 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002751}
2752
Herbert Xu25ee3282007-12-11 09:32:34 -08002753static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 do {
2756 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2757 u32 pmtu, route_mtu_cached;
2758
2759 pmtu = dst_mtu(dst->child);
2760 xdst->child_mtu_cached = pmtu;
2761
2762 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2763
2764 route_mtu_cached = dst_mtu(xdst->route);
2765 xdst->route_mtu_cached = route_mtu_cached;
2766
2767 if (pmtu > route_mtu_cached)
2768 pmtu = route_mtu_cached;
2769
David S. Millerdefb3512010-12-08 21:16:57 -08002770 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 } while ((dst = dst->next));
2772}
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774/* Check that the bundle accepts the flow and its components are
2775 * still valid.
2776 */
2777
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002778static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
2780 struct dst_entry *dst = &first->u.dst;
2781 struct xfrm_dst *last;
2782 u32 mtu;
2783
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002784 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 (dst->dev && !netif_running(dst->dev)))
2786 return 0;
2787
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002788 if (dst->flags & DST_XFRM_QUEUE)
2789 return 1;
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 last = NULL;
2792
2793 do {
2794 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2797 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002798 if (xdst->xfrm_genid != dst->xfrm->genid)
2799 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002800 if (xdst->num_pols > 0 &&
2801 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 mtu = dst_mtu(dst->child);
2805 if (xdst->child_mtu_cached != mtu) {
2806 last = xdst;
2807 xdst->child_mtu_cached = mtu;
2808 }
2809
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002810 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return 0;
2812 mtu = dst_mtu(xdst->route);
2813 if (xdst->route_mtu_cached != mtu) {
2814 last = xdst;
2815 xdst->route_mtu_cached = mtu;
2816 }
2817
2818 dst = dst->child;
2819 } while (dst->xfrm);
2820
2821 if (likely(!last))
2822 return 1;
2823
2824 mtu = last->child_mtu_cached;
2825 for (;;) {
2826 dst = &last->u.dst;
2827
2828 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2829 if (mtu > last->route_mtu_cached)
2830 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002831 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 if (last == first)
2834 break;
2835
Patrick McHardybd0bf072007-07-18 01:55:52 -07002836 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 last->child_mtu_cached = mtu;
2838 }
2839
2840 return 1;
2841}
2842
David S. Miller0dbaee32010-12-13 12:52:14 -08002843static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2844{
2845 return dst_metric_advmss(dst->path);
2846}
2847
Steffen Klassertebb762f2011-11-23 02:12:51 +00002848static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002849{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002850 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2851
2852 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002853}
2854
David S. Millerf894cbf2012-07-02 21:52:24 -07002855static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2856 struct sk_buff *skb,
2857 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002858{
David S. Millerf894cbf2012-07-02 21:52:24 -07002859 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002860}
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2863{
2864 int err = 0;
2865 if (unlikely(afinfo == NULL))
2866 return -EINVAL;
2867 if (unlikely(afinfo->family >= NPROTO))
2868 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002869 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002871 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 else {
2873 struct dst_ops *dst_ops = afinfo->dst_ops;
2874 if (likely(dst_ops->kmem_cachep == NULL))
2875 dst_ops->kmem_cachep = xfrm_dst_cache;
2876 if (likely(dst_ops->check == NULL))
2877 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002878 if (likely(dst_ops->default_advmss == NULL))
2879 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002880 if (likely(dst_ops->mtu == NULL))
2881 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (likely(dst_ops->negative_advice == NULL))
2883 dst_ops->negative_advice = xfrm_negative_advice;
2884 if (likely(dst_ops->link_failure == NULL))
2885 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002886 if (likely(dst_ops->neigh_lookup == NULL))
2887 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002889 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002890 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002892 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return err;
2895}
2896EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2897
2898int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2899{
2900 int err = 0;
2901 if (unlikely(afinfo == NULL))
2902 return -EINVAL;
2903 if (unlikely(afinfo->family >= NPROTO))
2904 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002905 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2907 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2908 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002909 else
2910 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2911 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002913 spin_unlock(&xfrm_policy_afinfo_lock);
2914 if (!err) {
2915 struct dst_ops *dst_ops = afinfo->dst_ops;
2916
2917 synchronize_rcu();
2918
2919 dst_ops->kmem_cachep = NULL;
2920 dst_ops->check = NULL;
2921 dst_ops->negative_advice = NULL;
2922 dst_ops->link_failure = NULL;
2923 afinfo->garbage_collect = NULL;
2924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return err;
2926}
2927EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2930{
Jiri Pirko351638e2013-05-28 01:30:21 +00002931 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 switch (event) {
2934 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002935 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
2937 return NOTIFY_DONE;
2938}
2939
2940static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002941 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942};
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002943void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
2944{
2945 spin_lock(&xfrm_if_cb_lock);
2946 rcu_assign_pointer(xfrm_if_cb, ifcb);
2947 spin_unlock(&xfrm_if_cb_lock);
2948}
2949EXPORT_SYMBOL(xfrm_if_register_cb);
2950
2951void xfrm_if_unregister_cb(void)
2952{
2953 RCU_INIT_POINTER(xfrm_if_cb, NULL);
2954 synchronize_rcu();
2955}
2956EXPORT_SYMBOL(xfrm_if_unregister_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002958#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002959static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002960{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002961 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002962 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2963 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002964 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002965 rv = xfrm_proc_init(net);
2966 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002967 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002968 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002969}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002970
2971static void xfrm_statistics_fini(struct net *net)
2972{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002973 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002974 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002975}
2976#else
2977static int __net_init xfrm_statistics_init(struct net *net)
2978{
2979 return 0;
2980}
2981
2982static void xfrm_statistics_fini(struct net *net)
2983{
2984}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002985#endif
2986
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002987static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
David S. Miller2518c7c2006-08-24 04:45:07 -07002989 unsigned int hmask, sz;
2990 int dir;
2991
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002992 if (net_eq(net, &init_net))
2993 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002995 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002996 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
David S. Miller2518c7c2006-08-24 04:45:07 -07002998 hmask = 8 - 1;
2999 sz = (hmask+1) * sizeof(struct hlist_head);
3000
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003001 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3002 if (!net->xfrm.policy_byidx)
3003 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003004 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07003005
Herbert Xu53c2e282014-11-13 17:09:49 +08003006 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07003007 struct xfrm_policy_hash *htab;
3008
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08003009 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08003010 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003011 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07003012
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003013 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07003014 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07003015 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003016 goto out_bydst;
3017 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02003018 htab->dbits4 = 32;
3019 htab->sbits4 = 32;
3020 htab->dbits6 = 128;
3021 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003022 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003023 net->xfrm.policy_hthresh.lbits4 = 32;
3024 net->xfrm.policy_hthresh.rbits4 = 32;
3025 net->xfrm.policy_hthresh.lbits6 = 128;
3026 net->xfrm.policy_hthresh.rbits6 = 128;
3027
3028 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003029
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003030 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003031 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003032 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003033 if (net_eq(net, &init_net))
3034 register_netdevice_notifier(&xfrm_dev_notifier);
3035 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003036
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003037out_bydst:
3038 for (dir--; dir >= 0; dir--) {
3039 struct xfrm_policy_hash *htab;
3040
3041 htab = &net->xfrm.policy_bydst[dir];
3042 xfrm_hash_free(htab->table, sz);
3043 }
3044 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003045out_byidx:
3046 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003049static void xfrm_policy_fini(struct net *net)
3050{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003051 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003052 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003053
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003054 flush_work(&net->xfrm.policy_hash_work);
3055#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003056 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003057#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003058 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003059
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003060 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003061
Herbert Xu53c2e282014-11-13 17:09:49 +08003062 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003063 struct xfrm_policy_hash *htab;
3064
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003065 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003066
3067 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003068 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003069 WARN_ON(!hlist_empty(htab->table));
3070 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003071 }
3072
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003073 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003074 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3075 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003076}
3077
3078static int __net_init xfrm_net_init(struct net *net)
3079{
3080 int rv;
3081
Florian Westphalf68a09c2017-02-08 11:52:29 +01003082 /* Initialize the per-net locks here */
3083 spin_lock_init(&net->xfrm.xfrm_state_lock);
3084 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3085 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3086
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003087 rv = xfrm_statistics_init(net);
3088 if (rv < 0)
3089 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003090 rv = xfrm_state_init(net);
3091 if (rv < 0)
3092 goto out_state;
3093 rv = xfrm_policy_init(net);
3094 if (rv < 0)
3095 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003096 rv = xfrm_sysctl_init(net);
3097 if (rv < 0)
3098 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003099 rv = flow_cache_init(net);
3100 if (rv < 0)
3101 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003102
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003103 return 0;
3104
Steffen Klassert4a93f502014-03-12 09:43:17 +01003105out:
3106 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003107out_sysctl:
3108 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003109out_policy:
3110 xfrm_state_fini(net);
3111out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003112 xfrm_statistics_fini(net);
3113out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003114 return rv;
3115}
3116
3117static void __net_exit xfrm_net_exit(struct net *net)
3118{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003119 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003120 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003121 xfrm_policy_fini(net);
3122 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003123 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003124}
3125
3126static struct pernet_operations __net_initdata xfrm_net_ops = {
3127 .init = xfrm_net_init,
3128 .exit = xfrm_net_exit,
3129};
3130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131void __init xfrm_init(void)
3132{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003133 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003134 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 xfrm_input_init();
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02003136
3137 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3138 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Joy Lattenab5f5e82007-09-17 11:51:22 -07003141#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003142static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3143 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003144{
Paul Moore875179f2007-12-01 23:27:18 +11003145 struct xfrm_sec_ctx *ctx = xp->security;
3146 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003147
Paul Moore875179f2007-12-01 23:27:18 +11003148 if (ctx)
3149 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3150 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3151
Weilong Chen9b7a7872013-12-24 09:43:46 +08003152 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003154 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003155 if (sel->prefixlen_s != 32)
3156 audit_log_format(audit_buf, " src_prefixlen=%d",
3157 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003158 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003159 if (sel->prefixlen_d != 32)
3160 audit_log_format(audit_buf, " dst_prefixlen=%d",
3161 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003162 break;
3163 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003164 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003165 if (sel->prefixlen_s != 128)
3166 audit_log_format(audit_buf, " src_prefixlen=%d",
3167 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003168 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003169 if (sel->prefixlen_d != 128)
3170 audit_log_format(audit_buf, " dst_prefixlen=%d",
3171 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003172 break;
3173 }
3174}
3175
Tetsuo Handa2e710292014-04-22 21:48:30 +09003176void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003177{
3178 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003179
Paul Mooreafeb14b2007-12-21 14:58:11 -08003180 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003181 if (audit_buf == NULL)
3182 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003183 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003184 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003185 xfrm_audit_common_policyinfo(xp, audit_buf);
3186 audit_log_end(audit_buf);
3187}
3188EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3189
Paul Moore68277ac2007-12-20 20:49:33 -08003190void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003191 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003192{
3193 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003194
Paul Mooreafeb14b2007-12-21 14:58:11 -08003195 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003196 if (audit_buf == NULL)
3197 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003198 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003199 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003200 xfrm_audit_common_policyinfo(xp, audit_buf);
3201 audit_log_end(audit_buf);
3202}
3203EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3204#endif
3205
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003206#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003207static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3208 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003209{
3210 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3211 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003212 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3213 sel_cmp->family) &&
3214 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3215 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003216 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3217 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003218 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003219 }
3220 } else {
3221 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003222 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003223 }
3224 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003225 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003226}
3227
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003228static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3229 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003230{
3231 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232 struct hlist_head *chain;
3233 u32 priority = ~0U;
3234
Florian Westphal9d0380d2016-08-11 15:17:59 +02003235 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003236 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003237 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003238 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3239 pol->type == type) {
3240 ret = pol;
3241 priority = ret->priority;
3242 break;
3243 }
3244 }
Fan Du8d549c42013-11-07 17:47:49 +08003245 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003246 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003247 if ((pol->priority >= priority) && ret)
3248 break;
3249
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003250 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003251 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003252 ret = pol;
3253 break;
3254 }
3255 }
3256
Li RongQing586f2eb2015-04-30 17:13:41 +08003257 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003258
Florian Westphal9d0380d2016-08-11 15:17:59 +02003259 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003260
3261 return ret;
3262}
3263
David S. Millerdd701752011-02-24 00:21:08 -05003264static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003265{
3266 int match = 0;
3267
3268 if (t->mode == m->mode && t->id.proto == m->proto &&
3269 (m->reqid == 0 || t->reqid == m->reqid)) {
3270 switch (t->mode) {
3271 case XFRM_MODE_TUNNEL:
3272 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003273 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3274 m->old_family) &&
3275 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3276 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003277 match = 1;
3278 }
3279 break;
3280 case XFRM_MODE_TRANSPORT:
3281 /* in case of transport mode, template does not store
3282 any IP addresses, hence we just compare mode and
3283 protocol */
3284 match = 1;
3285 break;
3286 default:
3287 break;
3288 }
3289 }
3290 return match;
3291}
3292
3293/* update endpoint address(es) of template(s) */
3294static int xfrm_policy_migrate(struct xfrm_policy *pol,
3295 struct xfrm_migrate *m, int num_migrate)
3296{
3297 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003298 int i, j, n = 0;
3299
3300 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003301 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003302 /* target policy has been deleted */
3303 write_unlock_bh(&pol->lock);
3304 return -ENOENT;
3305 }
3306
3307 for (i = 0; i < pol->xfrm_nr; i++) {
3308 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3309 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3310 continue;
3311 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003312 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3313 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003314 continue;
3315 /* update endpoints */
3316 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3317 sizeof(pol->xfrm_vec[i].id.daddr));
3318 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3319 sizeof(pol->xfrm_vec[i].saddr));
3320 pol->xfrm_vec[i].encap_family = mp->new_family;
3321 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003322 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003323 }
3324 }
3325
3326 write_unlock_bh(&pol->lock);
3327
3328 if (!n)
3329 return -ENODATA;
3330
3331 return 0;
3332}
3333
David S. Millerdd701752011-02-24 00:21:08 -05003334static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003335{
3336 int i, j;
3337
3338 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3339 return -EINVAL;
3340
3341 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003342 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3343 m[i].old_family) &&
3344 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3345 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003346 return -EINVAL;
3347 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3348 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3349 return -EINVAL;
3350
3351 /* check if there is any duplicated entry */
3352 for (j = i + 1; j < num_migrate; j++) {
3353 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3354 sizeof(m[i].old_daddr)) &&
3355 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3356 sizeof(m[i].old_saddr)) &&
3357 m[i].proto == m[j].proto &&
3358 m[i].mode == m[j].mode &&
3359 m[i].reqid == m[j].reqid &&
3360 m[i].old_family == m[j].old_family)
3361 return -EINVAL;
3362 }
3363 }
3364
3365 return 0;
3366}
3367
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003368int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003369 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003370 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003371{
3372 int i, err, nx_cur = 0, nx_new = 0;
3373 struct xfrm_policy *pol = NULL;
3374 struct xfrm_state *x, *xc;
3375 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3376 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3377 struct xfrm_migrate *mp;
3378
Vladis Dronov12a70cc2017-08-02 19:50:14 +02003379 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003380 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3381 goto out;
3382
Vladis Dronov12a70cc2017-08-02 19:50:14 +02003383 if (dir >= XFRM_POLICY_MAX) {
3384 err = -EINVAL;
3385 goto out;
3386 }
3387
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003388 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003389 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003390 err = -ENOENT;
3391 goto out;
3392 }
3393
3394 /* Stage 2 - find and update state(s) */
3395 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003396 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003397 x_cur[nx_cur] = x;
3398 nx_cur++;
3399 if ((xc = xfrm_state_migrate(x, mp))) {
3400 x_new[nx_new] = xc;
3401 nx_new++;
3402 } else {
3403 err = -ENODATA;
3404 goto restore_state;
3405 }
3406 }
3407 }
3408
3409 /* Stage 3 - update policy */
3410 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3411 goto restore_state;
3412
3413 /* Stage 4 - delete old state(s) */
3414 if (nx_cur) {
3415 xfrm_states_put(x_cur, nx_cur);
3416 xfrm_states_delete(x_cur, nx_cur);
3417 }
3418
3419 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003420 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003421
3422 xfrm_pol_put(pol);
3423
3424 return 0;
3425out:
3426 return err;
3427
3428restore_state:
3429 if (pol)
3430 xfrm_pol_put(pol);
3431 if (nx_cur)
3432 xfrm_states_put(x_cur, nx_cur);
3433 if (nx_new)
3434 xfrm_states_delete(x_new, nx_new);
3435
3436 return err;
3437}
David S. Millere610e672007-02-08 13:29:15 -08003438EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003439#endif