blob: 57e28dcd7c5355078eeb526c9ac7b264424a153c [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 Westphal6be3b0d2018-11-07 23:00:37 +010049/* prefixes smaller than this are stored in lists, not trees. */
50#define INEXACT_PREFIXLEN_IPV4 16
51#define INEXACT_PREFIXLEN_IPV6 48
Florian Westphal9cf545e2018-11-07 23:00:38 +010052
53struct xfrm_pol_inexact_node {
54 struct rb_node node;
55 union {
56 xfrm_address_t addr;
57 struct rcu_head rcu;
58 };
59 u8 prefixlen;
60
61 /* the policies matching this node, can be empty list */
62 struct hlist_head hhead;
63};
64
65/* xfrm inexact policy search tree:
66 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
67 * |
68 * +---- root_d: sorted by daddr:prefix
69 * | |
70 * | xfrm_pol_inexact_node
71 * | |
72 * | +- coarse policies and all any:daddr policies
73 * |
74 * +---- coarse policies and all any:any policies
75 *
76 * Lookups return two candidate lists:
77 * 1. any:any list from top-level xfrm_pol_inexact_bin
78 * 2. any:daddr list from daddr tree
79 *
80 * This result set then needs to be searched for the policy with
81 * the lowest priority. If two results have same prio, youngest one wins.
82 */
83
Florian Westphal24969fa2018-11-07 23:00:35 +010084struct xfrm_pol_inexact_key {
85 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +010086 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +010087 u16 family;
88 u8 dir, type;
89};
90
91struct xfrm_pol_inexact_bin {
92 struct xfrm_pol_inexact_key k;
93 struct rhash_head head;
Florian Westphal6be3b0d2018-11-07 23:00:37 +010094 /* list containing '*:*' policies */
Florian Westphal24969fa2018-11-07 23:00:35 +010095 struct hlist_head hhead;
96
Florian Westphal9cf545e2018-11-07 23:00:38 +010097 seqcount_t count;
98 /* tree sorted by daddr/prefix */
99 struct rb_root root_d;
100
Florian Westphal24969fa2018-11-07 23:00:35 +0100101 /* slow path below */
102 struct list_head inexact_bins;
103 struct rcu_head rcu;
104};
105
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100106enum xfrm_pol_inexact_candidate_type {
Florian Westphal9cf545e2018-11-07 23:00:38 +0100107 XFRM_POL_CAND_DADDR,
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100108 XFRM_POL_CAND_ANY,
109
110 XFRM_POL_CAND_MAX,
111};
112
113struct xfrm_pol_inexact_candidates {
114 struct hlist_head *res[XFRM_POL_CAND_MAX];
115};
116
Steffen Klassertf203b762018-06-12 14:07:12 +0200117static DEFINE_SPINLOCK(xfrm_if_cb_lock);
118static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
119
Priyanka Jain418a99a2012-08-12 21:22:29 +0000120static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +0100121static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +0000122 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +0300124static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +0200125static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Florian Westphal24969fa2018-11-07 23:00:35 +0100127static struct rhashtable xfrm_policy_inexact_table;
128static const struct rhashtable_params xfrm_pol_inexact_params;
129
David Miller54920932017-11-28 15:41:01 -0500130static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +0000131static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +0000132static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -0700133static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800135static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800136static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
137 int dir);
138
Florian Westphal24969fa2018-11-07 23:00:35 +0100139static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100140xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
141 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100142
143static struct xfrm_pol_inexact_bin *
144xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100145 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100146static struct xfrm_policy *
147xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
148 bool excl);
149static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
150 struct xfrm_policy *policy);
151
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100152static bool
153xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
154 struct xfrm_pol_inexact_bin *b,
155 const xfrm_address_t *saddr,
156 const xfrm_address_t *daddr);
157
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200158static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
159{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300160 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200161}
162
David S. Millerbc9b35a2012-05-15 15:04:57 -0400163static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500164__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800165{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500166 const struct flowi4 *fl4 = &fl->u.ip4;
167
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000168 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
169 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500170 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
171 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
172 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
173 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800174}
175
David S. Millerbc9b35a2012-05-15 15:04:57 -0400176static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500177__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800178{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500179 const struct flowi6 *fl6 = &fl->u.ip6;
180
181 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
182 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
183 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
184 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
185 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
186 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800187}
188
David S. Millerbc9b35a2012-05-15 15:04:57 -0400189bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
190 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800191{
192 switch (family) {
193 case AF_INET:
194 return __xfrm4_selector_match(sel, fl);
195 case AF_INET6:
196 return __xfrm6_selector_match(sel, fl);
197 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400198 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800199}
200
Florian Westphala2817d82017-02-07 15:00:17 +0100201static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200202{
Florian Westphala2817d82017-02-07 15:00:17 +0100203 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200204
Florian Westphala2817d82017-02-07 15:00:17 +0100205 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200206 return NULL;
207 rcu_read_lock();
208 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
209 if (unlikely(!afinfo))
210 rcu_read_unlock();
211 return afinfo;
212}
213
Steffen Klassertf203b762018-06-12 14:07:12 +0200214/* Called with rcu_read_lock(). */
215static const struct xfrm_if_cb *xfrm_if_get_cb(void)
216{
217 return rcu_dereference(xfrm_if_cb);
218}
219
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200220struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
221 const xfrm_address_t *saddr,
222 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900223 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Florian Westphal37b10382017-02-07 15:00:19 +0100225 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800226 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Herbert Xu25ee3282007-12-11 09:32:34 -0800228 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800230 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900232 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900233
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100234 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900235
236 return dst;
237}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200238EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900239
David Ahern42a7b322015-08-10 16:58:11 -0600240static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
241 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900242 xfrm_address_t *prev_saddr,
243 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900244 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900245{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800246 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900247 xfrm_address_t *saddr = &x->props.saddr;
248 xfrm_address_t *daddr = &x->id.daddr;
249 struct dst_entry *dst;
250
251 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
252 saddr = x->coaddr;
253 daddr = prev_daddr;
254 }
255 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
256 saddr = prev_saddr;
257 daddr = x->coaddr;
258 }
259
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900260 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900261
262 if (!IS_ERR(dst)) {
263 if (prev_saddr != saddr)
264 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
265 if (prev_daddr != daddr)
266 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
267 }
268
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800269 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static inline unsigned long make_jiffies(long secs)
273{
274 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
275 return MAX_SCHEDULE_TIMEOUT-1;
276 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900277 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Kees Cookc3aed702017-10-16 17:28:56 -0700280static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Kees Cookc3aed702017-10-16 17:28:56 -0700282 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200283 time64_t now = ktime_get_real_seconds();
284 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int warn = 0;
286 int dir;
287
288 read_lock(&xp->lock);
289
Timo Teräsea2dea92010-03-31 00:17:05 +0000290 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 goto out;
292
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700293 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200296 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 xp->curlft.add_time - now;
298 if (tmo <= 0)
299 goto expired;
300 if (tmo < next)
301 next = tmo;
302 }
303 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200304 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
306 if (tmo <= 0)
307 goto expired;
308 if (tmo < next)
309 next = tmo;
310 }
311 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200312 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 xp->curlft.add_time - now;
314 if (tmo <= 0) {
315 warn = 1;
316 tmo = XFRM_KM_TIMEOUT;
317 }
318 if (tmo < next)
319 next = tmo;
320 }
321 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200322 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
324 if (tmo <= 0) {
325 warn = 1;
326 tmo = XFRM_KM_TIMEOUT;
327 }
328 if (tmo < next)
329 next = tmo;
330 }
331
332 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800333 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200334 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
336 xfrm_pol_hold(xp);
337
338out:
339 read_unlock(&xp->lock);
340 xfrm_pol_put(xp);
341 return;
342
343expired:
344 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700345 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800346 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 xfrm_pol_put(xp);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
351 * SPD calls.
352 */
353
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800354struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct xfrm_policy *policy;
357
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700358 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800361 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700362 INIT_LIST_HEAD(&policy->walk.all);
Florian Westphal24969fa2018-11-07 23:00:35 +0100363 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700364 INIT_HLIST_NODE(&policy->bydst);
365 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300367 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100368 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700369 timer_setup(&policy->timer, xfrm_policy_timer, 0);
370 timer_setup(&policy->polq.hold_timer,
371 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 return policy;
374}
375EXPORT_SYMBOL(xfrm_policy_alloc);
376
Eric Dumazet56f04732015-12-08 07:22:01 -0800377static void xfrm_policy_destroy_rcu(struct rcu_head *head)
378{
379 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
380
381 security_xfrm_policy_free(policy->security);
382 kfree(policy);
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/* Destroy xfrm_policy: descendant resources must be released to this moment. */
386
WANG Cong64c31b32008-01-07 22:34:29 -0800387void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Herbert Xu12a169e2008-10-01 07:03:24 -0700389 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Fan Du0659eea2013-08-01 18:08:36 +0800391 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 BUG();
393
Eric Dumazet56f04732015-12-08 07:22:01 -0800394 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
WANG Cong64c31b32008-01-07 22:34:29 -0800396EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100398/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * entry dead. The rule must be unlinked from lists to the moment.
400 */
401
402static void xfrm_policy_kill(struct xfrm_policy *policy)
403{
Herbert Xu12a169e2008-10-01 07:03:24 -0700404 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Timo Teräs285ead12010-04-07 00:30:06 +0000406 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200408 if (del_timer(&policy->polq.hold_timer))
409 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800410 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100411
Timo Teräs285ead12010-04-07 00:30:06 +0000412 if (del_timer(&policy->timer))
413 xfrm_pol_put(policy);
414
415 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
David S. Miller2518c7c2006-08-24 04:45:07 -0700418static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
419
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800420static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700421{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800422 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700423}
424
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200425/* calculate policy hash thresholds */
426static void __get_hash_thresh(struct net *net,
427 unsigned short family, int dir,
428 u8 *dbits, u8 *sbits)
429{
430 switch (family) {
431 case AF_INET:
432 *dbits = net->xfrm.policy_bydst[dir].dbits4;
433 *sbits = net->xfrm.policy_bydst[dir].sbits4;
434 break;
435
436 case AF_INET6:
437 *dbits = net->xfrm.policy_bydst[dir].dbits6;
438 *sbits = net->xfrm.policy_bydst[dir].sbits6;
439 break;
440
441 default:
442 *dbits = 0;
443 *sbits = 0;
444 }
445}
446
David S. Miller5f803b52011-02-24 00:33:19 -0500447static struct hlist_head *policy_hash_bysel(struct net *net,
448 const struct xfrm_selector *sel,
449 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700450{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800451 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200452 unsigned int hash;
453 u8 dbits;
454 u8 sbits;
455
456 __get_hash_thresh(net, family, dir, &dbits, &sbits);
457 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700458
Florian Westphale1e551b2016-08-11 15:17:53 +0200459 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100460 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200461
462 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
463 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700464}
465
David S. Miller5f803b52011-02-24 00:33:19 -0500466static struct hlist_head *policy_hash_direct(struct net *net,
467 const xfrm_address_t *daddr,
468 const xfrm_address_t *saddr,
469 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700470{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800471 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200472 unsigned int hash;
473 u8 dbits;
474 u8 sbits;
475
476 __get_hash_thresh(net, family, dir, &dbits, &sbits);
477 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
Florian Westphale1e551b2016-08-11 15:17:53 +0200479 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
480 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700481}
482
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200483static void xfrm_dst_hash_transfer(struct net *net,
484 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700485 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200486 unsigned int nhashmask,
487 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700488{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800489 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700490 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800491 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200492 u8 dbits;
493 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700494
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800495redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800496 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700497 unsigned int h;
498
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200499 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700500 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200501 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800502 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200503 hlist_del_rcu(&pol->bydst);
504 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800505 h0 = h;
506 } else {
507 if (h != h0)
508 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200509 hlist_del_rcu(&pol->bydst);
510 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800511 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800512 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800513 }
514 if (!hlist_empty(list)) {
515 entry0 = NULL;
516 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700517 }
518}
519
520static void xfrm_idx_hash_transfer(struct hlist_head *list,
521 struct hlist_head *nidxtable,
522 unsigned int nhashmask)
523{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800524 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 struct xfrm_policy *pol;
526
Sasha Levinb67bfe02013-02-27 17:06:00 -0800527 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700528 unsigned int h;
529
530 h = __idx_hash(pol->index, nhashmask);
531 hlist_add_head(&pol->byidx, nidxtable+h);
532 }
533}
534
535static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
536{
537 return ((old_hmask + 1) << 1) - 1;
538}
539
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800540static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700541{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800542 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700543 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
544 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700545 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200546 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700547 int i;
548
549 if (!ndst)
550 return;
551
Florian Westphal9d0380d2016-08-11 15:17:59 +0200552 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200553 write_seqcount_begin(&xfrm_policy_hash_generation);
554
555 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
556 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700557
Florian Westphale1e551b2016-08-11 15:17:53 +0200558 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
559 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
560
David S. Miller2518c7c2006-08-24 04:45:07 -0700561 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200562 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700563
Florian Westphale1e551b2016-08-11 15:17:53 +0200564 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800565 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700566
Florian Westphal30846092016-08-11 15:17:54 +0200567 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200568 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700569
Florian Westphale1e551b2016-08-11 15:17:53 +0200570 synchronize_rcu();
571
David S. Miller44e36b42006-08-24 04:50:50 -0700572 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700573}
574
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800575static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700576{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800577 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700578 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
579 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800580 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700581 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700582 int i;
583
584 if (!nidx)
585 return;
586
Florian Westphal9d0380d2016-08-11 15:17:59 +0200587 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700588
589 for (i = hmask; i >= 0; i--)
590 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
591
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800592 net->xfrm.policy_byidx = nidx;
593 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700594
Florian Westphal9d0380d2016-08-11 15:17:59 +0200595 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700596
David S. Miller44e36b42006-08-24 04:50:50 -0700597 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700598}
599
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800600static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700601{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800602 unsigned int cnt = net->xfrm.policy_count[dir];
603 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700604
605 if (total)
606 *total += cnt;
607
608 if ((hmask + 1) < xfrm_policy_hashmax &&
609 cnt > hmask)
610 return 1;
611
612 return 0;
613}
614
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800615static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700616{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800617 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700618
619 if ((hmask + 1) < xfrm_policy_hashmax &&
620 total > hmask)
621 return 1;
622
623 return 0;
624}
625
Alexey Dobriyane0710412010-01-23 13:37:10 +0000626void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700627{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000628 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
629 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
630 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
631 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
632 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
633 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
634 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700635 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700636}
637EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700638
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700639static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800640static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700641{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800642 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700643 int dir, total;
644
645 mutex_lock(&hash_resize_mutex);
646
647 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800648 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800649 if (xfrm_bydst_should_resize(net, dir, &total))
650 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700651 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800652 if (xfrm_byidx_should_resize(net, total))
653 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700654
655 mutex_unlock(&hash_resize_mutex);
656}
657
Florian Westphal24969fa2018-11-07 23:00:35 +0100658static void xfrm_hash_reset_inexact_table(struct net *net)
659{
660 struct xfrm_pol_inexact_bin *b;
661
662 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
663
664 list_for_each_entry(b, &net->xfrm.inexact_bins, inexact_bins)
665 INIT_HLIST_HEAD(&b->hhead);
666}
667
668/* Make sure *pol can be inserted into fastbin.
669 * Useful to check that later insert requests will be sucessful
670 * (provided xfrm_policy_lock is held throughout).
671 */
672static struct xfrm_pol_inexact_bin *
673xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
674{
675 struct xfrm_pol_inexact_bin *bin, *prev;
676 struct xfrm_pol_inexact_key k = {
677 .family = pol->family,
678 .type = pol->type,
679 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100680 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100681 };
682 struct net *net = xp_net(pol);
683
684 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
685
686 write_pnet(&k.net, net);
687 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
688 xfrm_pol_inexact_params);
689 if (bin)
690 return bin;
691
692 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
693 if (!bin)
694 return NULL;
695
696 bin->k = k;
697 INIT_HLIST_HEAD(&bin->hhead);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100698 bin->root_d = RB_ROOT;
699 seqcount_init(&bin->count);
Florian Westphal24969fa2018-11-07 23:00:35 +0100700
701 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
702 &bin->k, &bin->head,
703 xfrm_pol_inexact_params);
704 if (!prev) {
705 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
706 return bin;
707 }
708
709 kfree(bin);
710
711 return IS_ERR(prev) ? NULL : prev;
712}
713
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100714static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
715 int family, u8 prefixlen)
Florian Westphal24969fa2018-11-07 23:00:35 +0100716{
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100717 if (xfrm_addr_any(addr, family))
718 return true;
Florian Westphal24969fa2018-11-07 23:00:35 +0100719
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100720 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
721 return true;
722
723 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
724 return true;
725
726 return false;
727}
728
729static bool
730xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
731{
732 const xfrm_address_t *addr;
733 bool saddr_any, daddr_any;
734 u8 prefixlen;
735
736 addr = &policy->selector.saddr;
737 prefixlen = policy->selector.prefixlen_s;
738
739 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
740 policy->family,
741 prefixlen);
742 addr = &policy->selector.daddr;
743 prefixlen = policy->selector.prefixlen_d;
744 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
745 policy->family,
746 prefixlen);
747 return saddr_any && daddr_any;
748}
749
Florian Westphal9cf545e2018-11-07 23:00:38 +0100750static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
751 const xfrm_address_t *addr, u8 prefixlen)
752{
753 node->addr = *addr;
754 node->prefixlen = prefixlen;
755}
756
757static struct xfrm_pol_inexact_node *
758xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
759{
760 struct xfrm_pol_inexact_node *node;
761
762 node = kzalloc(sizeof(*node), GFP_ATOMIC);
763 if (node)
764 xfrm_pol_inexact_node_init(node, addr, prefixlen);
765
766 return node;
767}
768
769static int xfrm_policy_addr_delta(const xfrm_address_t *a,
770 const xfrm_address_t *b,
771 u8 prefixlen, u16 family)
772{
773 unsigned int pdw, pbi;
774 int delta = 0;
775
776 switch (family) {
777 case AF_INET:
778 if (sizeof(long) == 4 && prefixlen == 0)
779 return ntohl(a->a4) - ntohl(b->a4);
780 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
781 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
782 case AF_INET6:
783 pdw = prefixlen >> 5;
784 pbi = prefixlen & 0x1f;
785
786 if (pdw) {
787 delta = memcmp(a->a6, b->a6, pdw << 2);
788 if (delta)
789 return delta;
790 }
791 if (pbi) {
792 u32 mask = ~0u << (32 - pbi);
793
794 delta = (ntohl(a->a6[pdw]) & mask) -
795 (ntohl(b->a6[pdw]) & mask);
796 }
797 break;
798 default:
799 break;
800 }
801
802 return delta;
803}
804
805static void xfrm_policy_inexact_list_reinsert(struct net *net,
806 struct xfrm_pol_inexact_node *n,
807 u16 family)
808{
Florian Westphale901cbc2018-11-07 23:00:39 +0100809 unsigned int matched_s, matched_d;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100810 struct hlist_node *newpos = NULL;
811 struct xfrm_policy *policy, *p;
812
Florian Westphale901cbc2018-11-07 23:00:39 +0100813 matched_s = 0;
814 matched_d = 0;
815
Florian Westphal9cf545e2018-11-07 23:00:38 +0100816 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphale901cbc2018-11-07 23:00:39 +0100817 bool matches_s, matches_d;
818
Florian Westphal9cf545e2018-11-07 23:00:38 +0100819 if (!policy->bydst_reinsert)
820 continue;
821
822 WARN_ON_ONCE(policy->family != family);
823
824 policy->bydst_reinsert = false;
825 hlist_for_each_entry(p, &n->hhead, bydst) {
826 if (policy->priority >= p->priority)
827 newpos = &p->bydst;
828 else
829 break;
830 }
831
832 if (newpos)
833 hlist_add_behind(&policy->bydst, newpos);
834 else
835 hlist_add_head(&policy->bydst, &n->hhead);
Florian Westphale901cbc2018-11-07 23:00:39 +0100836
837 /* paranoia checks follow.
838 * Check that the reinserted policy matches at least
839 * saddr or daddr for current node prefix.
840 *
841 * Matching both is fine, matching saddr in one policy
842 * (but not daddr) and then matching only daddr in another
843 * is a bug.
844 */
845 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
846 &n->addr,
847 n->prefixlen,
848 family) == 0;
849 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
850 &n->addr,
851 n->prefixlen,
852 family) == 0;
853 if (matches_s && matches_d)
854 continue;
855
856 WARN_ON_ONCE(!matches_s && !matches_d);
857 if (matches_s)
858 matched_s++;
859 if (matches_d)
860 matched_d++;
861 WARN_ON_ONCE(matched_s && matched_d);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100862 }
863}
864
865/* merge nodes v and n */
866static void xfrm_policy_inexact_node_merge(struct net *net,
867 struct xfrm_pol_inexact_node *v,
868 struct xfrm_pol_inexact_node *n,
869 u16 family)
870{
871 struct xfrm_policy *tmp;
872
873 hlist_for_each_entry(tmp, &v->hhead, bydst)
874 tmp->bydst_reinsert = true;
875 hlist_for_each_entry(tmp, &n->hhead, bydst)
876 tmp->bydst_reinsert = true;
877
878 INIT_HLIST_HEAD(&n->hhead);
879 xfrm_policy_inexact_list_reinsert(net, n, family);
880}
881
882static struct xfrm_pol_inexact_node *
883xfrm_policy_inexact_insert_node(struct net *net,
884 struct rb_root *root,
885 xfrm_address_t *addr,
886 u16 family, u8 prefixlen, u8 dir)
887{
888 struct xfrm_pol_inexact_node *cached = NULL;
889 struct rb_node **p, *parent = NULL;
890 struct xfrm_pol_inexact_node *node;
891
892 p = &root->rb_node;
893 while (*p) {
894 int delta;
895
896 parent = *p;
897 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
898
899 delta = xfrm_policy_addr_delta(addr, &node->addr,
900 node->prefixlen,
901 family);
902 if (delta == 0 && prefixlen >= node->prefixlen) {
903 WARN_ON_ONCE(cached); /* ipsec policies got lost */
904 return node;
905 }
906
907 if (delta < 0)
908 p = &parent->rb_left;
909 else
910 p = &parent->rb_right;
911
912 if (prefixlen < node->prefixlen) {
913 delta = xfrm_policy_addr_delta(addr, &node->addr,
914 prefixlen,
915 family);
916 if (delta)
917 continue;
918
919 /* This node is a subnet of the new prefix. It needs
920 * to be removed and re-inserted with the smaller
921 * prefix and all nodes that are now also covered
922 * by the reduced prefixlen.
923 */
924 rb_erase(&node->node, root);
925
926 if (!cached) {
927 xfrm_pol_inexact_node_init(node, addr,
928 prefixlen);
929 cached = node;
930 } else {
931 /* This node also falls within the new
932 * prefixlen. Merge the to-be-reinserted
933 * node and this one.
934 */
935 xfrm_policy_inexact_node_merge(net, node,
936 cached, family);
937 kfree_rcu(node, rcu);
938 }
939
940 /* restart */
941 p = &root->rb_node;
942 parent = NULL;
943 }
944 }
945
946 node = cached;
947 if (!node) {
948 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
949 if (!node)
950 return NULL;
951 }
952
953 rb_link_node_rcu(&node->node, parent, p);
954 rb_insert_color(&node->node, root);
955
956 return node;
957}
958
959static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
960{
961 struct xfrm_pol_inexact_node *node;
962 struct rb_node *rn = rb_first(r);
963
964 while (rn) {
965 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
966
967 rn = rb_next(rn);
968
969 if (!hlist_empty(&node->hhead)) {
970 WARN_ON_ONCE(rm);
971 continue;
972 }
973
974 rb_erase(&node->node, r);
975 kfree_rcu(node, rcu);
976 }
977}
978
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100979static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
980{
Florian Westphal9cf545e2018-11-07 23:00:38 +0100981 write_seqcount_begin(&b->count);
982 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
983 write_seqcount_end(&b->count);
984
985 if (!RB_EMPTY_ROOT(&b->root_d) ||
986 !hlist_empty(&b->hhead)) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100987 WARN_ON_ONCE(net_exit);
Florian Westphal24969fa2018-11-07 23:00:35 +0100988 return;
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100989 }
Florian Westphal24969fa2018-11-07 23:00:35 +0100990
991 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
992 xfrm_pol_inexact_params) == 0) {
993 list_del(&b->inexact_bins);
994 kfree_rcu(b, rcu);
995 }
996}
997
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100998static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
999{
1000 struct net *net = read_pnet(&b->k.net);
1001
1002 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1003 __xfrm_policy_inexact_prune_bin(b, false);
1004 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1005}
1006
Florian Westphal24969fa2018-11-07 23:00:35 +01001007static void __xfrm_policy_inexact_flush(struct net *net)
1008{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001009 struct xfrm_pol_inexact_bin *bin, *t;
Florian Westphal24969fa2018-11-07 23:00:35 +01001010
1011 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1012
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001013 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1014 __xfrm_policy_inexact_prune_bin(bin, false);
Florian Westphal24969fa2018-11-07 23:00:35 +01001015}
1016
Florian Westphal9cf545e2018-11-07 23:00:38 +01001017static struct hlist_head *
1018xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1019 struct xfrm_policy *policy, u8 dir)
1020{
1021 struct xfrm_pol_inexact_node *n;
1022 struct net *net;
1023
1024 net = xp_net(policy);
1025 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1026
1027 if (xfrm_policy_inexact_insert_use_any_list(policy))
1028 return &bin->hhead;
1029
1030 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1031 policy->family,
1032 policy->selector.prefixlen_d))
1033 return &bin->hhead;
1034
1035 /* daddr is fixed */
1036 write_seqcount_begin(&bin->count);
1037 n = xfrm_policy_inexact_insert_node(net,
1038 &bin->root_d,
1039 &policy->selector.daddr,
1040 policy->family,
1041 policy->selector.prefixlen_d, dir);
1042 write_seqcount_end(&bin->count);
1043 if (!n)
1044 return NULL;
1045 return &n->hhead;
1046}
1047
Florian Westphal24969fa2018-11-07 23:00:35 +01001048static struct xfrm_policy *
1049xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1050{
1051 struct xfrm_pol_inexact_bin *bin;
1052 struct xfrm_policy *delpol;
1053 struct hlist_head *chain;
1054 struct net *net;
1055
1056 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1057 if (!bin)
1058 return ERR_PTR(-ENOMEM);
1059
Florian Westphal24969fa2018-11-07 23:00:35 +01001060 net = xp_net(policy);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001061 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1062
Florian Westphal9cf545e2018-11-07 23:00:38 +01001063 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1064 if (!chain) {
1065 __xfrm_policy_inexact_prune_bin(bin, false);
1066 return ERR_PTR(-ENOMEM);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001067 }
1068
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001069 delpol = xfrm_policy_insert_list(chain, policy, excl);
1070 if (delpol && excl) {
1071 __xfrm_policy_inexact_prune_bin(bin, false);
1072 return ERR_PTR(-EEXIST);
1073 }
1074
Florian Westphal24969fa2018-11-07 23:00:35 +01001075 chain = &net->xfrm.policy_inexact[dir];
1076 xfrm_policy_insert_inexact_list(chain, policy);
1077
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001078 if (delpol)
1079 __xfrm_policy_inexact_prune_bin(bin, false);
1080
Florian Westphal24969fa2018-11-07 23:00:35 +01001081 return delpol;
1082}
1083
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001084static void xfrm_hash_rebuild(struct work_struct *work)
1085{
1086 struct net *net = container_of(work, struct net,
1087 xfrm.policy_hthresh.work);
1088 unsigned int hmask;
1089 struct xfrm_policy *pol;
1090 struct xfrm_policy *policy;
1091 struct hlist_head *chain;
1092 struct hlist_head *odst;
1093 struct hlist_node *newpos;
1094 int i;
1095 int dir;
1096 unsigned seq;
1097 u8 lbits4, rbits4, lbits6, rbits6;
1098
1099 mutex_lock(&hash_resize_mutex);
1100
1101 /* read selector prefixlen thresholds */
1102 do {
1103 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1104
1105 lbits4 = net->xfrm.policy_hthresh.lbits4;
1106 rbits4 = net->xfrm.policy_hthresh.rbits4;
1107 lbits6 = net->xfrm.policy_hthresh.lbits6;
1108 rbits6 = net->xfrm.policy_hthresh.rbits6;
1109 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1110
Florian Westphal9d0380d2016-08-11 15:17:59 +02001111 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001112
Florian Westphal24969fa2018-11-07 23:00:35 +01001113 /* make sure that we can insert the indirect policies again before
1114 * we start with destructive action.
1115 */
1116 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001117 struct xfrm_pol_inexact_bin *bin;
Florian Westphal24969fa2018-11-07 23:00:35 +01001118 u8 dbits, sbits;
1119
1120 dir = xfrm_policy_id2dir(policy->index);
1121 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1122 continue;
1123
1124 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1125 if (policy->family == AF_INET) {
1126 dbits = rbits4;
1127 sbits = lbits4;
1128 } else {
1129 dbits = rbits6;
1130 sbits = lbits6;
1131 }
1132 } else {
1133 if (policy->family == AF_INET) {
1134 dbits = lbits4;
1135 sbits = rbits4;
1136 } else {
1137 dbits = lbits6;
1138 sbits = rbits6;
1139 }
1140 }
1141
1142 if (policy->selector.prefixlen_d < dbits ||
1143 policy->selector.prefixlen_s < sbits)
1144 continue;
1145
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001146 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1147 if (!bin)
Florian Westphal24969fa2018-11-07 23:00:35 +01001148 goto out_unlock;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001149
1150 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1151 goto out_unlock;
Florian Westphal24969fa2018-11-07 23:00:35 +01001152 }
1153
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001154 /* reset the bydst and inexact table in all directions */
Florian Westphal24969fa2018-11-07 23:00:35 +01001155 xfrm_hash_reset_inexact_table(net);
1156
Herbert Xu53c2e282014-11-13 17:09:49 +08001157 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001158 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
1159 hmask = net->xfrm.policy_bydst[dir].hmask;
1160 odst = net->xfrm.policy_bydst[dir].table;
1161 for (i = hmask; i >= 0; i--)
1162 INIT_HLIST_HEAD(odst + i);
1163 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1164 /* dir out => dst = remote, src = local */
1165 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1166 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1167 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1168 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1169 } else {
1170 /* dir in/fwd => dst = local, src = remote */
1171 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1172 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1173 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1174 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1175 }
1176 }
1177
1178 /* re-insert all policies by order of creation */
1179 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal862591b2017-12-27 23:25:45 +01001180 if (policy->walk.dead ||
1181 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +02001182 /* skip socket policies */
1183 continue;
1184 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001185 newpos = NULL;
1186 chain = policy_hash_bysel(net, &policy->selector,
1187 policy->family,
1188 xfrm_policy_id2dir(policy->index));
Florian Westphal24969fa2018-11-07 23:00:35 +01001189 if (!chain) {
1190 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1191
1192 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1193 continue;
1194 }
1195
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001196 hlist_for_each_entry(pol, chain, bydst) {
1197 if (policy->priority >= pol->priority)
1198 newpos = &pol->bydst;
1199 else
1200 break;
1201 }
1202 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +02001203 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001204 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001205 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001206 }
1207
Florian Westphal24969fa2018-11-07 23:00:35 +01001208out_unlock:
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001209 __xfrm_policy_inexact_flush(net);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001210 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001211
1212 mutex_unlock(&hash_resize_mutex);
1213}
1214
1215void xfrm_policy_hash_rebuild(struct net *net)
1216{
1217 schedule_work(&net->xfrm.policy_hthresh.work);
1218}
1219EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/* Generate new index... KAME seems to generate them ordered by cost
1222 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +08001223static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 static u32 idx_generator;
1226
1227 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001228 struct hlist_head *list;
1229 struct xfrm_policy *p;
1230 u32 idx;
1231 int found;
1232
Fan Due682adf02013-11-07 17:47:48 +08001233 if (!index) {
1234 idx = (idx_generator | dir);
1235 idx_generator += 8;
1236 } else {
1237 idx = index;
1238 index = 0;
1239 }
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (idx == 0)
1242 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -08001243 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -07001244 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001245 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001246 if (p->index == idx) {
1247 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
David S. Miller2518c7c2006-08-24 04:45:07 -07001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001251 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return idx;
1253 }
1254}
1255
David S. Miller2518c7c2006-08-24 04:45:07 -07001256static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1257{
1258 u32 *p1 = (u32 *) s1;
1259 u32 *p2 = (u32 *) s2;
1260 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1261 int i;
1262
1263 for (i = 0; i < len; i++) {
1264 if (p1[i] != p2[i])
1265 return 1;
1266 }
1267
1268 return 0;
1269}
1270
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001271static void xfrm_policy_requeue(struct xfrm_policy *old,
1272 struct xfrm_policy *new)
1273{
1274 struct xfrm_policy_queue *pq = &old->polq;
1275 struct sk_buff_head list;
1276
Li RongQingde2ad482015-04-30 17:25:19 +08001277 if (skb_queue_empty(&pq->hold_queue))
1278 return;
1279
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001280 __skb_queue_head_init(&list);
1281
1282 spin_lock_bh(&pq->hold_queue.lock);
1283 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001284 if (del_timer(&pq->hold_timer))
1285 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001286 spin_unlock_bh(&pq->hold_queue.lock);
1287
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001288 pq = &new->polq;
1289
1290 spin_lock_bh(&pq->hold_queue.lock);
1291 skb_queue_splice(&list, &pq->hold_queue);
1292 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001293 if (!mod_timer(&pq->hold_timer, jiffies))
1294 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001295 spin_unlock_bh(&pq->hold_queue.lock);
1296}
1297
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001298static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1299 struct xfrm_policy *pol)
1300{
1301 u32 mark = policy->mark.v & policy->mark.m;
1302
1303 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1304 return true;
1305
1306 if ((mark & pol->mark.m) == pol->mark.v &&
1307 policy->priority == pol->priority)
1308 return true;
1309
1310 return false;
1311}
1312
Florian Westphal24969fa2018-11-07 23:00:35 +01001313static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1314{
1315 const struct xfrm_pol_inexact_key *k = data;
1316 u32 a = k->type << 24 | k->dir << 16 | k->family;
1317
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001318 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1319 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +01001320}
1321
1322static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1323{
1324 const struct xfrm_pol_inexact_bin *b = data;
1325
1326 return xfrm_pol_bin_key(&b->k, 0, seed);
1327}
1328
1329static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1330 const void *ptr)
1331{
1332 const struct xfrm_pol_inexact_key *key = arg->key;
1333 const struct xfrm_pol_inexact_bin *b = ptr;
1334 int ret;
1335
1336 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1337 return -1;
1338
1339 ret = b->k.dir ^ key->dir;
1340 if (ret)
1341 return ret;
1342
1343 ret = b->k.type ^ key->type;
1344 if (ret)
1345 return ret;
1346
1347 ret = b->k.family ^ key->family;
1348 if (ret)
1349 return ret;
1350
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001351 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +01001352}
1353
1354static const struct rhashtable_params xfrm_pol_inexact_params = {
1355 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1356 .hashfn = xfrm_pol_bin_key,
1357 .obj_hashfn = xfrm_pol_bin_obj,
1358 .obj_cmpfn = xfrm_pol_bin_cmp,
1359 .automatic_shrinking = true,
1360};
1361
1362static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1363 struct xfrm_policy *policy)
1364{
1365 struct xfrm_policy *pol, *delpol = NULL;
1366 struct hlist_node *newpos = NULL;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001367 int i = 0;
Florian Westphal24969fa2018-11-07 23:00:35 +01001368
1369 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1370 if (pol->type == policy->type &&
1371 pol->if_id == policy->if_id &&
1372 !selector_cmp(&pol->selector, &policy->selector) &&
1373 xfrm_policy_mark_match(policy, pol) &&
1374 xfrm_sec_ctx_match(pol->security, policy->security) &&
1375 !WARN_ON(delpol)) {
1376 delpol = pol;
1377 if (policy->priority > pol->priority)
1378 continue;
1379 } else if (policy->priority >= pol->priority) {
1380 newpos = &pol->bydst_inexact_list;
1381 continue;
1382 }
1383 if (delpol)
1384 break;
1385 }
1386
1387 if (newpos)
1388 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1389 else
1390 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001391
1392 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1393 pol->pos = i;
1394 i++;
1395 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001396}
1397
Florian Westphala927d6af2018-11-07 23:00:33 +01001398static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1399 struct xfrm_policy *policy,
1400 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Florian Westphala927d6af2018-11-07 23:00:33 +01001402 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Sasha Levinb67bfe02013-02-27 17:06:00 -08001404 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001405 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001406 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001407 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001408 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001409 xfrm_sec_ctx_match(pol->security, policy->security) &&
1410 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001411 if (excl)
1412 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 delpol = pol;
1414 if (policy->priority > pol->priority)
1415 continue;
1416 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001417 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 continue;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (delpol)
1421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001425 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001426 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001427 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001428
1429 return delpol;
1430}
1431
1432int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1433{
1434 struct net *net = xp_net(policy);
1435 struct xfrm_policy *delpol;
1436 struct hlist_head *chain;
1437
1438 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1439 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001440 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001441 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001442 else
1443 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001444
1445 if (IS_ERR(delpol)) {
1446 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1447 return PTR_ERR(delpol);
1448 }
1449
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001450 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001451
1452 /* After previous checking, family can either be AF_INET or AF_INET6 */
1453 if (policy->family == AF_INET)
1454 rt_genid_bump_ipv4(net);
1455 else
1456 rt_genid_bump_ipv6(net);
1457
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001458 if (delpol) {
1459 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001460 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001461 }
Fan Due682adf02013-11-07 17:47:48 +08001462 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001463 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001464 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 policy->curlft.use_time = 0;
1466 if (!mod_timer(&policy->timer, jiffies + HZ))
1467 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001468 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
David S. Miller9b78a822005-12-22 07:39:48 -08001470 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001472 else if (xfrm_bydst_should_resize(net, dir, NULL))
1473 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return 0;
1476}
1477EXPORT_SYMBOL(xfrm_policy_insert);
1478
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001479static struct xfrm_policy *
1480__xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1481 u8 type, int dir,
1482 struct xfrm_selector *sel,
1483 struct xfrm_sec_ctx *ctx)
1484{
1485 struct xfrm_policy *pol;
1486
1487 if (!chain)
1488 return NULL;
1489
1490 hlist_for_each_entry(pol, chain, bydst) {
1491 if (pol->type == type &&
1492 pol->if_id == if_id &&
1493 (mark & pol->mark.m) == pol->mark.v &&
1494 !selector_cmp(sel, &pol->selector) &&
1495 xfrm_sec_ctx_match(ctx, pol->security))
1496 return pol;
1497 }
1498
1499 return NULL;
1500}
1501
Steffen Klassert7e652642018-06-12 14:07:07 +02001502struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1503 u8 type, int dir,
1504 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001505 struct xfrm_sec_ctx *ctx, int delete,
1506 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Florian Westphal24969fa2018-11-07 23:00:35 +01001508 struct xfrm_pol_inexact_bin *bin = NULL;
1509 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001510 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Eric Parisef41aaa2007-03-07 15:37:58 -08001512 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001513 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001514 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001515 if (!chain) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001516 struct xfrm_pol_inexact_candidates cand;
1517 int i;
1518
Florian Westphal24969fa2018-11-07 23:00:35 +01001519 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001520 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001521 if (!bin) {
1522 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1523 return NULL;
1524 }
1525
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001526 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1527 &sel->saddr,
1528 &sel->daddr)) {
1529 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1530 return NULL;
1531 }
1532
1533 pol = NULL;
1534 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1535 struct xfrm_policy *tmp;
1536
1537 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1538 if_id, type, dir,
1539 sel, ctx);
1540 if (tmp && pol && tmp->pos < pol->pos)
1541 pol = tmp;
1542 }
1543 } else {
1544 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1545 sel, ctx);
Florian Westphal24969fa2018-11-07 23:00:35 +01001546 }
1547
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001548 if (pol) {
1549 xfrm_pol_hold(pol);
1550 if (delete) {
1551 *err = security_xfrm_policy_delete(pol->security);
1552 if (*err) {
1553 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1554 return pol;
David S. Miller2518c7c2006-08-24 04:45:07 -07001555 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001556 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001558 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001560 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001562 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001563 xfrm_policy_kill(ret);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001564 if (bin && delete)
1565 xfrm_policy_inexact_prune_bin(bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001566 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
Trent Jaegerdf718372005-12-13 23:12:27 -08001568EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Steffen Klassert7e652642018-06-12 14:07:07 +02001570struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1571 u8 type, int dir, u32 id, int delete,
1572 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
David S. Miller2518c7c2006-08-24 04:45:07 -07001574 struct xfrm_policy *pol, *ret;
1575 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Herbert Xub5505c62007-05-14 02:15:47 -07001577 *err = -ENOENT;
1578 if (xfrm_policy_id2dir(id) != dir)
1579 return NULL;
1580
Eric Parisef41aaa2007-03-07 15:37:58 -08001581 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001582 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001583 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001584 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001585 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001586 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001587 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001588 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001590 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001591 *err = security_xfrm_policy_delete(
1592 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001593 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001594 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001595 return pol;
1596 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001597 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001598 }
1599 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 break;
1601 }
1602 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001603 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001605 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001606 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609EXPORT_SYMBOL(xfrm_policy_byid);
1610
Joy Latten4aa2e622007-06-04 19:05:57 -04001611#ifdef CONFIG_SECURITY_NETWORK_XFRM
1612static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001613xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Florian Westphalceb159e2018-11-07 23:00:32 +01001615 struct xfrm_policy *pol;
1616 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001617
Florian Westphalceb159e2018-11-07 23:00:32 +01001618 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1619 if (pol->walk.dead ||
1620 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1621 pol->type != type)
1622 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001623
Florian Westphalceb159e2018-11-07 23:00:32 +01001624 err = security_xfrm_policy_delete(pol->security);
1625 if (err) {
1626 xfrm_audit_policy_delete(pol, 0, task_valid);
1627 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001628 }
1629 }
1630 return err;
1631}
1632#else
1633static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001634xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001635{
1636 return 0;
1637}
1638#endif
1639
Tetsuo Handa2e710292014-04-22 21:48:30 +09001640int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001641{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001642 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001643 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Florian Westphal9d0380d2016-08-11 15:17:59 +02001645 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001646
Tetsuo Handa2e710292014-04-22 21:48:30 +09001647 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001648 if (err)
1649 goto out;
1650
Florian Westphalceb159e2018-11-07 23:00:32 +01001651again:
1652 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1653 dir = xfrm_policy_id2dir(pol->index);
1654 if (pol->walk.dead ||
1655 dir >= XFRM_POLICY_MAX ||
1656 pol->type != type)
1657 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001658
Florian Westphalceb159e2018-11-07 23:00:32 +01001659 __xfrm_policy_unlink(pol, dir);
1660 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1661 cnt++;
1662 xfrm_audit_policy_delete(pol, 1, task_valid);
1663 xfrm_policy_kill(pol);
1664 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1665 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001667 if (cnt)
1668 __xfrm_policy_inexact_flush(net);
1669 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001670 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001671out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001672 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675EXPORT_SYMBOL(xfrm_policy_flush);
1676
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001677int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001678 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 void *data)
1680{
Herbert Xu12a169e2008-10-01 07:03:24 -07001681 struct xfrm_policy *pol;
1682 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001683 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Timo Teras4c563f72008-02-28 21:31:08 -08001685 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1686 walk->type != XFRM_POLICY_TYPE_ANY)
1687 return -EINVAL;
1688
Herbert Xu12a169e2008-10-01 07:03:24 -07001689 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001690 return 0;
1691
Florian Westphal9d0380d2016-08-11 15:17:59 +02001692 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001693 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001694 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001695 else
Li RongQing80077702015-04-22 17:09:54 +08001696 x = list_first_entry(&walk->walk.all,
1697 struct xfrm_policy_walk_entry, all);
1698
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001699 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001700 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001701 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001702 pol = container_of(x, struct xfrm_policy, walk);
1703 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1704 walk->type != pol->type)
1705 continue;
1706 error = func(pol, xfrm_policy_id2dir(pol->index),
1707 walk->seq, data);
1708 if (error) {
1709 list_move_tail(&walk->walk.all, &x->all);
1710 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001711 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001712 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001714 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001715 error = -ENOENT;
1716 goto out;
1717 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001718 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001720 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return error;
1722}
1723EXPORT_SYMBOL(xfrm_policy_walk);
1724
Herbert Xu12a169e2008-10-01 07:03:24 -07001725void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1726{
1727 INIT_LIST_HEAD(&walk->walk.all);
1728 walk->walk.dead = 1;
1729 walk->type = type;
1730 walk->seq = 0;
1731}
1732EXPORT_SYMBOL(xfrm_policy_walk_init);
1733
Fan Du283bc9f2013-11-07 17:47:50 +08001734void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001735{
1736 if (list_empty(&walk->walk.all))
1737 return;
1738
Florian Westphal9d0380d2016-08-11 15:17:59 +02001739 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001740 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001741 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001742}
1743EXPORT_SYMBOL(xfrm_policy_walk_done);
1744
James Morris134b0fc2006-10-05 15:42:27 -05001745/*
1746 * Find policy to apply to this flow.
1747 *
1748 * Returns 0 if policy found, else an -errno.
1749 */
David S. Millerf299d552011-02-24 01:23:30 -05001750static int xfrm_policy_match(const struct xfrm_policy *pol,
1751 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001752 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001753{
David S. Millerf299d552011-02-24 01:23:30 -05001754 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001755 int ret = -ESRCH;
1756 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001757
1758 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001759 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001760 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001761 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001762 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001763
1764 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001765 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001766 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001767 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001768 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001769}
1770
Florian Westphal9cf545e2018-11-07 23:00:38 +01001771static struct xfrm_pol_inexact_node *
1772xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
1773 seqcount_t *count,
1774 const xfrm_address_t *addr, u16 family)
1775{
1776 const struct rb_node *parent;
1777 int seq;
1778
1779again:
1780 seq = read_seqcount_begin(count);
1781
1782 parent = rcu_dereference_raw(r->rb_node);
1783 while (parent) {
1784 struct xfrm_pol_inexact_node *node;
1785 int delta;
1786
1787 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1788
1789 delta = xfrm_policy_addr_delta(addr, &node->addr,
1790 node->prefixlen, family);
1791 if (delta < 0) {
1792 parent = rcu_dereference_raw(parent->rb_left);
1793 continue;
1794 } else if (delta > 0) {
1795 parent = rcu_dereference_raw(parent->rb_right);
1796 continue;
1797 }
1798
1799 return node;
1800 }
1801
1802 if (read_seqcount_retry(count, seq))
1803 goto again;
1804
1805 return NULL;
1806}
1807
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001808static bool
1809xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1810 struct xfrm_pol_inexact_bin *b,
1811 const xfrm_address_t *saddr,
1812 const xfrm_address_t *daddr)
1813{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001814 struct xfrm_pol_inexact_node *n;
1815 u16 family;
1816
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001817 if (!b)
1818 return false;
1819
Florian Westphal9cf545e2018-11-07 23:00:38 +01001820 family = b->k.family;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001821 memset(cand, 0, sizeof(*cand));
1822 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001823
1824 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1825 family);
1826 if (n)
1827 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
1828
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001829 return true;
1830}
1831
Florian Westphal24969fa2018-11-07 23:00:35 +01001832static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001833xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1834 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001835{
1836 struct xfrm_pol_inexact_key k = {
1837 .family = family,
1838 .type = type,
1839 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001840 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001841 };
1842
1843 write_pnet(&k.net, net);
1844
1845 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1846 xfrm_pol_inexact_params);
1847}
1848
1849static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001850xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1851 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001852{
1853 struct xfrm_pol_inexact_bin *bin;
1854
1855 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1856
1857 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001858 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001859 rcu_read_unlock();
1860
1861 return bin;
1862}
1863
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001864static struct xfrm_policy *
1865__xfrm_policy_eval_candidates(struct hlist_head *chain,
1866 struct xfrm_policy *prefer,
1867 const struct flowi *fl,
1868 u8 type, u16 family, int dir, u32 if_id)
1869{
1870 u32 priority = prefer ? prefer->priority : ~0u;
1871 struct xfrm_policy *pol;
1872
1873 if (!chain)
1874 return NULL;
1875
1876 hlist_for_each_entry_rcu(pol, chain, bydst) {
1877 int err;
1878
1879 if (pol->priority > priority)
1880 break;
1881
1882 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
1883 if (err) {
1884 if (err != -ESRCH)
1885 return ERR_PTR(err);
1886
1887 continue;
1888 }
1889
1890 if (prefer) {
1891 /* matches. Is it older than *prefer? */
1892 if (pol->priority == priority &&
1893 prefer->pos < pol->pos)
1894 return prefer;
1895 }
1896
1897 return pol;
1898 }
1899
1900 return NULL;
1901}
1902
1903static struct xfrm_policy *
1904xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
1905 struct xfrm_policy *prefer,
1906 const struct flowi *fl,
1907 u8 type, u16 family, int dir, u32 if_id)
1908{
1909 struct xfrm_policy *tmp;
1910 int i;
1911
1912 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
1913 tmp = __xfrm_policy_eval_candidates(cand->res[i],
1914 prefer,
1915 fl, type, family, dir,
1916 if_id);
1917 if (!tmp)
1918 continue;
1919
1920 if (IS_ERR(tmp))
1921 return tmp;
1922 prefer = tmp;
1923 }
1924
1925 return prefer;
1926}
1927
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001928static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001929 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001930 u16 family, u8 dir,
1931 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001933 struct xfrm_pol_inexact_candidates cand;
David S. Miller0b597e72011-02-24 01:22:48 -05001934 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01001935 struct xfrm_pol_inexact_bin *bin;
1936 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001937 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001938 unsigned int sequence;
1939 u32 priority;
Florian Westphal24969fa2018-11-07 23:00:35 +01001940 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001941
1942 daddr = xfrm_flowi_daddr(fl, family);
1943 saddr = xfrm_flowi_saddr(fl, family);
1944 if (unlikely(!daddr || !saddr))
1945 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Florian Westphala7c442472016-08-11 15:17:56 +02001947 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001948 retry:
1949 do {
1950 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1951 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1952 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1953
1954 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001955 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001956 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07001957 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05001958 if (err) {
1959 if (err == -ESRCH)
1960 continue;
1961 else {
1962 ret = ERR_PTR(err);
1963 goto fail;
1964 }
1965 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001966 ret = pol;
1967 priority = ret->priority;
1968 break;
1969 }
1970 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001971 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001972 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
1973 daddr))
Florian Westphal24969fa2018-11-07 23:00:35 +01001974 goto skip_inexact;
Li RongQing8faf4912015-05-14 11:16:59 +08001975
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001976 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
1977 family, dir, if_id);
1978 if (pol) {
1979 ret = pol;
1980 if (IS_ERR(pol))
1981 goto fail;
David S. Miller2518c7c2006-08-24 04:45:07 -07001982 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001983
Florian Westphal24969fa2018-11-07 23:00:35 +01001984skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02001985 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1986 goto retry;
1987
Florian Westphale37cc8ad2016-08-11 15:17:55 +02001988 if (ret && !xfrm_pol_hold_rcu(ret))
1989 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001990fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001991 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001992
David S. Miller2518c7c2006-08-24 04:45:07 -07001993 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001994}
1995
Benedict Wongbc56b332018-07-19 10:50:44 -07001996static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
1997 const struct flowi *fl,
1998 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00001999{
2000#ifdef CONFIG_XFRM_SUB_POLICY
2001 struct xfrm_policy *pol;
2002
Benedict Wongbc56b332018-07-19 10:50:44 -07002003 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2004 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002005 if (pol != NULL)
2006 return pol;
2007#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07002008 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2009 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002010}
2011
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002012static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07002013 const struct flowi *fl,
2014 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 struct xfrm_policy *pol;
2017
Eric Dumazetd188ba82015-12-08 07:22:02 -08002018 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02002019 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002020 pol = rcu_dereference(sk->sk_policy[dir]);
2021 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01002022 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002023 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08002024
Steffen Klassertddc47e42017-11-29 06:53:55 +01002025 if (pol->family != family) {
2026 pol = NULL;
2027 goto out;
2028 }
2029
2030 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002031 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02002032 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07002033 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002034 pol = NULL;
2035 goto out;
2036 }
Paul Moore03e1ad72008-04-12 19:07:52 -07002037 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05002038 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02002039 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01002040 if (!err) {
2041 if (!xfrm_pol_hold_rcu(pol))
2042 goto again;
2043 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002044 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01002045 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002046 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01002047 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002048 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 pol = NULL;
2050 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002051out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002052 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return pol;
2054}
2055
2056static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2057{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002058 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002059
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002060 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002061 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 xfrm_pol_hold(pol);
2063}
2064
2065static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2066 int dir)
2067{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002068 struct net *net = xp_net(pol);
2069
Herbert Xu53c2e282014-11-13 17:09:49 +08002070 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07002071 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Herbert Xu53c2e282014-11-13 17:09:49 +08002073 /* Socket policies are not hashed. */
2074 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02002075 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01002076 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08002077 hlist_del(&pol->byidx);
2078 }
2079
2080 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002081 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07002082
2083 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Herbert Xu53c2e282014-11-13 17:09:49 +08002086static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2087{
2088 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2089}
2090
2091static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2092{
2093 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2094}
2095
Herbert Xu4666faa2005-06-18 22:43:22 -07002096int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Fan Du283bc9f2013-11-07 17:47:50 +08002098 struct net *net = xp_net(pol);
2099
Florian Westphal9d0380d2016-08-11 15:17:59 +02002100 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002102 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07002105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Herbert Xu4666faa2005-06-18 22:43:22 -07002107 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
David S. Millera70fcb02006-03-20 19:18:52 -08002109EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2112{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002113 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct xfrm_policy *old_pol;
2115
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002116#ifdef CONFIG_XFRM_SUB_POLICY
2117 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2118 return -EINVAL;
2119#endif
2120
Florian Westphal9d0380d2016-08-11 15:17:59 +02002121 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08002122 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2123 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02002125 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08002126 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08002127 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08002129 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002130 if (old_pol) {
2131 if (pol)
2132 xfrm_policy_requeue(old_pol, pol);
2133
Timo Teräsea2dea92010-03-31 00:17:05 +00002134 /* Unlinking succeeds always. This is the only function
2135 * allowed to delete or replace socket policy.
2136 */
Herbert Xu53c2e282014-11-13 17:09:49 +08002137 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002138 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02002139 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (old_pol) {
2142 xfrm_policy_kill(old_pol);
2143 }
2144 return 0;
2145}
2146
David S. Millerd3e40a92011-02-24 01:25:41 -05002147static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002149 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08002150 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 if (newp) {
2153 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07002154 if (security_xfrm_policy_clone(old->security,
2155 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08002156 kfree(newp);
2157 return NULL; /* ENOMEM */
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 newp->lft = old->lft;
2160 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08002161 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02002162 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 newp->action = old->action;
2164 newp->flags = old->flags;
2165 newp->xfrm_nr = old->xfrm_nr;
2166 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002167 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11002168 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 memcpy(newp->xfrm_vec, old->xfrm_vec,
2170 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02002171 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08002172 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002173 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 xfrm_pol_put(newp);
2175 }
2176 return newp;
2177}
2178
Eric Dumazetd188ba82015-12-08 07:22:02 -08002179int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Eric Dumazetd188ba82015-12-08 07:22:02 -08002181 const struct xfrm_policy *p;
2182 struct xfrm_policy *np;
2183 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Eric Dumazetd188ba82015-12-08 07:22:02 -08002185 rcu_read_lock();
2186 for (i = 0; i < 2; i++) {
2187 p = rcu_dereference(osk->sk_policy[i]);
2188 if (p) {
2189 np = clone_policy(p, i);
2190 if (unlikely(!np)) {
2191 ret = -ENOMEM;
2192 break;
2193 }
2194 rcu_assign_pointer(sk->sk_policy[i], np);
2195 }
2196 }
2197 rcu_read_unlock();
2198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002201static int
David Ahern42a7b322015-08-10 16:58:11 -06002202xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002203 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002204{
2205 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01002206 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002207
2208 if (unlikely(afinfo == NULL))
2209 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002210 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002211 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002212 return err;
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215/* Resolve list of templates for the flow, given policy. */
2216
2217static int
David S. Millera6c2e612011-02-22 18:35:39 -08002218xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2219 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08002221 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 int nx;
2223 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01002224 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2225 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002226 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Weilong Chen9b7a7872013-12-24 09:43:46 +08002228 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01002230 xfrm_address_t *remote = daddr;
2231 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2233
Steffen Klassert94802152017-11-15 06:40:57 +01002234 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2235 tmpl->mode == XFRM_MODE_BEET) {
2236 remote = &tmpl->id.daddr;
2237 local = &tmpl->saddr;
2238 if (xfrm_addr_any(local, tmpl->encap_family)) {
2239 error = xfrm_get_saddr(net, fl->flowi_oif,
2240 &tmp, remote,
2241 tmpl->encap_family, 0);
2242 if (error)
2243 goto fail;
2244 local = &tmp;
2245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247
Benedict Wongbc56b332018-07-19 10:50:44 -07002248 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2249 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 if (x && x->km.state == XFRM_STATE_VALID) {
2252 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01002253 daddr = remote;
2254 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 continue;
2256 }
2257 if (x) {
2258 error = (x->km.state == XFRM_STATE_ERROR ?
2259 -EINVAL : -EAGAIN);
2260 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08002261 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00002262 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08002263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 if (!tmpl->optional)
2266 goto fail;
2267 }
2268 return nx;
2269
2270fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002271 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 xfrm_state_put(xfrm[nx]);
2273 return error;
2274}
2275
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002276static int
David S. Millera6c2e612011-02-22 18:35:39 -08002277xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2278 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002279{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002280 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2281 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002282 int cnx = 0;
2283 int error;
2284 int ret;
2285 int i;
2286
2287 for (i = 0; i < npols; i++) {
2288 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2289 error = -ENOBUFS;
2290 goto fail;
2291 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002292
2293 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002294 if (ret < 0) {
2295 error = ret;
2296 goto fail;
2297 } else
2298 cnx += ret;
2299 }
2300
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002301 /* found states are sorted for outbound processing */
2302 if (npols > 1)
2303 xfrm_state_sort(xfrm, tpp, cnx, family);
2304
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002305 return cnx;
2306
2307 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002308 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002309 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002310 return error;
2311
2312}
2313
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002314static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002315{
Florian Westphal37b10382017-02-07 15:00:19 +01002316 const struct xfrm_policy_afinfo *afinfo;
Xin Long143a4452018-02-17 15:16:22 +08002317 int tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08002318
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002319 afinfo = xfrm_policy_get_afinfo(family);
Xin Long143a4452018-02-17 15:16:22 +08002320 if (!afinfo)
2321 return 0;
2322
2323 tos = afinfo->get_tos(fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002324
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002325 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002326
2327 return tos;
2328}
2329
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002330static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002331{
Florian Westphal37b10382017-02-07 15:00:19 +01002332 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002333 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08002334 struct xfrm_dst *xdst;
2335
2336 if (!afinfo)
2337 return ERR_PTR(-EINVAL);
2338
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002339 switch (family) {
2340 case AF_INET:
2341 dst_ops = &net->xfrm.xfrm4_dst_ops;
2342 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002343#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002344 case AF_INET6:
2345 dst_ops = &net->xfrm.xfrm6_dst_ops;
2346 break;
2347#endif
2348 default:
2349 BUG();
2350 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07002351 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08002352
Madalin Bucurd4cae562011-09-26 07:04:36 +00002353 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00002354 struct dst_entry *dst = &xdst->u.dst;
2355
2356 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00002357 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08002358 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00002359
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002360 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00002361
Herbert Xu25ee3282007-12-11 09:32:34 -08002362 return xdst;
2363}
2364
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002365static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2366 int nfheader_len)
2367{
Florian Westphal37b10382017-02-07 15:00:19 +01002368 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002369 xfrm_policy_get_afinfo(dst->ops->family);
2370 int err;
2371
2372 if (!afinfo)
2373 return -EINVAL;
2374
2375 err = afinfo->init_path(path, dst, nfheader_len);
2376
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002377 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002378
2379 return err;
2380}
2381
Herbert Xu87c1e122010-03-02 02:51:56 +00002382static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08002383 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08002384{
Florian Westphal37b10382017-02-07 15:00:19 +01002385 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08002386 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2387 int err;
2388
2389 if (!afinfo)
2390 return -EINVAL;
2391
Herbert Xu87c1e122010-03-02 02:51:56 +00002392 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002393
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002394 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002395
2396 return err;
2397}
2398
Timo Teräs80c802f2010-04-07 00:30:05 +00002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400/* Allocate chain of dst_entry's, attach known xfrm's, calculate
2401 * all the metrics... Shortly, bundle a bundle.
2402 */
2403
Herbert Xu25ee3282007-12-11 09:32:34 -08002404static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05002405 struct xfrm_state **xfrm,
2406 struct xfrm_dst **bundle,
2407 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08002408 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08002409 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002411 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08002412 unsigned long now = jiffies;
2413 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002414 struct xfrm_mode *inner_mode;
David Miller45b018be2017-11-28 15:40:28 -05002415 struct xfrm_dst *xdst_prev = NULL;
2416 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08002417 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08002419 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002420 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002421 int trailer_len = 0;
2422 int tos;
2423 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09002424 xfrm_address_t saddr, daddr;
2425
2426 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002427
2428 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002429
2430 dst_hold(dst);
2431
2432 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002433 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002434 struct dst_entry *dst1 = &xdst->u.dst;
2435
2436 err = PTR_ERR(xdst);
2437 if (IS_ERR(xdst)) {
2438 dst_release(dst);
2439 goto put_states;
2440 }
2441
David Miller54920932017-11-28 15:41:01 -05002442 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05002443 if (!xdst_prev)
2444 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07002445 else
2446 /* Ref count is taken during xfrm_alloc_dst()
2447 * No need to do dst_clone() on dst1
2448 */
David Miller45b018be2017-11-28 15:40:28 -05002449 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07002450
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002451 if (xfrm[i]->sel.family == AF_UNSPEC) {
2452 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2453 xfrm_af2proto(family));
2454 if (!inner_mode) {
2455 err = -EAFNOSUPPORT;
2456 dst_release(dst);
2457 goto put_states;
2458 }
2459 } else
2460 inner_mode = xfrm[i]->inner_mode;
2461
Herbert Xu25ee3282007-12-11 09:32:34 -08002462 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08002463 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08002464
2465 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002466 __u32 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
2467
Herbert Xu25ee3282007-12-11 09:32:34 -08002468 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06002469 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002470 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08002471 err = PTR_ERR(dst);
2472 if (IS_ERR(dst))
2473 goto put_states;
2474 } else
2475 dst_hold(dst);
2476
2477 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00002478 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08002479
David S. Millerf5b0a872012-07-19 12:31:33 -07002480 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08002481 dst1->flags |= DST_HOST;
2482 dst1->lastuse = now;
2483
2484 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002485 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08002486
David Miller45b018be2017-11-28 15:40:28 -05002487 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002488
2489 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002490 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2491 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002492 trailer_len += xfrm[i]->props.trailer_len;
2493 }
2494
David Miller45b018be2017-11-28 15:40:28 -05002495 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002496 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002497
2498 err = -ENODEV;
2499 dev = dst->dev;
2500 if (!dev)
2501 goto free_dst;
2502
David Miller45b018be2017-11-28 15:40:28 -05002503 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002504 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002505
David Miller45b018be2017-11-28 15:40:28 -05002506 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2507 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2508 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002509 if (err)
2510 goto free_dst;
2511
David Miller45b018be2017-11-28 15:40:28 -05002512 xdst_prev->u.dst.header_len = header_len;
2513 xdst_prev->u.dst.trailer_len = trailer_len;
2514 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2515 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002516 }
2517
David Miller45b018be2017-11-28 15:40:28 -05002518 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002519
2520put_states:
2521 for (; i < nx; i++)
2522 xfrm_state_put(xfrm[i]);
2523free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002524 if (xdst0)
2525 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002526
2527 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
David S. Miller73ff93c2011-02-22 18:33:42 -08002530static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002531 struct xfrm_policy **pols,
2532 int *num_pols, int *num_xfrms)
2533{
2534 int i;
2535
2536 if (*num_pols == 0 || !pols[0]) {
2537 *num_pols = 0;
2538 *num_xfrms = 0;
2539 return 0;
2540 }
2541 if (IS_ERR(pols[0]))
2542 return PTR_ERR(pols[0]);
2543
2544 *num_xfrms = pols[0]->xfrm_nr;
2545
2546#ifdef CONFIG_XFRM_SUB_POLICY
2547 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2548 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2549 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2550 XFRM_POLICY_TYPE_MAIN,
2551 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002552 XFRM_POLICY_OUT,
2553 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002554 if (pols[1]) {
2555 if (IS_ERR(pols[1])) {
2556 xfrm_pols_put(pols, *num_pols);
2557 return PTR_ERR(pols[1]);
2558 }
Weilong Chen02d08922013-12-24 09:43:48 +08002559 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002560 (*num_xfrms) += pols[1]->xfrm_nr;
2561 }
2562 }
2563#endif
2564 for (i = 0; i < *num_pols; i++) {
2565 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2566 *num_xfrms = -1;
2567 break;
2568 }
2569 }
2570
2571 return 0;
2572
2573}
2574
2575static struct xfrm_dst *
2576xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002577 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002578 struct dst_entry *dst_orig)
2579{
2580 struct net *net = xp_net(pols[0]);
2581 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002582 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002583 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002584 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002585 int err;
2586
2587 /* Try to instantiate a bundle */
2588 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002589 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002590 if (err == 0)
2591 return NULL;
2592
2593 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002594 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2595 return ERR_PTR(err);
2596 }
2597
David Miller54920932017-11-28 15:41:01 -05002598 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002599 if (IS_ERR(dst)) {
2600 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2601 return ERR_CAST(dst);
2602 }
2603
2604 xdst = (struct xfrm_dst *)dst;
2605 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002606 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002607 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002608 xdst->policy_genid = atomic_read(&pols[0]->genid);
2609
2610 return xdst;
2611}
2612
Kees Cookc3aed702017-10-16 17:28:56 -07002613static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002614{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002615 struct sk_buff *skb;
2616 struct sock *sk;
2617 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002618 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002619 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002620 struct xfrm_policy_queue *pq = &pol->polq;
2621 struct flowi fl;
2622 struct sk_buff_head list;
2623
2624 spin_lock(&pq->hold_queue.lock);
2625 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002626 if (!skb) {
2627 spin_unlock(&pq->hold_queue.lock);
2628 goto out;
2629 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002630 dst = skb_dst(skb);
2631 sk = skb->sk;
2632 xfrm_decode_session(skb, &fl, dst->ops->family);
2633 spin_unlock(&pq->hold_queue.lock);
2634
David Miller0f6c4802017-11-28 15:40:46 -05002635 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c982018-02-01 11:26:12 +01002636 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002637 if (IS_ERR(dst))
2638 goto purge_queue;
2639
2640 if (dst->flags & DST_XFRM_QUEUE) {
2641 dst_release(dst);
2642
2643 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2644 goto purge_queue;
2645
2646 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002647 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2648 xfrm_pol_hold(pol);
2649 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002650 }
2651
2652 dst_release(dst);
2653
2654 __skb_queue_head_init(&list);
2655
2656 spin_lock(&pq->hold_queue.lock);
2657 pq->timeout = 0;
2658 skb_queue_splice_init(&pq->hold_queue, &list);
2659 spin_unlock(&pq->hold_queue.lock);
2660
2661 while (!skb_queue_empty(&list)) {
2662 skb = __skb_dequeue(&list);
2663
2664 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002665 dst_hold(xfrm_dst_path(skb_dst(skb)));
2666 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002667 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002668 kfree_skb(skb);
2669 continue;
2670 }
2671
2672 nf_reset(skb);
2673 skb_dst_drop(skb);
2674 skb_dst_set(skb, dst);
2675
Eric W. Biederman13206b62015-10-07 16:48:35 -05002676 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002677 }
2678
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002679out:
2680 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002681 return;
2682
2683purge_queue:
2684 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002685 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002686 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002687}
2688
Eric W. Biedermanede20592015-10-07 16:48:47 -05002689static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002690{
2691 unsigned long sched_next;
2692 struct dst_entry *dst = skb_dst(skb);
2693 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002694 struct xfrm_policy *pol = xdst->pols[0];
2695 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002696
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002697 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002698 kfree_skb(skb);
2699 return 0;
2700 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002701
2702 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2703 kfree_skb(skb);
2704 return -EAGAIN;
2705 }
2706
2707 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002708
2709 spin_lock_bh(&pq->hold_queue.lock);
2710
2711 if (!pq->timeout)
2712 pq->timeout = XFRM_QUEUE_TMO_MIN;
2713
2714 sched_next = jiffies + pq->timeout;
2715
2716 if (del_timer(&pq->hold_timer)) {
2717 if (time_before(pq->hold_timer.expires, sched_next))
2718 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002719 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002720 }
2721
2722 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002723 if (!mod_timer(&pq->hold_timer, sched_next))
2724 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002725
2726 spin_unlock_bh(&pq->hold_queue.lock);
2727
2728 return 0;
2729}
2730
2731static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002732 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002733 const struct flowi *fl,
2734 int num_xfrms,
2735 u16 family)
2736{
2737 int err;
2738 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002739 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002740 struct dst_entry *dst1;
2741 struct xfrm_dst *xdst;
2742
2743 xdst = xfrm_alloc_dst(net, family);
2744 if (IS_ERR(xdst))
2745 return xdst;
2746
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002747 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2748 net->xfrm.sysctl_larval_drop ||
2749 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002750 return xdst;
2751
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002752 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002753 dst1 = &xdst->u.dst;
2754 dst_hold(dst);
2755 xdst->route = dst;
2756
2757 dst_copy_metrics(dst1, dst);
2758
2759 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2760 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2761 dst1->lastuse = jiffies;
2762
2763 dst1->input = dst_discard;
2764 dst1->output = xdst_queue_output;
2765
2766 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002767 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002768 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002769
2770 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2771
2772 err = -ENODEV;
2773 dev = dst->dev;
2774 if (!dev)
2775 goto free_dst;
2776
2777 err = xfrm_fill_dst(xdst, dev, fl);
2778 if (err)
2779 goto free_dst;
2780
2781out:
2782 return xdst;
2783
2784free_dst:
2785 dst_release(dst1);
2786 xdst = ERR_PTR(err);
2787 goto out;
2788}
2789
Benedict Wongbc56b332018-07-19 10:50:44 -07002790static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2791 const struct flowi *fl,
2792 u16 family, u8 dir,
2793 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002794{
Timo Teräs80c802f2010-04-07 00:30:05 +00002795 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002796 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002797 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002798
Timo Teräs80c802f2010-04-07 00:30:05 +00002799 /* Resolve policies to use if we couldn't get them from
2800 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002801 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002802 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002803 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002804 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002805 if (err < 0)
2806 goto inc_error;
2807 if (num_pols == 0)
2808 return NULL;
2809 if (num_xfrms <= 0)
2810 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002811
Florian Westphalbd45c532017-07-17 13:57:25 +02002812 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002813 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002814 if (IS_ERR(xdst)) {
2815 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002816 if (err == -EREMOTE) {
2817 xfrm_pols_put(pols, num_pols);
2818 return NULL;
2819 }
2820
Timo Teräs80c802f2010-04-07 00:30:05 +00002821 if (err != -EAGAIN)
2822 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002823 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002824 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002825 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002826 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002827 }
2828
Florian Westphalbd45c532017-07-17 13:57:25 +02002829 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002830
2831make_dummy_bundle:
2832 /* We found policies, but there's no bundles to instantiate:
2833 * either because the policy blocks, has no transformations or
2834 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002835 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002836 if (IS_ERR(xdst)) {
2837 xfrm_pols_put(pols, num_pols);
2838 return ERR_CAST(xdst);
2839 }
2840 xdst->num_pols = num_pols;
2841 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002842 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002843
Florian Westphalbd45c532017-07-17 13:57:25 +02002844 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002845
2846inc_error:
2847 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2848error:
Florian Westphal855dad92017-07-17 13:57:22 +02002849 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002850 return ERR_PTR(err);
2851}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
David S. Miller2774c132011-03-01 14:59:04 -08002853static struct dst_entry *make_blackhole(struct net *net, u16 family,
2854 struct dst_entry *dst_orig)
2855{
Florian Westphal37b10382017-02-07 15:00:19 +01002856 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002857 struct dst_entry *ret;
2858
2859 if (!afinfo) {
2860 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002861 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002862 } else {
2863 ret = afinfo->blackhole_route(net, dst_orig);
2864 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002865 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08002866
2867 return ret;
2868}
2869
Benedict Wongbc56b332018-07-19 10:50:44 -07002870/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 *
2872 * At the moment we eat a raw IP route. Mostly to speed up lookups
2873 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07002874 *
2875 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2876 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
Benedict Wongbc56b332018-07-19 10:50:44 -07002878struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
2879 struct dst_entry *dst_orig,
2880 const struct flowi *fl,
2881 const struct sock *sk,
2882 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002884 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002885 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002886 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002887 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02002888 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00002889 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002890
Timo Teräs80c802f2010-04-07 00:30:05 +00002891 dst = NULL;
2892 xdst = NULL;
2893 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002894
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002895 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002896 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002897 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002898 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
2899 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002900 err = xfrm_expand_policies(fl, family, pols,
2901 &num_pols, &num_xfrms);
2902 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002903 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002904
2905 if (num_pols) {
2906 if (num_xfrms <= 0) {
2907 drop_pols = num_pols;
2908 goto no_transform;
2909 }
2910
2911 xdst = xfrm_resolve_and_create_bundle(
2912 pols, num_pols, fl,
2913 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01002914
Timo Teräs80c802f2010-04-07 00:30:05 +00002915 if (IS_ERR(xdst)) {
2916 xfrm_pols_put(pols, num_pols);
2917 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002918 if (err == -EREMOTE)
2919 goto nopol;
2920
Timo Teräs80c802f2010-04-07 00:30:05 +00002921 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002922 } else if (xdst == NULL) {
2923 num_xfrms = 0;
2924 drop_pols = num_pols;
2925 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002926 }
2927
Timo Teräs80c802f2010-04-07 00:30:05 +00002928 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002929 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Timo Teräs80c802f2010-04-07 00:30:05 +00002932 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002933 struct xfrm_flo xflo;
2934
2935 xflo.dst_orig = dst_orig;
2936 xflo.flags = flags;
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002939 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002940 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002941 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Benedict Wongbc56b332018-07-19 10:50:44 -07002943 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02002944 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00002945 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02002946 if (IS_ERR(xdst)) {
2947 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002948 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002949 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002950
2951 num_pols = xdst->num_pols;
2952 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002953 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002954 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
Timo Teräs80c802f2010-04-07 00:30:05 +00002957 dst = &xdst->u.dst;
2958 if (route == NULL && num_xfrms > 0) {
2959 /* The only case when xfrm_bundle_lookup() returns a
2960 * bundle with null route, is when the template could
2961 * not be resolved. It means policies are there, but
2962 * bundle could not be created, since we don't yet
2963 * have the xfrm_state's. We need to wait for KM to
2964 * negotiate new SA's or bail out with error.*/
2965 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002966 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002967 err = -EREMOTE;
2968 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002969 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002970
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002971 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002972
2973 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2974 goto error;
2975 }
2976
2977no_transform:
2978 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002979 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Timo Teräs80c802f2010-04-07 00:30:05 +00002981 if ((flags & XFRM_LOOKUP_ICMP) &&
2982 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2983 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002984 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002985 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002986
Timo Teräs80c802f2010-04-07 00:30:05 +00002987 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02002988 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002989
Timo Teräs80c802f2010-04-07 00:30:05 +00002990 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002992 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002993 err = -EPERM;
2994 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002995 } else if (num_xfrms > 0) {
2996 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002997 dst_release(dst_orig);
2998 } else {
2999 /* Flow passes untransformed */
3000 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003001 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003003ok:
3004 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003005 if (dst && dst->xfrm &&
3006 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3007 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003008 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Timo Teräs80c802f2010-04-07 00:30:05 +00003010nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003011 if (!(flags & XFRM_LOOKUP_ICMP)) {
3012 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003013 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003014 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003015 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003017 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003018dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003019 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3020 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003021 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003022 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
Benedict Wongbc56b332018-07-19 10:50:44 -07003024EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3025
3026/* Main function: finds/creates a bundle for given flow.
3027 *
3028 * At the moment we eat a raw IP route. Mostly to speed up lookups
3029 * on interfaces with disabled IPsec.
3030 */
3031struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3032 const struct flowi *fl, const struct sock *sk,
3033 int flags)
3034{
3035 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037EXPORT_SYMBOL(xfrm_lookup);
3038
Steffen Klassertf92ee612014-09-16 10:08:40 +02003039/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3040 * Otherwise we may send out blackholed packets.
3041 */
3042struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3043 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003044 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003045{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003046 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003047 flags | XFRM_LOOKUP_QUEUE |
3048 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003049
3050 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
3051 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3052
Tommi Rantala8cc88772018-06-21 09:30:47 +03003053 if (IS_ERR(dst))
3054 dst_release(dst_orig);
3055
Steffen Klassertf92ee612014-09-16 10:08:40 +02003056 return dst;
3057}
3058EXPORT_SYMBOL(xfrm_lookup_route);
3059
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003060static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003061xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003062{
3063 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003064
3065 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
3066 return 0;
3067 x = skb->sp->xvec[idx];
3068 if (!x->type->reject)
3069 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003070 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073/* When skb is transformed back to its "native" form, we have to
3074 * check policy restrictions. At the moment we make this in maximally
3075 * stupid way. Shame on me. :-) Of course, connected sockets must
3076 * have policy cached at them.
3077 */
3078
3079static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003080xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 unsigned short family)
3082{
3083 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003084 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return x->id.proto == tmpl->id.proto &&
3086 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3087 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3088 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003089 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003090 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003091 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3092 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003095/*
3096 * 0 or more than 0 is returned when validation is succeeded (either bypass
3097 * because of optional transport mode, or next index of the mathced secpath
3098 * state with the template.
3099 * -1 is returned when no matching template is found.
3100 * Otherwise "-2 - errored_index" is returned.
3101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003103xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 unsigned short family)
3105{
3106 int idx = start;
3107
3108 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003109 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return start;
3111 } else
3112 start = -1;
3113 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003114 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003116 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3117 if (start == -1)
3118 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
3122 return start;
3123}
3124
Herbert Xud5422ef2007-12-12 10:44:16 -08003125int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3126 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Florian Westphal37b10382017-02-07 15:00:19 +01003128 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003129 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 if (unlikely(afinfo == NULL))
3132 return -EAFNOSUPPORT;
3133
Herbert Xud5422ef2007-12-12 10:44:16 -08003134 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf203b762018-06-12 14:07:12 +02003135
David S. Miller1d28f422011-03-12 00:29:39 -05003136 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003137 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
Herbert Xud5422ef2007-12-12 10:44:16 -08003140EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
David S. Miller9a7386e2011-02-24 01:44:12 -05003142static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003145 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003146 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
3150
3151 return 0;
3152}
3153
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003154int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 unsigned short family)
3156{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003157 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003159 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3160 int npols = 0;
3161 int xfrm_nr;
3162 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003163 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003165 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003166 const struct xfrm_if_cb *ifcb;
3167 struct xfrm_if *xi;
3168 u32 if_id = 0;
3169
3170 rcu_read_lock();
3171 ifcb = xfrm_if_get_cb();
3172
3173 if (ifcb) {
3174 xi = ifcb->decode_session(skb);
3175 if (xi)
3176 if_id = xi->p.if_id;
3177 }
3178 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Herbert Xud5422ef2007-12-12 10:44:16 -08003180 reverse = dir & ~XFRM_POLICY_MASK;
3181 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003182
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003183 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003184 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003186 }
3187
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003188 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* First, check used SA against their selectors. */
3191 if (skb->sp) {
3192 int i;
3193
Weilong Chen9b7a7872013-12-24 09:43:46 +08003194 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003195 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003196 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003197 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201 }
3202
3203 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003204 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003205 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003206 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003207 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003208 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003209 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003210 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003213 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003214 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003216 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003217 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003218 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003219 }
James Morris134b0fc2006-10-05 15:42:27 -05003220
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003221 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003222 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003223 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003224 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003225 return 0;
3226 }
3227 return 1;
3228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Arnd Bergmann386c5682018-07-11 12:19:13 +02003230 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003232 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003233 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003234#ifdef CONFIG_XFRM_SUB_POLICY
3235 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003236 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003237 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003238 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003239 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003240 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003241 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003242 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003243 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003244 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003245 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003246 }
3247 }
3248#endif
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (pol->action == XFRM_POLICY_ALLOW) {
3251 struct sec_path *sp;
3252 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003253 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003254 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003255 struct xfrm_tmpl **tpp = tp;
3256 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 int i, k;
3258
3259 if ((sp = skb->sp) == NULL)
3260 sp = &dummy;
3261
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003262 for (pi = 0; pi < npols; pi++) {
3263 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003264 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003265 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003266 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003267 }
3268 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003269 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003270 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003271 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003272 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3273 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3274 }
3275 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003276 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08003277 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003278 tpp = stp;
3279 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 /* For each tunnel xfrm, find the first matching tmpl.
3282 * For each tmpl before that, find corresponding xfrm.
3283 * Order is _important_. Later we will implement
3284 * some barriers, but at the moment barriers
3285 * are implied between each two transformations.
3286 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003287 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3288 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003289 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003290 if (k < -1)
3291 /* "-2 - errored_index" returned */
3292 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003293 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 }
3297
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003298 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003299 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003303 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 1;
3305 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003306 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003309 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003310reject_error:
3311 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 return 0;
3313}
3314EXPORT_SYMBOL(__xfrm_policy_check);
3315
3316int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3317{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003318 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003320 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003321 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003323 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003324 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003328 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02003329 if (!skb_dst(skb)) {
3330 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3331 return 0;
3332 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003333
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003334 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003335 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003336 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003337 dst = NULL;
3338 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003339 skb_dst_set(skb, dst);
3340 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342EXPORT_SYMBOL(__xfrm_route_forward);
3343
David S. Millerd49c73c2006-08-13 18:55:53 -07003344/* Optimize later using cookies and generation ids. */
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3347{
David S. Millerd49c73c2006-08-13 18:55:53 -07003348 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003349 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3350 * get validated by dst_ops->check on every use. We do this
3351 * because when a normal route referenced by an XFRM dst is
3352 * obsoleted we do not go looking around for all parent
3353 * referencing XFRM dsts so that we can invalidate them. It
3354 * is just too much work. Instead we make the checks here on
3355 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003356 *
3357 * XFRM dst A --> IPv4 dst X
3358 *
3359 * X is the "xdst->route" of A (X is also the "dst->path" of A
3360 * in this example). If X is marked obsolete, "A" will not
3361 * notice. That's what we are validating here via the
3362 * stale_bundle() check.
3363 *
Wei Wang52df1572017-06-17 10:42:38 -07003364 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3365 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003366 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003367 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003368 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003369 if (dst->obsolete < 0 && !stale_bundle(dst))
3370 return dst;
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return NULL;
3373}
3374
3375static int stale_bundle(struct dst_entry *dst)
3376{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003377 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
Herbert Xuaabc9762005-05-03 16:27:10 -07003380void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
David Millerb92cf4a2017-11-28 15:40:22 -05003382 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003383 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003384 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 dev_put(dev);
3386 }
3387}
Herbert Xuaabc9762005-05-03 16:27:10 -07003388EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390static void xfrm_link_failure(struct sk_buff *skb)
3391{
3392 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393}
3394
3395static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3396{
3397 if (dst) {
3398 if (dst->obsolete) {
3399 dst_release(dst);
3400 dst = NULL;
3401 }
3402 }
3403 return dst;
3404}
3405
David Miller54920932017-11-28 15:41:01 -05003406static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
David Miller54920932017-11-28 15:41:01 -05003408 while (nr--) {
3409 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003411 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
David Miller54920932017-11-28 15:41:01 -05003413 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003414 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 xdst->child_mtu_cached = pmtu;
3416
3417 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3418
3419 route_mtu_cached = dst_mtu(xdst->route);
3420 xdst->route_mtu_cached = route_mtu_cached;
3421
3422 if (pmtu > route_mtu_cached)
3423 pmtu = route_mtu_cached;
3424
David S. Millerdefb3512010-12-08 21:16:57 -08003425 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429/* Check that the bundle accepts the flow and its components are
3430 * still valid.
3431 */
3432
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003433static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
David Miller54920932017-11-28 15:41:01 -05003435 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003437 struct xfrm_dst *xdst;
3438 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 u32 mtu;
3440
David Miller0f6c4802017-11-28 15:40:46 -05003441 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 (dst->dev && !netif_running(dst->dev)))
3443 return 0;
3444
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003445 if (dst->flags & DST_XFRM_QUEUE)
3446 return 1;
3447
David Miller54920932017-11-28 15:41:01 -05003448 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 do {
3450 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3453 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003454 if (xdst->xfrm_genid != dst->xfrm->genid)
3455 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003456 if (xdst->num_pols > 0 &&
3457 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
David Miller54920932017-11-28 15:41:01 -05003460 bundle[nr++] = xdst;
3461
David Millerb92cf4a2017-11-28 15:40:22 -05003462 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003464 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 xdst->child_mtu_cached = mtu;
3466 }
3467
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003468 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 return 0;
3470 mtu = dst_mtu(xdst->route);
3471 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003472 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 xdst->route_mtu_cached = mtu;
3474 }
3475
David Millerb92cf4a2017-11-28 15:40:22 -05003476 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 } while (dst->xfrm);
3478
David Miller54920932017-11-28 15:41:01 -05003479 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return 1;
3481
David Miller54920932017-11-28 15:41:01 -05003482 xdst = bundle[start_from - 1];
3483 mtu = xdst->child_mtu_cached;
3484 while (start_from--) {
3485 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003488 if (mtu > xdst->route_mtu_cached)
3489 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003490 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003491 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 break;
3493
David Miller54920932017-11-28 15:41:01 -05003494 xdst = bundle[start_from - 1];
3495 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497
3498 return 1;
3499}
3500
David S. Miller0dbaee32010-12-13 12:52:14 -08003501static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3502{
David Miller0f6c4802017-11-28 15:40:46 -05003503 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003504}
3505
Steffen Klassertebb762f2011-11-23 02:12:51 +00003506static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003507{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003508 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3509
David Miller0f6c4802017-11-28 15:40:46 -05003510 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003511}
3512
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003513static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3514 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003515{
David Miller0f6c4802017-11-28 15:40:46 -05003516 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003517 const struct xfrm_state *xfrm = dst->xfrm;
3518
Steffen Klassert013cb812018-02-19 07:44:07 +01003519 dst = xfrm_dst_child(dst);
3520
Julian Anastasov63fca652017-02-06 23:14:15 +02003521 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3522 continue;
3523 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3524 daddr = xfrm->coaddr;
3525 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3526 daddr = &xfrm->id.daddr;
3527 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003528 return daddr;
3529}
3530
3531static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3532 struct sk_buff *skb,
3533 const void *daddr)
3534{
David Miller0f6c4802017-11-28 15:40:46 -05003535 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003536
3537 if (!skb)
3538 daddr = xfrm_get_dst_nexthop(dst, daddr);
3539 return path->ops->neigh_lookup(path, skb, daddr);
3540}
3541
3542static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3543{
David Miller0f6c4802017-11-28 15:40:46 -05003544 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003545
3546 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003547 path->ops->confirm_neigh(path, daddr);
3548}
3549
Florian Westphala2817d82017-02-07 15:00:17 +01003550int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
3552 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003553
3554 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003556
Eric Dumazetef8531b2012-08-19 12:31:48 +02003557 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003558 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003559 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 else {
3561 struct dst_ops *dst_ops = afinfo->dst_ops;
3562 if (likely(dst_ops->kmem_cachep == NULL))
3563 dst_ops->kmem_cachep = xfrm_dst_cache;
3564 if (likely(dst_ops->check == NULL))
3565 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003566 if (likely(dst_ops->default_advmss == NULL))
3567 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003568 if (likely(dst_ops->mtu == NULL))
3569 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (likely(dst_ops->negative_advice == NULL))
3571 dst_ops->negative_advice = xfrm_negative_advice;
3572 if (likely(dst_ops->link_failure == NULL))
3573 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003574 if (likely(dst_ops->neigh_lookup == NULL))
3575 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003576 if (likely(!dst_ops->confirm_neigh))
3577 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003578 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003580 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 return err;
3583}
3584EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3585
Florian Westphala2817d82017-02-07 15:00:17 +01003586void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Florian Westphal2b619972017-02-07 15:00:15 +01003588 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003589 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003590
Florian Westphala2817d82017-02-07 15:00:17 +01003591 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3592 if (xfrm_policy_afinfo[i] != afinfo)
3593 continue;
3594 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3595 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Florian Westphal2b619972017-02-07 15:00:15 +01003598 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Florian Westphal2b619972017-02-07 15:00:15 +01003600 dst_ops->kmem_cachep = NULL;
3601 dst_ops->check = NULL;
3602 dst_ops->negative_advice = NULL;
3603 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3606
Steffen Klassertf203b762018-06-12 14:07:12 +02003607void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3608{
3609 spin_lock(&xfrm_if_cb_lock);
3610 rcu_assign_pointer(xfrm_if_cb, ifcb);
3611 spin_unlock(&xfrm_if_cb_lock);
3612}
3613EXPORT_SYMBOL(xfrm_if_register_cb);
3614
3615void xfrm_if_unregister_cb(void)
3616{
3617 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3618 synchronize_rcu();
3619}
3620EXPORT_SYMBOL(xfrm_if_unregister_cb);
3621
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003622#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003623static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003624{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003625 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003626 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3627 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003628 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003629 rv = xfrm_proc_init(net);
3630 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003631 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003632 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003633}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003634
3635static void xfrm_statistics_fini(struct net *net)
3636{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003637 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003638 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003639}
3640#else
3641static int __net_init xfrm_statistics_init(struct net *net)
3642{
3643 return 0;
3644}
3645
3646static void xfrm_statistics_fini(struct net *net)
3647{
3648}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003649#endif
3650
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003651static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
David S. Miller2518c7c2006-08-24 04:45:07 -07003653 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01003654 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07003655
Florian Westphal24969fa2018-11-07 23:00:35 +01003656 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003657 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003659 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003660 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01003661 err = rhashtable_init(&xfrm_policy_inexact_table,
3662 &xfrm_pol_inexact_params);
3663 BUG_ON(err);
3664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
David S. Miller2518c7c2006-08-24 04:45:07 -07003666 hmask = 8 - 1;
3667 sz = (hmask+1) * sizeof(struct hlist_head);
3668
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003669 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3670 if (!net->xfrm.policy_byidx)
3671 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003672 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07003673
Herbert Xu53c2e282014-11-13 17:09:49 +08003674 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07003675 struct xfrm_policy_hash *htab;
3676
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08003677 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08003678 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003679 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07003680
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003681 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07003682 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07003683 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003684 goto out_bydst;
3685 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02003686 htab->dbits4 = 32;
3687 htab->sbits4 = 32;
3688 htab->dbits6 = 128;
3689 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003690 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003691 net->xfrm.policy_hthresh.lbits4 = 32;
3692 net->xfrm.policy_hthresh.rbits4 = 32;
3693 net->xfrm.policy_hthresh.lbits6 = 128;
3694 net->xfrm.policy_hthresh.rbits6 = 128;
3695
3696 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003697
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003698 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01003699 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003700 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003701 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003702 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003703
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003704out_bydst:
3705 for (dir--; dir >= 0; dir--) {
3706 struct xfrm_policy_hash *htab;
3707
3708 htab = &net->xfrm.policy_bydst[dir];
3709 xfrm_hash_free(htab->table, sz);
3710 }
3711 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003712out_byidx:
3713 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714}
3715
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003716static void xfrm_policy_fini(struct net *net)
3717{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01003718 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003719 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003720 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003721
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003722 flush_work(&net->xfrm.policy_hash_work);
3723#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003724 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003725#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003726 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003727
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003728 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003729
Herbert Xu53c2e282014-11-13 17:09:49 +08003730 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003731 struct xfrm_policy_hash *htab;
3732
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003733 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003734
3735 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003736 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003737 WARN_ON(!hlist_empty(htab->table));
3738 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003739 }
3740
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003741 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003742 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3743 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01003744
Florian Westphal6be3b0d2018-11-07 23:00:37 +01003745 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
3746 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
3747 __xfrm_policy_inexact_prune_bin(b, true);
3748 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003749}
3750
3751static int __net_init xfrm_net_init(struct net *net)
3752{
3753 int rv;
3754
Florian Westphalc2822222017-02-08 11:52:29 +01003755 /* Initialize the per-net locks here */
3756 spin_lock_init(&net->xfrm.xfrm_state_lock);
3757 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3758 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3759
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003760 rv = xfrm_statistics_init(net);
3761 if (rv < 0)
3762 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003763 rv = xfrm_state_init(net);
3764 if (rv < 0)
3765 goto out_state;
3766 rv = xfrm_policy_init(net);
3767 if (rv < 0)
3768 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003769 rv = xfrm_sysctl_init(net);
3770 if (rv < 0)
3771 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08003772
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003773 return 0;
3774
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003775out_sysctl:
3776 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003777out_policy:
3778 xfrm_state_fini(net);
3779out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003780 xfrm_statistics_fini(net);
3781out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003782 return rv;
3783}
3784
3785static void __net_exit xfrm_net_exit(struct net *net)
3786{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003787 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003788 xfrm_policy_fini(net);
3789 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003790 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003791}
3792
3793static struct pernet_operations __net_initdata xfrm_net_ops = {
3794 .init = xfrm_net_init,
3795 .exit = xfrm_net_exit,
3796};
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798void __init xfrm_init(void)
3799{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003800 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03003801 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02003802 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02003804
3805 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3806 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Joy Lattenab5f5e82007-09-17 11:51:22 -07003809#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003810static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3811 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003812{
Paul Moore875179f2007-12-01 23:27:18 +11003813 struct xfrm_sec_ctx *ctx = xp->security;
3814 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003815
Paul Moore875179f2007-12-01 23:27:18 +11003816 if (ctx)
3817 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3818 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3819
Weilong Chen9b7a7872013-12-24 09:43:46 +08003820 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003821 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003822 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003823 if (sel->prefixlen_s != 32)
3824 audit_log_format(audit_buf, " src_prefixlen=%d",
3825 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003826 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003827 if (sel->prefixlen_d != 32)
3828 audit_log_format(audit_buf, " dst_prefixlen=%d",
3829 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003830 break;
3831 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003832 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003833 if (sel->prefixlen_s != 128)
3834 audit_log_format(audit_buf, " src_prefixlen=%d",
3835 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003836 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003837 if (sel->prefixlen_d != 128)
3838 audit_log_format(audit_buf, " dst_prefixlen=%d",
3839 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003840 break;
3841 }
3842}
3843
Tetsuo Handa2e710292014-04-22 21:48:30 +09003844void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003845{
3846 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003847
Paul Mooreafeb14b2007-12-21 14:58:11 -08003848 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003849 if (audit_buf == NULL)
3850 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003851 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003852 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003853 xfrm_audit_common_policyinfo(xp, audit_buf);
3854 audit_log_end(audit_buf);
3855}
3856EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3857
Paul Moore68277ac2007-12-20 20:49:33 -08003858void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003859 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003860{
3861 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003862
Paul Mooreafeb14b2007-12-21 14:58:11 -08003863 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003864 if (audit_buf == NULL)
3865 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003866 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003867 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003868 xfrm_audit_common_policyinfo(xp, audit_buf);
3869 audit_log_end(audit_buf);
3870}
3871EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3872#endif
3873
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003874#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003875static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3876 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003877{
3878 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3879 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003880 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3881 sel_cmp->family) &&
3882 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3883 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003884 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3885 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003886 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003887 }
3888 } else {
3889 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003890 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003891 }
3892 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003893 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003894}
3895
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003896static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3897 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003898{
3899 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003900 struct hlist_head *chain;
3901 u32 priority = ~0U;
3902
Florian Westphal9d0380d2016-08-11 15:17:59 +02003903 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003904 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003905 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003906 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3907 pol->type == type) {
3908 ret = pol;
3909 priority = ret->priority;
3910 break;
3911 }
3912 }
Fan Du8d549c42013-11-07 17:47:49 +08003913 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01003914 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08003915 if ((pol->priority >= priority) && ret)
3916 break;
3917
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003918 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003919 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003920 ret = pol;
3921 break;
3922 }
3923 }
3924
Li RongQing586f2eb2015-04-30 17:13:41 +08003925 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003926
Florian Westphal9d0380d2016-08-11 15:17:59 +02003927 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003928
3929 return ret;
3930}
3931
David S. Millerdd701752011-02-24 00:21:08 -05003932static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003933{
3934 int match = 0;
3935
3936 if (t->mode == m->mode && t->id.proto == m->proto &&
3937 (m->reqid == 0 || t->reqid == m->reqid)) {
3938 switch (t->mode) {
3939 case XFRM_MODE_TUNNEL:
3940 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003941 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3942 m->old_family) &&
3943 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3944 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003945 match = 1;
3946 }
3947 break;
3948 case XFRM_MODE_TRANSPORT:
3949 /* in case of transport mode, template does not store
3950 any IP addresses, hence we just compare mode and
3951 protocol */
3952 match = 1;
3953 break;
3954 default:
3955 break;
3956 }
3957 }
3958 return match;
3959}
3960
3961/* update endpoint address(es) of template(s) */
3962static int xfrm_policy_migrate(struct xfrm_policy *pol,
3963 struct xfrm_migrate *m, int num_migrate)
3964{
3965 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003966 int i, j, n = 0;
3967
3968 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003969 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003970 /* target policy has been deleted */
3971 write_unlock_bh(&pol->lock);
3972 return -ENOENT;
3973 }
3974
3975 for (i = 0; i < pol->xfrm_nr; i++) {
3976 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3977 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3978 continue;
3979 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003980 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3981 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003982 continue;
3983 /* update endpoints */
3984 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3985 sizeof(pol->xfrm_vec[i].id.daddr));
3986 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3987 sizeof(pol->xfrm_vec[i].saddr));
3988 pol->xfrm_vec[i].encap_family = mp->new_family;
3989 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003990 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003991 }
3992 }
3993
3994 write_unlock_bh(&pol->lock);
3995
3996 if (!n)
3997 return -ENODATA;
3998
3999 return 0;
4000}
4001
David S. Millerdd701752011-02-24 00:21:08 -05004002static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004003{
4004 int i, j;
4005
4006 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4007 return -EINVAL;
4008
4009 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004010 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4011 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4012 return -EINVAL;
4013
4014 /* check if there is any duplicated entry */
4015 for (j = i + 1; j < num_migrate; j++) {
4016 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4017 sizeof(m[i].old_daddr)) &&
4018 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4019 sizeof(m[i].old_saddr)) &&
4020 m[i].proto == m[j].proto &&
4021 m[i].mode == m[j].mode &&
4022 m[i].reqid == m[j].reqid &&
4023 m[i].old_family == m[j].old_family)
4024 return -EINVAL;
4025 }
4026 }
4027
4028 return 0;
4029}
4030
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004031int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004032 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004033 struct xfrm_kmaddress *k, struct net *net,
4034 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004035{
4036 int i, err, nx_cur = 0, nx_new = 0;
4037 struct xfrm_policy *pol = NULL;
4038 struct xfrm_state *x, *xc;
4039 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4040 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4041 struct xfrm_migrate *mp;
4042
Vladis Dronov7bab0962017-08-02 19:50:14 +02004043 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004044 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4045 goto out;
4046
Vladis Dronov7bab0962017-08-02 19:50:14 +02004047 if (dir >= XFRM_POLICY_MAX) {
4048 err = -EINVAL;
4049 goto out;
4050 }
4051
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004052 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004053 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004054 err = -ENOENT;
4055 goto out;
4056 }
4057
4058 /* Stage 2 - find and update state(s) */
4059 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004060 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004061 x_cur[nx_cur] = x;
4062 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004063 xc = xfrm_state_migrate(x, mp, encap);
4064 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004065 x_new[nx_new] = xc;
4066 nx_new++;
4067 } else {
4068 err = -ENODATA;
4069 goto restore_state;
4070 }
4071 }
4072 }
4073
4074 /* Stage 3 - update policy */
4075 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4076 goto restore_state;
4077
4078 /* Stage 4 - delete old state(s) */
4079 if (nx_cur) {
4080 xfrm_states_put(x_cur, nx_cur);
4081 xfrm_states_delete(x_cur, nx_cur);
4082 }
4083
4084 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004085 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004086
4087 xfrm_pol_put(pol);
4088
4089 return 0;
4090out:
4091 return err;
4092
4093restore_state:
4094 if (pol)
4095 xfrm_pol_put(pol);
4096 if (nx_cur)
4097 xfrm_states_put(x_cur, nx_cur);
4098 if (nx_new)
4099 xfrm_states_delete(x_new, nx_new);
4100
4101 return err;
4102}
David S. Millere610e672007-02-08 13:29:15 -08004103EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004104#endif