blob: 088ea615338b75cb5c2fd812155c5d525d4b1b8b [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{
YueHaibing86e98ce2020-03-23 15:32:39 +0800348 write_lock_bh(&policy->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700349 policy->walk.dead = 1;
YueHaibing86e98ce2020-03-23 15:32:39 +0800350 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Timo Teräs285ead12010-04-07 00:30:06 +0000352 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200354 if (del_timer(&policy->polq.hold_timer))
355 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800356 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100357
Timo Teräs285ead12010-04-07 00:30:06 +0000358 if (del_timer(&policy->timer))
359 xfrm_pol_put(policy);
360
361 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
David S. Miller2518c7c2006-08-24 04:45:07 -0700364static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
365
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800366static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700367{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800368 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700369}
370
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200371/* calculate policy hash thresholds */
372static void __get_hash_thresh(struct net *net,
373 unsigned short family, int dir,
374 u8 *dbits, u8 *sbits)
375{
376 switch (family) {
377 case AF_INET:
378 *dbits = net->xfrm.policy_bydst[dir].dbits4;
379 *sbits = net->xfrm.policy_bydst[dir].sbits4;
380 break;
381
382 case AF_INET6:
383 *dbits = net->xfrm.policy_bydst[dir].dbits6;
384 *sbits = net->xfrm.policy_bydst[dir].sbits6;
385 break;
386
387 default:
388 *dbits = 0;
389 *sbits = 0;
390 }
391}
392
David S. Miller5f803b52011-02-24 00:33:19 -0500393static struct hlist_head *policy_hash_bysel(struct net *net,
394 const struct xfrm_selector *sel,
395 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700396{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800397 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200398 unsigned int hash;
399 u8 dbits;
400 u8 sbits;
401
402 __get_hash_thresh(net, family, dir, &dbits, &sbits);
403 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700404
Florian Westphale1e551b2016-08-11 15:17:53 +0200405 if (hash == hmask + 1)
406 return &net->xfrm.policy_inexact[dir];
407
408 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
409 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700410}
411
David S. Miller5f803b52011-02-24 00:33:19 -0500412static struct hlist_head *policy_hash_direct(struct net *net,
413 const xfrm_address_t *daddr,
414 const xfrm_address_t *saddr,
415 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700416{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800417 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418 unsigned int hash;
419 u8 dbits;
420 u8 sbits;
421
422 __get_hash_thresh(net, family, dir, &dbits, &sbits);
423 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700424
Florian Westphale1e551b2016-08-11 15:17:53 +0200425 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
426 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700427}
428
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200429static void xfrm_dst_hash_transfer(struct net *net,
430 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700431 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200432 unsigned int nhashmask,
433 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700434{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700436 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200438 u8 dbits;
439 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700440
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800441redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800442 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700443 unsigned int h;
444
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200445 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700446 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200447 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800448 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200449 hlist_del_rcu(&pol->bydst);
450 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800451 h0 = h;
452 } else {
453 if (h != h0)
454 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200455 hlist_del_rcu(&pol->bydst);
456 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800457 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800458 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800459 }
460 if (!hlist_empty(list)) {
461 entry0 = NULL;
462 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700463 }
464}
465
466static void xfrm_idx_hash_transfer(struct hlist_head *list,
467 struct hlist_head *nidxtable,
468 unsigned int nhashmask)
469{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800470 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700471 struct xfrm_policy *pol;
472
Sasha Levinb67bfe02013-02-27 17:06:00 -0800473 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700474 unsigned int h;
475
476 h = __idx_hash(pol->index, nhashmask);
477 hlist_add_head(&pol->byidx, nidxtable+h);
478 }
479}
480
481static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
482{
483 return ((old_hmask + 1) << 1) - 1;
484}
485
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800486static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700487{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800488 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700489 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
490 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700491 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200492 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700493 int i;
494
495 if (!ndst)
496 return;
497
Florian Westphal9d0380d2016-08-11 15:17:59 +0200498 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200499 write_seqcount_begin(&xfrm_policy_hash_generation);
500
501 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
502 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
Florian Westphale1e551b2016-08-11 15:17:53 +0200504 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
505 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
506
David S. Miller2518c7c2006-08-24 04:45:07 -0700507 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200508 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700509
Florian Westphale1e551b2016-08-11 15:17:53 +0200510 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800511 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
Florian Westphal30846092016-08-11 15:17:54 +0200513 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200514 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700515
Florian Westphale1e551b2016-08-11 15:17:53 +0200516 synchronize_rcu();
517
David S. Miller44e36b42006-08-24 04:50:50 -0700518 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700519}
520
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800521static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700522{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800523 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700524 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
525 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800526 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700527 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700528 int i;
529
530 if (!nidx)
531 return;
532
Florian Westphal9d0380d2016-08-11 15:17:59 +0200533 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700534
535 for (i = hmask; i >= 0; i--)
536 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
537
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800538 net->xfrm.policy_byidx = nidx;
539 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700540
Florian Westphal9d0380d2016-08-11 15:17:59 +0200541 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700542
David S. Miller44e36b42006-08-24 04:50:50 -0700543 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700544}
545
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800546static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700547{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800548 unsigned int cnt = net->xfrm.policy_count[dir];
549 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700550
551 if (total)
552 *total += cnt;
553
554 if ((hmask + 1) < xfrm_policy_hashmax &&
555 cnt > hmask)
556 return 1;
557
558 return 0;
559}
560
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800561static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700562{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800563 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700564
565 if ((hmask + 1) < xfrm_policy_hashmax &&
566 total > hmask)
567 return 1;
568
569 return 0;
570}
571
Alexey Dobriyane0710412010-01-23 13:37:10 +0000572void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700573{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000574 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
575 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
576 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
577 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
578 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
579 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
580 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700581 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700582}
583EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700584
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700585static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800586static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700587{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800588 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700589 int dir, total;
590
591 mutex_lock(&hash_resize_mutex);
592
593 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800594 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800595 if (xfrm_bydst_should_resize(net, dir, &total))
596 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700597 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800598 if (xfrm_byidx_should_resize(net, total))
599 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700600
601 mutex_unlock(&hash_resize_mutex);
602}
603
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200604static void xfrm_hash_rebuild(struct work_struct *work)
605{
606 struct net *net = container_of(work, struct net,
607 xfrm.policy_hthresh.work);
608 unsigned int hmask;
609 struct xfrm_policy *pol;
610 struct xfrm_policy *policy;
611 struct hlist_head *chain;
612 struct hlist_head *odst;
613 struct hlist_node *newpos;
614 int i;
615 int dir;
616 unsigned seq;
617 u8 lbits4, rbits4, lbits6, rbits6;
618
619 mutex_lock(&hash_resize_mutex);
620
621 /* read selector prefixlen thresholds */
622 do {
623 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
624
625 lbits4 = net->xfrm.policy_hthresh.lbits4;
626 rbits4 = net->xfrm.policy_hthresh.rbits4;
627 lbits6 = net->xfrm.policy_hthresh.lbits6;
628 rbits6 = net->xfrm.policy_hthresh.rbits6;
629 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
630
Florian Westphal9d0380d2016-08-11 15:17:59 +0200631 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200632
633 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800634 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200635 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
636 hmask = net->xfrm.policy_bydst[dir].hmask;
637 odst = net->xfrm.policy_bydst[dir].table;
638 for (i = hmask; i >= 0; i--)
639 INIT_HLIST_HEAD(odst + i);
640 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
641 /* dir out => dst = remote, src = local */
642 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
643 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
644 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
645 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
646 } else {
647 /* dir in/fwd => dst = local, src = remote */
648 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
649 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
650 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
651 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
652 }
653 }
654
655 /* re-insert all policies by order of creation */
656 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal5d899172017-12-27 23:25:45 +0100657 if (policy->walk.dead ||
658 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200659 /* skip socket policies */
660 continue;
661 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200662 newpos = NULL;
663 chain = policy_hash_bysel(net, &policy->selector,
664 policy->family,
665 xfrm_policy_id2dir(policy->index));
666 hlist_for_each_entry(pol, chain, bydst) {
667 if (policy->priority >= pol->priority)
668 newpos = &pol->bydst;
669 else
670 break;
671 }
672 if (newpos)
Florian Westphal9da255b2018-10-10 18:02:21 +0200673 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200674 else
Florian Westphal9da255b2018-10-10 18:02:21 +0200675 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200676 }
677
Florian Westphal9d0380d2016-08-11 15:17:59 +0200678 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200679
680 mutex_unlock(&hash_resize_mutex);
681}
682
683void xfrm_policy_hash_rebuild(struct net *net)
684{
685 schedule_work(&net->xfrm.policy_hthresh.work);
686}
687EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/* Generate new index... KAME seems to generate them ordered by cost
690 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800691static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 static u32 idx_generator;
694
695 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700696 struct hlist_head *list;
697 struct xfrm_policy *p;
698 u32 idx;
699 int found;
700
Fan Due682adf2013-11-07 17:47:48 +0800701 if (!index) {
702 idx = (idx_generator | dir);
703 idx_generator += 8;
704 } else {
705 idx = index;
706 index = 0;
707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (idx == 0)
710 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800711 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700712 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800713 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700714 if (p->index == idx) {
715 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700719 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return idx;
721 }
722}
723
David S. Miller2518c7c2006-08-24 04:45:07 -0700724static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
725{
726 u32 *p1 = (u32 *) s1;
727 u32 *p2 = (u32 *) s2;
728 int len = sizeof(struct xfrm_selector) / sizeof(u32);
729 int i;
730
731 for (i = 0; i < len; i++) {
732 if (p1[i] != p2[i])
733 return 1;
734 }
735
736 return 0;
737}
738
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100739static void xfrm_policy_requeue(struct xfrm_policy *old,
740 struct xfrm_policy *new)
741{
742 struct xfrm_policy_queue *pq = &old->polq;
743 struct sk_buff_head list;
744
Li RongQingde2ad482015-04-30 17:25:19 +0800745 if (skb_queue_empty(&pq->hold_queue))
746 return;
747
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100748 __skb_queue_head_init(&list);
749
750 spin_lock_bh(&pq->hold_queue.lock);
751 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200752 if (del_timer(&pq->hold_timer))
753 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100754 spin_unlock_bh(&pq->hold_queue.lock);
755
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100756 pq = &new->polq;
757
758 spin_lock_bh(&pq->hold_queue.lock);
759 skb_queue_splice(&list, &pq->hold_queue);
760 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200761 if (!mod_timer(&pq->hold_timer, jiffies))
762 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100763 spin_unlock_bh(&pq->hold_queue.lock);
764}
765
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100766static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
767 struct xfrm_policy *pol)
768{
769 u32 mark = policy->mark.v & policy->mark.m;
770
771 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
772 return true;
773
774 if ((mark & pol->mark.m) == pol->mark.v &&
775 policy->priority == pol->priority)
776 return true;
777
778 return false;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
782{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800783 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700784 struct xfrm_policy *pol;
785 struct xfrm_policy *delpol;
786 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800787 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Florian Westphal9d0380d2016-08-11 15:17:59 +0200789 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800790 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700791 delpol = NULL;
792 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800793 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800794 if (pol->type == policy->type &&
Steffen Klasserta80f5602018-06-12 14:07:07 +0200795 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700796 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100797 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800798 xfrm_sec_ctx_match(pol->security, policy->security) &&
799 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200801 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EEXIST;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 delpol = pol;
805 if (policy->priority > pol->priority)
806 continue;
807 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800808 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 continue;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (delpol)
812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 if (newpos)
Florian Westphal9da255b2018-10-10 18:02:21 +0200815 hlist_add_behind_rcu(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700816 else
Florian Westphal9da255b2018-10-10 18:02:21 +0200817 hlist_add_head_rcu(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800818 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800819 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800820
821 /* After previous checking, family can either be AF_INET or AF_INET6 */
822 if (policy->family == AF_INET)
823 rt_genid_bump_ipv4(net);
824 else
825 rt_genid_bump_ipv6(net);
826
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100827 if (delpol) {
828 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800829 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100830 }
Fan Due682adf2013-11-07 17:47:48 +0800831 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800832 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800833 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 policy->curlft.use_time = 0;
835 if (!mod_timer(&policy->timer, jiffies + HZ))
836 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200837 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David S. Miller9b78a822005-12-22 07:39:48 -0800839 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800841 else if (xfrm_bydst_should_resize(net, dir, NULL))
842 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845}
846EXPORT_SYMBOL(xfrm_policy_insert);
847
Steffen Klasserta80f5602018-06-12 14:07:07 +0200848struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
849 u8 type, int dir,
850 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800851 struct xfrm_sec_ctx *ctx, int delete,
852 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
David S. Miller2518c7c2006-08-24 04:45:07 -0700854 struct xfrm_policy *pol, *ret;
855 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Eric Parisef41aaa2007-03-07 15:37:58 -0800857 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200858 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800859 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700860 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800861 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 if (pol->type == type &&
Steffen Klasserta80f5602018-06-12 14:07:07 +0200863 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000864 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700865 !selector_cmp(sel, &pol->selector) &&
866 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700868 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700869 *err = security_xfrm_policy_delete(
870 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800871 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200872 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800873 return pol;
874 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800875 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700876 }
877 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
879 }
880 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200881 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000883 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700884 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Trent Jaegerdf718372005-12-13 23:12:27 -0800887EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Steffen Klasserta80f5602018-06-12 14:07:07 +0200889struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
890 u8 type, int dir, u32 id, int delete,
891 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
David S. Miller2518c7c2006-08-24 04:45:07 -0700893 struct xfrm_policy *pol, *ret;
894 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Herbert Xub5505c62007-05-14 02:15:47 -0700896 *err = -ENOENT;
897 if (xfrm_policy_id2dir(id) != dir)
898 return NULL;
899
Eric Parisef41aaa2007-03-07 15:37:58 -0800900 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200901 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800902 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700903 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800904 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000905 if (pol->type == type && pol->index == id &&
Steffen Klasserta80f5602018-06-12 14:07:07 +0200906 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000907 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700909 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700910 *err = security_xfrm_policy_delete(
911 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800912 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200913 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800914 return pol;
915 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800916 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700917 }
918 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 }
921 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200922 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000924 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700925 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928EXPORT_SYMBOL(xfrm_policy_byid);
929
Joy Latten4aa2e622007-06-04 19:05:57 -0400930#ifdef CONFIG_SECURITY_NETWORK_XFRM
931static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900932xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Joy Latten4aa2e622007-06-04 19:05:57 -0400934 int dir, err = 0;
935
936 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
937 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400938 int i;
939
Sasha Levinb67bfe02013-02-27 17:06:00 -0800940 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800941 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400942 if (pol->type != type)
943 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700944 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400945 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900946 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400947 return err;
948 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900949 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800950 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800951 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800952 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400953 bydst) {
954 if (pol->type != type)
955 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700956 err = security_xfrm_policy_delete(
957 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400958 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700959 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900960 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400961 return err;
962 }
963 }
964 }
965 }
966 return err;
967}
968#else
969static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900970xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400971{
972 return 0;
973}
974#endif
975
Tetsuo Handa2e710292014-04-22 21:48:30 +0900976int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400977{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000978 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Florian Westphal9d0380d2016-08-11 15:17:59 +0200980 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400981
Tetsuo Handa2e710292014-04-22 21:48:30 +0900982 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400983 if (err)
984 goto out;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700987 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800988 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700989
990 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800991 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800992 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700993 if (pol->type != type)
994 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000995 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200996 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000997 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Tetsuo Handa2e710292014-04-22 21:48:30 +0900999 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -06001000
David S. Miller2518c7c2006-08-24 04:45:07 -07001001 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Steffen Klassert4141b362016-08-24 13:08:40 +02001003 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001004 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001006
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -08001007 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001008 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001009 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -08001010 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -07001011 bydst) {
1012 if (pol->type != type)
1013 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +00001014 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001015 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +00001016 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -07001017
Tetsuo Handa2e710292014-04-22 21:48:30 +09001018 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -07001019 xfrm_policy_kill(pol);
1020
Florian Westphal9d0380d2016-08-11 15:17:59 +02001021 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001022 goto again2;
1023 }
1024 }
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001027 if (!cnt)
1028 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001029out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001030 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033EXPORT_SYMBOL(xfrm_policy_flush);
1034
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001035int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001036 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 void *data)
1038{
Herbert Xu12a169e2008-10-01 07:03:24 -07001039 struct xfrm_policy *pol;
1040 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001041 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Timo Teras4c563f72008-02-28 21:31:08 -08001043 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1044 walk->type != XFRM_POLICY_TYPE_ANY)
1045 return -EINVAL;
1046
Herbert Xu12a169e2008-10-01 07:03:24 -07001047 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001048 return 0;
1049
Florian Westphal9d0380d2016-08-11 15:17:59 +02001050 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001051 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001052 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001053 else
Li RongQing80077702015-04-22 17:09:54 +08001054 x = list_first_entry(&walk->walk.all,
1055 struct xfrm_policy_walk_entry, all);
1056
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001057 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001058 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001059 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001060 pol = container_of(x, struct xfrm_policy, walk);
1061 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1062 walk->type != pol->type)
1063 continue;
1064 error = func(pol, xfrm_policy_id2dir(pol->index),
1065 walk->seq, data);
1066 if (error) {
1067 list_move_tail(&walk->walk.all, &x->all);
1068 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001069 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001070 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001072 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001073 error = -ENOENT;
1074 goto out;
1075 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001076 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001078 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return error;
1080}
1081EXPORT_SYMBOL(xfrm_policy_walk);
1082
Herbert Xu12a169e2008-10-01 07:03:24 -07001083void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1084{
1085 INIT_LIST_HEAD(&walk->walk.all);
1086 walk->walk.dead = 1;
1087 walk->type = type;
1088 walk->seq = 0;
1089}
1090EXPORT_SYMBOL(xfrm_policy_walk_init);
1091
Fan Du283bc9f2013-11-07 17:47:50 +08001092void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001093{
1094 if (list_empty(&walk->walk.all))
1095 return;
1096
Florian Westphal9d0380d2016-08-11 15:17:59 +02001097 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001098 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001099 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001100}
1101EXPORT_SYMBOL(xfrm_policy_walk_done);
1102
James Morris134b0fc2006-10-05 15:42:27 -05001103/*
1104 * Find policy to apply to this flow.
1105 *
1106 * Returns 0 if policy found, else an -errno.
1107 */
David S. Millerf299d552011-02-24 01:23:30 -05001108static int xfrm_policy_match(const struct xfrm_policy *pol,
1109 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001110 u8 type, u16 family, int dir)
1111{
David S. Millerf299d552011-02-24 01:23:30 -05001112 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001113 int ret = -ESRCH;
1114 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001115
1116 if (pol->family != family ||
Steffen Klasserta80f5602018-06-12 14:07:07 +02001117 pol->if_id != fl->flowi_xfrm.if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001118 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001119 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001120 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001121
1122 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001123 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001124 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001125 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001126
James Morris134b0fc2006-10-05 15:42:27 -05001127 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001128}
1129
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001130static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001131 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001132 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
James Morris134b0fc2006-10-05 15:42:27 -05001134 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001135 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001136 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001137 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001138 unsigned int sequence;
1139 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001140
1141 daddr = xfrm_flowi_daddr(fl, family);
1142 saddr = xfrm_flowi_saddr(fl, family);
1143 if (unlikely(!daddr || !saddr))
1144 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Florian Westphala7c442472016-08-11 15:17:56 +02001146 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001147 retry:
1148 do {
1149 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1150 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1151 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1152
1153 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001154 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001155 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001156 err = xfrm_policy_match(pol, fl, type, family, dir);
1157 if (err) {
1158 if (err == -ESRCH)
1159 continue;
1160 else {
1161 ret = ERR_PTR(err);
1162 goto fail;
1163 }
1164 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001165 ret = pol;
1166 priority = ret->priority;
1167 break;
1168 }
1169 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001170 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001171 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001172 if ((pol->priority >= priority) && ret)
1173 break;
1174
James Morris134b0fc2006-10-05 15:42:27 -05001175 err = xfrm_policy_match(pol, fl, type, family, dir);
1176 if (err) {
1177 if (err == -ESRCH)
1178 continue;
1179 else {
1180 ret = ERR_PTR(err);
1181 goto fail;
1182 }
Li RongQing8faf4912015-05-14 11:16:59 +08001183 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001184 ret = pol;
1185 break;
1186 }
1187 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001188
Florian Westphal30846092016-08-11 15:17:54 +02001189 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1190 goto retry;
1191
Florian Westphale37cc8a2016-08-11 15:17:55 +02001192 if (ret && !xfrm_pol_hold_rcu(ret))
1193 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001194fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001195 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001196
David S. Miller2518c7c2006-08-24 04:45:07 -07001197 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001198}
1199
Timo Teräs80c802f2010-04-07 00:30:05 +00001200static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001201__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001202{
1203#ifdef CONFIG_XFRM_SUB_POLICY
1204 struct xfrm_policy *pol;
1205
1206 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1207 if (pol != NULL)
1208 return pol;
1209#endif
1210 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1211}
1212
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001213static int flow_to_policy_dir(int dir)
1214{
1215 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1216 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1217 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1218 return dir;
1219
1220 switch (dir) {
1221 default:
1222 case FLOW_DIR_IN:
1223 return XFRM_POLICY_IN;
1224 case FLOW_DIR_OUT:
1225 return XFRM_POLICY_OUT;
1226 case FLOW_DIR_FWD:
1227 return XFRM_POLICY_FWD;
1228 }
1229}
1230
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001231static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001232xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001233 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001234{
1235 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001236
1237 if (old_obj)
1238 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001239
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001240 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001241 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001242 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001243
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001244 /* Resolver returns two references:
1245 * one for cache and one for caller of flow_cache_lookup() */
1246 xfrm_pol_hold(pol);
1247
1248 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Trent Jaegerdf718372005-12-13 23:12:27 -08001251static inline int policy_to_flow_dir(int dir)
1252{
1253 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001254 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1255 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1256 return dir;
1257 switch (dir) {
1258 default:
1259 case XFRM_POLICY_IN:
1260 return FLOW_DIR_IN;
1261 case XFRM_POLICY_OUT:
1262 return FLOW_DIR_OUT;
1263 case XFRM_POLICY_FWD:
1264 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001265 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001266}
1267
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001268static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Steffen Klassert0b865642017-02-14 07:43:56 +01001269 const struct flowi *fl, u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct xfrm_policy *pol;
1272
Eric Dumazetd188ba82015-12-08 07:22:02 -08001273 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001274 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001275 pol = rcu_dereference(sk->sk_policy[dir]);
1276 if (pol != NULL) {
Steffen Klassert46b31712017-11-29 06:53:55 +01001277 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001278 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001279
Steffen Klassert46b31712017-11-29 06:53:55 +01001280 if (pol->family != family) {
1281 pol = NULL;
1282 goto out;
1283 }
1284
1285 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001286 if (match) {
Steffen Klasserta80f5602018-06-12 14:07:07 +02001287 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
1288 pol->if_id != fl->flowi_xfrm.if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001289 pol = NULL;
1290 goto out;
1291 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001292 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001293 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001294 policy_to_flow_dir(dir));
Florian Westphal330e8322016-11-17 13:21:46 +01001295 if (!err) {
1296 if (!xfrm_pol_hold_rcu(pol))
1297 goto again;
1298 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001299 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001300 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001301 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001302 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001303 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 pol = NULL;
1305 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001306out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001307 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return pol;
1309}
1310
1311static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1312{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001313 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001314
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001315 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001316 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 xfrm_pol_hold(pol);
1318}
1319
1320static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1321 int dir)
1322{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001323 struct net *net = xp_net(pol);
1324
Herbert Xu53c2e282014-11-13 17:09:49 +08001325 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001326 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Herbert Xu53c2e282014-11-13 17:09:49 +08001328 /* Socket policies are not hashed. */
1329 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001330 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001331 hlist_del(&pol->byidx);
1332 }
1333
1334 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001335 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001336
1337 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Herbert Xu53c2e282014-11-13 17:09:49 +08001340static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1341{
1342 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1343}
1344
1345static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1346{
1347 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1348}
1349
Herbert Xu4666faa2005-06-18 22:43:22 -07001350int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Fan Du283bc9f2013-11-07 17:47:50 +08001352 struct net *net = xp_net(pol);
1353
Florian Westphal9d0380d2016-08-11 15:17:59 +02001354 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001356 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001361 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
David S. Millera70fcb02006-03-20 19:18:52 -08001363EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1366{
Lorenzo Colitti38884cc2017-11-20 19:26:02 +09001367 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct xfrm_policy *old_pol;
1369
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001370#ifdef CONFIG_XFRM_SUB_POLICY
1371 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1372 return -EINVAL;
1373#endif
1374
Florian Westphal9d0380d2016-08-11 15:17:59 +02001375 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001376 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1377 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001379 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001380 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001381 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001383 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001384 if (old_pol) {
1385 if (pol)
1386 xfrm_policy_requeue(old_pol, pol);
1387
Timo Teräsea2dea92010-03-31 00:17:05 +00001388 /* Unlinking succeeds always. This is the only function
1389 * allowed to delete or replace socket policy.
1390 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001391 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001392 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001393 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (old_pol) {
1396 xfrm_policy_kill(old_pol);
1397 }
1398 return 0;
1399}
1400
David S. Millerd3e40a92011-02-24 01:25:41 -05001401static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001403 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001404 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (newp) {
1407 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001408 if (security_xfrm_policy_clone(old->security,
1409 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001410 kfree(newp);
1411 return NULL; /* ENOMEM */
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 newp->lft = old->lft;
1414 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001415 newp->mark = old->mark;
Steffen Klasserta80f5602018-06-12 14:07:07 +02001416 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 newp->action = old->action;
1418 newp->flags = old->flags;
1419 newp->xfrm_nr = old->xfrm_nr;
1420 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001421 newp->type = old->type;
Herbert Xu6ceabde2017-11-10 14:14:06 +11001422 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 memcpy(newp->xfrm_vec, old->xfrm_vec,
1424 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001425 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001426 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001427 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 xfrm_pol_put(newp);
1429 }
1430 return newp;
1431}
1432
Eric Dumazetd188ba82015-12-08 07:22:02 -08001433int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001435 const struct xfrm_policy *p;
1436 struct xfrm_policy *np;
1437 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Eric Dumazetd188ba82015-12-08 07:22:02 -08001439 rcu_read_lock();
1440 for (i = 0; i < 2; i++) {
1441 p = rcu_dereference(osk->sk_policy[i]);
1442 if (p) {
1443 np = clone_policy(p, i);
1444 if (unlikely(!np)) {
1445 ret = -ENOMEM;
1446 break;
1447 }
1448 rcu_assign_pointer(sk->sk_policy[i], np);
1449 }
1450 }
1451 rcu_read_unlock();
1452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001455static int
David Ahern42a7b322015-08-10 16:58:11 -06001456xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti34e23de2017-08-11 02:11:33 +09001457 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001458{
1459 int err;
1460 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1461
1462 if (unlikely(afinfo == NULL))
1463 return -EINVAL;
Lorenzo Colitti34e23de2017-08-11 02:11:33 +09001464 err = afinfo->get_saddr(net, oif, local, remote, mark);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001465 xfrm_policy_put_afinfo(afinfo);
1466 return err;
1467}
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/* Resolve list of templates for the flow, given policy. */
1470
1471static int
David S. Millera6c2e612011-02-22 18:35:39 -08001472xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1473 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001475 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int nx;
1477 int i, error;
1478 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1479 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001480 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Weilong Chen9b7a7872013-12-24 09:43:46 +08001482 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct xfrm_state *x;
1484 xfrm_address_t *remote = daddr;
1485 xfrm_address_t *local = saddr;
1486 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1487
Joakim Koskela48b8d782007-07-26 00:08:42 -07001488 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1489 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 remote = &tmpl->id.daddr;
1491 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001492 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001493 error = xfrm_get_saddr(net, fl->flowi_oif,
1494 &tmp, remote,
Lorenzo Colitti34e23de2017-08-11 02:11:33 +09001495 tmpl->encap_family, 0);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001496 if (error)
1497 goto fail;
1498 local = &tmp;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501
1502 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1503
1504 if (x && x->km.state == XFRM_STATE_VALID) {
1505 xfrm[nx++] = x;
1506 daddr = remote;
1507 saddr = local;
1508 continue;
1509 }
1510 if (x) {
1511 error = (x->km.state == XFRM_STATE_ERROR ?
1512 -EINVAL : -EAGAIN);
1513 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001514 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001515 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (!tmpl->optional)
1519 goto fail;
1520 }
1521 return nx;
1522
1523fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001524 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 xfrm_state_put(xfrm[nx]);
1526 return error;
1527}
1528
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001529static int
David S. Millera6c2e612011-02-22 18:35:39 -08001530xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1531 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001532{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001533 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1534 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001535 int cnx = 0;
1536 int error;
1537 int ret;
1538 int i;
1539
1540 for (i = 0; i < npols; i++) {
1541 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1542 error = -ENOBUFS;
1543 goto fail;
1544 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001545
1546 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001547 if (ret < 0) {
1548 error = ret;
1549 goto fail;
1550 } else
1551 cnx += ret;
1552 }
1553
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001554 /* found states are sorted for outbound processing */
1555 if (npols > 1)
1556 xfrm_state_sort(xfrm, tpp, cnx, family);
1557
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001558 return cnx;
1559
1560 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001561 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001562 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001563 return error;
1564
1565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/* Check that the bundle accepts the flow and its components are
1568 * still valid.
1569 */
1570
David S. Miller05d84022011-02-22 17:47:10 -08001571static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001572{
1573 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1574 int tos;
1575
1576 if (!afinfo)
1577 return -EINVAL;
1578
1579 tos = afinfo->get_tos(fl);
1580
1581 xfrm_policy_put_afinfo(afinfo);
1582
1583 return tos;
1584}
1585
Timo Teräs80c802f2010-04-07 00:30:05 +00001586static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1587{
1588 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1589 struct dst_entry *dst = &xdst->u.dst;
1590
1591 if (xdst->route == NULL) {
1592 /* Dummy bundle - if it has xfrms we were not
1593 * able to build bundle as template resolution failed.
1594 * It means we need to try again resolving. */
1595 if (xdst->num_xfrms > 0)
1596 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001597 } else if (dst->flags & DST_XFRM_QUEUE) {
1598 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001599 } else {
1600 /* Real bundle */
1601 if (stale_bundle(dst))
1602 return NULL;
1603 }
1604
1605 dst_hold(dst);
1606 return flo;
1607}
1608
1609static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1610{
1611 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1612 struct dst_entry *dst = &xdst->u.dst;
1613
1614 if (!xdst->route)
1615 return 0;
1616 if (stale_bundle(dst))
1617 return 0;
1618
1619 return 1;
1620}
1621
1622static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1623{
1624 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1625 struct dst_entry *dst = &xdst->u.dst;
1626
1627 dst_free(dst);
1628}
1629
1630static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1631 .get = xfrm_bundle_flo_get,
1632 .check = xfrm_bundle_flo_check,
1633 .delete = xfrm_bundle_flo_delete,
1634};
1635
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001636static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001637{
1638 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001639 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001640 struct xfrm_dst *xdst;
1641
1642 if (!afinfo)
1643 return ERR_PTR(-EINVAL);
1644
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001645 switch (family) {
1646 case AF_INET:
1647 dst_ops = &net->xfrm.xfrm4_dst_ops;
1648 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001649#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001650 case AF_INET6:
1651 dst_ops = &net->xfrm.xfrm6_dst_ops;
1652 break;
1653#endif
1654 default:
1655 BUG();
1656 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001657 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001658
Madalin Bucurd4cae562011-09-26 07:04:36 +00001659 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001660 struct dst_entry *dst = &xdst->u.dst;
1661
1662 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001663 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001664 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001665 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001666
Madalin Bucurd4cae562011-09-26 07:04:36 +00001667 xfrm_policy_put_afinfo(afinfo);
1668
Herbert Xu25ee3282007-12-11 09:32:34 -08001669 return xdst;
1670}
1671
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001672static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1673 int nfheader_len)
1674{
1675 struct xfrm_policy_afinfo *afinfo =
1676 xfrm_policy_get_afinfo(dst->ops->family);
1677 int err;
1678
1679 if (!afinfo)
1680 return -EINVAL;
1681
1682 err = afinfo->init_path(path, dst, nfheader_len);
1683
1684 xfrm_policy_put_afinfo(afinfo);
1685
1686 return err;
1687}
1688
Herbert Xu87c1e122010-03-02 02:51:56 +00001689static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001690 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001691{
1692 struct xfrm_policy_afinfo *afinfo =
1693 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1694 int err;
1695
1696 if (!afinfo)
1697 return -EINVAL;
1698
Herbert Xu87c1e122010-03-02 02:51:56 +00001699 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001700
1701 xfrm_policy_put_afinfo(afinfo);
1702
1703 return err;
1704}
1705
Timo Teräs80c802f2010-04-07 00:30:05 +00001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1708 * all the metrics... Shortly, bundle a bundle.
1709 */
1710
Herbert Xu25ee3282007-12-11 09:32:34 -08001711static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1712 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001713 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001714 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001716 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001717 unsigned long now = jiffies;
1718 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001719 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001720 struct dst_entry *dst_prev = NULL;
1721 struct dst_entry *dst0 = NULL;
1722 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001724 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001725 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001726 int trailer_len = 0;
1727 int tos;
1728 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001729 xfrm_address_t saddr, daddr;
1730
1731 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001732
1733 tos = xfrm_get_tos(fl, family);
1734 err = tos;
1735 if (tos < 0)
1736 goto put_states;
1737
1738 dst_hold(dst);
1739
1740 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001741 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001742 struct dst_entry *dst1 = &xdst->u.dst;
1743
1744 err = PTR_ERR(xdst);
1745 if (IS_ERR(xdst)) {
1746 dst_release(dst);
1747 goto put_states;
1748 }
1749
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001750 if (xfrm[i]->sel.family == AF_UNSPEC) {
1751 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1752 xfrm_af2proto(family));
1753 if (!inner_mode) {
1754 err = -EAFNOSUPPORT;
1755 dst_release(dst);
1756 goto put_states;
1757 }
1758 } else
1759 inner_mode = xfrm[i]->inner_mode;
1760
Herbert Xu25ee3282007-12-11 09:32:34 -08001761 if (!dst_prev)
1762 dst0 = dst1;
1763 else {
1764 dst_prev->child = dst_clone(dst1);
1765 dst1->flags |= DST_NOHASH;
1766 }
1767
1768 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001769 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001770
1771 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Benedict Wong6fffec42019-01-14 11:24:38 -08001772 __u32 mark = 0;
1773
1774 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
1775 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
Steffen Klassert78f10c52018-06-12 12:44:26 +02001776
Herbert Xu25ee3282007-12-11 09:32:34 -08001777 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001778 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert78f10c52018-06-12 12:44:26 +02001779 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08001780 err = PTR_ERR(dst);
1781 if (IS_ERR(dst))
1782 goto put_states;
1783 } else
1784 dst_hold(dst);
1785
1786 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001787 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001788
David S. Millerf5b0a872012-07-19 12:31:33 -07001789 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001790 dst1->flags |= DST_HOST;
1791 dst1->lastuse = now;
1792
1793 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001794 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001795
1796 dst1->next = dst_prev;
1797 dst_prev = dst1;
1798
1799 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001800 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1801 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001802 trailer_len += xfrm[i]->props.trailer_len;
1803 }
1804
1805 dst_prev->child = dst;
1806 dst0->path = dst;
1807
1808 err = -ENODEV;
1809 dev = dst->dev;
1810 if (!dev)
1811 goto free_dst;
1812
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001813 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001814 xfrm_init_pmtu(dst_prev);
1815
1816 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1817 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1818
Herbert Xu87c1e122010-03-02 02:51:56 +00001819 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001820 if (err)
1821 goto free_dst;
1822
1823 dst_prev->header_len = header_len;
1824 dst_prev->trailer_len = trailer_len;
1825 header_len -= xdst->u.dst.xfrm->props.header_len;
1826 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1827 }
1828
1829out:
1830 return dst0;
1831
1832put_states:
1833 for (; i < nx; i++)
1834 xfrm_state_put(xfrm[i]);
1835free_dst:
1836 if (dst0)
1837 dst_free(dst0);
1838 dst0 = ERR_PTR(err);
1839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
David S. Miller73ff93c2011-02-22 18:33:42 -08001842static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001843 struct xfrm_policy **pols,
1844 int *num_pols, int *num_xfrms)
1845{
1846 int i;
1847
1848 if (*num_pols == 0 || !pols[0]) {
1849 *num_pols = 0;
1850 *num_xfrms = 0;
1851 return 0;
1852 }
1853 if (IS_ERR(pols[0]))
1854 return PTR_ERR(pols[0]);
1855
1856 *num_xfrms = pols[0]->xfrm_nr;
1857
1858#ifdef CONFIG_XFRM_SUB_POLICY
1859 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1860 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1861 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1862 XFRM_POLICY_TYPE_MAIN,
1863 fl, family,
1864 XFRM_POLICY_OUT);
1865 if (pols[1]) {
1866 if (IS_ERR(pols[1])) {
1867 xfrm_pols_put(pols, *num_pols);
1868 return PTR_ERR(pols[1]);
1869 }
Weilong Chen02d08922013-12-24 09:43:48 +08001870 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001871 (*num_xfrms) += pols[1]->xfrm_nr;
1872 }
1873 }
1874#endif
1875 for (i = 0; i < *num_pols; i++) {
1876 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1877 *num_xfrms = -1;
1878 break;
1879 }
1880 }
1881
1882 return 0;
1883
1884}
1885
1886static struct xfrm_dst *
1887xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001888 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001889 struct dst_entry *dst_orig)
1890{
1891 struct net *net = xp_net(pols[0]);
1892 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1893 struct dst_entry *dst;
1894 struct xfrm_dst *xdst;
1895 int err;
1896
1897 /* Try to instantiate a bundle */
1898 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001899 if (err <= 0) {
YueHaibingaf322ba2018-07-25 16:54:33 +08001900 if (err == 0)
1901 return NULL;
1902
1903 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001904 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1905 return ERR_PTR(err);
1906 }
1907
1908 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1909 if (IS_ERR(dst)) {
1910 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1911 return ERR_CAST(dst);
1912 }
1913
1914 xdst = (struct xfrm_dst *)dst;
1915 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00001916 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001917 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001918 xdst->policy_genid = atomic_read(&pols[0]->genid);
1919
1920 return xdst;
1921}
1922
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001923static void xfrm_policy_queue_process(unsigned long arg)
1924{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001925 struct sk_buff *skb;
1926 struct sock *sk;
1927 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001928 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001929 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001930 struct xfrm_policy_queue *pq = &pol->polq;
1931 struct flowi fl;
1932 struct sk_buff_head list;
1933
1934 spin_lock(&pq->hold_queue.lock);
1935 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001936 if (!skb) {
1937 spin_unlock(&pq->hold_queue.lock);
1938 goto out;
1939 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001940 dst = skb_dst(skb);
1941 sk = skb->sk;
1942 xfrm_decode_session(skb, &fl, dst->ops->family);
1943 spin_unlock(&pq->hold_queue.lock);
1944
1945 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001946 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001947 if (IS_ERR(dst))
1948 goto purge_queue;
1949
1950 if (dst->flags & DST_XFRM_QUEUE) {
1951 dst_release(dst);
1952
1953 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1954 goto purge_queue;
1955
1956 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001957 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1958 xfrm_pol_hold(pol);
1959 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001960 }
1961
1962 dst_release(dst);
1963
1964 __skb_queue_head_init(&list);
1965
1966 spin_lock(&pq->hold_queue.lock);
1967 pq->timeout = 0;
1968 skb_queue_splice_init(&pq->hold_queue, &list);
1969 spin_unlock(&pq->hold_queue.lock);
1970
1971 while (!skb_queue_empty(&list)) {
1972 skb = __skb_dequeue(&list);
1973
1974 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1975 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001976 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001977 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001978 kfree_skb(skb);
1979 continue;
1980 }
1981
1982 nf_reset(skb);
1983 skb_dst_drop(skb);
1984 skb_dst_set(skb, dst);
1985
Eric W. Biederman13206b62015-10-07 16:48:35 -05001986 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001987 }
1988
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001989out:
1990 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001991 return;
1992
1993purge_queue:
1994 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001995 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001996 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001997}
1998
Eric W. Biedermanede20592015-10-07 16:48:47 -05001999static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002000{
2001 unsigned long sched_next;
2002 struct dst_entry *dst = skb_dst(skb);
2003 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002004 struct xfrm_policy *pol = xdst->pols[0];
2005 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002006
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002007 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002008 kfree_skb(skb);
2009 return 0;
2010 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002011
2012 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2013 kfree_skb(skb);
2014 return -EAGAIN;
2015 }
2016
2017 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002018
2019 spin_lock_bh(&pq->hold_queue.lock);
2020
2021 if (!pq->timeout)
2022 pq->timeout = XFRM_QUEUE_TMO_MIN;
2023
2024 sched_next = jiffies + pq->timeout;
2025
2026 if (del_timer(&pq->hold_timer)) {
2027 if (time_before(pq->hold_timer.expires, sched_next))
2028 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002029 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002030 }
2031
2032 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002033 if (!mod_timer(&pq->hold_timer, sched_next))
2034 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002035
2036 spin_unlock_bh(&pq->hold_queue.lock);
2037
2038 return 0;
2039}
2040
2041static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002042 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002043 const struct flowi *fl,
2044 int num_xfrms,
2045 u16 family)
2046{
2047 int err;
2048 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002049 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002050 struct dst_entry *dst1;
2051 struct xfrm_dst *xdst;
2052
2053 xdst = xfrm_alloc_dst(net, family);
2054 if (IS_ERR(xdst))
2055 return xdst;
2056
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002057 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2058 net->xfrm.sysctl_larval_drop ||
2059 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002060 return xdst;
2061
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002062 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002063 dst1 = &xdst->u.dst;
2064 dst_hold(dst);
2065 xdst->route = dst;
2066
2067 dst_copy_metrics(dst1, dst);
2068
2069 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2070 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2071 dst1->lastuse = jiffies;
2072
2073 dst1->input = dst_discard;
2074 dst1->output = xdst_queue_output;
2075
2076 dst_hold(dst);
2077 dst1->child = dst;
2078 dst1->path = dst;
2079
2080 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2081
2082 err = -ENODEV;
2083 dev = dst->dev;
2084 if (!dev)
2085 goto free_dst;
2086
2087 err = xfrm_fill_dst(xdst, dev, fl);
2088 if (err)
2089 goto free_dst;
2090
2091out:
2092 return xdst;
2093
2094free_dst:
2095 dst_release(dst1);
2096 xdst = ERR_PTR(err);
2097 goto out;
2098}
2099
Timo Teräs80c802f2010-04-07 00:30:05 +00002100static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002101xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002102 struct flow_cache_object *oldflo, void *ctx)
2103{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002104 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002105 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2106 struct xfrm_dst *xdst, *new_xdst;
2107 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2108
2109 /* Check if the policies from old bundle are usable */
2110 xdst = NULL;
2111 if (oldflo) {
2112 xdst = container_of(oldflo, struct xfrm_dst, flo);
2113 num_pols = xdst->num_pols;
2114 num_xfrms = xdst->num_xfrms;
2115 pol_dead = 0;
2116 for (i = 0; i < num_pols; i++) {
2117 pols[i] = xdst->pols[i];
2118 pol_dead |= pols[i]->walk.dead;
2119 }
2120 if (pol_dead) {
2121 dst_free(&xdst->u.dst);
2122 xdst = NULL;
2123 num_pols = 0;
2124 num_xfrms = 0;
2125 oldflo = NULL;
2126 }
2127 }
2128
2129 /* Resolve policies to use if we couldn't get them from
2130 * previous cache entry */
2131 if (xdst == NULL) {
2132 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002133 pols[0] = __xfrm_policy_lookup(net, fl, family,
2134 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002135 err = xfrm_expand_policies(fl, family, pols,
2136 &num_pols, &num_xfrms);
2137 if (err < 0)
2138 goto inc_error;
2139 if (num_pols == 0)
2140 return NULL;
2141 if (num_xfrms <= 0)
2142 goto make_dummy_bundle;
2143 }
2144
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002145 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2146 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002147 if (IS_ERR(new_xdst)) {
2148 err = PTR_ERR(new_xdst);
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002149 if (err == -EREMOTE) {
2150 xfrm_pols_put(pols, num_pols);
2151 return NULL;
2152 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002153 if (err != -EAGAIN)
2154 goto error;
2155 if (oldflo == NULL)
2156 goto make_dummy_bundle;
2157 dst_hold(&xdst->u.dst);
2158 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002159 } else if (new_xdst == NULL) {
2160 num_xfrms = 0;
2161 if (oldflo == NULL)
2162 goto make_dummy_bundle;
2163 xdst->num_xfrms = 0;
2164 dst_hold(&xdst->u.dst);
2165 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002166 }
2167
2168 /* Kill the previous bundle */
2169 if (xdst) {
2170 /* The policies were stolen for newly generated bundle */
2171 xdst->num_pols = 0;
2172 dst_free(&xdst->u.dst);
2173 }
2174
2175 /* Flow cache does not have reference, it dst_free()'s,
2176 * but we do need to return one reference for original caller */
2177 dst_hold(&new_xdst->u.dst);
2178 return &new_xdst->flo;
2179
2180make_dummy_bundle:
2181 /* We found policies, but there's no bundles to instantiate:
2182 * either because the policy blocks, has no transformations or
2183 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002184 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002185 if (IS_ERR(xdst)) {
2186 xfrm_pols_put(pols, num_pols);
2187 return ERR_CAST(xdst);
2188 }
2189 xdst->num_pols = num_pols;
2190 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002191 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002192
2193 dst_hold(&xdst->u.dst);
2194 return &xdst->flo;
2195
2196inc_error:
2197 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2198error:
2199 if (xdst != NULL)
2200 dst_free(&xdst->u.dst);
2201 else
2202 xfrm_pols_put(pols, num_pols);
2203 return ERR_PTR(err);
2204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
David S. Miller2774c132011-03-01 14:59:04 -08002206static struct dst_entry *make_blackhole(struct net *net, u16 family,
2207 struct dst_entry *dst_orig)
2208{
2209 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2210 struct dst_entry *ret;
2211
2212 if (!afinfo) {
2213 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002214 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002215 } else {
2216 ret = afinfo->blackhole_route(net, dst_orig);
2217 }
2218 xfrm_policy_put_afinfo(afinfo);
2219
2220 return ret;
2221}
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223/* Main function: finds/creates a bundle for given flow.
2224 *
2225 * At the moment we eat a raw IP route. Mostly to speed up lookups
2226 * on interfaces with disabled IPsec.
2227 */
David S. Miller452edd52011-03-02 13:27:41 -08002228struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2229 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002230 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002232 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002233 struct flow_cache_object *flo;
2234 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002235 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002236 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002237 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002238 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002239
Timo Teräs80c802f2010-04-07 00:30:05 +00002240 dst = NULL;
2241 xdst = NULL;
2242 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002243
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002244 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002245 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002246 num_pols = 1;
Steffen Klassert0b865642017-02-14 07:43:56 +01002247 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002248 err = xfrm_expand_policies(fl, family, pols,
2249 &num_pols, &num_xfrms);
2250 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002251 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002252
2253 if (num_pols) {
2254 if (num_xfrms <= 0) {
2255 drop_pols = num_pols;
2256 goto no_transform;
2257 }
2258
2259 xdst = xfrm_resolve_and_create_bundle(
2260 pols, num_pols, fl,
2261 family, dst_orig);
2262 if (IS_ERR(xdst)) {
2263 xfrm_pols_put(pols, num_pols);
2264 err = PTR_ERR(xdst);
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002265 if (err == -EREMOTE)
2266 goto nopol;
2267
Timo Teräs80c802f2010-04-07 00:30:05 +00002268 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002269 } else if (xdst == NULL) {
2270 num_xfrms = 0;
2271 drop_pols = num_pols;
2272 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002273 }
2274
Steffen Klassertb7eea452014-06-18 12:34:21 +02002275 dst_hold(&xdst->u.dst);
2276 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002277 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002278 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Timo Teräs80c802f2010-04-07 00:30:05 +00002281 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002282 struct xfrm_flo xflo;
2283
2284 xflo.dst_orig = dst_orig;
2285 xflo.flags = flags;
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002288 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002289 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002290 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Timo Teräs80c802f2010-04-07 00:30:05 +00002292 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002293 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002294 if (flo == NULL)
2295 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002296 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002297 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002298 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002299 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002300 xdst = container_of(flo, struct xfrm_dst, flo);
2301
2302 num_pols = xdst->num_pols;
2303 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002304 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002305 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
Timo Teräs80c802f2010-04-07 00:30:05 +00002308 dst = &xdst->u.dst;
2309 if (route == NULL && num_xfrms > 0) {
2310 /* The only case when xfrm_bundle_lookup() returns a
2311 * bundle with null route, is when the template could
2312 * not be resolved. It means policies are there, but
2313 * bundle could not be created, since we don't yet
2314 * have the xfrm_state's. We need to wait for KM to
2315 * negotiate new SA's or bail out with error.*/
2316 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002317 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002318 err = -EREMOTE;
2319 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002320 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002321
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002322 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002323
2324 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2325 goto error;
2326 }
2327
2328no_transform:
2329 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002330 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Timo Teräs80c802f2010-04-07 00:30:05 +00002332 if ((flags & XFRM_LOOKUP_ICMP) &&
2333 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2334 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002335 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002336 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002337
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 for (i = 0; i < num_pols; i++)
2339 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002340
Timo Teräs80c802f2010-04-07 00:30:05 +00002341 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002343 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002344 err = -EPERM;
2345 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 } else if (num_xfrms > 0) {
2347 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002348 dst_release(dst_orig);
2349 } else {
2350 /* Flow passes untransformed */
2351 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002352 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002354ok:
2355 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002356 if (dst && dst->xfrm &&
2357 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2358 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002359 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Timo Teräs80c802f2010-04-07 00:30:05 +00002361nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002362 if (!(flags & XFRM_LOOKUP_ICMP)) {
2363 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002364 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002365 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002366 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002368 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002369dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002370 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2371 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002372 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002373 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375EXPORT_SYMBOL(xfrm_lookup);
2376
Steffen Klassertf92ee612014-09-16 10:08:40 +02002377/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2378 * Otherwise we may send out blackholed packets.
2379 */
2380struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2381 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002382 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002383{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002384 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002385 flags | XFRM_LOOKUP_QUEUE |
2386 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002387
2388 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2389 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2390
Tommi Rantala590f3122018-06-21 09:30:47 +03002391 if (IS_ERR(dst))
2392 dst_release(dst_orig);
2393
Steffen Klassertf92ee612014-09-16 10:08:40 +02002394 return dst;
2395}
2396EXPORT_SYMBOL(xfrm_lookup_route);
2397
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002398static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002399xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002400{
2401 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002402
2403 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2404 return 0;
2405 x = skb->sp->xvec[idx];
2406 if (!x->type->reject)
2407 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002408 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002409}
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411/* When skb is transformed back to its "native" form, we have to
2412 * check policy restrictions. At the moment we make this in maximally
2413 * stupid way. Shame on me. :-) Of course, connected sockets must
2414 * have policy cached at them.
2415 */
2416
2417static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002418xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 unsigned short family)
2420{
2421 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002422 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return x->id.proto == tmpl->id.proto &&
2424 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2425 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2426 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002427 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002428 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002429 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2430 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002433/*
2434 * 0 or more than 0 is returned when validation is succeeded (either bypass
2435 * because of optional transport mode, or next index of the mathced secpath
2436 * state with the template.
2437 * -1 is returned when no matching template is found.
2438 * Otherwise "-2 - errored_index" is returned.
2439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002441xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 unsigned short family)
2443{
2444 int idx = start;
2445
2446 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002447 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return start;
2449 } else
2450 start = -1;
2451 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002452 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002454 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2455 if (start == -1)
2456 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460 return start;
2461}
2462
Herbert Xud5422ef2007-12-12 10:44:16 -08002463int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2464 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002466 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2467 const struct xfrm_if_cb *ifcb = xfrm_if_get_cb();
2468 struct xfrm_if *xi;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002469 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 if (unlikely(afinfo == NULL))
2472 return -EAFNOSUPPORT;
2473
Herbert Xud5422ef2007-12-12 10:44:16 -08002474 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002475 if (ifcb) {
2476 xi = ifcb->decode_session(skb);
2477 if (xi)
2478 fl->flowi_xfrm.if_id = xi->p.if_id;
2479 }
2480
David S. Miller1d28f422011-03-12 00:29:39 -05002481 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
Herbert Xud5422ef2007-12-12 10:44:16 -08002485EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
David S. Miller9a7386e2011-02-24 01:44:12 -05002487static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002490 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002491 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495
2496 return 0;
2497}
2498
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002499int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 unsigned short family)
2501{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002502 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002504 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2505 int npols = 0;
2506 int xfrm_nr;
2507 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002508 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002510 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002511 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Herbert Xud5422ef2007-12-12 10:44:16 -08002513 reverse = dir & ~XFRM_POLICY_MASK;
2514 dir &= XFRM_POLICY_MASK;
2515 fl_dir = policy_to_flow_dir(dir);
2516
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002517 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002518 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002520 }
2521
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002522 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 /* First, check used SA against their selectors. */
2525 if (skb->sp) {
2526 int i;
2527
Weilong Chen9b7a7872013-12-24 09:43:46 +08002528 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002529 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002530 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002531 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535 }
2536
2537 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002538 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002539 if (sk && sk->sk_policy[dir]) {
Steffen Klassert0b865642017-02-14 07:43:56 +01002540 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002541 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002542 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002543 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002544 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002547 if (!pol) {
2548 struct flow_cache_object *flo;
2549
2550 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2551 xfrm_policy_lookup, NULL);
2552 if (IS_ERR_OR_NULL(flo))
2553 pol = ERR_CAST(flo);
2554 else
2555 pol = container_of(flo, struct xfrm_policy, flo);
2556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002558 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002559 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002560 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002561 }
James Morris134b0fc2006-10-05 15:42:27 -05002562
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002563 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002564 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002565 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002566 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002567 return 0;
2568 }
2569 return 1;
2570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
James Morris9d729f72007-03-04 16:12:44 -08002572 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002574 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002575 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002576#ifdef CONFIG_XFRM_SUB_POLICY
2577 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002578 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002579 &fl, family,
2580 XFRM_POLICY_IN);
2581 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002582 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002583 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002584 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002585 }
James Morris9d729f72007-03-04 16:12:44 -08002586 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002587 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002588 }
2589 }
2590#endif
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (pol->action == XFRM_POLICY_ALLOW) {
2593 struct sec_path *sp;
2594 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002595 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002596 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002597 struct xfrm_tmpl **tpp = tp;
2598 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 int i, k;
2600
2601 if ((sp = skb->sp) == NULL)
2602 sp = &dummy;
2603
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002604 for (pi = 0; pi < npols; pi++) {
2605 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002606 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002607 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002608 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002609 }
2610 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002611 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002612 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002613 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002614 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2615 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2616 }
2617 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002618 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002619 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002620 tpp = stp;
2621 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 /* For each tunnel xfrm, find the first matching tmpl.
2624 * For each tmpl before that, find corresponding xfrm.
2625 * Order is _important_. Later we will implement
2626 * some barriers, but at the moment barriers
2627 * are implied between each two transformations.
2628 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002629 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2630 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002631 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002632 if (k < -1)
2633 /* "-2 - errored_index" returned */
2634 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002635 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002640 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002641 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002645 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return 1;
2647 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002648 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002651 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002652reject_error:
2653 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return 0;
2655}
2656EXPORT_SYMBOL(__xfrm_policy_check);
2657
2658int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2659{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002660 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002662 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002663 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002665 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002666 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002670 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002671
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002672 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002673 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002674 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002675 dst = NULL;
2676 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002677 skb_dst_set(skb, dst);
2678 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680EXPORT_SYMBOL(__xfrm_route_forward);
2681
David S. Millerd49c73c2006-08-13 18:55:53 -07002682/* Optimize later using cookies and generation ids. */
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2685{
David S. Millerd49c73c2006-08-13 18:55:53 -07002686 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002687 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2688 * get validated by dst_ops->check on every use. We do this
2689 * because when a normal route referenced by an XFRM dst is
2690 * obsoleted we do not go looking around for all parent
2691 * referencing XFRM dsts so that we can invalidate them. It
2692 * is just too much work. Instead we make the checks here on
2693 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002694 *
2695 * XFRM dst A --> IPv4 dst X
2696 *
2697 * X is the "xdst->route" of A (X is also the "dst->path" of A
2698 * in this example). If X is marked obsolete, "A" will not
2699 * notice. That's what we are validating here via the
2700 * stale_bundle() check.
2701 *
2702 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002703 * dst which causes it's ->obsolete field to be set to
2704 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2705 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002706 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002707 if (dst->obsolete < 0 && !stale_bundle(dst))
2708 return dst;
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return NULL;
2711}
2712
2713static int stale_bundle(struct dst_entry *dst)
2714{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002715 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Herbert Xuaabc9762005-05-03 16:27:10 -07002718void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002721 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002722 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 dev_put(dev);
2724 }
2725}
Herbert Xuaabc9762005-05-03 16:27:10 -07002726EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728static void xfrm_link_failure(struct sk_buff *skb)
2729{
2730 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
2733static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2734{
2735 if (dst) {
2736 if (dst->obsolete) {
2737 dst_release(dst);
2738 dst = NULL;
2739 }
2740 }
2741 return dst;
2742}
2743
Paul Mooree4c17212013-05-29 07:36:25 +00002744void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002745{
Fan Duca925cf2014-01-18 09:55:27 +08002746 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002747}
Paul Mooree4c17212013-05-29 07:36:25 +00002748EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002749
2750static void xfrm_garbage_collect_deferred(struct net *net)
2751{
Fan Duca925cf2014-01-18 09:55:27 +08002752 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002753}
2754
Herbert Xu25ee3282007-12-11 09:32:34 -08002755static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 do {
2758 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2759 u32 pmtu, route_mtu_cached;
2760
2761 pmtu = dst_mtu(dst->child);
2762 xdst->child_mtu_cached = pmtu;
2763
2764 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2765
2766 route_mtu_cached = dst_mtu(xdst->route);
2767 xdst->route_mtu_cached = route_mtu_cached;
2768
2769 if (pmtu > route_mtu_cached)
2770 pmtu = route_mtu_cached;
2771
David S. Millerdefb3512010-12-08 21:16:57 -08002772 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 } while ((dst = dst->next));
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776/* Check that the bundle accepts the flow and its components are
2777 * still valid.
2778 */
2779
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002780static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 struct dst_entry *dst = &first->u.dst;
2783 struct xfrm_dst *last;
2784 u32 mtu;
2785
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002786 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 (dst->dev && !netif_running(dst->dev)))
2788 return 0;
2789
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002790 if (dst->flags & DST_XFRM_QUEUE)
2791 return 1;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 last = NULL;
2794
2795 do {
2796 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2799 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002800 if (xdst->xfrm_genid != dst->xfrm->genid)
2801 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002802 if (xdst->num_pols > 0 &&
2803 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 mtu = dst_mtu(dst->child);
2807 if (xdst->child_mtu_cached != mtu) {
2808 last = xdst;
2809 xdst->child_mtu_cached = mtu;
2810 }
2811
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002812 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return 0;
2814 mtu = dst_mtu(xdst->route);
2815 if (xdst->route_mtu_cached != mtu) {
2816 last = xdst;
2817 xdst->route_mtu_cached = mtu;
2818 }
2819
2820 dst = dst->child;
2821 } while (dst->xfrm);
2822
2823 if (likely(!last))
2824 return 1;
2825
2826 mtu = last->child_mtu_cached;
2827 for (;;) {
2828 dst = &last->u.dst;
2829
2830 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2831 if (mtu > last->route_mtu_cached)
2832 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002833 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 if (last == first)
2836 break;
2837
Patrick McHardybd0bf072007-07-18 01:55:52 -07002838 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 last->child_mtu_cached = mtu;
2840 }
2841
2842 return 1;
2843}
2844
David S. Miller0dbaee32010-12-13 12:52:14 -08002845static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2846{
2847 return dst_metric_advmss(dst->path);
2848}
2849
Steffen Klassertebb762f2011-11-23 02:12:51 +00002850static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002851{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002852 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2853
2854 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002855}
2856
David S. Millerf894cbf2012-07-02 21:52:24 -07002857static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2858 struct sk_buff *skb,
2859 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002860{
David S. Millerf894cbf2012-07-02 21:52:24 -07002861 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002862}
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2865{
2866 int err = 0;
2867 if (unlikely(afinfo == NULL))
2868 return -EINVAL;
2869 if (unlikely(afinfo->family >= NPROTO))
2870 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002871 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002873 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 else {
2875 struct dst_ops *dst_ops = afinfo->dst_ops;
2876 if (likely(dst_ops->kmem_cachep == NULL))
2877 dst_ops->kmem_cachep = xfrm_dst_cache;
2878 if (likely(dst_ops->check == NULL))
2879 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002880 if (likely(dst_ops->default_advmss == NULL))
2881 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002882 if (likely(dst_ops->mtu == NULL))
2883 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (likely(dst_ops->negative_advice == NULL))
2885 dst_ops->negative_advice = xfrm_negative_advice;
2886 if (likely(dst_ops->link_failure == NULL))
2887 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002888 if (likely(dst_ops->neigh_lookup == NULL))
2889 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002891 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002892 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002894 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return err;
2897}
2898EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2899
2900int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2901{
2902 int err = 0;
2903 if (unlikely(afinfo == NULL))
2904 return -EINVAL;
2905 if (unlikely(afinfo->family >= NPROTO))
2906 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002907 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2909 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2910 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002911 else
2912 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2913 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002915 spin_unlock(&xfrm_policy_afinfo_lock);
2916 if (!err) {
2917 struct dst_ops *dst_ops = afinfo->dst_ops;
2918
2919 synchronize_rcu();
2920
2921 dst_ops->kmem_cachep = NULL;
2922 dst_ops->check = NULL;
2923 dst_ops->negative_advice = NULL;
2924 dst_ops->link_failure = NULL;
2925 afinfo->garbage_collect = NULL;
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return err;
2928}
2929EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2932{
Jiri Pirko351638e2013-05-28 01:30:21 +00002933 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 switch (event) {
2936 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002937 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
2939 return NOTIFY_DONE;
2940}
2941
2942static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002943 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944};
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02002945void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
2946{
2947 spin_lock(&xfrm_if_cb_lock);
2948 rcu_assign_pointer(xfrm_if_cb, ifcb);
2949 spin_unlock(&xfrm_if_cb_lock);
2950}
2951EXPORT_SYMBOL(xfrm_if_register_cb);
2952
2953void xfrm_if_unregister_cb(void)
2954{
2955 RCU_INIT_POINTER(xfrm_if_cb, NULL);
2956 synchronize_rcu();
2957}
2958EXPORT_SYMBOL(xfrm_if_unregister_cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002960#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002961static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002962{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002963 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002964 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2965 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002966 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002967 rv = xfrm_proc_init(net);
2968 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002969 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002970 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002971}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002972
2973static void xfrm_statistics_fini(struct net *net)
2974{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002975 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002976 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002977}
2978#else
2979static int __net_init xfrm_statistics_init(struct net *net)
2980{
2981 return 0;
2982}
2983
2984static void xfrm_statistics_fini(struct net *net)
2985{
2986}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002987#endif
2988
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002989static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
David S. Miller2518c7c2006-08-24 04:45:07 -07002991 unsigned int hmask, sz;
2992 int dir;
2993
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002994 if (net_eq(net, &init_net))
2995 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002997 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002998 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
David S. Miller2518c7c2006-08-24 04:45:07 -07003000 hmask = 8 - 1;
3001 sz = (hmask+1) * sizeof(struct hlist_head);
3002
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003003 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3004 if (!net->xfrm.policy_byidx)
3005 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003006 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07003007
Herbert Xu53c2e282014-11-13 17:09:49 +08003008 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07003009 struct xfrm_policy_hash *htab;
3010
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08003011 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08003012 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003013 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07003014
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003015 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07003016 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07003017 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003018 goto out_bydst;
3019 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02003020 htab->dbits4 = 32;
3021 htab->sbits4 = 32;
3022 htab->dbits6 = 128;
3023 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003024 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003025 net->xfrm.policy_hthresh.lbits4 = 32;
3026 net->xfrm.policy_hthresh.rbits4 = 32;
3027 net->xfrm.policy_hthresh.lbits6 = 128;
3028 net->xfrm.policy_hthresh.rbits6 = 128;
3029
3030 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003031
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003032 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003033 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003034 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003035 if (net_eq(net, &init_net))
3036 register_netdevice_notifier(&xfrm_dev_notifier);
3037 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003038
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003039out_bydst:
3040 for (dir--; dir >= 0; dir--) {
3041 struct xfrm_policy_hash *htab;
3042
3043 htab = &net->xfrm.policy_bydst[dir];
3044 xfrm_hash_free(htab->table, sz);
3045 }
3046 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003047out_byidx:
3048 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003051static void xfrm_policy_fini(struct net *net)
3052{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003053 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003054 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003055
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003056 flush_work(&net->xfrm.policy_hash_work);
3057#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003058 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003059#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003060 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003061
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003062 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003063
Herbert Xu53c2e282014-11-13 17:09:49 +08003064 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003065 struct xfrm_policy_hash *htab;
3066
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003067 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003068
3069 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003070 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003071 WARN_ON(!hlist_empty(htab->table));
3072 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003073 }
3074
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003075 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003076 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3077 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003078}
3079
3080static int __net_init xfrm_net_init(struct net *net)
3081{
3082 int rv;
3083
Florian Westphalf68a09c2017-02-08 11:52:29 +01003084 /* Initialize the per-net locks here */
3085 spin_lock_init(&net->xfrm.xfrm_state_lock);
3086 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3087 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3088
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003089 rv = xfrm_statistics_init(net);
3090 if (rv < 0)
3091 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003092 rv = xfrm_state_init(net);
3093 if (rv < 0)
3094 goto out_state;
3095 rv = xfrm_policy_init(net);
3096 if (rv < 0)
3097 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003098 rv = xfrm_sysctl_init(net);
3099 if (rv < 0)
3100 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003101 rv = flow_cache_init(net);
3102 if (rv < 0)
3103 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003104
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003105 return 0;
3106
Steffen Klassert4a93f502014-03-12 09:43:17 +01003107out:
3108 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003109out_sysctl:
3110 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003111out_policy:
3112 xfrm_state_fini(net);
3113out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003114 xfrm_statistics_fini(net);
3115out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003116 return rv;
3117}
3118
3119static void __net_exit xfrm_net_exit(struct net *net)
3120{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003121 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003122 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003123 xfrm_policy_fini(net);
3124 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003125 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003126}
3127
3128static struct pernet_operations __net_initdata xfrm_net_ops = {
3129 .init = xfrm_net_init,
3130 .exit = xfrm_net_exit,
3131};
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133void __init xfrm_init(void)
3134{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003135 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003136 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 xfrm_input_init();
Steffen Klassertf5bb9e92018-06-12 14:07:12 +02003138
3139 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3140 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Joy Lattenab5f5e82007-09-17 11:51:22 -07003143#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003144static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3145 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003146{
Paul Moore875179f2007-12-01 23:27:18 +11003147 struct xfrm_sec_ctx *ctx = xp->security;
3148 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003149
Paul Moore875179f2007-12-01 23:27:18 +11003150 if (ctx)
3151 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3152 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3153
Weilong Chen9b7a7872013-12-24 09:43:46 +08003154 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003155 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003156 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003157 if (sel->prefixlen_s != 32)
3158 audit_log_format(audit_buf, " src_prefixlen=%d",
3159 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003160 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003161 if (sel->prefixlen_d != 32)
3162 audit_log_format(audit_buf, " dst_prefixlen=%d",
3163 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003164 break;
3165 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003166 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003167 if (sel->prefixlen_s != 128)
3168 audit_log_format(audit_buf, " src_prefixlen=%d",
3169 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003170 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003171 if (sel->prefixlen_d != 128)
3172 audit_log_format(audit_buf, " dst_prefixlen=%d",
3173 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003174 break;
3175 }
3176}
3177
Tetsuo Handa2e710292014-04-22 21:48:30 +09003178void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003179{
3180 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003181
Paul Mooreafeb14b2007-12-21 14:58:11 -08003182 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003183 if (audit_buf == NULL)
3184 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003185 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003186 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003187 xfrm_audit_common_policyinfo(xp, audit_buf);
3188 audit_log_end(audit_buf);
3189}
3190EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3191
Paul Moore68277ac2007-12-20 20:49:33 -08003192void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003193 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003194{
3195 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003196
Paul Mooreafeb14b2007-12-21 14:58:11 -08003197 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003198 if (audit_buf == NULL)
3199 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003200 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003201 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003202 xfrm_audit_common_policyinfo(xp, audit_buf);
3203 audit_log_end(audit_buf);
3204}
3205EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3206#endif
3207
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003208#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003209static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3210 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003211{
3212 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3213 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003214 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3215 sel_cmp->family) &&
3216 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3217 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003218 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3219 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003220 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003221 }
3222 } else {
3223 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003224 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003225 }
3226 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003227 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003228}
3229
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003230static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3231 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232{
3233 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003234 struct hlist_head *chain;
3235 u32 priority = ~0U;
3236
Florian Westphal9d0380d2016-08-11 15:17:59 +02003237 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003238 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003239 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003240 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3241 pol->type == type) {
3242 ret = pol;
3243 priority = ret->priority;
3244 break;
3245 }
3246 }
Fan Du8d549c42013-11-07 17:47:49 +08003247 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003248 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003249 if ((pol->priority >= priority) && ret)
3250 break;
3251
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003252 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003253 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003254 ret = pol;
3255 break;
3256 }
3257 }
3258
Li RongQing586f2eb2015-04-30 17:13:41 +08003259 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003260
Florian Westphal9d0380d2016-08-11 15:17:59 +02003261 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003262
3263 return ret;
3264}
3265
David S. Millerdd701752011-02-24 00:21:08 -05003266static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003267{
3268 int match = 0;
3269
3270 if (t->mode == m->mode && t->id.proto == m->proto &&
3271 (m->reqid == 0 || t->reqid == m->reqid)) {
3272 switch (t->mode) {
3273 case XFRM_MODE_TUNNEL:
3274 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003275 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3276 m->old_family) &&
3277 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3278 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003279 match = 1;
3280 }
3281 break;
3282 case XFRM_MODE_TRANSPORT:
3283 /* in case of transport mode, template does not store
3284 any IP addresses, hence we just compare mode and
3285 protocol */
3286 match = 1;
3287 break;
3288 default:
3289 break;
3290 }
3291 }
3292 return match;
3293}
3294
3295/* update endpoint address(es) of template(s) */
3296static int xfrm_policy_migrate(struct xfrm_policy *pol,
3297 struct xfrm_migrate *m, int num_migrate)
3298{
3299 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003300 int i, j, n = 0;
3301
3302 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003303 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003304 /* target policy has been deleted */
3305 write_unlock_bh(&pol->lock);
3306 return -ENOENT;
3307 }
3308
3309 for (i = 0; i < pol->xfrm_nr; i++) {
3310 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3311 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3312 continue;
3313 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003314 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3315 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003316 continue;
3317 /* update endpoints */
3318 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3319 sizeof(pol->xfrm_vec[i].id.daddr));
3320 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3321 sizeof(pol->xfrm_vec[i].saddr));
3322 pol->xfrm_vec[i].encap_family = mp->new_family;
3323 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003324 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003325 }
3326 }
3327
3328 write_unlock_bh(&pol->lock);
3329
3330 if (!n)
3331 return -ENODATA;
3332
3333 return 0;
3334}
3335
David S. Millerdd701752011-02-24 00:21:08 -05003336static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003337{
3338 int i, j;
3339
3340 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3341 return -EINVAL;
3342
3343 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003344 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3345 m[i].old_family) &&
3346 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3347 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003348 return -EINVAL;
3349 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3350 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3351 return -EINVAL;
3352
3353 /* check if there is any duplicated entry */
3354 for (j = i + 1; j < num_migrate; j++) {
3355 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3356 sizeof(m[i].old_daddr)) &&
3357 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3358 sizeof(m[i].old_saddr)) &&
3359 m[i].proto == m[j].proto &&
3360 m[i].mode == m[j].mode &&
3361 m[i].reqid == m[j].reqid &&
3362 m[i].old_family == m[j].old_family)
3363 return -EINVAL;
3364 }
3365 }
3366
3367 return 0;
3368}
3369
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003370int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003371 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003372 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003373{
3374 int i, err, nx_cur = 0, nx_new = 0;
3375 struct xfrm_policy *pol = NULL;
3376 struct xfrm_state *x, *xc;
3377 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3378 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3379 struct xfrm_migrate *mp;
3380
Vladis Dronov12a70cc2017-08-02 19:50:14 +02003381 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003382 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3383 goto out;
3384
Vladis Dronov12a70cc2017-08-02 19:50:14 +02003385 if (dir >= XFRM_POLICY_MAX) {
3386 err = -EINVAL;
3387 goto out;
3388 }
3389
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003390 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003391 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003392 err = -ENOENT;
3393 goto out;
3394 }
3395
3396 /* Stage 2 - find and update state(s) */
3397 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003398 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003399 x_cur[nx_cur] = x;
3400 nx_cur++;
3401 if ((xc = xfrm_state_migrate(x, mp))) {
3402 x_new[nx_new] = xc;
3403 nx_new++;
3404 } else {
3405 err = -ENODATA;
3406 goto restore_state;
3407 }
3408 }
3409 }
3410
3411 /* Stage 3 - update policy */
3412 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3413 goto restore_state;
3414
3415 /* Stage 4 - delete old state(s) */
3416 if (nx_cur) {
3417 xfrm_states_put(x_cur, nx_cur);
3418 xfrm_states_delete(x_cur, nx_cur);
3419 }
3420
3421 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003422 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003423
3424 xfrm_pol_put(pol);
3425
3426 return 0;
3427out:
3428 return err;
3429
3430restore_state:
3431 if (pol)
3432 xfrm_pol_put(pol);
3433 if (nx_cur)
3434 xfrm_states_put(x_cur, nx_cur);
3435 if (nx_new)
3436 xfrm_states_delete(x_new, nx_new);
3437
3438 return err;
3439}
David S. Millere610e672007-02-08 13:29:15 -08003440EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003441#endif