blob: dda27fd7b8a4303d06ae4bb7707cce77dbac2582 [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>
Florian Westphalec30d782017-07-17 13:57:27 +020027#include <linux/cpu.h>
Paul Moore68277ac2007-12-20 20:49:33 -080028#include <linux/audit.h>
Florian Westphal24969fa2018-11-07 23:00:35 +010029#include <linux/rhashtable.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080030#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050031#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/xfrm.h>
33#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080034#ifdef CONFIG_XFRM_STATISTICS
35#include <net/snmp.h>
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David S. Miller44e36b42006-08-24 04:50:50 -070038#include "xfrm_hash.h"
39
Steffen Klasserta0073fe2013-02-05 12:52:55 +010040#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
41#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
42#define XFRM_MAX_QUEUE_LEN 100
43
Steffen Klassertb8c203b2014-09-16 10:08:49 +020044struct xfrm_flo {
45 struct dst_entry *dst_orig;
46 u8 flags;
47};
48
Florian Westphal24969fa2018-11-07 23:00:35 +010049struct xfrm_pol_inexact_key {
50 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +010051 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +010052 u16 family;
53 u8 dir, type;
54};
55
56struct xfrm_pol_inexact_bin {
57 struct xfrm_pol_inexact_key k;
58 struct rhash_head head;
59 struct hlist_head hhead;
60
61 /* slow path below */
62 struct list_head inexact_bins;
63 struct rcu_head rcu;
64};
65
Steffen Klassertf203b762018-06-12 14:07:12 +020066static DEFINE_SPINLOCK(xfrm_if_cb_lock);
67static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
68
Priyanka Jain418a99a2012-08-12 21:22:29 +000069static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +010070static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +000071 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +030073static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +020074static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Florian Westphal24969fa2018-11-07 23:00:35 +010076static struct rhashtable xfrm_policy_inexact_table;
77static const struct rhashtable_params xfrm_pol_inexact_params;
78
David Miller54920932017-11-28 15:41:01 -050079static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +000080static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000081static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -070082static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Herbert Xu12bfa8b2014-11-13 17:09:50 +080084static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080085static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
86 int dir);
87
Florian Westphal24969fa2018-11-07 23:00:35 +010088static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +010089xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
90 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +010091
92static struct xfrm_pol_inexact_bin *
93xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +010094 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +010095static struct xfrm_policy *
96xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
97 bool excl);
98static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
99 struct xfrm_policy *policy);
100
Florian Westphale37cc8a2016-08-11 15:17:55 +0200101static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
102{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300103 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8a2016-08-11 15:17:55 +0200104}
105
David S. Millerbc9b35a2012-05-15 15:04:57 -0400106static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500107__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800108{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500109 const struct flowi4 *fl4 = &fl->u.ip4;
110
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000111 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
112 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500113 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
114 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
115 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
116 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800117}
118
David S. Millerbc9b35a2012-05-15 15:04:57 -0400119static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500120__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800121{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500122 const struct flowi6 *fl6 = &fl->u.ip6;
123
124 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
125 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
126 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
127 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
128 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
129 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800130}
131
David S. Millerbc9b35a2012-05-15 15:04:57 -0400132bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
133 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800134{
135 switch (family) {
136 case AF_INET:
137 return __xfrm4_selector_match(sel, fl);
138 case AF_INET6:
139 return __xfrm6_selector_match(sel, fl);
140 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400141 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800142}
143
Florian Westphala2817d82017-02-07 15:00:17 +0100144static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200145{
Florian Westphala2817d82017-02-07 15:00:17 +0100146 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200147
Florian Westphala2817d82017-02-07 15:00:17 +0100148 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200149 return NULL;
150 rcu_read_lock();
151 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
152 if (unlikely(!afinfo))
153 rcu_read_unlock();
154 return afinfo;
155}
156
Steffen Klassertf203b762018-06-12 14:07:12 +0200157/* Called with rcu_read_lock(). */
158static const struct xfrm_if_cb *xfrm_if_get_cb(void)
159{
160 return rcu_dereference(xfrm_if_cb);
161}
162
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200163struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
164 const xfrm_address_t *saddr,
165 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900166 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Florian Westphal37b10382017-02-07 15:00:19 +0100168 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800169 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Herbert Xu25ee3282007-12-11 09:32:34 -0800171 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800173 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900175 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900176
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100177 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900178
179 return dst;
180}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200181EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900182
David Ahern42a7b322015-08-10 16:58:11 -0600183static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
184 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900185 xfrm_address_t *prev_saddr,
186 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900187 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900188{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800189 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900190 xfrm_address_t *saddr = &x->props.saddr;
191 xfrm_address_t *daddr = &x->id.daddr;
192 struct dst_entry *dst;
193
194 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
195 saddr = x->coaddr;
196 daddr = prev_daddr;
197 }
198 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
199 saddr = prev_saddr;
200 daddr = x->coaddr;
201 }
202
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900203 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900204
205 if (!IS_ERR(dst)) {
206 if (prev_saddr != saddr)
207 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
208 if (prev_daddr != daddr)
209 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
210 }
211
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800212 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static inline unsigned long make_jiffies(long secs)
216{
217 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
218 return MAX_SCHEDULE_TIMEOUT-1;
219 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900220 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Kees Cookc3aed702017-10-16 17:28:56 -0700223static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Kees Cookc3aed702017-10-16 17:28:56 -0700225 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200226 time64_t now = ktime_get_real_seconds();
227 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int warn = 0;
229 int dir;
230
231 read_lock(&xp->lock);
232
Timo Teräsea2dea92010-03-31 00:17:05 +0000233 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out;
235
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700236 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200239 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 xp->curlft.add_time - now;
241 if (tmo <= 0)
242 goto expired;
243 if (tmo < next)
244 next = tmo;
245 }
246 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200247 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
249 if (tmo <= 0)
250 goto expired;
251 if (tmo < next)
252 next = tmo;
253 }
254 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200255 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 xp->curlft.add_time - now;
257 if (tmo <= 0) {
258 warn = 1;
259 tmo = XFRM_KM_TIMEOUT;
260 }
261 if (tmo < next)
262 next = tmo;
263 }
264 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200265 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
267 if (tmo <= 0) {
268 warn = 1;
269 tmo = XFRM_KM_TIMEOUT;
270 }
271 if (tmo < next)
272 next = tmo;
273 }
274
275 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800276 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200277 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
279 xfrm_pol_hold(xp);
280
281out:
282 read_unlock(&xp->lock);
283 xfrm_pol_put(xp);
284 return;
285
286expired:
287 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700288 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800289 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 xfrm_pol_put(xp);
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* 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);
Florian Westphal24969fa2018-11-07 23:00:35 +0100306 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700307 INIT_HLIST_NODE(&policy->bydst);
308 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300310 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100311 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700312 timer_setup(&policy->timer, xfrm_policy_timer, 0);
313 timer_setup(&policy->polq.hold_timer,
314 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316 return policy;
317}
318EXPORT_SYMBOL(xfrm_policy_alloc);
319
Eric Dumazet56f04732015-12-08 07:22:01 -0800320static void xfrm_policy_destroy_rcu(struct rcu_head *head)
321{
322 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
323
324 security_xfrm_policy_free(policy->security);
325 kfree(policy);
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/* Destroy xfrm_policy: descendant resources must be released to this moment. */
329
WANG Cong64c31b32008-01-07 22:34:29 -0800330void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Herbert Xu12a169e2008-10-01 07:03:24 -0700332 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Fan Du0659eea2013-08-01 18:08:36 +0800334 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 BUG();
336
Eric Dumazet56f04732015-12-08 07:22:01 -0800337 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
WANG Cong64c31b32008-01-07 22:34:29 -0800339EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100341/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * entry dead. The rule must be unlinked from lists to the moment.
343 */
344
345static void xfrm_policy_kill(struct xfrm_policy *policy)
346{
Herbert Xu12a169e2008-10-01 07:03:24 -0700347 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Timo Teräs285ead12010-04-07 00:30:06 +0000349 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200351 if (del_timer(&policy->polq.hold_timer))
352 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800353 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100354
Timo Teräs285ead12010-04-07 00:30:06 +0000355 if (del_timer(&policy->timer))
356 xfrm_pol_put(policy);
357
358 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
David S. Miller2518c7c2006-08-24 04:45:07 -0700361static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
362
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800363static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700364{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800365 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700366}
367
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200368/* calculate policy hash thresholds */
369static void __get_hash_thresh(struct net *net,
370 unsigned short family, int dir,
371 u8 *dbits, u8 *sbits)
372{
373 switch (family) {
374 case AF_INET:
375 *dbits = net->xfrm.policy_bydst[dir].dbits4;
376 *sbits = net->xfrm.policy_bydst[dir].sbits4;
377 break;
378
379 case AF_INET6:
380 *dbits = net->xfrm.policy_bydst[dir].dbits6;
381 *sbits = net->xfrm.policy_bydst[dir].sbits6;
382 break;
383
384 default:
385 *dbits = 0;
386 *sbits = 0;
387 }
388}
389
David S. Miller5f803b52011-02-24 00:33:19 -0500390static struct hlist_head *policy_hash_bysel(struct net *net,
391 const struct xfrm_selector *sel,
392 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700393{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800394 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200395 unsigned int hash;
396 u8 dbits;
397 u8 sbits;
398
399 __get_hash_thresh(net, family, dir, &dbits, &sbits);
400 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700401
Florian Westphale1e551b2016-08-11 15:17:53 +0200402 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100403 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200404
405 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
406 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407}
408
David S. Miller5f803b52011-02-24 00:33:19 -0500409static struct hlist_head *policy_hash_direct(struct net *net,
410 const xfrm_address_t *daddr,
411 const xfrm_address_t *saddr,
412 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700413{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800414 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200415 unsigned int hash;
416 u8 dbits;
417 u8 sbits;
418
419 __get_hash_thresh(net, family, dir, &dbits, &sbits);
420 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700421
Florian Westphale1e551b2016-08-11 15:17:53 +0200422 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
423 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700424}
425
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200426static void xfrm_dst_hash_transfer(struct net *net,
427 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700428 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200429 unsigned int nhashmask,
430 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700431{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800432 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700433 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800434 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200435 u8 dbits;
436 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700437
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800438redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800439 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700440 unsigned int h;
441
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200442 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700443 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200444 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800445 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200446 hlist_del_rcu(&pol->bydst);
447 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800448 h0 = h;
449 } else {
450 if (h != h0)
451 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200452 hlist_del_rcu(&pol->bydst);
453 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800454 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800455 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800456 }
457 if (!hlist_empty(list)) {
458 entry0 = NULL;
459 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 }
461}
462
463static void xfrm_idx_hash_transfer(struct hlist_head *list,
464 struct hlist_head *nidxtable,
465 unsigned int nhashmask)
466{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800467 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700468 struct xfrm_policy *pol;
469
Sasha Levinb67bfe02013-02-27 17:06:00 -0800470 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700471 unsigned int h;
472
473 h = __idx_hash(pol->index, nhashmask);
474 hlist_add_head(&pol->byidx, nidxtable+h);
475 }
476}
477
478static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
479{
480 return ((old_hmask + 1) << 1) - 1;
481}
482
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800483static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700484{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800485 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700486 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
487 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700488 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200489 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700490 int i;
491
492 if (!ndst)
493 return;
494
Florian Westphal9d0380d2016-08-11 15:17:59 +0200495 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200496 write_seqcount_begin(&xfrm_policy_hash_generation);
497
498 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
499 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700500
Florian Westphale1e551b2016-08-11 15:17:53 +0200501 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
502 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
503
David S. Miller2518c7c2006-08-24 04:45:07 -0700504 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200505 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700506
Florian Westphale1e551b2016-08-11 15:17:53 +0200507 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800508 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700509
Florian Westphal30846092016-08-11 15:17:54 +0200510 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200511 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
Florian Westphale1e551b2016-08-11 15:17:53 +0200513 synchronize_rcu();
514
David S. Miller44e36b42006-08-24 04:50:50 -0700515 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700516}
517
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800518static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700519{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800520 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700521 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
522 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800523 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700524 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 int i;
526
527 if (!nidx)
528 return;
529
Florian Westphal9d0380d2016-08-11 15:17:59 +0200530 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700531
532 for (i = hmask; i >= 0; i--)
533 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
534
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800535 net->xfrm.policy_byidx = nidx;
536 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700537
Florian Westphal9d0380d2016-08-11 15:17:59 +0200538 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700539
David S. Miller44e36b42006-08-24 04:50:50 -0700540 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700541}
542
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800543static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700544{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800545 unsigned int cnt = net->xfrm.policy_count[dir];
546 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700547
548 if (total)
549 *total += cnt;
550
551 if ((hmask + 1) < xfrm_policy_hashmax &&
552 cnt > hmask)
553 return 1;
554
555 return 0;
556}
557
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800558static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700559{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800560 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700561
562 if ((hmask + 1) < xfrm_policy_hashmax &&
563 total > hmask)
564 return 1;
565
566 return 0;
567}
568
Alexey Dobriyane0710412010-01-23 13:37:10 +0000569void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700570{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000571 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
572 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
573 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
574 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
575 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
576 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
577 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700578 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700579}
580EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700581
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700582static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800583static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700584{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800585 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700586 int dir, total;
587
588 mutex_lock(&hash_resize_mutex);
589
590 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800591 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800592 if (xfrm_bydst_should_resize(net, dir, &total))
593 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700594 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800595 if (xfrm_byidx_should_resize(net, total))
596 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700597
598 mutex_unlock(&hash_resize_mutex);
599}
600
Florian Westphal24969fa2018-11-07 23:00:35 +0100601static void xfrm_hash_reset_inexact_table(struct net *net)
602{
603 struct xfrm_pol_inexact_bin *b;
604
605 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
606
607 list_for_each_entry(b, &net->xfrm.inexact_bins, inexact_bins)
608 INIT_HLIST_HEAD(&b->hhead);
609}
610
611/* Make sure *pol can be inserted into fastbin.
612 * Useful to check that later insert requests will be sucessful
613 * (provided xfrm_policy_lock is held throughout).
614 */
615static struct xfrm_pol_inexact_bin *
616xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
617{
618 struct xfrm_pol_inexact_bin *bin, *prev;
619 struct xfrm_pol_inexact_key k = {
620 .family = pol->family,
621 .type = pol->type,
622 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100623 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100624 };
625 struct net *net = xp_net(pol);
626
627 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
628
629 write_pnet(&k.net, net);
630 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
631 xfrm_pol_inexact_params);
632 if (bin)
633 return bin;
634
635 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
636 if (!bin)
637 return NULL;
638
639 bin->k = k;
640 INIT_HLIST_HEAD(&bin->hhead);
641
642 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
643 &bin->k, &bin->head,
644 xfrm_pol_inexact_params);
645 if (!prev) {
646 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
647 return bin;
648 }
649
650 kfree(bin);
651
652 return IS_ERR(prev) ? NULL : prev;
653}
654
655static void xfrm_policy_inexact_delete_bin(struct net *net,
656 struct xfrm_pol_inexact_bin *b)
657{
658 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
659
660 if (!hlist_empty(&b->hhead))
661 return;
662
663 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
664 xfrm_pol_inexact_params) == 0) {
665 list_del(&b->inexact_bins);
666 kfree_rcu(b, rcu);
667 }
668}
669
670static void __xfrm_policy_inexact_flush(struct net *net)
671{
672 struct xfrm_pol_inexact_bin *bin;
673
674 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
675
676 list_for_each_entry(bin, &net->xfrm.inexact_bins, inexact_bins)
677 xfrm_policy_inexact_delete_bin(net, bin);
678}
679
680static struct xfrm_policy *
681xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
682{
683 struct xfrm_pol_inexact_bin *bin;
684 struct xfrm_policy *delpol;
685 struct hlist_head *chain;
686 struct net *net;
687
688 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
689 if (!bin)
690 return ERR_PTR(-ENOMEM);
691
692 delpol = xfrm_policy_insert_list(&bin->hhead, policy, excl);
693 if (delpol && excl)
694 return ERR_PTR(-EEXIST);
695
696 net = xp_net(policy);
697 chain = &net->xfrm.policy_inexact[dir];
698 xfrm_policy_insert_inexact_list(chain, policy);
699
700 return delpol;
701}
702
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200703static void xfrm_hash_rebuild(struct work_struct *work)
704{
705 struct net *net = container_of(work, struct net,
706 xfrm.policy_hthresh.work);
707 unsigned int hmask;
708 struct xfrm_policy *pol;
709 struct xfrm_policy *policy;
710 struct hlist_head *chain;
711 struct hlist_head *odst;
712 struct hlist_node *newpos;
713 int i;
714 int dir;
715 unsigned seq;
716 u8 lbits4, rbits4, lbits6, rbits6;
717
718 mutex_lock(&hash_resize_mutex);
719
720 /* read selector prefixlen thresholds */
721 do {
722 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
723
724 lbits4 = net->xfrm.policy_hthresh.lbits4;
725 rbits4 = net->xfrm.policy_hthresh.rbits4;
726 lbits6 = net->xfrm.policy_hthresh.lbits6;
727 rbits6 = net->xfrm.policy_hthresh.rbits6;
728 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
729
Florian Westphal9d0380d2016-08-11 15:17:59 +0200730 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200731
Florian Westphal24969fa2018-11-07 23:00:35 +0100732 /* make sure that we can insert the indirect policies again before
733 * we start with destructive action.
734 */
735 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
736 u8 dbits, sbits;
737
738 dir = xfrm_policy_id2dir(policy->index);
739 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
740 continue;
741
742 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
743 if (policy->family == AF_INET) {
744 dbits = rbits4;
745 sbits = lbits4;
746 } else {
747 dbits = rbits6;
748 sbits = lbits6;
749 }
750 } else {
751 if (policy->family == AF_INET) {
752 dbits = lbits4;
753 sbits = rbits4;
754 } else {
755 dbits = lbits6;
756 sbits = rbits6;
757 }
758 }
759
760 if (policy->selector.prefixlen_d < dbits ||
761 policy->selector.prefixlen_s < sbits)
762 continue;
763
764 if (!xfrm_policy_inexact_alloc_bin(policy, dir))
765 goto out_unlock;
766 }
767
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200768 /* reset the bydst and inexact table in all directions */
Florian Westphal24969fa2018-11-07 23:00:35 +0100769 xfrm_hash_reset_inexact_table(net);
770
Herbert Xu53c2e282014-11-13 17:09:49 +0800771 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200772 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
773 hmask = net->xfrm.policy_bydst[dir].hmask;
774 odst = net->xfrm.policy_bydst[dir].table;
775 for (i = hmask; i >= 0; i--)
776 INIT_HLIST_HEAD(odst + i);
777 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
778 /* dir out => dst = remote, src = local */
779 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
780 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
781 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
782 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
783 } else {
784 /* dir in/fwd => dst = local, src = remote */
785 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
786 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
787 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
788 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
789 }
790 }
791
792 /* re-insert all policies by order of creation */
793 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal862591b2017-12-27 23:25:45 +0100794 if (policy->walk.dead ||
795 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200796 /* skip socket policies */
797 continue;
798 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200799 newpos = NULL;
800 chain = policy_hash_bysel(net, &policy->selector,
801 policy->family,
802 xfrm_policy_id2dir(policy->index));
Florian Westphal24969fa2018-11-07 23:00:35 +0100803 if (!chain) {
804 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
805
806 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
807 continue;
808 }
809
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200810 hlist_for_each_entry(pol, chain, bydst) {
811 if (policy->priority >= pol->priority)
812 newpos = &pol->bydst;
813 else
814 break;
815 }
816 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +0200817 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200818 else
Florian Westphal9dffff22018-10-10 18:02:21 +0200819 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200820 }
821
Florian Westphal24969fa2018-11-07 23:00:35 +0100822out_unlock:
Florian Westphal9d0380d2016-08-11 15:17:59 +0200823 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200824
825 mutex_unlock(&hash_resize_mutex);
826}
827
828void xfrm_policy_hash_rebuild(struct net *net)
829{
830 schedule_work(&net->xfrm.policy_hthresh.work);
831}
832EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/* Generate new index... KAME seems to generate them ordered by cost
835 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800836static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 static u32 idx_generator;
839
840 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 struct hlist_head *list;
842 struct xfrm_policy *p;
843 u32 idx;
844 int found;
845
Fan Due682adf02013-11-07 17:47:48 +0800846 if (!index) {
847 idx = (idx_generator | dir);
848 idx_generator += 8;
849 } else {
850 idx = index;
851 index = 0;
852 }
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (idx == 0)
855 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800856 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700857 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800858 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700859 if (p->index == idx) {
860 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return idx;
866 }
867}
868
David S. Miller2518c7c2006-08-24 04:45:07 -0700869static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
870{
871 u32 *p1 = (u32 *) s1;
872 u32 *p2 = (u32 *) s2;
873 int len = sizeof(struct xfrm_selector) / sizeof(u32);
874 int i;
875
876 for (i = 0; i < len; i++) {
877 if (p1[i] != p2[i])
878 return 1;
879 }
880
881 return 0;
882}
883
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100884static void xfrm_policy_requeue(struct xfrm_policy *old,
885 struct xfrm_policy *new)
886{
887 struct xfrm_policy_queue *pq = &old->polq;
888 struct sk_buff_head list;
889
Li RongQingde2ad482015-04-30 17:25:19 +0800890 if (skb_queue_empty(&pq->hold_queue))
891 return;
892
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100893 __skb_queue_head_init(&list);
894
895 spin_lock_bh(&pq->hold_queue.lock);
896 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200897 if (del_timer(&pq->hold_timer))
898 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100899 spin_unlock_bh(&pq->hold_queue.lock);
900
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100901 pq = &new->polq;
902
903 spin_lock_bh(&pq->hold_queue.lock);
904 skb_queue_splice(&list, &pq->hold_queue);
905 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200906 if (!mod_timer(&pq->hold_timer, jiffies))
907 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100908 spin_unlock_bh(&pq->hold_queue.lock);
909}
910
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100911static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
912 struct xfrm_policy *pol)
913{
914 u32 mark = policy->mark.v & policy->mark.m;
915
916 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
917 return true;
918
919 if ((mark & pol->mark.m) == pol->mark.v &&
920 policy->priority == pol->priority)
921 return true;
922
923 return false;
924}
925
Florian Westphal24969fa2018-11-07 23:00:35 +0100926static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
927{
928 const struct xfrm_pol_inexact_key *k = data;
929 u32 a = k->type << 24 | k->dir << 16 | k->family;
930
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100931 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
932 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +0100933}
934
935static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
936{
937 const struct xfrm_pol_inexact_bin *b = data;
938
939 return xfrm_pol_bin_key(&b->k, 0, seed);
940}
941
942static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
943 const void *ptr)
944{
945 const struct xfrm_pol_inexact_key *key = arg->key;
946 const struct xfrm_pol_inexact_bin *b = ptr;
947 int ret;
948
949 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
950 return -1;
951
952 ret = b->k.dir ^ key->dir;
953 if (ret)
954 return ret;
955
956 ret = b->k.type ^ key->type;
957 if (ret)
958 return ret;
959
960 ret = b->k.family ^ key->family;
961 if (ret)
962 return ret;
963
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100964 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +0100965}
966
967static const struct rhashtable_params xfrm_pol_inexact_params = {
968 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
969 .hashfn = xfrm_pol_bin_key,
970 .obj_hashfn = xfrm_pol_bin_obj,
971 .obj_cmpfn = xfrm_pol_bin_cmp,
972 .automatic_shrinking = true,
973};
974
975static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
976 struct xfrm_policy *policy)
977{
978 struct xfrm_policy *pol, *delpol = NULL;
979 struct hlist_node *newpos = NULL;
980
981 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
982 if (pol->type == policy->type &&
983 pol->if_id == policy->if_id &&
984 !selector_cmp(&pol->selector, &policy->selector) &&
985 xfrm_policy_mark_match(policy, pol) &&
986 xfrm_sec_ctx_match(pol->security, policy->security) &&
987 !WARN_ON(delpol)) {
988 delpol = pol;
989 if (policy->priority > pol->priority)
990 continue;
991 } else if (policy->priority >= pol->priority) {
992 newpos = &pol->bydst_inexact_list;
993 continue;
994 }
995 if (delpol)
996 break;
997 }
998
999 if (newpos)
1000 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1001 else
1002 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
1003}
1004
Florian Westphala927d6af2018-11-07 23:00:33 +01001005static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1006 struct xfrm_policy *policy,
1007 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Florian Westphala927d6af2018-11-07 23:00:33 +01001009 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Sasha Levinb67bfe02013-02-27 17:06:00 -08001011 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001012 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001013 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001014 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001015 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001016 xfrm_sec_ctx_match(pol->security, policy->security) &&
1017 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001018 if (excl)
1019 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 delpol = pol;
1021 if (policy->priority > pol->priority)
1022 continue;
1023 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001024 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 continue;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (delpol)
1028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001032 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001033 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001034 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001035
1036 return delpol;
1037}
1038
1039int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1040{
1041 struct net *net = xp_net(policy);
1042 struct xfrm_policy *delpol;
1043 struct hlist_head *chain;
1044
1045 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1046 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001047 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001048 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001049 else
1050 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001051
1052 if (IS_ERR(delpol)) {
1053 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1054 return PTR_ERR(delpol);
1055 }
1056
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001057 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001058
1059 /* After previous checking, family can either be AF_INET or AF_INET6 */
1060 if (policy->family == AF_INET)
1061 rt_genid_bump_ipv4(net);
1062 else
1063 rt_genid_bump_ipv6(net);
1064
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001065 if (delpol) {
1066 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001067 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001068 }
Fan Due682adf02013-11-07 17:47:48 +08001069 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001070 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001071 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 policy->curlft.use_time = 0;
1073 if (!mod_timer(&policy->timer, jiffies + HZ))
1074 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001075 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
David S. Miller9b78a822005-12-22 07:39:48 -08001077 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001079 else if (xfrm_bydst_should_resize(net, dir, NULL))
1080 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084EXPORT_SYMBOL(xfrm_policy_insert);
1085
Steffen Klassert7e652642018-06-12 14:07:07 +02001086struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1087 u8 type, int dir,
1088 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001089 struct xfrm_sec_ctx *ctx, int delete,
1090 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Florian Westphal24969fa2018-11-07 23:00:35 +01001092 struct xfrm_pol_inexact_bin *bin = NULL;
1093 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001094 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Eric Parisef41aaa2007-03-07 15:37:58 -08001096 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001097 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001098 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001099 if (!chain) {
1100 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001101 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001102 if (!bin) {
1103 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1104 return NULL;
1105 }
1106
1107 chain = &bin->hhead;
1108 }
1109
David S. Miller2518c7c2006-08-24 04:45:07 -07001110 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001111 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001112 if (pol->type == type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001113 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001114 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001115 !selector_cmp(sel, &pol->selector) &&
1116 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001118 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001119 *err = security_xfrm_policy_delete(
1120 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001121 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001122 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001123 return pol;
1124 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001125 __xfrm_policy_unlink(pol, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001126 xfrm_policy_inexact_delete_bin(net, bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001127 }
1128 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 }
1131 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001132 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001134 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001135 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
Trent Jaegerdf718372005-12-13 23:12:27 -08001138EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Steffen Klassert7e652642018-06-12 14:07:07 +02001140struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1141 u8 type, int dir, u32 id, int delete,
1142 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
David S. Miller2518c7c2006-08-24 04:45:07 -07001144 struct xfrm_policy *pol, *ret;
1145 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Herbert Xub5505c62007-05-14 02:15:47 -07001147 *err = -ENOENT;
1148 if (xfrm_policy_id2dir(id) != dir)
1149 return NULL;
1150
Eric Parisef41aaa2007-03-07 15:37:58 -08001151 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001152 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001153 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001154 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001155 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001156 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001157 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001158 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001160 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001161 *err = security_xfrm_policy_delete(
1162 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001163 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001164 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001165 return pol;
1166 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001167 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001168 }
1169 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 }
1172 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001173 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001175 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001176 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179EXPORT_SYMBOL(xfrm_policy_byid);
1180
Joy Latten4aa2e622007-06-04 19:05:57 -04001181#ifdef CONFIG_SECURITY_NETWORK_XFRM
1182static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001183xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Florian Westphalceb159e2018-11-07 23:00:32 +01001185 struct xfrm_policy *pol;
1186 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001187
Florian Westphalceb159e2018-11-07 23:00:32 +01001188 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1189 if (pol->walk.dead ||
1190 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1191 pol->type != type)
1192 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001193
Florian Westphalceb159e2018-11-07 23:00:32 +01001194 err = security_xfrm_policy_delete(pol->security);
1195 if (err) {
1196 xfrm_audit_policy_delete(pol, 0, task_valid);
1197 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001198 }
1199 }
1200 return err;
1201}
1202#else
1203static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001204xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001205{
1206 return 0;
1207}
1208#endif
1209
Tetsuo Handa2e710292014-04-22 21:48:30 +09001210int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001211{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001212 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001213 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Florian Westphal9d0380d2016-08-11 15:17:59 +02001215 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001216
Tetsuo Handa2e710292014-04-22 21:48:30 +09001217 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001218 if (err)
1219 goto out;
1220
Florian Westphalceb159e2018-11-07 23:00:32 +01001221again:
1222 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1223 dir = xfrm_policy_id2dir(pol->index);
1224 if (pol->walk.dead ||
1225 dir >= XFRM_POLICY_MAX ||
1226 pol->type != type)
1227 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001228
Florian Westphalceb159e2018-11-07 23:00:32 +01001229 __xfrm_policy_unlink(pol, dir);
1230 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1231 cnt++;
1232 xfrm_audit_policy_delete(pol, 1, task_valid);
1233 xfrm_policy_kill(pol);
1234 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1235 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001237 if (cnt)
1238 __xfrm_policy_inexact_flush(net);
1239 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001240 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001241out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001242 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245EXPORT_SYMBOL(xfrm_policy_flush);
1246
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001247int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001248 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 void *data)
1250{
Herbert Xu12a169e2008-10-01 07:03:24 -07001251 struct xfrm_policy *pol;
1252 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001253 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Timo Teras4c563f72008-02-28 21:31:08 -08001255 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1256 walk->type != XFRM_POLICY_TYPE_ANY)
1257 return -EINVAL;
1258
Herbert Xu12a169e2008-10-01 07:03:24 -07001259 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001260 return 0;
1261
Florian Westphal9d0380d2016-08-11 15:17:59 +02001262 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001263 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001264 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001265 else
Li RongQing80077702015-04-22 17:09:54 +08001266 x = list_first_entry(&walk->walk.all,
1267 struct xfrm_policy_walk_entry, all);
1268
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001269 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001270 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001271 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001272 pol = container_of(x, struct xfrm_policy, walk);
1273 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1274 walk->type != pol->type)
1275 continue;
1276 error = func(pol, xfrm_policy_id2dir(pol->index),
1277 walk->seq, data);
1278 if (error) {
1279 list_move_tail(&walk->walk.all, &x->all);
1280 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001281 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001282 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001284 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001285 error = -ENOENT;
1286 goto out;
1287 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001288 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001290 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return error;
1292}
1293EXPORT_SYMBOL(xfrm_policy_walk);
1294
Herbert Xu12a169e2008-10-01 07:03:24 -07001295void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1296{
1297 INIT_LIST_HEAD(&walk->walk.all);
1298 walk->walk.dead = 1;
1299 walk->type = type;
1300 walk->seq = 0;
1301}
1302EXPORT_SYMBOL(xfrm_policy_walk_init);
1303
Fan Du283bc9f2013-11-07 17:47:50 +08001304void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001305{
1306 if (list_empty(&walk->walk.all))
1307 return;
1308
Florian Westphal9d0380d2016-08-11 15:17:59 +02001309 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001310 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001311 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001312}
1313EXPORT_SYMBOL(xfrm_policy_walk_done);
1314
James Morris134b0fc2006-10-05 15:42:27 -05001315/*
1316 * Find policy to apply to this flow.
1317 *
1318 * Returns 0 if policy found, else an -errno.
1319 */
David S. Millerf299d552011-02-24 01:23:30 -05001320static int xfrm_policy_match(const struct xfrm_policy *pol,
1321 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001322 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001323{
David S. Millerf299d552011-02-24 01:23:30 -05001324 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001325 int ret = -ESRCH;
1326 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001327
1328 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001329 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001330 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001331 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001332 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001333
1334 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001335 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001336 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001337 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001338 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001339}
1340
Florian Westphal24969fa2018-11-07 23:00:35 +01001341static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001342xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1343 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001344{
1345 struct xfrm_pol_inexact_key k = {
1346 .family = family,
1347 .type = type,
1348 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001349 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001350 };
1351
1352 write_pnet(&k.net, net);
1353
1354 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1355 xfrm_pol_inexact_params);
1356}
1357
1358static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001359xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1360 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001361{
1362 struct xfrm_pol_inexact_bin *bin;
1363
1364 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1365
1366 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001367 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001368 rcu_read_unlock();
1369
1370 return bin;
1371}
1372
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001373static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001374 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001375 u16 family, u8 dir,
1376 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
David S. Miller0b597e72011-02-24 01:22:48 -05001378 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01001379 struct xfrm_pol_inexact_bin *bin;
1380 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001381 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001382 unsigned int sequence;
1383 u32 priority;
Florian Westphal24969fa2018-11-07 23:00:35 +01001384 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001385
1386 daddr = xfrm_flowi_daddr(fl, family);
1387 saddr = xfrm_flowi_saddr(fl, family);
1388 if (unlikely(!daddr || !saddr))
1389 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Florian Westphala7c442472016-08-11 15:17:56 +02001391 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001392 retry:
1393 do {
1394 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1395 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1396 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1397
1398 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001399 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001400 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07001401 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05001402 if (err) {
1403 if (err == -ESRCH)
1404 continue;
1405 else {
1406 ret = ERR_PTR(err);
1407 goto fail;
1408 }
1409 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001410 ret = pol;
1411 priority = ret->priority;
1412 break;
1413 }
1414 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001415 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001416 if (!bin)
1417 goto skip_inexact;
1418 chain = &bin->hhead;
Florian Westphala5eefc12016-08-11 15:17:52 +02001419 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001420 if ((pol->priority >= priority) && ret)
1421 break;
1422
Benedict Wongbc56b332018-07-19 10:50:44 -07001423 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05001424 if (err) {
1425 if (err == -ESRCH)
1426 continue;
1427 else {
1428 ret = ERR_PTR(err);
1429 goto fail;
1430 }
Li RongQing8faf4912015-05-14 11:16:59 +08001431 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001432 ret = pol;
1433 break;
1434 }
1435 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001436
Florian Westphal24969fa2018-11-07 23:00:35 +01001437skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02001438 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1439 goto retry;
1440
Florian Westphale37cc8a2016-08-11 15:17:55 +02001441 if (ret && !xfrm_pol_hold_rcu(ret))
1442 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001443fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001444 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001445
David S. Miller2518c7c2006-08-24 04:45:07 -07001446 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001447}
1448
Benedict Wongbc56b332018-07-19 10:50:44 -07001449static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
1450 const struct flowi *fl,
1451 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00001452{
1453#ifdef CONFIG_XFRM_SUB_POLICY
1454 struct xfrm_policy *pol;
1455
Benedict Wongbc56b332018-07-19 10:50:44 -07001456 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
1457 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00001458 if (pol != NULL)
1459 return pol;
1460#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07001461 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
1462 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00001463}
1464
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001465static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07001466 const struct flowi *fl,
1467 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct xfrm_policy *pol;
1470
Eric Dumazetd188ba82015-12-08 07:22:02 -08001471 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001472 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001473 pol = rcu_dereference(sk->sk_policy[dir]);
1474 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01001475 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001476 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001477
Steffen Klassertddc47e42017-11-29 06:53:55 +01001478 if (pol->family != family) {
1479 pol = NULL;
1480 goto out;
1481 }
1482
1483 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001484 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02001485 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001486 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001487 pol = NULL;
1488 goto out;
1489 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001490 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001491 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02001492 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01001493 if (!err) {
1494 if (!xfrm_pol_hold_rcu(pol))
1495 goto again;
1496 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001497 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001498 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001499 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001500 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001501 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 pol = NULL;
1503 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001504out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001505 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return pol;
1507}
1508
1509static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1510{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001511 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001512
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001513 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001514 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 xfrm_pol_hold(pol);
1516}
1517
1518static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1519 int dir)
1520{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001521 struct net *net = xp_net(pol);
1522
Herbert Xu53c2e282014-11-13 17:09:49 +08001523 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001524 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Herbert Xu53c2e282014-11-13 17:09:49 +08001526 /* Socket policies are not hashed. */
1527 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001528 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01001529 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08001530 hlist_del(&pol->byidx);
1531 }
1532
1533 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001534 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001535
1536 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Herbert Xu53c2e282014-11-13 17:09:49 +08001539static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1540{
1541 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1542}
1543
1544static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1545{
1546 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1547}
1548
Herbert Xu4666faa2005-06-18 22:43:22 -07001549int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Fan Du283bc9f2013-11-07 17:47:50 +08001551 struct net *net = xp_net(pol);
1552
Florian Westphal9d0380d2016-08-11 15:17:59 +02001553 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001555 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001560 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
David S. Millera70fcb02006-03-20 19:18:52 -08001562EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1565{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09001566 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 struct xfrm_policy *old_pol;
1568
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001569#ifdef CONFIG_XFRM_SUB_POLICY
1570 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1571 return -EINVAL;
1572#endif
1573
Florian Westphal9d0380d2016-08-11 15:17:59 +02001574 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001575 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1576 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02001578 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001579 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001580 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001582 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001583 if (old_pol) {
1584 if (pol)
1585 xfrm_policy_requeue(old_pol, pol);
1586
Timo Teräsea2dea92010-03-31 00:17:05 +00001587 /* Unlinking succeeds always. This is the only function
1588 * allowed to delete or replace socket policy.
1589 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001590 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001591 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001592 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (old_pol) {
1595 xfrm_policy_kill(old_pol);
1596 }
1597 return 0;
1598}
1599
David S. Millerd3e40a92011-02-24 01:25:41 -05001600static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001602 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001603 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (newp) {
1606 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001607 if (security_xfrm_policy_clone(old->security,
1608 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001609 kfree(newp);
1610 return NULL; /* ENOMEM */
1611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 newp->lft = old->lft;
1613 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001614 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02001615 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 newp->action = old->action;
1617 newp->flags = old->flags;
1618 newp->xfrm_nr = old->xfrm_nr;
1619 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001620 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11001621 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 memcpy(newp->xfrm_vec, old->xfrm_vec,
1623 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001624 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001625 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001626 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 xfrm_pol_put(newp);
1628 }
1629 return newp;
1630}
1631
Eric Dumazetd188ba82015-12-08 07:22:02 -08001632int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001634 const struct xfrm_policy *p;
1635 struct xfrm_policy *np;
1636 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Eric Dumazetd188ba82015-12-08 07:22:02 -08001638 rcu_read_lock();
1639 for (i = 0; i < 2; i++) {
1640 p = rcu_dereference(osk->sk_policy[i]);
1641 if (p) {
1642 np = clone_policy(p, i);
1643 if (unlikely(!np)) {
1644 ret = -ENOMEM;
1645 break;
1646 }
1647 rcu_assign_pointer(sk->sk_policy[i], np);
1648 }
1649 }
1650 rcu_read_unlock();
1651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001654static int
David Ahern42a7b322015-08-10 16:58:11 -06001655xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09001656 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001657{
1658 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01001659 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001660
1661 if (unlikely(afinfo == NULL))
1662 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09001663 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001664 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001665 return err;
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/* Resolve list of templates for the flow, given policy. */
1669
1670static int
David S. Millera6c2e612011-02-22 18:35:39 -08001671xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1672 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001674 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int nx;
1676 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01001677 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1678 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001679 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Weilong Chen9b7a7872013-12-24 09:43:46 +08001681 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01001683 xfrm_address_t *remote = daddr;
1684 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1686
Steffen Klassert94802152017-11-15 06:40:57 +01001687 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1688 tmpl->mode == XFRM_MODE_BEET) {
1689 remote = &tmpl->id.daddr;
1690 local = &tmpl->saddr;
1691 if (xfrm_addr_any(local, tmpl->encap_family)) {
1692 error = xfrm_get_saddr(net, fl->flowi_oif,
1693 &tmp, remote,
1694 tmpl->encap_family, 0);
1695 if (error)
1696 goto fail;
1697 local = &tmp;
1698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
Benedict Wongbc56b332018-07-19 10:50:44 -07001701 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
1702 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 if (x && x->km.state == XFRM_STATE_VALID) {
1705 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01001706 daddr = remote;
1707 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 continue;
1709 }
1710 if (x) {
1711 error = (x->km.state == XFRM_STATE_ERROR ?
1712 -EINVAL : -EAGAIN);
1713 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001714 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001715 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (!tmpl->optional)
1719 goto fail;
1720 }
1721 return nx;
1722
1723fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001724 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 xfrm_state_put(xfrm[nx]);
1726 return error;
1727}
1728
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001729static int
David S. Millera6c2e612011-02-22 18:35:39 -08001730xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1731 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001732{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001733 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1734 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001735 int cnx = 0;
1736 int error;
1737 int ret;
1738 int i;
1739
1740 for (i = 0; i < npols; i++) {
1741 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1742 error = -ENOBUFS;
1743 goto fail;
1744 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001745
1746 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001747 if (ret < 0) {
1748 error = ret;
1749 goto fail;
1750 } else
1751 cnx += ret;
1752 }
1753
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001754 /* found states are sorted for outbound processing */
1755 if (npols > 1)
1756 xfrm_state_sort(xfrm, tpp, cnx, family);
1757
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001758 return cnx;
1759
1760 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001761 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001762 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001763 return error;
1764
1765}
1766
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001767static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001768{
Florian Westphal37b10382017-02-07 15:00:19 +01001769 const struct xfrm_policy_afinfo *afinfo;
Xin Long143a4452018-02-17 15:16:22 +08001770 int tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08001771
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001772 afinfo = xfrm_policy_get_afinfo(family);
Xin Long143a4452018-02-17 15:16:22 +08001773 if (!afinfo)
1774 return 0;
1775
1776 tos = afinfo->get_tos(fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001777
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001778 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001779
1780 return tos;
1781}
1782
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001783static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001784{
Florian Westphal37b10382017-02-07 15:00:19 +01001785 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001786 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001787 struct xfrm_dst *xdst;
1788
1789 if (!afinfo)
1790 return ERR_PTR(-EINVAL);
1791
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001792 switch (family) {
1793 case AF_INET:
1794 dst_ops = &net->xfrm.xfrm4_dst_ops;
1795 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001796#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001797 case AF_INET6:
1798 dst_ops = &net->xfrm.xfrm6_dst_ops;
1799 break;
1800#endif
1801 default:
1802 BUG();
1803 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07001804 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001805
Madalin Bucurd4cae562011-09-26 07:04:36 +00001806 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001807 struct dst_entry *dst = &xdst->u.dst;
1808
1809 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00001810 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001811 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001812
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001813 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00001814
Herbert Xu25ee3282007-12-11 09:32:34 -08001815 return xdst;
1816}
1817
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001818static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1819 int nfheader_len)
1820{
Florian Westphal37b10382017-02-07 15:00:19 +01001821 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001822 xfrm_policy_get_afinfo(dst->ops->family);
1823 int err;
1824
1825 if (!afinfo)
1826 return -EINVAL;
1827
1828 err = afinfo->init_path(path, dst, nfheader_len);
1829
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001830 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001831
1832 return err;
1833}
1834
Herbert Xu87c1e122010-03-02 02:51:56 +00001835static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001836 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001837{
Florian Westphal37b10382017-02-07 15:00:19 +01001838 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08001839 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1840 int err;
1841
1842 if (!afinfo)
1843 return -EINVAL;
1844
Herbert Xu87c1e122010-03-02 02:51:56 +00001845 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001846
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001847 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001848
1849 return err;
1850}
1851
Timo Teräs80c802f2010-04-07 00:30:05 +00001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1854 * all the metrics... Shortly, bundle a bundle.
1855 */
1856
Herbert Xu25ee3282007-12-11 09:32:34 -08001857static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05001858 struct xfrm_state **xfrm,
1859 struct xfrm_dst **bundle,
1860 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001861 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001862 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001864 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001865 unsigned long now = jiffies;
1866 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001867 struct xfrm_mode *inner_mode;
David Miller45b018be2017-11-28 15:40:28 -05001868 struct xfrm_dst *xdst_prev = NULL;
1869 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08001870 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001872 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001873 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001874 int trailer_len = 0;
1875 int tos;
1876 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001877 xfrm_address_t saddr, daddr;
1878
1879 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001880
1881 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001882
1883 dst_hold(dst);
1884
1885 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001886 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001887 struct dst_entry *dst1 = &xdst->u.dst;
1888
1889 err = PTR_ERR(xdst);
1890 if (IS_ERR(xdst)) {
1891 dst_release(dst);
1892 goto put_states;
1893 }
1894
David Miller54920932017-11-28 15:41:01 -05001895 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05001896 if (!xdst_prev)
1897 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07001898 else
1899 /* Ref count is taken during xfrm_alloc_dst()
1900 * No need to do dst_clone() on dst1
1901 */
David Miller45b018be2017-11-28 15:40:28 -05001902 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07001903
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001904 if (xfrm[i]->sel.family == AF_UNSPEC) {
1905 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1906 xfrm_af2proto(family));
1907 if (!inner_mode) {
1908 err = -EAFNOSUPPORT;
1909 dst_release(dst);
1910 goto put_states;
1911 }
1912 } else
1913 inner_mode = xfrm[i]->inner_mode;
1914
Herbert Xu25ee3282007-12-11 09:32:34 -08001915 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001916 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001917
1918 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02001919 __u32 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
1920
Herbert Xu25ee3282007-12-11 09:32:34 -08001921 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001922 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02001923 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08001924 err = PTR_ERR(dst);
1925 if (IS_ERR(dst))
1926 goto put_states;
1927 } else
1928 dst_hold(dst);
1929
1930 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001931 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001932
David S. Millerf5b0a872012-07-19 12:31:33 -07001933 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001934 dst1->flags |= DST_HOST;
1935 dst1->lastuse = now;
1936
1937 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001938 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001939
David Miller45b018be2017-11-28 15:40:28 -05001940 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08001941
1942 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001943 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1944 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001945 trailer_len += xfrm[i]->props.trailer_len;
1946 }
1947
David Miller45b018be2017-11-28 15:40:28 -05001948 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05001949 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08001950
1951 err = -ENODEV;
1952 dev = dst->dev;
1953 if (!dev)
1954 goto free_dst;
1955
David Miller45b018be2017-11-28 15:40:28 -05001956 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05001957 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08001958
David Miller45b018be2017-11-28 15:40:28 -05001959 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
1960 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
1961 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001962 if (err)
1963 goto free_dst;
1964
David Miller45b018be2017-11-28 15:40:28 -05001965 xdst_prev->u.dst.header_len = header_len;
1966 xdst_prev->u.dst.trailer_len = trailer_len;
1967 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
1968 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001969 }
1970
David Miller45b018be2017-11-28 15:40:28 -05001971 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08001972
1973put_states:
1974 for (; i < nx; i++)
1975 xfrm_state_put(xfrm[i]);
1976free_dst:
David Miller45b018be2017-11-28 15:40:28 -05001977 if (xdst0)
1978 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02001979
1980 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
David S. Miller73ff93c2011-02-22 18:33:42 -08001983static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001984 struct xfrm_policy **pols,
1985 int *num_pols, int *num_xfrms)
1986{
1987 int i;
1988
1989 if (*num_pols == 0 || !pols[0]) {
1990 *num_pols = 0;
1991 *num_xfrms = 0;
1992 return 0;
1993 }
1994 if (IS_ERR(pols[0]))
1995 return PTR_ERR(pols[0]);
1996
1997 *num_xfrms = pols[0]->xfrm_nr;
1998
1999#ifdef CONFIG_XFRM_SUB_POLICY
2000 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2001 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2002 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2003 XFRM_POLICY_TYPE_MAIN,
2004 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002005 XFRM_POLICY_OUT,
2006 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002007 if (pols[1]) {
2008 if (IS_ERR(pols[1])) {
2009 xfrm_pols_put(pols, *num_pols);
2010 return PTR_ERR(pols[1]);
2011 }
Weilong Chen02d08922013-12-24 09:43:48 +08002012 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002013 (*num_xfrms) += pols[1]->xfrm_nr;
2014 }
2015 }
2016#endif
2017 for (i = 0; i < *num_pols; i++) {
2018 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2019 *num_xfrms = -1;
2020 break;
2021 }
2022 }
2023
2024 return 0;
2025
2026}
2027
2028static struct xfrm_dst *
2029xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002030 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002031 struct dst_entry *dst_orig)
2032{
2033 struct net *net = xp_net(pols[0]);
2034 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002035 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002036 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002037 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002038 int err;
2039
2040 /* Try to instantiate a bundle */
2041 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002042 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002043 if (err == 0)
2044 return NULL;
2045
2046 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002047 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2048 return ERR_PTR(err);
2049 }
2050
David Miller54920932017-11-28 15:41:01 -05002051 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002052 if (IS_ERR(dst)) {
2053 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2054 return ERR_CAST(dst);
2055 }
2056
2057 xdst = (struct xfrm_dst *)dst;
2058 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002059 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002060 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002061 xdst->policy_genid = atomic_read(&pols[0]->genid);
2062
2063 return xdst;
2064}
2065
Kees Cookc3aed702017-10-16 17:28:56 -07002066static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002067{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002068 struct sk_buff *skb;
2069 struct sock *sk;
2070 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002071 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002072 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002073 struct xfrm_policy_queue *pq = &pol->polq;
2074 struct flowi fl;
2075 struct sk_buff_head list;
2076
2077 spin_lock(&pq->hold_queue.lock);
2078 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002079 if (!skb) {
2080 spin_unlock(&pq->hold_queue.lock);
2081 goto out;
2082 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002083 dst = skb_dst(skb);
2084 sk = skb->sk;
2085 xfrm_decode_session(skb, &fl, dst->ops->family);
2086 spin_unlock(&pq->hold_queue.lock);
2087
David Miller0f6c4802017-11-28 15:40:46 -05002088 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c982018-02-01 11:26:12 +01002089 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002090 if (IS_ERR(dst))
2091 goto purge_queue;
2092
2093 if (dst->flags & DST_XFRM_QUEUE) {
2094 dst_release(dst);
2095
2096 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2097 goto purge_queue;
2098
2099 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002100 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2101 xfrm_pol_hold(pol);
2102 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002103 }
2104
2105 dst_release(dst);
2106
2107 __skb_queue_head_init(&list);
2108
2109 spin_lock(&pq->hold_queue.lock);
2110 pq->timeout = 0;
2111 skb_queue_splice_init(&pq->hold_queue, &list);
2112 spin_unlock(&pq->hold_queue.lock);
2113
2114 while (!skb_queue_empty(&list)) {
2115 skb = __skb_dequeue(&list);
2116
2117 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002118 dst_hold(xfrm_dst_path(skb_dst(skb)));
2119 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002120 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002121 kfree_skb(skb);
2122 continue;
2123 }
2124
2125 nf_reset(skb);
2126 skb_dst_drop(skb);
2127 skb_dst_set(skb, dst);
2128
Eric W. Biederman13206b62015-10-07 16:48:35 -05002129 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002130 }
2131
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002132out:
2133 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002134 return;
2135
2136purge_queue:
2137 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002138 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002139 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002140}
2141
Eric W. Biedermanede20592015-10-07 16:48:47 -05002142static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002143{
2144 unsigned long sched_next;
2145 struct dst_entry *dst = skb_dst(skb);
2146 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002147 struct xfrm_policy *pol = xdst->pols[0];
2148 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002149
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002150 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002151 kfree_skb(skb);
2152 return 0;
2153 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002154
2155 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2156 kfree_skb(skb);
2157 return -EAGAIN;
2158 }
2159
2160 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002161
2162 spin_lock_bh(&pq->hold_queue.lock);
2163
2164 if (!pq->timeout)
2165 pq->timeout = XFRM_QUEUE_TMO_MIN;
2166
2167 sched_next = jiffies + pq->timeout;
2168
2169 if (del_timer(&pq->hold_timer)) {
2170 if (time_before(pq->hold_timer.expires, sched_next))
2171 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002172 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002173 }
2174
2175 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002176 if (!mod_timer(&pq->hold_timer, sched_next))
2177 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002178
2179 spin_unlock_bh(&pq->hold_queue.lock);
2180
2181 return 0;
2182}
2183
2184static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002185 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002186 const struct flowi *fl,
2187 int num_xfrms,
2188 u16 family)
2189{
2190 int err;
2191 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002192 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002193 struct dst_entry *dst1;
2194 struct xfrm_dst *xdst;
2195
2196 xdst = xfrm_alloc_dst(net, family);
2197 if (IS_ERR(xdst))
2198 return xdst;
2199
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002200 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2201 net->xfrm.sysctl_larval_drop ||
2202 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002203 return xdst;
2204
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002205 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002206 dst1 = &xdst->u.dst;
2207 dst_hold(dst);
2208 xdst->route = dst;
2209
2210 dst_copy_metrics(dst1, dst);
2211
2212 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2213 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2214 dst1->lastuse = jiffies;
2215
2216 dst1->input = dst_discard;
2217 dst1->output = xdst_queue_output;
2218
2219 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002220 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002221 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002222
2223 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2224
2225 err = -ENODEV;
2226 dev = dst->dev;
2227 if (!dev)
2228 goto free_dst;
2229
2230 err = xfrm_fill_dst(xdst, dev, fl);
2231 if (err)
2232 goto free_dst;
2233
2234out:
2235 return xdst;
2236
2237free_dst:
2238 dst_release(dst1);
2239 xdst = ERR_PTR(err);
2240 goto out;
2241}
2242
Benedict Wongbc56b332018-07-19 10:50:44 -07002243static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2244 const struct flowi *fl,
2245 u16 family, u8 dir,
2246 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002247{
Timo Teräs80c802f2010-04-07 00:30:05 +00002248 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002249 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002250 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002251
Timo Teräs80c802f2010-04-07 00:30:05 +00002252 /* Resolve policies to use if we couldn't get them from
2253 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002254 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002255 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002256 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002257 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002258 if (err < 0)
2259 goto inc_error;
2260 if (num_pols == 0)
2261 return NULL;
2262 if (num_xfrms <= 0)
2263 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002264
Florian Westphalbd45c532017-07-17 13:57:25 +02002265 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002266 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002267 if (IS_ERR(xdst)) {
2268 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002269 if (err == -EREMOTE) {
2270 xfrm_pols_put(pols, num_pols);
2271 return NULL;
2272 }
2273
Timo Teräs80c802f2010-04-07 00:30:05 +00002274 if (err != -EAGAIN)
2275 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002276 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002277 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002278 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002279 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002280 }
2281
Florian Westphalbd45c532017-07-17 13:57:25 +02002282 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002283
2284make_dummy_bundle:
2285 /* We found policies, but there's no bundles to instantiate:
2286 * either because the policy blocks, has no transformations or
2287 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002288 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002289 if (IS_ERR(xdst)) {
2290 xfrm_pols_put(pols, num_pols);
2291 return ERR_CAST(xdst);
2292 }
2293 xdst->num_pols = num_pols;
2294 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002295 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002296
Florian Westphalbd45c532017-07-17 13:57:25 +02002297 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002298
2299inc_error:
2300 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2301error:
Florian Westphal855dad92017-07-17 13:57:22 +02002302 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002303 return ERR_PTR(err);
2304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
David S. Miller2774c132011-03-01 14:59:04 -08002306static struct dst_entry *make_blackhole(struct net *net, u16 family,
2307 struct dst_entry *dst_orig)
2308{
Florian Westphal37b10382017-02-07 15:00:19 +01002309 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002310 struct dst_entry *ret;
2311
2312 if (!afinfo) {
2313 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002314 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002315 } else {
2316 ret = afinfo->blackhole_route(net, dst_orig);
2317 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002318 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08002319
2320 return ret;
2321}
2322
Benedict Wongbc56b332018-07-19 10:50:44 -07002323/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 *
2325 * At the moment we eat a raw IP route. Mostly to speed up lookups
2326 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07002327 *
2328 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2329 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 */
Benedict Wongbc56b332018-07-19 10:50:44 -07002331struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
2332 struct dst_entry *dst_orig,
2333 const struct flowi *fl,
2334 const struct sock *sk,
2335 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002337 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002339 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002340 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02002341 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00002342 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002343
Timo Teräs80c802f2010-04-07 00:30:05 +00002344 dst = NULL;
2345 xdst = NULL;
2346 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002347
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002348 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002349 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002350 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002351 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
2352 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002353 err = xfrm_expand_policies(fl, family, pols,
2354 &num_pols, &num_xfrms);
2355 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002356 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002357
2358 if (num_pols) {
2359 if (num_xfrms <= 0) {
2360 drop_pols = num_pols;
2361 goto no_transform;
2362 }
2363
2364 xdst = xfrm_resolve_and_create_bundle(
2365 pols, num_pols, fl,
2366 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01002367
Timo Teräs80c802f2010-04-07 00:30:05 +00002368 if (IS_ERR(xdst)) {
2369 xfrm_pols_put(pols, num_pols);
2370 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002371 if (err == -EREMOTE)
2372 goto nopol;
2373
Timo Teräs80c802f2010-04-07 00:30:05 +00002374 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002375 } else if (xdst == NULL) {
2376 num_xfrms = 0;
2377 drop_pols = num_pols;
2378 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002379 }
2380
Timo Teräs80c802f2010-04-07 00:30:05 +00002381 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002382 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Timo Teräs80c802f2010-04-07 00:30:05 +00002385 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002386 struct xfrm_flo xflo;
2387
2388 xflo.dst_orig = dst_orig;
2389 xflo.flags = flags;
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002392 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002393 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002394 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Benedict Wongbc56b332018-07-19 10:50:44 -07002396 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02002397 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00002398 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02002399 if (IS_ERR(xdst)) {
2400 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002401 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002402 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002403
2404 num_pols = xdst->num_pols;
2405 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002406 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002407 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409
Timo Teräs80c802f2010-04-07 00:30:05 +00002410 dst = &xdst->u.dst;
2411 if (route == NULL && num_xfrms > 0) {
2412 /* The only case when xfrm_bundle_lookup() returns a
2413 * bundle with null route, is when the template could
2414 * not be resolved. It means policies are there, but
2415 * bundle could not be created, since we don't yet
2416 * have the xfrm_state's. We need to wait for KM to
2417 * negotiate new SA's or bail out with error.*/
2418 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002419 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002420 err = -EREMOTE;
2421 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002422 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002423
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002424 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002425
2426 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2427 goto error;
2428 }
2429
2430no_transform:
2431 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002432 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Timo Teräs80c802f2010-04-07 00:30:05 +00002434 if ((flags & XFRM_LOOKUP_ICMP) &&
2435 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2436 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002437 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002438 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002439
Timo Teräs80c802f2010-04-07 00:30:05 +00002440 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02002441 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002442
Timo Teräs80c802f2010-04-07 00:30:05 +00002443 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002445 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002446 err = -EPERM;
2447 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002448 } else if (num_xfrms > 0) {
2449 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002450 dst_release(dst_orig);
2451 } else {
2452 /* Flow passes untransformed */
2453 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002454 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002456ok:
2457 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002458 if (dst && dst->xfrm &&
2459 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2460 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002461 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Timo Teräs80c802f2010-04-07 00:30:05 +00002463nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002464 if (!(flags & XFRM_LOOKUP_ICMP)) {
2465 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002466 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002467 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002468 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002470 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002471dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002472 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2473 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002474 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002475 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
Benedict Wongbc56b332018-07-19 10:50:44 -07002477EXPORT_SYMBOL(xfrm_lookup_with_ifid);
2478
2479/* Main function: finds/creates a bundle for given flow.
2480 *
2481 * At the moment we eat a raw IP route. Mostly to speed up lookups
2482 * on interfaces with disabled IPsec.
2483 */
2484struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2485 const struct flowi *fl, const struct sock *sk,
2486 int flags)
2487{
2488 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
2489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490EXPORT_SYMBOL(xfrm_lookup);
2491
Steffen Klassertf92ee612014-09-16 10:08:40 +02002492/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2493 * Otherwise we may send out blackholed packets.
2494 */
2495struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2496 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002497 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002498{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002499 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002500 flags | XFRM_LOOKUP_QUEUE |
2501 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002502
2503 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2504 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2505
Tommi Rantala8cc88772018-06-21 09:30:47 +03002506 if (IS_ERR(dst))
2507 dst_release(dst_orig);
2508
Steffen Klassertf92ee612014-09-16 10:08:40 +02002509 return dst;
2510}
2511EXPORT_SYMBOL(xfrm_lookup_route);
2512
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002513static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002514xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002515{
2516 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002517
2518 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2519 return 0;
2520 x = skb->sp->xvec[idx];
2521 if (!x->type->reject)
2522 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002523 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526/* When skb is transformed back to its "native" form, we have to
2527 * check policy restrictions. At the moment we make this in maximally
2528 * stupid way. Shame on me. :-) Of course, connected sockets must
2529 * have policy cached at them.
2530 */
2531
2532static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002533xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 unsigned short family)
2535{
2536 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002537 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return x->id.proto == tmpl->id.proto &&
2539 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2540 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2541 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002542 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002543 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002544 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2545 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002548/*
2549 * 0 or more than 0 is returned when validation is succeeded (either bypass
2550 * because of optional transport mode, or next index of the mathced secpath
2551 * state with the template.
2552 * -1 is returned when no matching template is found.
2553 * Otherwise "-2 - errored_index" is returned.
2554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002556xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 unsigned short family)
2558{
2559 int idx = start;
2560
2561 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002562 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return start;
2564 } else
2565 start = -1;
2566 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002567 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002569 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2570 if (start == -1)
2571 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575 return start;
2576}
2577
Herbert Xud5422ef2007-12-12 10:44:16 -08002578int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2579 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Florian Westphal37b10382017-02-07 15:00:19 +01002581 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002582 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 if (unlikely(afinfo == NULL))
2585 return -EAFNOSUPPORT;
2586
Herbert Xud5422ef2007-12-12 10:44:16 -08002587 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf203b762018-06-12 14:07:12 +02002588
David S. Miller1d28f422011-03-12 00:29:39 -05002589 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002590 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
Herbert Xud5422ef2007-12-12 10:44:16 -08002593EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
David S. Miller9a7386e2011-02-24 01:44:12 -05002595static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002598 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002599 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603
2604 return 0;
2605}
2606
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002607int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 unsigned short family)
2609{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002610 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002612 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2613 int npols = 0;
2614 int xfrm_nr;
2615 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002616 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002618 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002619 const struct xfrm_if_cb *ifcb;
2620 struct xfrm_if *xi;
2621 u32 if_id = 0;
2622
2623 rcu_read_lock();
2624 ifcb = xfrm_if_get_cb();
2625
2626 if (ifcb) {
2627 xi = ifcb->decode_session(skb);
2628 if (xi)
2629 if_id = xi->p.if_id;
2630 }
2631 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Herbert Xud5422ef2007-12-12 10:44:16 -08002633 reverse = dir & ~XFRM_POLICY_MASK;
2634 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08002635
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002636 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002637 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002639 }
2640
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002641 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 /* First, check used SA against their selectors. */
2644 if (skb->sp) {
2645 int i;
2646
Weilong Chen9b7a7872013-12-24 09:43:46 +08002647 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002648 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002649 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002650 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654 }
2655
2656 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002657 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002658 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07002659 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002660 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002661 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002662 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002663 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Florian Westphal86dc8ee2017-07-17 13:57:24 +02002666 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07002667 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002669 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002670 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002671 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002672 }
James Morris134b0fc2006-10-05 15:42:27 -05002673
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002674 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002675 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002676 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002677 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002678 return 0;
2679 }
2680 return 1;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Arnd Bergmann386c5682018-07-11 12:19:13 +02002683 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002685 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002686 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002687#ifdef CONFIG_XFRM_SUB_POLICY
2688 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002689 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002690 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002691 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002692 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002693 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002694 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002695 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002696 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02002697 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002698 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002699 }
2700 }
2701#endif
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (pol->action == XFRM_POLICY_ALLOW) {
2704 struct sec_path *sp;
2705 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002706 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002707 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002708 struct xfrm_tmpl **tpp = tp;
2709 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 int i, k;
2711
2712 if ((sp = skb->sp) == NULL)
2713 sp = &dummy;
2714
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002715 for (pi = 0; pi < npols; pi++) {
2716 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002717 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002718 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002719 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002720 }
2721 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002722 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002723 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002724 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002725 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2726 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2727 }
2728 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002729 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002730 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002731 tpp = stp;
2732 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* For each tunnel xfrm, find the first matching tmpl.
2735 * For each tmpl before that, find corresponding xfrm.
2736 * Order is _important_. Later we will implement
2737 * some barriers, but at the moment barriers
2738 * are implied between each two transformations.
2739 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002740 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2741 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002742 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002743 if (k < -1)
2744 /* "-2 - errored_index" returned */
2745 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002746 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002751 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002752 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002756 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return 1;
2758 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002759 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002762 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002763reject_error:
2764 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return 0;
2766}
2767EXPORT_SYMBOL(__xfrm_policy_check);
2768
2769int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2770{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002771 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002773 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002774 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002776 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002777 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002781 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02002782 if (!skb_dst(skb)) {
2783 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2784 return 0;
2785 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002786
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002787 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002788 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002789 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002790 dst = NULL;
2791 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002792 skb_dst_set(skb, dst);
2793 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795EXPORT_SYMBOL(__xfrm_route_forward);
2796
David S. Millerd49c73c2006-08-13 18:55:53 -07002797/* Optimize later using cookies and generation ids. */
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2800{
David S. Millerd49c73c2006-08-13 18:55:53 -07002801 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002802 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2803 * get validated by dst_ops->check on every use. We do this
2804 * because when a normal route referenced by an XFRM dst is
2805 * obsoleted we do not go looking around for all parent
2806 * referencing XFRM dsts so that we can invalidate them. It
2807 * is just too much work. Instead we make the checks here on
2808 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002809 *
2810 * XFRM dst A --> IPv4 dst X
2811 *
2812 * X is the "xdst->route" of A (X is also the "dst->path" of A
2813 * in this example). If X is marked obsolete, "A" will not
2814 * notice. That's what we are validating here via the
2815 * stale_bundle() check.
2816 *
Wei Wang52df1572017-06-17 10:42:38 -07002817 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2818 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02002819 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07002820 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08002821 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002822 if (dst->obsolete < 0 && !stale_bundle(dst))
2823 return dst;
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return NULL;
2826}
2827
2828static int stale_bundle(struct dst_entry *dst)
2829{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002830 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
Herbert Xuaabc9762005-05-03 16:27:10 -07002833void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
David Millerb92cf4a2017-11-28 15:40:22 -05002835 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002836 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002837 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 dev_put(dev);
2839 }
2840}
Herbert Xuaabc9762005-05-03 16:27:10 -07002841EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843static void xfrm_link_failure(struct sk_buff *skb)
2844{
2845 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2849{
2850 if (dst) {
2851 if (dst->obsolete) {
2852 dst_release(dst);
2853 dst = NULL;
2854 }
2855 }
2856 return dst;
2857}
2858
David Miller54920932017-11-28 15:41:01 -05002859static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
David Miller54920932017-11-28 15:41:01 -05002861 while (nr--) {
2862 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05002864 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
David Miller54920932017-11-28 15:41:01 -05002866 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05002867 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 xdst->child_mtu_cached = pmtu;
2869
2870 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2871
2872 route_mtu_cached = dst_mtu(xdst->route);
2873 xdst->route_mtu_cached = route_mtu_cached;
2874
2875 if (pmtu > route_mtu_cached)
2876 pmtu = route_mtu_cached;
2877
David S. Millerdefb3512010-12-08 21:16:57 -08002878 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05002879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/* Check that the bundle accepts the flow and its components are
2883 * still valid.
2884 */
2885
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002886static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
David Miller54920932017-11-28 15:41:01 -05002888 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05002890 struct xfrm_dst *xdst;
2891 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 u32 mtu;
2893
David Miller0f6c4802017-11-28 15:40:46 -05002894 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 (dst->dev && !netif_running(dst->dev)))
2896 return 0;
2897
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002898 if (dst->flags & DST_XFRM_QUEUE)
2899 return 1;
2900
David Miller54920932017-11-28 15:41:01 -05002901 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 do {
2903 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2906 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002907 if (xdst->xfrm_genid != dst->xfrm->genid)
2908 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002909 if (xdst->num_pols > 0 &&
2910 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
David Miller54920932017-11-28 15:41:01 -05002913 bundle[nr++] = xdst;
2914
David Millerb92cf4a2017-11-28 15:40:22 -05002915 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05002917 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 xdst->child_mtu_cached = mtu;
2919 }
2920
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002921 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return 0;
2923 mtu = dst_mtu(xdst->route);
2924 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05002925 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 xdst->route_mtu_cached = mtu;
2927 }
2928
David Millerb92cf4a2017-11-28 15:40:22 -05002929 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 } while (dst->xfrm);
2931
David Miller54920932017-11-28 15:41:01 -05002932 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return 1;
2934
David Miller54920932017-11-28 15:41:01 -05002935 xdst = bundle[start_from - 1];
2936 mtu = xdst->child_mtu_cached;
2937 while (start_from--) {
2938 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05002941 if (mtu > xdst->route_mtu_cached)
2942 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002943 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05002944 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 break;
2946
David Miller54920932017-11-28 15:41:01 -05002947 xdst = bundle[start_from - 1];
2948 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950
2951 return 1;
2952}
2953
David S. Miller0dbaee32010-12-13 12:52:14 -08002954static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2955{
David Miller0f6c4802017-11-28 15:40:46 -05002956 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08002957}
2958
Steffen Klassertebb762f2011-11-23 02:12:51 +00002959static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002960{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002961 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2962
David Miller0f6c4802017-11-28 15:40:46 -05002963 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08002964}
2965
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002966static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
2967 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02002968{
David Miller0f6c4802017-11-28 15:40:46 -05002969 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02002970 const struct xfrm_state *xfrm = dst->xfrm;
2971
Steffen Klassert013cb812018-02-19 07:44:07 +01002972 dst = xfrm_dst_child(dst);
2973
Julian Anastasov63fca652017-02-06 23:14:15 +02002974 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
2975 continue;
2976 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
2977 daddr = xfrm->coaddr;
2978 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
2979 daddr = &xfrm->id.daddr;
2980 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002981 return daddr;
2982}
2983
2984static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2985 struct sk_buff *skb,
2986 const void *daddr)
2987{
David Miller0f6c4802017-11-28 15:40:46 -05002988 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002989
2990 if (!skb)
2991 daddr = xfrm_get_dst_nexthop(dst, daddr);
2992 return path->ops->neigh_lookup(path, skb, daddr);
2993}
2994
2995static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
2996{
David Miller0f6c4802017-11-28 15:40:46 -05002997 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002998
2999 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003000 path->ops->confirm_neigh(path, daddr);
3001}
3002
Florian Westphala2817d82017-02-07 15:00:17 +01003003int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003006
3007 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003009
Eric Dumazetef8531b2012-08-19 12:31:48 +02003010 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003011 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003012 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 else {
3014 struct dst_ops *dst_ops = afinfo->dst_ops;
3015 if (likely(dst_ops->kmem_cachep == NULL))
3016 dst_ops->kmem_cachep = xfrm_dst_cache;
3017 if (likely(dst_ops->check == NULL))
3018 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003019 if (likely(dst_ops->default_advmss == NULL))
3020 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003021 if (likely(dst_ops->mtu == NULL))
3022 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (likely(dst_ops->negative_advice == NULL))
3024 dst_ops->negative_advice = xfrm_negative_advice;
3025 if (likely(dst_ops->link_failure == NULL))
3026 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003027 if (likely(dst_ops->neigh_lookup == NULL))
3028 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003029 if (likely(!dst_ops->confirm_neigh))
3030 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003031 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003033 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return err;
3036}
3037EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3038
Florian Westphala2817d82017-02-07 15:00:17 +01003039void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Florian Westphal2b619972017-02-07 15:00:15 +01003041 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003042 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003043
Florian Westphala2817d82017-02-07 15:00:17 +01003044 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3045 if (xfrm_policy_afinfo[i] != afinfo)
3046 continue;
3047 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3048 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Florian Westphal2b619972017-02-07 15:00:15 +01003051 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Florian Westphal2b619972017-02-07 15:00:15 +01003053 dst_ops->kmem_cachep = NULL;
3054 dst_ops->check = NULL;
3055 dst_ops->negative_advice = NULL;
3056 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3059
Steffen Klassertf203b762018-06-12 14:07:12 +02003060void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3061{
3062 spin_lock(&xfrm_if_cb_lock);
3063 rcu_assign_pointer(xfrm_if_cb, ifcb);
3064 spin_unlock(&xfrm_if_cb_lock);
3065}
3066EXPORT_SYMBOL(xfrm_if_register_cb);
3067
3068void xfrm_if_unregister_cb(void)
3069{
3070 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3071 synchronize_rcu();
3072}
3073EXPORT_SYMBOL(xfrm_if_unregister_cb);
3074
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003075#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003076static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003077{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003078 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003079 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3080 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003081 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003082 rv = xfrm_proc_init(net);
3083 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003084 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003085 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003086}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003087
3088static void xfrm_statistics_fini(struct net *net)
3089{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003090 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003091 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003092}
3093#else
3094static int __net_init xfrm_statistics_init(struct net *net)
3095{
3096 return 0;
3097}
3098
3099static void xfrm_statistics_fini(struct net *net)
3100{
3101}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003102#endif
3103
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003104static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
David S. Miller2518c7c2006-08-24 04:45:07 -07003106 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01003107 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07003108
Florian Westphal24969fa2018-11-07 23:00:35 +01003109 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003110 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003112 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003113 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01003114 err = rhashtable_init(&xfrm_policy_inexact_table,
3115 &xfrm_pol_inexact_params);
3116 BUG_ON(err);
3117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
David S. Miller2518c7c2006-08-24 04:45:07 -07003119 hmask = 8 - 1;
3120 sz = (hmask+1) * sizeof(struct hlist_head);
3121
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003122 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3123 if (!net->xfrm.policy_byidx)
3124 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003125 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07003126
Herbert Xu53c2e282014-11-13 17:09:49 +08003127 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07003128 struct xfrm_policy_hash *htab;
3129
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08003130 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08003131 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003132 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07003133
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003134 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07003135 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07003136 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003137 goto out_bydst;
3138 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02003139 htab->dbits4 = 32;
3140 htab->sbits4 = 32;
3141 htab->dbits6 = 128;
3142 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003143 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003144 net->xfrm.policy_hthresh.lbits4 = 32;
3145 net->xfrm.policy_hthresh.rbits4 = 32;
3146 net->xfrm.policy_hthresh.lbits6 = 128;
3147 net->xfrm.policy_hthresh.rbits6 = 128;
3148
3149 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003150
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003151 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01003152 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003153 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003154 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003155 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003156
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003157out_bydst:
3158 for (dir--; dir >= 0; dir--) {
3159 struct xfrm_policy_hash *htab;
3160
3161 htab = &net->xfrm.policy_bydst[dir];
3162 xfrm_hash_free(htab->table, sz);
3163 }
3164 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003165out_byidx:
3166 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003169static void xfrm_policy_fini(struct net *net)
3170{
Florian Westphal24969fa2018-11-07 23:00:35 +01003171 struct xfrm_pol_inexact_bin *bin, *tmp;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003172 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003173 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003174
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003175 flush_work(&net->xfrm.policy_hash_work);
3176#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003177 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003178#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003179 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003180
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003181 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003182
Herbert Xu53c2e282014-11-13 17:09:49 +08003183 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003184 struct xfrm_policy_hash *htab;
3185
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003186 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003187
3188 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003189 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003190 WARN_ON(!hlist_empty(htab->table));
3191 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003192 }
3193
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003194 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003195 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3196 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01003197
3198 list_for_each_entry_safe(bin, tmp, &net->xfrm.inexact_bins,
3199 inexact_bins) {
3200 WARN_ON(!hlist_empty(&bin->hhead));
3201 xfrm_policy_inexact_delete_bin(net, bin);
3202 }
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003203}
3204
3205static int __net_init xfrm_net_init(struct net *net)
3206{
3207 int rv;
3208
Florian Westphalc2822222017-02-08 11:52:29 +01003209 /* Initialize the per-net locks here */
3210 spin_lock_init(&net->xfrm.xfrm_state_lock);
3211 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3212 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3213
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003214 rv = xfrm_statistics_init(net);
3215 if (rv < 0)
3216 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003217 rv = xfrm_state_init(net);
3218 if (rv < 0)
3219 goto out_state;
3220 rv = xfrm_policy_init(net);
3221 if (rv < 0)
3222 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003223 rv = xfrm_sysctl_init(net);
3224 if (rv < 0)
3225 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08003226
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003227 return 0;
3228
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003229out_sysctl:
3230 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003231out_policy:
3232 xfrm_state_fini(net);
3233out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003234 xfrm_statistics_fini(net);
3235out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003236 return rv;
3237}
3238
3239static void __net_exit xfrm_net_exit(struct net *net)
3240{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003241 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003242 xfrm_policy_fini(net);
3243 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003244 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003245}
3246
3247static struct pernet_operations __net_initdata xfrm_net_ops = {
3248 .init = xfrm_net_init,
3249 .exit = xfrm_net_exit,
3250};
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252void __init xfrm_init(void)
3253{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003254 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03003255 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02003256 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02003258
3259 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3260 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Joy Lattenab5f5e82007-09-17 11:51:22 -07003263#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003264static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3265 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003266{
Paul Moore875179f2007-12-01 23:27:18 +11003267 struct xfrm_sec_ctx *ctx = xp->security;
3268 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003269
Paul Moore875179f2007-12-01 23:27:18 +11003270 if (ctx)
3271 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3272 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3273
Weilong Chen9b7a7872013-12-24 09:43:46 +08003274 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003275 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003276 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003277 if (sel->prefixlen_s != 32)
3278 audit_log_format(audit_buf, " src_prefixlen=%d",
3279 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003280 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003281 if (sel->prefixlen_d != 32)
3282 audit_log_format(audit_buf, " dst_prefixlen=%d",
3283 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003284 break;
3285 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003286 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003287 if (sel->prefixlen_s != 128)
3288 audit_log_format(audit_buf, " src_prefixlen=%d",
3289 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003290 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003291 if (sel->prefixlen_d != 128)
3292 audit_log_format(audit_buf, " dst_prefixlen=%d",
3293 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003294 break;
3295 }
3296}
3297
Tetsuo Handa2e710292014-04-22 21:48:30 +09003298void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003299{
3300 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003301
Paul Mooreafeb14b2007-12-21 14:58:11 -08003302 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003303 if (audit_buf == NULL)
3304 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003305 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003306 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003307 xfrm_audit_common_policyinfo(xp, audit_buf);
3308 audit_log_end(audit_buf);
3309}
3310EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3311
Paul Moore68277ac2007-12-20 20:49:33 -08003312void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003313 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003314{
3315 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003316
Paul Mooreafeb14b2007-12-21 14:58:11 -08003317 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003318 if (audit_buf == NULL)
3319 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003320 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003321 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003322 xfrm_audit_common_policyinfo(xp, audit_buf);
3323 audit_log_end(audit_buf);
3324}
3325EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3326#endif
3327
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003328#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003329static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3330 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003331{
3332 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3333 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003334 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3335 sel_cmp->family) &&
3336 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3337 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003338 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3339 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003340 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003341 }
3342 } else {
3343 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003344 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003345 }
3346 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003347 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003348}
3349
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003350static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3351 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003352{
3353 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003354 struct hlist_head *chain;
3355 u32 priority = ~0U;
3356
Florian Westphal9d0380d2016-08-11 15:17:59 +02003357 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003358 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003359 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003360 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3361 pol->type == type) {
3362 ret = pol;
3363 priority = ret->priority;
3364 break;
3365 }
3366 }
Fan Du8d549c42013-11-07 17:47:49 +08003367 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01003368 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08003369 if ((pol->priority >= priority) && ret)
3370 break;
3371
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003372 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003373 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003374 ret = pol;
3375 break;
3376 }
3377 }
3378
Li RongQing586f2eb2015-04-30 17:13:41 +08003379 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003380
Florian Westphal9d0380d2016-08-11 15:17:59 +02003381 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003382
3383 return ret;
3384}
3385
David S. Millerdd701752011-02-24 00:21:08 -05003386static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003387{
3388 int match = 0;
3389
3390 if (t->mode == m->mode && t->id.proto == m->proto &&
3391 (m->reqid == 0 || t->reqid == m->reqid)) {
3392 switch (t->mode) {
3393 case XFRM_MODE_TUNNEL:
3394 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003395 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3396 m->old_family) &&
3397 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3398 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003399 match = 1;
3400 }
3401 break;
3402 case XFRM_MODE_TRANSPORT:
3403 /* in case of transport mode, template does not store
3404 any IP addresses, hence we just compare mode and
3405 protocol */
3406 match = 1;
3407 break;
3408 default:
3409 break;
3410 }
3411 }
3412 return match;
3413}
3414
3415/* update endpoint address(es) of template(s) */
3416static int xfrm_policy_migrate(struct xfrm_policy *pol,
3417 struct xfrm_migrate *m, int num_migrate)
3418{
3419 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003420 int i, j, n = 0;
3421
3422 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003423 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003424 /* target policy has been deleted */
3425 write_unlock_bh(&pol->lock);
3426 return -ENOENT;
3427 }
3428
3429 for (i = 0; i < pol->xfrm_nr; i++) {
3430 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3431 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3432 continue;
3433 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003434 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3435 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003436 continue;
3437 /* update endpoints */
3438 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3439 sizeof(pol->xfrm_vec[i].id.daddr));
3440 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3441 sizeof(pol->xfrm_vec[i].saddr));
3442 pol->xfrm_vec[i].encap_family = mp->new_family;
3443 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003444 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003445 }
3446 }
3447
3448 write_unlock_bh(&pol->lock);
3449
3450 if (!n)
3451 return -ENODATA;
3452
3453 return 0;
3454}
3455
David S. Millerdd701752011-02-24 00:21:08 -05003456static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003457{
3458 int i, j;
3459
3460 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3461 return -EINVAL;
3462
3463 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003464 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3465 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3466 return -EINVAL;
3467
3468 /* check if there is any duplicated entry */
3469 for (j = i + 1; j < num_migrate; j++) {
3470 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3471 sizeof(m[i].old_daddr)) &&
3472 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3473 sizeof(m[i].old_saddr)) &&
3474 m[i].proto == m[j].proto &&
3475 m[i].mode == m[j].mode &&
3476 m[i].reqid == m[j].reqid &&
3477 m[i].old_family == m[j].old_family)
3478 return -EINVAL;
3479 }
3480 }
3481
3482 return 0;
3483}
3484
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003485int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003486 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02003487 struct xfrm_kmaddress *k, struct net *net,
3488 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003489{
3490 int i, err, nx_cur = 0, nx_new = 0;
3491 struct xfrm_policy *pol = NULL;
3492 struct xfrm_state *x, *xc;
3493 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3494 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3495 struct xfrm_migrate *mp;
3496
Vladis Dronov7bab0962017-08-02 19:50:14 +02003497 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003498 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3499 goto out;
3500
Vladis Dronov7bab0962017-08-02 19:50:14 +02003501 if (dir >= XFRM_POLICY_MAX) {
3502 err = -EINVAL;
3503 goto out;
3504 }
3505
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003506 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003507 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003508 err = -ENOENT;
3509 goto out;
3510 }
3511
3512 /* Stage 2 - find and update state(s) */
3513 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003514 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003515 x_cur[nx_cur] = x;
3516 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02003517 xc = xfrm_state_migrate(x, mp, encap);
3518 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003519 x_new[nx_new] = xc;
3520 nx_new++;
3521 } else {
3522 err = -ENODATA;
3523 goto restore_state;
3524 }
3525 }
3526 }
3527
3528 /* Stage 3 - update policy */
3529 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3530 goto restore_state;
3531
3532 /* Stage 4 - delete old state(s) */
3533 if (nx_cur) {
3534 xfrm_states_put(x_cur, nx_cur);
3535 xfrm_states_delete(x_cur, nx_cur);
3536 }
3537
3538 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02003539 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003540
3541 xfrm_pol_put(pol);
3542
3543 return 0;
3544out:
3545 return err;
3546
3547restore_state:
3548 if (pol)
3549 xfrm_pol_put(pol);
3550 if (nx_cur)
3551 xfrm_states_put(x_cur, nx_cur);
3552 if (nx_new)
3553 xfrm_states_delete(x_new, nx_new);
3554
3555 return err;
3556}
David S. Millere610e672007-02-08 13:29:15 -08003557EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003558#endif