blob: 297b2fdb3c2977fd57c3a640550d50b5b661902c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * xfrm_policy.c
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
10 * Kazunori MIYAZAWA @USAGI
11 * YOSHIFUJI Hideaki
12 * Split up af-specific portion
13 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Herbert Xu66cdb3c2007-11-13 21:37:28 -080017#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/slab.h>
19#include <linux/kmod.h>
20#include <linux/list.h>
21#include <linux/spinlock.h>
22#include <linux/workqueue.h>
23#include <linux/notifier.h>
24#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080025#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070027#include <linux/cache.h>
Florian Westphalec30d782017-07-17 13:57:27 +020028#include <linux/cpu.h>
Paul Moore68277ac2007-12-20 20:49:33 -080029#include <linux/audit.h>
Florian Westphal24969fa2018-11-07 23:00:35 +010030#include <linux/rhashtable.h>
Florian Westphalc53ac41e2019-04-16 16:44:39 +020031#include <linux/if_tunnel.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080032#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050033#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/xfrm.h>
35#include <net/ip.h>
Florian Westphalc53ac41e2019-04-16 16:44:39 +020036#if IS_ENABLED(CONFIG_IPV6_MIP6)
37#include <net/mip6.h>
38#endif
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080039#ifdef CONFIG_XFRM_STATISTICS
40#include <net/snmp.h>
41#endif
Sabrina Dubrocae27cca92019-11-25 14:49:02 +010042#ifdef CONFIG_INET_ESPINTCP
43#include <net/espintcp.h>
44#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David S. Miller44e36b42006-08-24 04:50:50 -070046#include "xfrm_hash.h"
47
Steffen Klasserta0073fe2013-02-05 12:52:55 +010048#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
49#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
50#define XFRM_MAX_QUEUE_LEN 100
51
Steffen Klassertb8c203b2014-09-16 10:08:49 +020052struct xfrm_flo {
53 struct dst_entry *dst_orig;
54 u8 flags;
55};
56
Florian Westphal6be3b0d2018-11-07 23:00:37 +010057/* prefixes smaller than this are stored in lists, not trees. */
58#define INEXACT_PREFIXLEN_IPV4 16
59#define INEXACT_PREFIXLEN_IPV6 48
Florian Westphal9cf545e2018-11-07 23:00:38 +010060
61struct xfrm_pol_inexact_node {
62 struct rb_node node;
63 union {
64 xfrm_address_t addr;
65 struct rcu_head rcu;
66 };
67 u8 prefixlen;
68
Florian Westphal6ac098b22018-11-07 23:00:41 +010069 struct rb_root root;
70
Florian Westphal9cf545e2018-11-07 23:00:38 +010071 /* the policies matching this node, can be empty list */
72 struct hlist_head hhead;
73};
74
75/* xfrm inexact policy search tree:
76 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
77 * |
78 * +---- root_d: sorted by daddr:prefix
79 * | |
80 * | xfrm_pol_inexact_node
81 * | |
Florian Westphal6ac098b22018-11-07 23:00:41 +010082 * | +- root: sorted by saddr/prefix
83 * | | |
84 * | | xfrm_pol_inexact_node
85 * | | |
86 * | | + root: unused
87 * | | |
88 * | | + hhead: saddr:daddr policies
89 * | |
Florian Westphal9cf545e2018-11-07 23:00:38 +010090 * | +- coarse policies and all any:daddr policies
91 * |
Florian Westphal64a09a72018-11-07 23:00:40 +010092 * +---- root_s: sorted by saddr:prefix
93 * | |
94 * | xfrm_pol_inexact_node
95 * | |
96 * | + root: unused
97 * | |
98 * | + hhead: saddr:any policies
99 * |
Florian Westphal9cf545e2018-11-07 23:00:38 +0100100 * +---- coarse policies and all any:any policies
101 *
Florian Westphal6ac098b22018-11-07 23:00:41 +0100102 * Lookups return four candidate lists:
Florian Westphal9cf545e2018-11-07 23:00:38 +0100103 * 1. any:any list from top-level xfrm_pol_inexact_bin
104 * 2. any:daddr list from daddr tree
Florian Westphal6ac098b22018-11-07 23:00:41 +0100105 * 3. saddr:daddr list from 2nd level daddr tree
106 * 4. saddr:any list from saddr tree
Florian Westphal9cf545e2018-11-07 23:00:38 +0100107 *
108 * This result set then needs to be searched for the policy with
109 * the lowest priority. If two results have same prio, youngest one wins.
110 */
111
Florian Westphal24969fa2018-11-07 23:00:35 +0100112struct xfrm_pol_inexact_key {
113 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100114 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +0100115 u16 family;
116 u8 dir, type;
117};
118
119struct xfrm_pol_inexact_bin {
120 struct xfrm_pol_inexact_key k;
121 struct rhash_head head;
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100122 /* list containing '*:*' policies */
Florian Westphal24969fa2018-11-07 23:00:35 +0100123 struct hlist_head hhead;
124
Florian Westphal9cf545e2018-11-07 23:00:38 +0100125 seqcount_t count;
126 /* tree sorted by daddr/prefix */
127 struct rb_root root_d;
128
Florian Westphal64a09a72018-11-07 23:00:40 +0100129 /* tree sorted by saddr/prefix */
130 struct rb_root root_s;
131
Florian Westphal24969fa2018-11-07 23:00:35 +0100132 /* slow path below */
133 struct list_head inexact_bins;
134 struct rcu_head rcu;
135};
136
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100137enum xfrm_pol_inexact_candidate_type {
Florian Westphal6ac098b22018-11-07 23:00:41 +0100138 XFRM_POL_CAND_BOTH,
Florian Westphal64a09a72018-11-07 23:00:40 +0100139 XFRM_POL_CAND_SADDR,
Florian Westphal9cf545e2018-11-07 23:00:38 +0100140 XFRM_POL_CAND_DADDR,
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100141 XFRM_POL_CAND_ANY,
142
143 XFRM_POL_CAND_MAX,
144};
145
146struct xfrm_pol_inexact_candidates {
147 struct hlist_head *res[XFRM_POL_CAND_MAX];
148};
149
Steffen Klassertf203b762018-06-12 14:07:12 +0200150static DEFINE_SPINLOCK(xfrm_if_cb_lock);
151static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
152
Priyanka Jain418a99a2012-08-12 21:22:29 +0000153static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +0100154static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +0000155 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +0300157static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +0200158static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Florian Westphal24969fa2018-11-07 23:00:35 +0100160static struct rhashtable xfrm_policy_inexact_table;
161static const struct rhashtable_params xfrm_pol_inexact_params;
162
David Miller54920932017-11-28 15:41:01 -0500163static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +0000164static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +0000165static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -0700166static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800168static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800169static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
170 int dir);
171
Florian Westphal24969fa2018-11-07 23:00:35 +0100172static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100173xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
174 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100175
176static struct xfrm_pol_inexact_bin *
177xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100178 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100179static struct xfrm_policy *
180xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
181 bool excl);
182static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
183 struct xfrm_policy *policy);
184
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100185static bool
186xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
187 struct xfrm_pol_inexact_bin *b,
188 const xfrm_address_t *saddr,
189 const xfrm_address_t *daddr);
190
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200191static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
192{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300193 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200194}
195
David S. Millerbc9b35a2012-05-15 15:04:57 -0400196static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500197__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800198{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500199 const struct flowi4 *fl4 = &fl->u.ip4;
200
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000201 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
202 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500203 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
204 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
205 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
206 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800207}
208
David S. Millerbc9b35a2012-05-15 15:04:57 -0400209static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500210__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800211{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500212 const struct flowi6 *fl6 = &fl->u.ip6;
213
214 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
215 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
216 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
217 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
218 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
219 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800220}
221
David S. Millerbc9b35a2012-05-15 15:04:57 -0400222bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
223 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800224{
225 switch (family) {
226 case AF_INET:
227 return __xfrm4_selector_match(sel, fl);
228 case AF_INET6:
229 return __xfrm6_selector_match(sel, fl);
230 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400231 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800232}
233
Florian Westphala2817d82017-02-07 15:00:17 +0100234static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200235{
Florian Westphala2817d82017-02-07 15:00:17 +0100236 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200237
Florian Westphala2817d82017-02-07 15:00:17 +0100238 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200239 return NULL;
240 rcu_read_lock();
241 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
242 if (unlikely(!afinfo))
243 rcu_read_unlock();
244 return afinfo;
245}
246
Steffen Klassertf203b762018-06-12 14:07:12 +0200247/* Called with rcu_read_lock(). */
248static const struct xfrm_if_cb *xfrm_if_get_cb(void)
249{
250 return rcu_dereference(xfrm_if_cb);
251}
252
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200253struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
254 const xfrm_address_t *saddr,
255 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900256 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Florian Westphal37b10382017-02-07 15:00:19 +0100258 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800259 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Herbert Xu25ee3282007-12-11 09:32:34 -0800261 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800263 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900265 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900266
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100267 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900268
269 return dst;
270}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200271EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900272
David Ahern42a7b322015-08-10 16:58:11 -0600273static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
274 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900275 xfrm_address_t *prev_saddr,
276 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900277 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900278{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800279 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900280 xfrm_address_t *saddr = &x->props.saddr;
281 xfrm_address_t *daddr = &x->id.daddr;
282 struct dst_entry *dst;
283
284 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
285 saddr = x->coaddr;
286 daddr = prev_daddr;
287 }
288 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
289 saddr = prev_saddr;
290 daddr = x->coaddr;
291 }
292
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900293 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900294
295 if (!IS_ERR(dst)) {
296 if (prev_saddr != saddr)
297 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
298 if (prev_daddr != daddr)
299 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
300 }
301
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800302 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static inline unsigned long make_jiffies(long secs)
306{
307 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
308 return MAX_SCHEDULE_TIMEOUT-1;
309 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900310 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Kees Cookc3aed702017-10-16 17:28:56 -0700313static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Kees Cookc3aed702017-10-16 17:28:56 -0700315 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200316 time64_t now = ktime_get_real_seconds();
317 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int warn = 0;
319 int dir;
320
321 read_lock(&xp->lock);
322
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000323 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto out;
325
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700326 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200329 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 xp->curlft.add_time - now;
331 if (tmo <= 0)
332 goto expired;
333 if (tmo < next)
334 next = tmo;
335 }
336 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200337 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
339 if (tmo <= 0)
340 goto expired;
341 if (tmo < next)
342 next = tmo;
343 }
344 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200345 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 xp->curlft.add_time - now;
347 if (tmo <= 0) {
348 warn = 1;
349 tmo = XFRM_KM_TIMEOUT;
350 }
351 if (tmo < next)
352 next = tmo;
353 }
354 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200355 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
357 if (tmo <= 0) {
358 warn = 1;
359 tmo = XFRM_KM_TIMEOUT;
360 }
361 if (tmo < next)
362 next = tmo;
363 }
364
365 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800366 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200367 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
369 xfrm_pol_hold(xp);
370
371out:
372 read_unlock(&xp->lock);
373 xfrm_pol_put(xp);
374 return;
375
376expired:
377 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700378 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800379 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfrm_pol_put(xp);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
384 * SPD calls.
385 */
386
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800387struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct xfrm_policy *policy;
390
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700391 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800394 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700395 INIT_LIST_HEAD(&policy->walk.all);
Florian Westphal24969fa2018-11-07 23:00:35 +0100396 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700397 INIT_HLIST_NODE(&policy->bydst);
398 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300400 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100401 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700402 timer_setup(&policy->timer, xfrm_policy_timer, 0);
403 timer_setup(&policy->polq.hold_timer,
404 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406 return policy;
407}
408EXPORT_SYMBOL(xfrm_policy_alloc);
409
Eric Dumazet56f04732015-12-08 07:22:01 -0800410static void xfrm_policy_destroy_rcu(struct rcu_head *head)
411{
412 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
413
414 security_xfrm_policy_free(policy->security);
415 kfree(policy);
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* Destroy xfrm_policy: descendant resources must be released to this moment. */
419
WANG Cong64c31b32008-01-07 22:34:29 -0800420void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Herbert Xu12a169e2008-10-01 07:03:24 -0700422 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Fan Du0659eea2013-08-01 18:08:36 +0800424 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 BUG();
426
Eric Dumazet56f04732015-12-08 07:22:01 -0800427 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
WANG Cong64c31b32008-01-07 22:34:29 -0800429EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100431/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * entry dead. The rule must be unlinked from lists to the moment.
433 */
434
435static void xfrm_policy_kill(struct xfrm_policy *policy)
436{
YueHaibing4c594062020-03-23 15:32:39 +0800437 write_lock_bh(&policy->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700438 policy->walk.dead = 1;
YueHaibing4c594062020-03-23 15:32:39 +0800439 write_unlock_bh(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Timo Teräs285ead12010-04-07 00:30:06 +0000441 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200443 if (del_timer(&policy->polq.hold_timer))
444 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800445 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100446
Timo Teräs285ead12010-04-07 00:30:06 +0000447 if (del_timer(&policy->timer))
448 xfrm_pol_put(policy);
449
450 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
David S. Miller2518c7c2006-08-24 04:45:07 -0700453static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
454
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800455static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700456{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800457 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700458}
459
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200460/* calculate policy hash thresholds */
461static void __get_hash_thresh(struct net *net,
462 unsigned short family, int dir,
463 u8 *dbits, u8 *sbits)
464{
465 switch (family) {
466 case AF_INET:
467 *dbits = net->xfrm.policy_bydst[dir].dbits4;
468 *sbits = net->xfrm.policy_bydst[dir].sbits4;
469 break;
470
471 case AF_INET6:
472 *dbits = net->xfrm.policy_bydst[dir].dbits6;
473 *sbits = net->xfrm.policy_bydst[dir].sbits6;
474 break;
475
476 default:
477 *dbits = 0;
478 *sbits = 0;
479 }
480}
481
David S. Miller5f803b52011-02-24 00:33:19 -0500482static struct hlist_head *policy_hash_bysel(struct net *net,
483 const struct xfrm_selector *sel,
484 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700485{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800486 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200487 unsigned int hash;
488 u8 dbits;
489 u8 sbits;
490
491 __get_hash_thresh(net, family, dir, &dbits, &sbits);
492 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700493
Florian Westphale1e551b2016-08-11 15:17:53 +0200494 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100495 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200496
497 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
498 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700499}
500
David S. Miller5f803b52011-02-24 00:33:19 -0500501static struct hlist_head *policy_hash_direct(struct net *net,
502 const xfrm_address_t *daddr,
503 const xfrm_address_t *saddr,
504 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700505{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800506 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200507 unsigned int hash;
508 u8 dbits;
509 u8 sbits;
510
511 __get_hash_thresh(net, family, dir, &dbits, &sbits);
512 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700513
Florian Westphale1e551b2016-08-11 15:17:53 +0200514 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
515 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700516}
517
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200518static void xfrm_dst_hash_transfer(struct net *net,
519 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700520 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200521 unsigned int nhashmask,
522 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700523{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800524 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800526 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200527 u8 dbits;
528 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700529
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800530redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800531 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700532 unsigned int h;
533
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200534 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700535 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200536 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800537 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200538 hlist_del_rcu(&pol->bydst);
539 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800540 h0 = h;
541 } else {
542 if (h != h0)
543 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200544 hlist_del_rcu(&pol->bydst);
545 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800546 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800547 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800548 }
549 if (!hlist_empty(list)) {
550 entry0 = NULL;
551 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700552 }
553}
554
555static void xfrm_idx_hash_transfer(struct hlist_head *list,
556 struct hlist_head *nidxtable,
557 unsigned int nhashmask)
558{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800559 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 struct xfrm_policy *pol;
561
Sasha Levinb67bfe02013-02-27 17:06:00 -0800562 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700563 unsigned int h;
564
565 h = __idx_hash(pol->index, nhashmask);
566 hlist_add_head(&pol->byidx, nidxtable+h);
567 }
568}
569
570static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
571{
572 return ((old_hmask + 1) << 1) - 1;
573}
574
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800575static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700576{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800577 unsigned int hmask = net->xfrm.policy_bydst[dir].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);
David S. Miller44e36b42006-08-24 04:50:50 -0700580 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200581 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700582 int i;
583
584 if (!ndst)
585 return;
586
Florian Westphal9d0380d2016-08-11 15:17:59 +0200587 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200588 write_seqcount_begin(&xfrm_policy_hash_generation);
589
590 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
591 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700592
593 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200594 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700595
Florian Westphale1e551b2016-08-11 15:17:53 +0200596 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800597 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700598
Florian Westphal30846092016-08-11 15:17:54 +0200599 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200600 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700601
Florian Westphale1e551b2016-08-11 15:17:53 +0200602 synchronize_rcu();
603
David S. Miller44e36b42006-08-24 04:50:50 -0700604 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700605}
606
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800607static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700608{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800609 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700610 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
611 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800612 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700613 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700614 int i;
615
616 if (!nidx)
617 return;
618
Florian Westphal9d0380d2016-08-11 15:17:59 +0200619 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700620
621 for (i = hmask; i >= 0; i--)
622 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
623
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800624 net->xfrm.policy_byidx = nidx;
625 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700626
Florian Westphal9d0380d2016-08-11 15:17:59 +0200627 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700628
David S. Miller44e36b42006-08-24 04:50:50 -0700629 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700630}
631
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800632static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700633{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800634 unsigned int cnt = net->xfrm.policy_count[dir];
635 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700636
637 if (total)
638 *total += cnt;
639
640 if ((hmask + 1) < xfrm_policy_hashmax &&
641 cnt > hmask)
642 return 1;
643
644 return 0;
645}
646
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800647static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700648{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800649 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700650
651 if ((hmask + 1) < xfrm_policy_hashmax &&
652 total > hmask)
653 return 1;
654
655 return 0;
656}
657
Alexey Dobriyane0710412010-01-23 13:37:10 +0000658void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700659{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000660 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
661 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
662 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
663 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
664 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
665 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
666 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700667 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700668}
669EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700670
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700671static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800672static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700673{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800674 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700675 int dir, total;
676
677 mutex_lock(&hash_resize_mutex);
678
679 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800680 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800681 if (xfrm_bydst_should_resize(net, dir, &total))
682 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700683 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800684 if (xfrm_byidx_should_resize(net, total))
685 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700686
687 mutex_unlock(&hash_resize_mutex);
688}
689
Florian Westphal24969fa2018-11-07 23:00:35 +0100690/* Make sure *pol can be inserted into fastbin.
691 * Useful to check that later insert requests will be sucessful
692 * (provided xfrm_policy_lock is held throughout).
693 */
694static struct xfrm_pol_inexact_bin *
695xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
696{
697 struct xfrm_pol_inexact_bin *bin, *prev;
698 struct xfrm_pol_inexact_key k = {
699 .family = pol->family,
700 .type = pol->type,
701 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100702 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100703 };
704 struct net *net = xp_net(pol);
705
706 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
707
708 write_pnet(&k.net, net);
709 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
710 xfrm_pol_inexact_params);
711 if (bin)
712 return bin;
713
714 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
715 if (!bin)
716 return NULL;
717
718 bin->k = k;
719 INIT_HLIST_HEAD(&bin->hhead);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100720 bin->root_d = RB_ROOT;
Florian Westphal64a09a72018-11-07 23:00:40 +0100721 bin->root_s = RB_ROOT;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100722 seqcount_init(&bin->count);
Florian Westphal24969fa2018-11-07 23:00:35 +0100723
724 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
725 &bin->k, &bin->head,
726 xfrm_pol_inexact_params);
727 if (!prev) {
728 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
729 return bin;
730 }
731
732 kfree(bin);
733
734 return IS_ERR(prev) ? NULL : prev;
735}
736
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100737static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
738 int family, u8 prefixlen)
Florian Westphal24969fa2018-11-07 23:00:35 +0100739{
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100740 if (xfrm_addr_any(addr, family))
741 return true;
Florian Westphal24969fa2018-11-07 23:00:35 +0100742
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100743 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
744 return true;
745
746 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
747 return true;
748
749 return false;
750}
751
752static bool
753xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
754{
755 const xfrm_address_t *addr;
756 bool saddr_any, daddr_any;
757 u8 prefixlen;
758
759 addr = &policy->selector.saddr;
760 prefixlen = policy->selector.prefixlen_s;
761
762 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
763 policy->family,
764 prefixlen);
765 addr = &policy->selector.daddr;
766 prefixlen = policy->selector.prefixlen_d;
767 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
768 policy->family,
769 prefixlen);
770 return saddr_any && daddr_any;
771}
772
Florian Westphal9cf545e2018-11-07 23:00:38 +0100773static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
774 const xfrm_address_t *addr, u8 prefixlen)
775{
776 node->addr = *addr;
777 node->prefixlen = prefixlen;
778}
779
780static struct xfrm_pol_inexact_node *
781xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
782{
783 struct xfrm_pol_inexact_node *node;
784
785 node = kzalloc(sizeof(*node), GFP_ATOMIC);
786 if (node)
787 xfrm_pol_inexact_node_init(node, addr, prefixlen);
788
789 return node;
790}
791
792static int xfrm_policy_addr_delta(const xfrm_address_t *a,
793 const xfrm_address_t *b,
794 u8 prefixlen, u16 family)
795{
796 unsigned int pdw, pbi;
797 int delta = 0;
798
799 switch (family) {
800 case AF_INET:
801 if (sizeof(long) == 4 && prefixlen == 0)
802 return ntohl(a->a4) - ntohl(b->a4);
803 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
804 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
805 case AF_INET6:
806 pdw = prefixlen >> 5;
807 pbi = prefixlen & 0x1f;
808
809 if (pdw) {
810 delta = memcmp(a->a6, b->a6, pdw << 2);
811 if (delta)
812 return delta;
813 }
814 if (pbi) {
815 u32 mask = ~0u << (32 - pbi);
816
817 delta = (ntohl(a->a6[pdw]) & mask) -
818 (ntohl(b->a6[pdw]) & mask);
819 }
820 break;
821 default:
822 break;
823 }
824
825 return delta;
826}
827
828static void xfrm_policy_inexact_list_reinsert(struct net *net,
829 struct xfrm_pol_inexact_node *n,
830 u16 family)
831{
Florian Westphale901cbc2018-11-07 23:00:39 +0100832 unsigned int matched_s, matched_d;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100833 struct xfrm_policy *policy, *p;
834
Florian Westphale901cbc2018-11-07 23:00:39 +0100835 matched_s = 0;
836 matched_d = 0;
837
Florian Westphal9cf545e2018-11-07 23:00:38 +0100838 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal1d389002019-01-04 14:17:03 +0100839 struct hlist_node *newpos = NULL;
Florian Westphale901cbc2018-11-07 23:00:39 +0100840 bool matches_s, matches_d;
841
Florian Westphal9cf545e2018-11-07 23:00:38 +0100842 if (!policy->bydst_reinsert)
843 continue;
844
845 WARN_ON_ONCE(policy->family != family);
846
847 policy->bydst_reinsert = false;
848 hlist_for_each_entry(p, &n->hhead, bydst) {
Florian Westphal1d389002019-01-04 14:17:03 +0100849 if (policy->priority > p->priority)
850 newpos = &p->bydst;
851 else if (policy->priority == p->priority &&
852 policy->pos > p->pos)
Florian Westphal9cf545e2018-11-07 23:00:38 +0100853 newpos = &p->bydst;
854 else
855 break;
856 }
857
858 if (newpos)
Florian Westphal355b00d2019-01-04 14:17:00 +0100859 hlist_add_behind_rcu(&policy->bydst, newpos);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100860 else
Florian Westphal355b00d2019-01-04 14:17:00 +0100861 hlist_add_head_rcu(&policy->bydst, &n->hhead);
Florian Westphale901cbc2018-11-07 23:00:39 +0100862
863 /* paranoia checks follow.
864 * Check that the reinserted policy matches at least
865 * saddr or daddr for current node prefix.
866 *
867 * Matching both is fine, matching saddr in one policy
868 * (but not daddr) and then matching only daddr in another
869 * is a bug.
870 */
871 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
872 &n->addr,
873 n->prefixlen,
874 family) == 0;
875 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
876 &n->addr,
877 n->prefixlen,
878 family) == 0;
879 if (matches_s && matches_d)
880 continue;
881
882 WARN_ON_ONCE(!matches_s && !matches_d);
883 if (matches_s)
884 matched_s++;
885 if (matches_d)
886 matched_d++;
887 WARN_ON_ONCE(matched_s && matched_d);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100888 }
889}
890
Florian Westphal6ac098b22018-11-07 23:00:41 +0100891static void xfrm_policy_inexact_node_reinsert(struct net *net,
892 struct xfrm_pol_inexact_node *n,
893 struct rb_root *new,
894 u16 family)
895{
Florian Westphal6ac098b22018-11-07 23:00:41 +0100896 struct xfrm_pol_inexact_node *node;
Florian Westphal12750ab2019-01-04 14:17:05 +0100897 struct rb_node **p, *parent;
Florian Westphal6ac098b22018-11-07 23:00:41 +0100898
899 /* we should not have another subtree here */
900 WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root));
Florian Westphal12750ab2019-01-04 14:17:05 +0100901restart:
902 parent = NULL;
Florian Westphal6ac098b22018-11-07 23:00:41 +0100903 p = &new->rb_node;
904 while (*p) {
905 u8 prefixlen;
906 int delta;
907
908 parent = *p;
909 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
910
911 prefixlen = min(node->prefixlen, n->prefixlen);
912
913 delta = xfrm_policy_addr_delta(&n->addr, &node->addr,
914 prefixlen, family);
915 if (delta < 0) {
916 p = &parent->rb_left;
917 } else if (delta > 0) {
918 p = &parent->rb_right;
919 } else {
Florian Westphal769a8072019-08-12 10:32:13 +0200920 bool same_prefixlen = node->prefixlen == n->prefixlen;
Florian Westphal6ac098b22018-11-07 23:00:41 +0100921 struct xfrm_policy *tmp;
922
Florian Westphal12750ab2019-01-04 14:17:05 +0100923 hlist_for_each_entry(tmp, &n->hhead, bydst) {
Florian Westphal6ac098b22018-11-07 23:00:41 +0100924 tmp->bydst_reinsert = true;
Florian Westphal12750ab2019-01-04 14:17:05 +0100925 hlist_del_rcu(&tmp->bydst);
926 }
Florian Westphal6ac098b22018-11-07 23:00:41 +0100927
Florian Westphal769a8072019-08-12 10:32:13 +0200928 node->prefixlen = prefixlen;
929
Florian Westphal6ac098b22018-11-07 23:00:41 +0100930 xfrm_policy_inexact_list_reinsert(net, node, family);
931
Florian Westphal769a8072019-08-12 10:32:13 +0200932 if (same_prefixlen) {
Florian Westphal6ac098b22018-11-07 23:00:41 +0100933 kfree_rcu(n, rcu);
934 return;
935 }
936
937 rb_erase(*p, new);
938 kfree_rcu(n, rcu);
939 n = node;
Florian Westphal12750ab2019-01-04 14:17:05 +0100940 goto restart;
Florian Westphal6ac098b22018-11-07 23:00:41 +0100941 }
942 }
943
944 rb_link_node_rcu(&n->node, parent, p);
945 rb_insert_color(&n->node, new);
946}
947
Florian Westphal9cf545e2018-11-07 23:00:38 +0100948/* merge nodes v and n */
949static void xfrm_policy_inexact_node_merge(struct net *net,
950 struct xfrm_pol_inexact_node *v,
951 struct xfrm_pol_inexact_node *n,
952 u16 family)
953{
Florian Westphal6ac098b22018-11-07 23:00:41 +0100954 struct xfrm_pol_inexact_node *node;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100955 struct xfrm_policy *tmp;
Florian Westphal6ac098b22018-11-07 23:00:41 +0100956 struct rb_node *rnode;
957
958 /* To-be-merged node v has a subtree.
959 *
960 * Dismantle it and insert its nodes to n->root.
961 */
962 while ((rnode = rb_first(&v->root)) != NULL) {
963 node = rb_entry(rnode, struct xfrm_pol_inexact_node, node);
964 rb_erase(&node->node, &v->root);
965 xfrm_policy_inexact_node_reinsert(net, node, &n->root,
966 family);
967 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100968
Florian Westphal1d389002019-01-04 14:17:03 +0100969 hlist_for_each_entry(tmp, &v->hhead, bydst) {
Florian Westphal9cf545e2018-11-07 23:00:38 +0100970 tmp->bydst_reinsert = true;
Florian Westphal1d389002019-01-04 14:17:03 +0100971 hlist_del_rcu(&tmp->bydst);
972 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100973
Florian Westphal9cf545e2018-11-07 23:00:38 +0100974 xfrm_policy_inexact_list_reinsert(net, n, family);
975}
976
977static struct xfrm_pol_inexact_node *
978xfrm_policy_inexact_insert_node(struct net *net,
979 struct rb_root *root,
980 xfrm_address_t *addr,
981 u16 family, u8 prefixlen, u8 dir)
982{
983 struct xfrm_pol_inexact_node *cached = NULL;
984 struct rb_node **p, *parent = NULL;
985 struct xfrm_pol_inexact_node *node;
986
987 p = &root->rb_node;
988 while (*p) {
989 int delta;
990
991 parent = *p;
992 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
993
994 delta = xfrm_policy_addr_delta(addr, &node->addr,
995 node->prefixlen,
996 family);
997 if (delta == 0 && prefixlen >= node->prefixlen) {
998 WARN_ON_ONCE(cached); /* ipsec policies got lost */
999 return node;
1000 }
1001
1002 if (delta < 0)
1003 p = &parent->rb_left;
1004 else
1005 p = &parent->rb_right;
1006
1007 if (prefixlen < node->prefixlen) {
1008 delta = xfrm_policy_addr_delta(addr, &node->addr,
1009 prefixlen,
1010 family);
1011 if (delta)
1012 continue;
1013
1014 /* This node is a subnet of the new prefix. It needs
1015 * to be removed and re-inserted with the smaller
1016 * prefix and all nodes that are now also covered
1017 * by the reduced prefixlen.
1018 */
1019 rb_erase(&node->node, root);
1020
1021 if (!cached) {
1022 xfrm_pol_inexact_node_init(node, addr,
1023 prefixlen);
1024 cached = node;
1025 } else {
1026 /* This node also falls within the new
1027 * prefixlen. Merge the to-be-reinserted
1028 * node and this one.
1029 */
1030 xfrm_policy_inexact_node_merge(net, node,
1031 cached, family);
1032 kfree_rcu(node, rcu);
1033 }
1034
1035 /* restart */
1036 p = &root->rb_node;
1037 parent = NULL;
1038 }
1039 }
1040
1041 node = cached;
1042 if (!node) {
1043 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
1044 if (!node)
1045 return NULL;
1046 }
1047
1048 rb_link_node_rcu(&node->node, parent, p);
1049 rb_insert_color(&node->node, root);
1050
1051 return node;
1052}
1053
1054static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
1055{
1056 struct xfrm_pol_inexact_node *node;
1057 struct rb_node *rn = rb_first(r);
1058
1059 while (rn) {
1060 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
1061
Florian Westphal6ac098b22018-11-07 23:00:41 +01001062 xfrm_policy_inexact_gc_tree(&node->root, rm);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001063 rn = rb_next(rn);
1064
Florian Westphal6ac098b22018-11-07 23:00:41 +01001065 if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001066 WARN_ON_ONCE(rm);
1067 continue;
1068 }
1069
1070 rb_erase(&node->node, r);
1071 kfree_rcu(node, rcu);
1072 }
1073}
1074
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001075static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
1076{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001077 write_seqcount_begin(&b->count);
1078 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
Florian Westphal64a09a72018-11-07 23:00:40 +01001079 xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001080 write_seqcount_end(&b->count);
1081
Florian Westphal64a09a72018-11-07 23:00:40 +01001082 if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
Florian Westphal9cf545e2018-11-07 23:00:38 +01001083 !hlist_empty(&b->hhead)) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001084 WARN_ON_ONCE(net_exit);
Florian Westphal24969fa2018-11-07 23:00:35 +01001085 return;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001086 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001087
1088 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
1089 xfrm_pol_inexact_params) == 0) {
1090 list_del(&b->inexact_bins);
1091 kfree_rcu(b, rcu);
1092 }
1093}
1094
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001095static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
1096{
1097 struct net *net = read_pnet(&b->k.net);
1098
1099 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1100 __xfrm_policy_inexact_prune_bin(b, false);
1101 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1102}
1103
Florian Westphal24969fa2018-11-07 23:00:35 +01001104static void __xfrm_policy_inexact_flush(struct net *net)
1105{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001106 struct xfrm_pol_inexact_bin *bin, *t;
Florian Westphal24969fa2018-11-07 23:00:35 +01001107
1108 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1109
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001110 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1111 __xfrm_policy_inexact_prune_bin(bin, false);
Florian Westphal24969fa2018-11-07 23:00:35 +01001112}
1113
Florian Westphal9cf545e2018-11-07 23:00:38 +01001114static struct hlist_head *
1115xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1116 struct xfrm_policy *policy, u8 dir)
1117{
1118 struct xfrm_pol_inexact_node *n;
1119 struct net *net;
1120
1121 net = xp_net(policy);
1122 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1123
1124 if (xfrm_policy_inexact_insert_use_any_list(policy))
1125 return &bin->hhead;
1126
1127 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1128 policy->family,
Florian Westphal64a09a72018-11-07 23:00:40 +01001129 policy->selector.prefixlen_d)) {
1130 write_seqcount_begin(&bin->count);
1131 n = xfrm_policy_inexact_insert_node(net,
1132 &bin->root_s,
1133 &policy->selector.saddr,
1134 policy->family,
1135 policy->selector.prefixlen_s,
1136 dir);
1137 write_seqcount_end(&bin->count);
1138 if (!n)
1139 return NULL;
1140
1141 return &n->hhead;
1142 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001143
1144 /* daddr is fixed */
1145 write_seqcount_begin(&bin->count);
1146 n = xfrm_policy_inexact_insert_node(net,
1147 &bin->root_d,
1148 &policy->selector.daddr,
1149 policy->family,
1150 policy->selector.prefixlen_d, dir);
1151 write_seqcount_end(&bin->count);
1152 if (!n)
1153 return NULL;
Florian Westphal6ac098b22018-11-07 23:00:41 +01001154
1155 /* saddr is wildcard */
1156 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
1157 policy->family,
1158 policy->selector.prefixlen_s))
1159 return &n->hhead;
1160
1161 write_seqcount_begin(&bin->count);
1162 n = xfrm_policy_inexact_insert_node(net,
1163 &n->root,
1164 &policy->selector.saddr,
1165 policy->family,
1166 policy->selector.prefixlen_s, dir);
1167 write_seqcount_end(&bin->count);
1168 if (!n)
1169 return NULL;
1170
Florian Westphal9cf545e2018-11-07 23:00:38 +01001171 return &n->hhead;
1172}
1173
Florian Westphal24969fa2018-11-07 23:00:35 +01001174static struct xfrm_policy *
1175xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1176{
1177 struct xfrm_pol_inexact_bin *bin;
1178 struct xfrm_policy *delpol;
1179 struct hlist_head *chain;
1180 struct net *net;
1181
1182 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1183 if (!bin)
1184 return ERR_PTR(-ENOMEM);
1185
Florian Westphal24969fa2018-11-07 23:00:35 +01001186 net = xp_net(policy);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001187 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1188
Florian Westphal9cf545e2018-11-07 23:00:38 +01001189 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1190 if (!chain) {
1191 __xfrm_policy_inexact_prune_bin(bin, false);
1192 return ERR_PTR(-ENOMEM);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001193 }
1194
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001195 delpol = xfrm_policy_insert_list(chain, policy, excl);
1196 if (delpol && excl) {
1197 __xfrm_policy_inexact_prune_bin(bin, false);
1198 return ERR_PTR(-EEXIST);
1199 }
1200
Florian Westphal24969fa2018-11-07 23:00:35 +01001201 chain = &net->xfrm.policy_inexact[dir];
1202 xfrm_policy_insert_inexact_list(chain, policy);
1203
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001204 if (delpol)
1205 __xfrm_policy_inexact_prune_bin(bin, false);
1206
Florian Westphal24969fa2018-11-07 23:00:35 +01001207 return delpol;
1208}
1209
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001210static void xfrm_hash_rebuild(struct work_struct *work)
1211{
1212 struct net *net = container_of(work, struct net,
1213 xfrm.policy_hthresh.work);
1214 unsigned int hmask;
1215 struct xfrm_policy *pol;
1216 struct xfrm_policy *policy;
1217 struct hlist_head *chain;
1218 struct hlist_head *odst;
1219 struct hlist_node *newpos;
1220 int i;
1221 int dir;
1222 unsigned seq;
1223 u8 lbits4, rbits4, lbits6, rbits6;
1224
1225 mutex_lock(&hash_resize_mutex);
1226
1227 /* read selector prefixlen thresholds */
1228 do {
1229 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1230
1231 lbits4 = net->xfrm.policy_hthresh.lbits4;
1232 rbits4 = net->xfrm.policy_hthresh.rbits4;
1233 lbits6 = net->xfrm.policy_hthresh.lbits6;
1234 rbits6 = net->xfrm.policy_hthresh.rbits6;
1235 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1236
Florian Westphal9d0380d2016-08-11 15:17:59 +02001237 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal7a474c32019-01-04 14:17:01 +01001238 write_seqcount_begin(&xfrm_policy_hash_generation);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001239
Florian Westphal24969fa2018-11-07 23:00:35 +01001240 /* make sure that we can insert the indirect policies again before
1241 * we start with destructive action.
1242 */
1243 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001244 struct xfrm_pol_inexact_bin *bin;
Florian Westphal24969fa2018-11-07 23:00:35 +01001245 u8 dbits, sbits;
1246
1247 dir = xfrm_policy_id2dir(policy->index);
1248 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1249 continue;
1250
1251 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1252 if (policy->family == AF_INET) {
1253 dbits = rbits4;
1254 sbits = lbits4;
1255 } else {
1256 dbits = rbits6;
1257 sbits = lbits6;
1258 }
1259 } else {
1260 if (policy->family == AF_INET) {
1261 dbits = lbits4;
1262 sbits = rbits4;
1263 } else {
1264 dbits = lbits6;
1265 sbits = rbits6;
1266 }
1267 }
1268
1269 if (policy->selector.prefixlen_d < dbits ||
1270 policy->selector.prefixlen_s < sbits)
1271 continue;
1272
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001273 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1274 if (!bin)
Florian Westphal24969fa2018-11-07 23:00:35 +01001275 goto out_unlock;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001276
1277 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1278 goto out_unlock;
Florian Westphal24969fa2018-11-07 23:00:35 +01001279 }
1280
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001281 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +08001282 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Florian Westphal1548bc42019-01-04 14:17:02 +01001283 struct hlist_node *n;
1284
1285 hlist_for_each_entry_safe(policy, n,
1286 &net->xfrm.policy_inexact[dir],
Florian Westphalfd709722019-07-02 12:46:00 +02001287 bydst_inexact_list) {
1288 hlist_del_rcu(&policy->bydst);
Florian Westphal1548bc42019-01-04 14:17:02 +01001289 hlist_del_init(&policy->bydst_inexact_list);
Florian Westphalfd709722019-07-02 12:46:00 +02001290 }
Florian Westphal1548bc42019-01-04 14:17:02 +01001291
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001292 hmask = net->xfrm.policy_bydst[dir].hmask;
1293 odst = net->xfrm.policy_bydst[dir].table;
Florian Westphalfd709722019-07-02 12:46:00 +02001294 for (i = hmask; i >= 0; i--) {
1295 hlist_for_each_entry_safe(policy, n, odst + i, bydst)
1296 hlist_del_rcu(&policy->bydst);
1297 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001298 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1299 /* dir out => dst = remote, src = local */
1300 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1301 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1302 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1303 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1304 } else {
1305 /* dir in/fwd => dst = local, src = remote */
1306 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1307 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1308 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1309 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1310 }
1311 }
1312
1313 /* re-insert all policies by order of creation */
1314 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal88584c32018-11-27 13:28:54 +01001315 if (policy->walk.dead)
1316 continue;
1317 dir = xfrm_policy_id2dir(policy->index);
1318 if (dir >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +02001319 /* skip socket policies */
1320 continue;
1321 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001322 newpos = NULL;
1323 chain = policy_hash_bysel(net, &policy->selector,
Florian Westphal88584c32018-11-27 13:28:54 +01001324 policy->family, dir);
Florian Westphal1548bc42019-01-04 14:17:02 +01001325
Florian Westphal24969fa2018-11-07 23:00:35 +01001326 if (!chain) {
1327 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1328
1329 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1330 continue;
1331 }
1332
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001333 hlist_for_each_entry(pol, chain, bydst) {
1334 if (policy->priority >= pol->priority)
1335 newpos = &pol->bydst;
1336 else
1337 break;
1338 }
1339 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +02001340 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001341 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001342 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001343 }
1344
Florian Westphal24969fa2018-11-07 23:00:35 +01001345out_unlock:
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001346 __xfrm_policy_inexact_flush(net);
Florian Westphal7a474c32019-01-04 14:17:01 +01001347 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001348 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001349
1350 mutex_unlock(&hash_resize_mutex);
1351}
1352
1353void xfrm_policy_hash_rebuild(struct net *net)
1354{
1355 schedule_work(&net->xfrm.policy_hthresh.work);
1356}
1357EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359/* Generate new index... KAME seems to generate them ordered by cost
1360 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +08001361static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 static u32 idx_generator;
1364
1365 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001366 struct hlist_head *list;
1367 struct xfrm_policy *p;
1368 u32 idx;
1369 int found;
1370
Fan Due682adf02013-11-07 17:47:48 +08001371 if (!index) {
1372 idx = (idx_generator | dir);
1373 idx_generator += 8;
1374 } else {
1375 idx = index;
1376 index = 0;
1377 }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (idx == 0)
1380 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -08001381 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -07001382 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001383 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001384 if (p->index == idx) {
1385 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
David S. Miller2518c7c2006-08-24 04:45:07 -07001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001389 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return idx;
1391 }
1392}
1393
David S. Miller2518c7c2006-08-24 04:45:07 -07001394static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1395{
1396 u32 *p1 = (u32 *) s1;
1397 u32 *p2 = (u32 *) s2;
1398 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1399 int i;
1400
1401 for (i = 0; i < len; i++) {
1402 if (p1[i] != p2[i])
1403 return 1;
1404 }
1405
1406 return 0;
1407}
1408
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001409static void xfrm_policy_requeue(struct xfrm_policy *old,
1410 struct xfrm_policy *new)
1411{
1412 struct xfrm_policy_queue *pq = &old->polq;
1413 struct sk_buff_head list;
1414
Li RongQingde2ad482015-04-30 17:25:19 +08001415 if (skb_queue_empty(&pq->hold_queue))
1416 return;
1417
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001418 __skb_queue_head_init(&list);
1419
1420 spin_lock_bh(&pq->hold_queue.lock);
1421 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001422 if (del_timer(&pq->hold_timer))
1423 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001424 spin_unlock_bh(&pq->hold_queue.lock);
1425
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001426 pq = &new->polq;
1427
1428 spin_lock_bh(&pq->hold_queue.lock);
1429 skb_queue_splice(&list, &pq->hold_queue);
1430 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001431 if (!mod_timer(&pq->hold_timer, jiffies))
1432 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001433 spin_unlock_bh(&pq->hold_queue.lock);
1434}
1435
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001436static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1437 struct xfrm_policy *pol)
1438{
1439 u32 mark = policy->mark.v & policy->mark.m;
1440
1441 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1442 return true;
1443
1444 if ((mark & pol->mark.m) == pol->mark.v &&
1445 policy->priority == pol->priority)
1446 return true;
1447
1448 return false;
1449}
1450
Florian Westphal24969fa2018-11-07 23:00:35 +01001451static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1452{
1453 const struct xfrm_pol_inexact_key *k = data;
1454 u32 a = k->type << 24 | k->dir << 16 | k->family;
1455
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001456 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1457 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +01001458}
1459
1460static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1461{
1462 const struct xfrm_pol_inexact_bin *b = data;
1463
1464 return xfrm_pol_bin_key(&b->k, 0, seed);
1465}
1466
1467static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1468 const void *ptr)
1469{
1470 const struct xfrm_pol_inexact_key *key = arg->key;
1471 const struct xfrm_pol_inexact_bin *b = ptr;
1472 int ret;
1473
1474 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1475 return -1;
1476
1477 ret = b->k.dir ^ key->dir;
1478 if (ret)
1479 return ret;
1480
1481 ret = b->k.type ^ key->type;
1482 if (ret)
1483 return ret;
1484
1485 ret = b->k.family ^ key->family;
1486 if (ret)
1487 return ret;
1488
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001489 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +01001490}
1491
1492static const struct rhashtable_params xfrm_pol_inexact_params = {
1493 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1494 .hashfn = xfrm_pol_bin_key,
1495 .obj_hashfn = xfrm_pol_bin_obj,
1496 .obj_cmpfn = xfrm_pol_bin_cmp,
1497 .automatic_shrinking = true,
1498};
1499
1500static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1501 struct xfrm_policy *policy)
1502{
1503 struct xfrm_policy *pol, *delpol = NULL;
1504 struct hlist_node *newpos = NULL;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001505 int i = 0;
Florian Westphal24969fa2018-11-07 23:00:35 +01001506
1507 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1508 if (pol->type == policy->type &&
1509 pol->if_id == policy->if_id &&
1510 !selector_cmp(&pol->selector, &policy->selector) &&
1511 xfrm_policy_mark_match(policy, pol) &&
1512 xfrm_sec_ctx_match(pol->security, policy->security) &&
1513 !WARN_ON(delpol)) {
1514 delpol = pol;
1515 if (policy->priority > pol->priority)
1516 continue;
1517 } else if (policy->priority >= pol->priority) {
1518 newpos = &pol->bydst_inexact_list;
1519 continue;
1520 }
1521 if (delpol)
1522 break;
1523 }
1524
1525 if (newpos)
1526 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1527 else
1528 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001529
1530 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1531 pol->pos = i;
1532 i++;
1533 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001534}
1535
Florian Westphala927d6af2018-11-07 23:00:33 +01001536static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1537 struct xfrm_policy *policy,
1538 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Florian Westphala927d6af2018-11-07 23:00:33 +01001540 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Sasha Levinb67bfe02013-02-27 17:06:00 -08001542 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001543 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001544 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001545 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001546 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001547 xfrm_sec_ctx_match(pol->security, policy->security) &&
1548 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001549 if (excl)
1550 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 delpol = pol;
1552 if (policy->priority > pol->priority)
1553 continue;
1554 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001555 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 continue;
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (delpol)
1559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001563 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001564 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001565 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001566
1567 return delpol;
1568}
1569
1570int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1571{
1572 struct net *net = xp_net(policy);
1573 struct xfrm_policy *delpol;
1574 struct hlist_head *chain;
1575
1576 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1577 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001578 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001579 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001580 else
1581 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001582
1583 if (IS_ERR(delpol)) {
1584 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1585 return PTR_ERR(delpol);
1586 }
1587
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001588 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001589
1590 /* After previous checking, family can either be AF_INET or AF_INET6 */
1591 if (policy->family == AF_INET)
1592 rt_genid_bump_ipv4(net);
1593 else
1594 rt_genid_bump_ipv6(net);
1595
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001596 if (delpol) {
1597 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001598 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001599 }
Fan Due682adf02013-11-07 17:47:48 +08001600 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001601 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001602 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 policy->curlft.use_time = 0;
1604 if (!mod_timer(&policy->timer, jiffies + HZ))
1605 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001606 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
David S. Miller9b78a822005-12-22 07:39:48 -08001608 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001610 else if (xfrm_bydst_should_resize(net, dir, NULL))
1611 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
1614}
1615EXPORT_SYMBOL(xfrm_policy_insert);
1616
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001617static struct xfrm_policy *
1618__xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1619 u8 type, int dir,
1620 struct xfrm_selector *sel,
1621 struct xfrm_sec_ctx *ctx)
1622{
1623 struct xfrm_policy *pol;
1624
1625 if (!chain)
1626 return NULL;
1627
1628 hlist_for_each_entry(pol, chain, bydst) {
1629 if (pol->type == type &&
1630 pol->if_id == if_id &&
1631 (mark & pol->mark.m) == pol->mark.v &&
1632 !selector_cmp(sel, &pol->selector) &&
1633 xfrm_sec_ctx_match(ctx, pol->security))
1634 return pol;
1635 }
1636
1637 return NULL;
1638}
1639
Steffen Klassert7e652642018-06-12 14:07:07 +02001640struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1641 u8 type, int dir,
1642 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001643 struct xfrm_sec_ctx *ctx, int delete,
1644 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Florian Westphal24969fa2018-11-07 23:00:35 +01001646 struct xfrm_pol_inexact_bin *bin = NULL;
1647 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001648 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Eric Parisef41aaa2007-03-07 15:37:58 -08001650 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001651 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001652 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001653 if (!chain) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001654 struct xfrm_pol_inexact_candidates cand;
1655 int i;
1656
Florian Westphal24969fa2018-11-07 23:00:35 +01001657 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001658 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001659 if (!bin) {
1660 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1661 return NULL;
1662 }
1663
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001664 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1665 &sel->saddr,
1666 &sel->daddr)) {
1667 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1668 return NULL;
1669 }
1670
1671 pol = NULL;
1672 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1673 struct xfrm_policy *tmp;
1674
1675 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1676 if_id, type, dir,
1677 sel, ctx);
Florian Westphal39aa6922018-11-15 02:51:57 +01001678 if (!tmp)
1679 continue;
1680
1681 if (!pol || tmp->pos < pol->pos)
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001682 pol = tmp;
1683 }
1684 } else {
1685 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1686 sel, ctx);
Florian Westphal24969fa2018-11-07 23:00:35 +01001687 }
1688
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001689 if (pol) {
1690 xfrm_pol_hold(pol);
1691 if (delete) {
1692 *err = security_xfrm_policy_delete(pol->security);
1693 if (*err) {
1694 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1695 return pol;
David S. Miller2518c7c2006-08-24 04:45:07 -07001696 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001697 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001699 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001701 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001703 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001704 xfrm_policy_kill(ret);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001705 if (bin && delete)
1706 xfrm_policy_inexact_prune_bin(bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001707 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
Trent Jaegerdf718372005-12-13 23:12:27 -08001709EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Steffen Klassert7e652642018-06-12 14:07:07 +02001711struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1712 u8 type, int dir, u32 id, int delete,
1713 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
David S. Miller2518c7c2006-08-24 04:45:07 -07001715 struct xfrm_policy *pol, *ret;
1716 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Herbert Xub5505c62007-05-14 02:15:47 -07001718 *err = -ENOENT;
1719 if (xfrm_policy_id2dir(id) != dir)
1720 return NULL;
1721
Eric Parisef41aaa2007-03-07 15:37:58 -08001722 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001723 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001724 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001725 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001726 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001727 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001728 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001729 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001731 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001732 *err = security_xfrm_policy_delete(
1733 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001734 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001735 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001736 return pol;
1737 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001738 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001739 }
1740 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 break;
1742 }
1743 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001744 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001746 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001747 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001748 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750EXPORT_SYMBOL(xfrm_policy_byid);
1751
Joy Latten4aa2e622007-06-04 19:05:57 -04001752#ifdef CONFIG_SECURITY_NETWORK_XFRM
1753static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001754xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Florian Westphalceb159e2018-11-07 23:00:32 +01001756 struct xfrm_policy *pol;
1757 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001758
Florian Westphalceb159e2018-11-07 23:00:32 +01001759 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1760 if (pol->walk.dead ||
1761 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1762 pol->type != type)
1763 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001764
Florian Westphalceb159e2018-11-07 23:00:32 +01001765 err = security_xfrm_policy_delete(pol->security);
1766 if (err) {
1767 xfrm_audit_policy_delete(pol, 0, task_valid);
1768 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001769 }
1770 }
1771 return err;
1772}
1773#else
1774static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001775xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001776{
1777 return 0;
1778}
1779#endif
1780
Tetsuo Handa2e710292014-04-22 21:48:30 +09001781int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001782{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001783 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001784 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Florian Westphal9d0380d2016-08-11 15:17:59 +02001786 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001787
Tetsuo Handa2e710292014-04-22 21:48:30 +09001788 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001789 if (err)
1790 goto out;
1791
Florian Westphalceb159e2018-11-07 23:00:32 +01001792again:
1793 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1794 dir = xfrm_policy_id2dir(pol->index);
1795 if (pol->walk.dead ||
1796 dir >= XFRM_POLICY_MAX ||
1797 pol->type != type)
1798 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001799
Florian Westphalceb159e2018-11-07 23:00:32 +01001800 __xfrm_policy_unlink(pol, dir);
1801 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1802 cnt++;
1803 xfrm_audit_policy_delete(pol, 1, task_valid);
1804 xfrm_policy_kill(pol);
1805 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1806 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001808 if (cnt)
1809 __xfrm_policy_inexact_flush(net);
1810 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001811 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001812out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001813 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001814 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816EXPORT_SYMBOL(xfrm_policy_flush);
1817
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001818int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001819 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 void *data)
1821{
Herbert Xu12a169e2008-10-01 07:03:24 -07001822 struct xfrm_policy *pol;
1823 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001824 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Timo Teras4c563f72008-02-28 21:31:08 -08001826 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1827 walk->type != XFRM_POLICY_TYPE_ANY)
1828 return -EINVAL;
1829
Herbert Xu12a169e2008-10-01 07:03:24 -07001830 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001831 return 0;
1832
Florian Westphal9d0380d2016-08-11 15:17:59 +02001833 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001834 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001835 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001836 else
Li RongQing80077702015-04-22 17:09:54 +08001837 x = list_first_entry(&walk->walk.all,
1838 struct xfrm_policy_walk_entry, all);
1839
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001840 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001841 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001842 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001843 pol = container_of(x, struct xfrm_policy, walk);
1844 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1845 walk->type != pol->type)
1846 continue;
1847 error = func(pol, xfrm_policy_id2dir(pol->index),
1848 walk->seq, data);
1849 if (error) {
1850 list_move_tail(&walk->walk.all, &x->all);
1851 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001852 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001853 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001855 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001856 error = -ENOENT;
1857 goto out;
1858 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001859 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001861 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return error;
1863}
1864EXPORT_SYMBOL(xfrm_policy_walk);
1865
Herbert Xu12a169e2008-10-01 07:03:24 -07001866void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1867{
1868 INIT_LIST_HEAD(&walk->walk.all);
1869 walk->walk.dead = 1;
1870 walk->type = type;
1871 walk->seq = 0;
1872}
1873EXPORT_SYMBOL(xfrm_policy_walk_init);
1874
Fan Du283bc9f2013-11-07 17:47:50 +08001875void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001876{
1877 if (list_empty(&walk->walk.all))
1878 return;
1879
Florian Westphal9d0380d2016-08-11 15:17:59 +02001880 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001881 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001882 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001883}
1884EXPORT_SYMBOL(xfrm_policy_walk_done);
1885
James Morris134b0fc2006-10-05 15:42:27 -05001886/*
1887 * Find policy to apply to this flow.
1888 *
1889 * Returns 0 if policy found, else an -errno.
1890 */
David S. Millerf299d552011-02-24 01:23:30 -05001891static int xfrm_policy_match(const struct xfrm_policy *pol,
1892 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001893 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001894{
David S. Millerf299d552011-02-24 01:23:30 -05001895 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001896 int ret = -ESRCH;
1897 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001898
1899 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001900 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001901 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001902 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001903 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001904
1905 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001906 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001907 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001908 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001909 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001910}
1911
Florian Westphal9cf545e2018-11-07 23:00:38 +01001912static struct xfrm_pol_inexact_node *
1913xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
1914 seqcount_t *count,
1915 const xfrm_address_t *addr, u16 family)
1916{
1917 const struct rb_node *parent;
1918 int seq;
1919
1920again:
1921 seq = read_seqcount_begin(count);
1922
1923 parent = rcu_dereference_raw(r->rb_node);
1924 while (parent) {
1925 struct xfrm_pol_inexact_node *node;
1926 int delta;
1927
1928 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1929
1930 delta = xfrm_policy_addr_delta(addr, &node->addr,
1931 node->prefixlen, family);
1932 if (delta < 0) {
1933 parent = rcu_dereference_raw(parent->rb_left);
1934 continue;
1935 } else if (delta > 0) {
1936 parent = rcu_dereference_raw(parent->rb_right);
1937 continue;
1938 }
1939
1940 return node;
1941 }
1942
1943 if (read_seqcount_retry(count, seq))
1944 goto again;
1945
1946 return NULL;
1947}
1948
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001949static bool
1950xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1951 struct xfrm_pol_inexact_bin *b,
1952 const xfrm_address_t *saddr,
1953 const xfrm_address_t *daddr)
1954{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001955 struct xfrm_pol_inexact_node *n;
1956 u16 family;
1957
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001958 if (!b)
1959 return false;
1960
Florian Westphal9cf545e2018-11-07 23:00:38 +01001961 family = b->k.family;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001962 memset(cand, 0, sizeof(*cand));
1963 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001964
1965 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1966 family);
Florian Westphal6ac098b22018-11-07 23:00:41 +01001967 if (n) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001968 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
Florian Westphal6ac098b22018-11-07 23:00:41 +01001969 n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr,
1970 family);
1971 if (n)
1972 cand->res[XFRM_POL_CAND_BOTH] = &n->hhead;
1973 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001974
Florian Westphal64a09a72018-11-07 23:00:40 +01001975 n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
1976 family);
1977 if (n)
1978 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
1979
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001980 return true;
1981}
1982
Florian Westphal24969fa2018-11-07 23:00:35 +01001983static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001984xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1985 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001986{
1987 struct xfrm_pol_inexact_key k = {
1988 .family = family,
1989 .type = type,
1990 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001991 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001992 };
1993
1994 write_pnet(&k.net, net);
1995
1996 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1997 xfrm_pol_inexact_params);
1998}
1999
2000static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002001xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
2002 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01002003{
2004 struct xfrm_pol_inexact_bin *bin;
2005
2006 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
2007
2008 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002009 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01002010 rcu_read_unlock();
2011
2012 return bin;
2013}
2014
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002015static struct xfrm_policy *
2016__xfrm_policy_eval_candidates(struct hlist_head *chain,
2017 struct xfrm_policy *prefer,
2018 const struct flowi *fl,
2019 u8 type, u16 family, int dir, u32 if_id)
2020{
2021 u32 priority = prefer ? prefer->priority : ~0u;
2022 struct xfrm_policy *pol;
2023
2024 if (!chain)
2025 return NULL;
2026
2027 hlist_for_each_entry_rcu(pol, chain, bydst) {
2028 int err;
2029
2030 if (pol->priority > priority)
2031 break;
2032
2033 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
2034 if (err) {
2035 if (err != -ESRCH)
2036 return ERR_PTR(err);
2037
2038 continue;
2039 }
2040
2041 if (prefer) {
2042 /* matches. Is it older than *prefer? */
2043 if (pol->priority == priority &&
2044 prefer->pos < pol->pos)
2045 return prefer;
2046 }
2047
2048 return pol;
2049 }
2050
2051 return NULL;
2052}
2053
2054static struct xfrm_policy *
2055xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
2056 struct xfrm_policy *prefer,
2057 const struct flowi *fl,
2058 u8 type, u16 family, int dir, u32 if_id)
2059{
2060 struct xfrm_policy *tmp;
2061 int i;
2062
2063 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
2064 tmp = __xfrm_policy_eval_candidates(cand->res[i],
2065 prefer,
2066 fl, type, family, dir,
2067 if_id);
2068 if (!tmp)
2069 continue;
2070
2071 if (IS_ERR(tmp))
2072 return tmp;
2073 prefer = tmp;
2074 }
2075
2076 return prefer;
2077}
2078
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002079static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08002080 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07002081 u16 family, u8 dir,
2082 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002084 struct xfrm_pol_inexact_candidates cand;
David S. Miller0b597e72011-02-24 01:22:48 -05002085 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01002086 struct xfrm_pol_inexact_bin *bin;
2087 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07002088 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02002089 unsigned int sequence;
Florian Westphal24969fa2018-11-07 23:00:35 +01002090 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07002091
2092 daddr = xfrm_flowi_daddr(fl, family);
2093 saddr = xfrm_flowi_saddr(fl, family);
2094 if (unlikely(!daddr || !saddr))
2095 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Florian Westphala7c442472016-08-11 15:17:56 +02002097 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02002098 retry:
2099 do {
2100 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
2101 chain = policy_hash_direct(net, daddr, saddr, family, dir);
2102 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
2103
David S. Miller2518c7c2006-08-24 04:45:07 -07002104 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02002105 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07002106 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05002107 if (err) {
2108 if (err == -ESRCH)
2109 continue;
2110 else {
2111 ret = ERR_PTR(err);
2112 goto fail;
2113 }
2114 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07002115 ret = pol;
David S. Milleracba48e2006-08-25 15:46:46 -07002116 break;
2117 }
2118 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002119 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002120 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
2121 daddr))
Florian Westphal24969fa2018-11-07 23:00:35 +01002122 goto skip_inexact;
Li RongQing8faf4912015-05-14 11:16:59 +08002123
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002124 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
2125 family, dir, if_id);
2126 if (pol) {
2127 ret = pol;
2128 if (IS_ERR(pol))
2129 goto fail;
David S. Miller2518c7c2006-08-24 04:45:07 -07002130 }
Li RongQing586f2eb2015-04-30 17:13:41 +08002131
Florian Westphal24969fa2018-11-07 23:00:35 +01002132skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02002133 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
2134 goto retry;
2135
Florian Westphale37cc8ad2016-08-11 15:17:55 +02002136 if (ret && !xfrm_pol_hold_rcu(ret))
2137 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05002138fail:
Florian Westphala7c442472016-08-11 15:17:56 +02002139 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002140
David S. Miller2518c7c2006-08-24 04:45:07 -07002141 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002142}
2143
Benedict Wongbc56b332018-07-19 10:50:44 -07002144static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
2145 const struct flowi *fl,
2146 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002147{
2148#ifdef CONFIG_XFRM_SUB_POLICY
2149 struct xfrm_policy *pol;
2150
Benedict Wongbc56b332018-07-19 10:50:44 -07002151 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2152 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002153 if (pol != NULL)
2154 return pol;
2155#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07002156 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2157 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002158}
2159
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002160static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07002161 const struct flowi *fl,
2162 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 struct xfrm_policy *pol;
2165
Eric Dumazetd188ba82015-12-08 07:22:02 -08002166 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02002167 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002168 pol = rcu_dereference(sk->sk_policy[dir]);
2169 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01002170 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002171 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08002172
Steffen Klassertddc47e42017-11-29 06:53:55 +01002173 if (pol->family != family) {
2174 pol = NULL;
2175 goto out;
2176 }
2177
2178 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002179 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02002180 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07002181 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002182 pol = NULL;
2183 goto out;
2184 }
Paul Moore03e1ad72008-04-12 19:07:52 -07002185 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05002186 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02002187 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01002188 if (!err) {
2189 if (!xfrm_pol_hold_rcu(pol))
2190 goto again;
2191 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002192 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01002193 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002194 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01002195 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002196 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 pol = NULL;
2198 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002199out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002200 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return pol;
2202}
2203
2204static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2205{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002206 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002207
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002208 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002209 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 xfrm_pol_hold(pol);
2211}
2212
2213static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2214 int dir)
2215{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002216 struct net *net = xp_net(pol);
2217
Herbert Xu53c2e282014-11-13 17:09:49 +08002218 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07002219 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Herbert Xu53c2e282014-11-13 17:09:49 +08002221 /* Socket policies are not hashed. */
2222 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02002223 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01002224 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08002225 hlist_del(&pol->byidx);
2226 }
2227
2228 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002229 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07002230
2231 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Herbert Xu53c2e282014-11-13 17:09:49 +08002234static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2235{
2236 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2237}
2238
2239static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2240{
2241 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2242}
2243
Herbert Xu4666faa2005-06-18 22:43:22 -07002244int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Fan Du283bc9f2013-11-07 17:47:50 +08002246 struct net *net = xp_net(pol);
2247
Florian Westphal9d0380d2016-08-11 15:17:59 +02002248 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002250 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07002253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
Herbert Xu4666faa2005-06-18 22:43:22 -07002255 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
David S. Millera70fcb02006-03-20 19:18:52 -08002257EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2260{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002261 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 struct xfrm_policy *old_pol;
2263
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002264#ifdef CONFIG_XFRM_SUB_POLICY
2265 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2266 return -EINVAL;
2267#endif
2268
Florian Westphal9d0380d2016-08-11 15:17:59 +02002269 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08002270 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2271 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02002273 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08002274 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08002275 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08002277 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002278 if (old_pol) {
2279 if (pol)
2280 xfrm_policy_requeue(old_pol, pol);
2281
Timo Teräsea2dea9d2010-03-31 00:17:05 +00002282 /* Unlinking succeeds always. This is the only function
2283 * allowed to delete or replace socket policy.
2284 */
Herbert Xu53c2e282014-11-13 17:09:49 +08002285 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002286 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02002287 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (old_pol) {
2290 xfrm_policy_kill(old_pol);
2291 }
2292 return 0;
2293}
2294
David S. Millerd3e40a92011-02-24 01:25:41 -05002295static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002297 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08002298 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 if (newp) {
2301 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07002302 if (security_xfrm_policy_clone(old->security,
2303 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08002304 kfree(newp);
2305 return NULL; /* ENOMEM */
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 newp->lft = old->lft;
2308 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08002309 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02002310 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 newp->action = old->action;
2312 newp->flags = old->flags;
2313 newp->xfrm_nr = old->xfrm_nr;
2314 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002315 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11002316 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 memcpy(newp->xfrm_vec, old->xfrm_vec,
2318 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02002319 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08002320 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002321 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 xfrm_pol_put(newp);
2323 }
2324 return newp;
2325}
2326
Eric Dumazetd188ba82015-12-08 07:22:02 -08002327int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Eric Dumazetd188ba82015-12-08 07:22:02 -08002329 const struct xfrm_policy *p;
2330 struct xfrm_policy *np;
2331 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Eric Dumazetd188ba82015-12-08 07:22:02 -08002333 rcu_read_lock();
2334 for (i = 0; i < 2; i++) {
2335 p = rcu_dereference(osk->sk_policy[i]);
2336 if (p) {
2337 np = clone_policy(p, i);
2338 if (unlikely(!np)) {
2339 ret = -ENOMEM;
2340 break;
2341 }
2342 rcu_assign_pointer(sk->sk_policy[i], np);
2343 }
2344 }
2345 rcu_read_unlock();
2346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002349static int
David Ahern42a7b322015-08-10 16:58:11 -06002350xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002351 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002352{
2353 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01002354 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002355
2356 if (unlikely(afinfo == NULL))
2357 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002358 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002359 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002360 return err;
2361}
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/* Resolve list of templates for the flow, given policy. */
2364
2365static int
David S. Millera6c2e612011-02-22 18:35:39 -08002366xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2367 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08002369 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 int nx;
2371 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01002372 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2373 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002374 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Weilong Chen9b7a7872013-12-24 09:43:46 +08002376 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01002378 xfrm_address_t *remote = daddr;
2379 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2381
Steffen Klassert94802152017-11-15 06:40:57 +01002382 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2383 tmpl->mode == XFRM_MODE_BEET) {
2384 remote = &tmpl->id.daddr;
2385 local = &tmpl->saddr;
2386 if (xfrm_addr_any(local, tmpl->encap_family)) {
2387 error = xfrm_get_saddr(net, fl->flowi_oif,
2388 &tmp, remote,
2389 tmpl->encap_family, 0);
2390 if (error)
2391 goto fail;
2392 local = &tmp;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395
Benedict Wongbc56b332018-07-19 10:50:44 -07002396 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2397 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 if (x && x->km.state == XFRM_STATE_VALID) {
2400 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01002401 daddr = remote;
2402 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 continue;
2404 }
2405 if (x) {
2406 error = (x->km.state == XFRM_STATE_ERROR ?
2407 -EINVAL : -EAGAIN);
2408 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08002409 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00002410 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08002411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 if (!tmpl->optional)
2414 goto fail;
2415 }
2416 return nx;
2417
2418fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002419 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 xfrm_state_put(xfrm[nx]);
2421 return error;
2422}
2423
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002424static int
David S. Millera6c2e612011-02-22 18:35:39 -08002425xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2426 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002427{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002428 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2429 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002430 int cnx = 0;
2431 int error;
2432 int ret;
2433 int i;
2434
2435 for (i = 0; i < npols; i++) {
2436 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2437 error = -ENOBUFS;
2438 goto fail;
2439 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002440
2441 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002442 if (ret < 0) {
2443 error = ret;
2444 goto fail;
2445 } else
2446 cnx += ret;
2447 }
2448
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002449 /* found states are sorted for outbound processing */
2450 if (npols > 1)
2451 xfrm_state_sort(xfrm, tpp, cnx, family);
2452
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002453 return cnx;
2454
2455 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002456 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002457 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002458 return error;
2459
2460}
2461
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002462static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002463{
Florian Westphalf24ea5282019-04-16 16:44:37 +02002464 if (family == AF_INET)
2465 return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08002466
Florian Westphalf24ea5282019-04-16 16:44:37 +02002467 return 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002468}
2469
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002470static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002471{
Florian Westphal37b10382017-02-07 15:00:19 +01002472 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002473 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08002474 struct xfrm_dst *xdst;
2475
2476 if (!afinfo)
2477 return ERR_PTR(-EINVAL);
2478
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002479 switch (family) {
2480 case AF_INET:
2481 dst_ops = &net->xfrm.xfrm4_dst_ops;
2482 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002483#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002484 case AF_INET6:
2485 dst_ops = &net->xfrm.xfrm6_dst_ops;
2486 break;
2487#endif
2488 default:
2489 BUG();
2490 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07002491 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08002492
Madalin Bucurd4cae562011-09-26 07:04:36 +00002493 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00002494 struct dst_entry *dst = &xdst->u.dst;
2495
2496 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00002497 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08002498 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00002499
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002500 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00002501
Herbert Xu25ee3282007-12-11 09:32:34 -08002502 return xdst;
2503}
2504
Florian Westphal2e8b4aa2019-04-16 16:44:38 +02002505static void xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2506 int nfheader_len)
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002507{
Florian Westphal2e8b4aa2019-04-16 16:44:38 +02002508 if (dst->ops->family == AF_INET6) {
2509 struct rt6_info *rt = (struct rt6_info *)dst;
2510 path->path_cookie = rt6_get_cookie(rt);
2511 path->u.rt6.rt6i_nfheader_len = nfheader_len;
2512 }
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002513}
2514
Herbert Xu87c1e122010-03-02 02:51:56 +00002515static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08002516 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08002517{
Florian Westphal37b10382017-02-07 15:00:19 +01002518 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08002519 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2520 int err;
2521
2522 if (!afinfo)
2523 return -EINVAL;
2524
Herbert Xu87c1e122010-03-02 02:51:56 +00002525 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002526
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002527 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002528
2529 return err;
2530}
2531
Timo Teräs80c802f2010-04-07 00:30:05 +00002532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533/* Allocate chain of dst_entry's, attach known xfrm's, calculate
2534 * all the metrics... Shortly, bundle a bundle.
2535 */
2536
Herbert Xu25ee3282007-12-11 09:32:34 -08002537static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05002538 struct xfrm_state **xfrm,
2539 struct xfrm_dst **bundle,
2540 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08002541 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08002542 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Florian Westphal733a5fa2019-03-29 21:16:30 +01002544 const struct xfrm_state_afinfo *afinfo;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002545 const struct xfrm_mode *inner_mode;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002546 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08002547 unsigned long now = jiffies;
2548 struct net_device *dev;
David Miller45b018be2017-11-28 15:40:28 -05002549 struct xfrm_dst *xdst_prev = NULL;
2550 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08002551 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08002553 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002554 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002555 int trailer_len = 0;
2556 int tos;
2557 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09002558 xfrm_address_t saddr, daddr;
2559
2560 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002561
2562 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002563
2564 dst_hold(dst);
2565
2566 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002567 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002568 struct dst_entry *dst1 = &xdst->u.dst;
2569
2570 err = PTR_ERR(xdst);
2571 if (IS_ERR(xdst)) {
2572 dst_release(dst);
2573 goto put_states;
2574 }
2575
David Miller54920932017-11-28 15:41:01 -05002576 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05002577 if (!xdst_prev)
2578 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07002579 else
2580 /* Ref count is taken during xfrm_alloc_dst()
2581 * No need to do dst_clone() on dst1
2582 */
David Miller45b018be2017-11-28 15:40:28 -05002583 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07002584
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002585 if (xfrm[i]->sel.family == AF_UNSPEC) {
2586 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2587 xfrm_af2proto(family));
2588 if (!inner_mode) {
2589 err = -EAFNOSUPPORT;
2590 dst_release(dst);
2591 goto put_states;
2592 }
2593 } else
Florian Westphalc9500d72019-03-29 21:16:32 +01002594 inner_mode = &xfrm[i]->inner_mode;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002595
Herbert Xu25ee3282007-12-11 09:32:34 -08002596 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08002597 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08002598
2599 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Benedict Wonge2612cd2019-01-14 11:24:38 -08002600 __u32 mark = 0;
2601
2602 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
2603 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002604
Herbert Xu25ee3282007-12-11 09:32:34 -08002605 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06002606 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002607 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08002608 err = PTR_ERR(dst);
2609 if (IS_ERR(dst))
2610 goto put_states;
2611 } else
2612 dst_hold(dst);
2613
2614 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00002615 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08002616
David S. Millerf5b0a872012-07-19 12:31:33 -07002617 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08002618 dst1->lastuse = now;
2619
2620 dst1->input = dst_discard;
Florian Westphal733a5fa2019-03-29 21:16:30 +01002621
2622 rcu_read_lock();
2623 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
2624 if (likely(afinfo))
2625 dst1->output = afinfo->output;
2626 else
2627 dst1->output = dst_discard_out;
2628 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002629
David Miller45b018be2017-11-28 15:40:28 -05002630 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002631
2632 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002633 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2634 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002635 trailer_len += xfrm[i]->props.trailer_len;
2636 }
2637
David Miller45b018be2017-11-28 15:40:28 -05002638 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002639 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002640
2641 err = -ENODEV;
2642 dev = dst->dev;
2643 if (!dev)
2644 goto free_dst;
2645
David Miller45b018be2017-11-28 15:40:28 -05002646 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002647 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002648
David Miller45b018be2017-11-28 15:40:28 -05002649 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2650 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2651 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002652 if (err)
2653 goto free_dst;
2654
David Miller45b018be2017-11-28 15:40:28 -05002655 xdst_prev->u.dst.header_len = header_len;
2656 xdst_prev->u.dst.trailer_len = trailer_len;
2657 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2658 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002659 }
2660
David Miller45b018be2017-11-28 15:40:28 -05002661 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002662
2663put_states:
2664 for (; i < nx; i++)
2665 xfrm_state_put(xfrm[i]);
2666free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002667 if (xdst0)
2668 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002669
2670 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
David S. Miller73ff93c2011-02-22 18:33:42 -08002673static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002674 struct xfrm_policy **pols,
2675 int *num_pols, int *num_xfrms)
2676{
2677 int i;
2678
2679 if (*num_pols == 0 || !pols[0]) {
2680 *num_pols = 0;
2681 *num_xfrms = 0;
2682 return 0;
2683 }
2684 if (IS_ERR(pols[0]))
2685 return PTR_ERR(pols[0]);
2686
2687 *num_xfrms = pols[0]->xfrm_nr;
2688
2689#ifdef CONFIG_XFRM_SUB_POLICY
2690 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2691 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2692 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2693 XFRM_POLICY_TYPE_MAIN,
2694 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002695 XFRM_POLICY_OUT,
2696 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002697 if (pols[1]) {
2698 if (IS_ERR(pols[1])) {
2699 xfrm_pols_put(pols, *num_pols);
2700 return PTR_ERR(pols[1]);
2701 }
Weilong Chen02d08922013-12-24 09:43:48 +08002702 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002703 (*num_xfrms) += pols[1]->xfrm_nr;
2704 }
2705 }
2706#endif
2707 for (i = 0; i < *num_pols; i++) {
2708 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2709 *num_xfrms = -1;
2710 break;
2711 }
2712 }
2713
2714 return 0;
2715
2716}
2717
2718static struct xfrm_dst *
2719xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002720 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002721 struct dst_entry *dst_orig)
2722{
2723 struct net *net = xp_net(pols[0]);
2724 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002725 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002726 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002727 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002728 int err;
2729
2730 /* Try to instantiate a bundle */
2731 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002732 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002733 if (err == 0)
2734 return NULL;
2735
2736 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002737 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2738 return ERR_PTR(err);
2739 }
2740
David Miller54920932017-11-28 15:41:01 -05002741 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002742 if (IS_ERR(dst)) {
2743 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2744 return ERR_CAST(dst);
2745 }
2746
2747 xdst = (struct xfrm_dst *)dst;
2748 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002749 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002750 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002751 xdst->policy_genid = atomic_read(&pols[0]->genid);
2752
2753 return xdst;
2754}
2755
Kees Cookc3aed702017-10-16 17:28:56 -07002756static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002757{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002758 struct sk_buff *skb;
2759 struct sock *sk;
2760 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002761 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002762 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002763 struct xfrm_policy_queue *pq = &pol->polq;
2764 struct flowi fl;
2765 struct sk_buff_head list;
2766
2767 spin_lock(&pq->hold_queue.lock);
2768 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002769 if (!skb) {
2770 spin_unlock(&pq->hold_queue.lock);
2771 goto out;
2772 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002773 dst = skb_dst(skb);
2774 sk = skb->sk;
2775 xfrm_decode_session(skb, &fl, dst->ops->family);
2776 spin_unlock(&pq->hold_queue.lock);
2777
David Miller0f6c4802017-11-28 15:40:46 -05002778 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c9812018-02-01 11:26:12 +01002779 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002780 if (IS_ERR(dst))
2781 goto purge_queue;
2782
2783 if (dst->flags & DST_XFRM_QUEUE) {
2784 dst_release(dst);
2785
2786 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2787 goto purge_queue;
2788
2789 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002790 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2791 xfrm_pol_hold(pol);
Colin Ian King7759d6a2018-11-13 14:28:42 +00002792 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002793 }
2794
2795 dst_release(dst);
2796
2797 __skb_queue_head_init(&list);
2798
2799 spin_lock(&pq->hold_queue.lock);
2800 pq->timeout = 0;
2801 skb_queue_splice_init(&pq->hold_queue, &list);
2802 spin_unlock(&pq->hold_queue.lock);
2803
2804 while (!skb_queue_empty(&list)) {
2805 skb = __skb_dequeue(&list);
2806
2807 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002808 dst_hold(xfrm_dst_path(skb_dst(skb)));
2809 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002810 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002811 kfree_skb(skb);
2812 continue;
2813 }
2814
Florian Westphal895b5c92019-09-29 20:54:03 +02002815 nf_reset_ct(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002816 skb_dst_drop(skb);
2817 skb_dst_set(skb, dst);
2818
Eric W. Biederman13206b62015-10-07 16:48:35 -05002819 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002820 }
2821
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002822out:
2823 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002824 return;
2825
2826purge_queue:
2827 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002828 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002829 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002830}
2831
Eric W. Biedermanede20592015-10-07 16:48:47 -05002832static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002833{
2834 unsigned long sched_next;
2835 struct dst_entry *dst = skb_dst(skb);
2836 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002837 struct xfrm_policy *pol = xdst->pols[0];
2838 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002839
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002840 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002841 kfree_skb(skb);
2842 return 0;
2843 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002844
2845 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2846 kfree_skb(skb);
2847 return -EAGAIN;
2848 }
2849
2850 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002851
2852 spin_lock_bh(&pq->hold_queue.lock);
2853
2854 if (!pq->timeout)
2855 pq->timeout = XFRM_QUEUE_TMO_MIN;
2856
2857 sched_next = jiffies + pq->timeout;
2858
2859 if (del_timer(&pq->hold_timer)) {
2860 if (time_before(pq->hold_timer.expires, sched_next))
2861 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002862 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002863 }
2864
2865 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002866 if (!mod_timer(&pq->hold_timer, sched_next))
2867 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002868
2869 spin_unlock_bh(&pq->hold_queue.lock);
2870
2871 return 0;
2872}
2873
2874static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002875 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002876 const struct flowi *fl,
2877 int num_xfrms,
2878 u16 family)
2879{
2880 int err;
2881 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002882 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002883 struct dst_entry *dst1;
2884 struct xfrm_dst *xdst;
2885
2886 xdst = xfrm_alloc_dst(net, family);
2887 if (IS_ERR(xdst))
2888 return xdst;
2889
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002890 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2891 net->xfrm.sysctl_larval_drop ||
2892 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002893 return xdst;
2894
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002895 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002896 dst1 = &xdst->u.dst;
2897 dst_hold(dst);
2898 xdst->route = dst;
2899
2900 dst_copy_metrics(dst1, dst);
2901
2902 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
David Laightaf13b3c2020-03-23 14:31:19 +00002903 dst1->flags |= DST_XFRM_QUEUE;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002904 dst1->lastuse = jiffies;
2905
2906 dst1->input = dst_discard;
2907 dst1->output = xdst_queue_output;
2908
2909 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002910 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002911 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002912
2913 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2914
2915 err = -ENODEV;
2916 dev = dst->dev;
2917 if (!dev)
2918 goto free_dst;
2919
2920 err = xfrm_fill_dst(xdst, dev, fl);
2921 if (err)
2922 goto free_dst;
2923
2924out:
2925 return xdst;
2926
2927free_dst:
2928 dst_release(dst1);
2929 xdst = ERR_PTR(err);
2930 goto out;
2931}
2932
Benedict Wongbc56b332018-07-19 10:50:44 -07002933static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2934 const struct flowi *fl,
2935 u16 family, u8 dir,
2936 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002937{
Timo Teräs80c802f2010-04-07 00:30:05 +00002938 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002939 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002940 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002941
Timo Teräs80c802f2010-04-07 00:30:05 +00002942 /* Resolve policies to use if we couldn't get them from
2943 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002944 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002945 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002946 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002947 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002948 if (err < 0)
2949 goto inc_error;
2950 if (num_pols == 0)
2951 return NULL;
2952 if (num_xfrms <= 0)
2953 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002954
Florian Westphalbd45c532017-07-17 13:57:25 +02002955 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002956 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002957 if (IS_ERR(xdst)) {
2958 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002959 if (err == -EREMOTE) {
2960 xfrm_pols_put(pols, num_pols);
2961 return NULL;
2962 }
2963
Timo Teräs80c802f2010-04-07 00:30:05 +00002964 if (err != -EAGAIN)
2965 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002966 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002967 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002968 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002969 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002970 }
2971
Florian Westphalbd45c532017-07-17 13:57:25 +02002972 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002973
2974make_dummy_bundle:
2975 /* We found policies, but there's no bundles to instantiate:
2976 * either because the policy blocks, has no transformations or
2977 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002978 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002979 if (IS_ERR(xdst)) {
2980 xfrm_pols_put(pols, num_pols);
2981 return ERR_CAST(xdst);
2982 }
2983 xdst->num_pols = num_pols;
2984 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002985 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002986
Florian Westphalbd45c532017-07-17 13:57:25 +02002987 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002988
2989inc_error:
2990 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2991error:
Florian Westphal855dad92017-07-17 13:57:22 +02002992 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002993 return ERR_PTR(err);
2994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
David S. Miller2774c132011-03-01 14:59:04 -08002996static struct dst_entry *make_blackhole(struct net *net, u16 family,
2997 struct dst_entry *dst_orig)
2998{
Florian Westphal37b10382017-02-07 15:00:19 +01002999 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08003000 struct dst_entry *ret;
3001
3002 if (!afinfo) {
3003 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00003004 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08003005 } else {
3006 ret = afinfo->blackhole_route(net, dst_orig);
3007 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003008 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08003009
3010 return ret;
3011}
3012
Benedict Wongbc56b332018-07-19 10:50:44 -07003013/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 *
3015 * At the moment we eat a raw IP route. Mostly to speed up lookups
3016 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07003017 *
3018 * xfrm_lookup uses an if_id of 0 by default, and is provided for
3019 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 */
Benedict Wongbc56b332018-07-19 10:50:44 -07003021struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
3022 struct dst_entry *dst_orig,
3023 const struct flowi *fl,
3024 const struct sock *sk,
3025 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003027 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00003028 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08003029 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00003030 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02003031 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00003032 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003033
Timo Teräs80c802f2010-04-07 00:30:05 +00003034 dst = NULL;
3035 xdst = NULL;
3036 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003037
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003038 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07003039 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003040 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003041 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
3042 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00003043 err = xfrm_expand_policies(fl, family, pols,
3044 &num_pols, &num_xfrms);
3045 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08003046 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00003047
3048 if (num_pols) {
3049 if (num_xfrms <= 0) {
3050 drop_pols = num_pols;
3051 goto no_transform;
3052 }
3053
3054 xdst = xfrm_resolve_and_create_bundle(
3055 pols, num_pols, fl,
3056 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01003057
Timo Teräs80c802f2010-04-07 00:30:05 +00003058 if (IS_ERR(xdst)) {
3059 xfrm_pols_put(pols, num_pols);
3060 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02003061 if (err == -EREMOTE)
3062 goto nopol;
3063
Timo Teräs80c802f2010-04-07 00:30:05 +00003064 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00003065 } else if (xdst == NULL) {
3066 num_xfrms = 0;
3067 drop_pols = num_pols;
3068 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00003069 }
3070
Timo Teräs80c802f2010-04-07 00:30:05 +00003071 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003072 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Timo Teräs80c802f2010-04-07 00:30:05 +00003075 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003076 struct xfrm_flo xflo;
3077
3078 xflo.dst_orig = dst_orig;
3079 xflo.flags = flags;
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07003082 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08003083 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08003084 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Benedict Wongbc56b332018-07-19 10:50:44 -07003086 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02003087 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00003088 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02003089 if (IS_ERR(xdst)) {
3090 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003091 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08003092 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003093
3094 num_pols = xdst->num_pols;
3095 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003096 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00003097 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099
Timo Teräs80c802f2010-04-07 00:30:05 +00003100 dst = &xdst->u.dst;
3101 if (route == NULL && num_xfrms > 0) {
3102 /* The only case when xfrm_bundle_lookup() returns a
3103 * bundle with null route, is when the template could
3104 * not be resolved. It means policies are there, but
3105 * bundle could not be created, since we don't yet
3106 * have the xfrm_state's. We need to wait for KM to
3107 * negotiate new SA's or bail out with error.*/
3108 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003109 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01003110 err = -EREMOTE;
3111 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003112 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003113
Steffen Klassert5b8ef342013-08-27 13:43:30 +02003114 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00003115
3116 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3117 goto error;
3118 }
3119
3120no_transform:
3121 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08003122 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Timo Teräs80c802f2010-04-07 00:30:05 +00003124 if ((flags & XFRM_LOOKUP_ICMP) &&
3125 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3126 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08003127 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003128 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08003129
Timo Teräs80c802f2010-04-07 00:30:05 +00003130 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02003131 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08003132
Timo Teräs80c802f2010-04-07 00:30:05 +00003133 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003135 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07003136 err = -EPERM;
3137 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003138 } else if (num_xfrms > 0) {
3139 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00003140 dst_release(dst_orig);
3141 } else {
3142 /* Flow passes untransformed */
3143 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003144 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003146ok:
3147 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003148 if (dst && dst->xfrm &&
3149 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3150 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003151 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Timo Teräs80c802f2010-04-07 00:30:05 +00003153nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003154 if (!(flags & XFRM_LOOKUP_ICMP)) {
3155 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003156 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003157 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003158 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003160 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003161dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003162 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3163 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003164 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003165 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
Benedict Wongbc56b332018-07-19 10:50:44 -07003167EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3168
3169/* Main function: finds/creates a bundle for given flow.
3170 *
3171 * At the moment we eat a raw IP route. Mostly to speed up lookups
3172 * on interfaces with disabled IPsec.
3173 */
3174struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3175 const struct flowi *fl, const struct sock *sk,
3176 int flags)
3177{
3178 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180EXPORT_SYMBOL(xfrm_lookup);
3181
Steffen Klassertf92ee612014-09-16 10:08:40 +02003182/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3183 * Otherwise we may send out blackholed packets.
3184 */
3185struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3186 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003187 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003188{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003189 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003190 flags | XFRM_LOOKUP_QUEUE |
3191 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003192
Masahiro Yamada45586c72020-02-03 17:37:45 -08003193 if (PTR_ERR(dst) == -EREMOTE)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003194 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3195
Tommi Rantala8cc88772018-06-21 09:30:47 +03003196 if (IS_ERR(dst))
3197 dst_release(dst_orig);
3198
Steffen Klassertf92ee612014-09-16 10:08:40 +02003199 return dst;
3200}
3201EXPORT_SYMBOL(xfrm_lookup_route);
3202
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003203static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003204xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003205{
Florian Westphal2294be0f2018-12-18 17:15:20 +01003206 struct sec_path *sp = skb_sec_path(skb);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003207 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003208
Florian Westphal2294be0f2018-12-18 17:15:20 +01003209 if (!sp || idx < 0 || idx >= sp->len)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003210 return 0;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003211 x = sp->xvec[idx];
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003212 if (!x->type->reject)
3213 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003214 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003215}
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217/* When skb is transformed back to its "native" form, we have to
3218 * check policy restrictions. At the moment we make this in maximally
3219 * stupid way. Shame on me. :-) Of course, connected sockets must
3220 * have policy cached at them.
3221 */
3222
3223static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003224xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 unsigned short family)
3226{
3227 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003228 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return x->id.proto == tmpl->id.proto &&
3230 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3231 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3232 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003233 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003234 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003235 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3236 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003239/*
3240 * 0 or more than 0 is returned when validation is succeeded (either bypass
3241 * because of optional transport mode, or next index of the mathced secpath
3242 * state with the template.
3243 * -1 is returned when no matching template is found.
3244 * Otherwise "-2 - errored_index" is returned.
3245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003247xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 unsigned short family)
3249{
3250 int idx = start;
3251
3252 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003253 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return start;
3255 } else
3256 start = -1;
3257 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003258 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003260 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3261 if (start == -1)
3262 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 return start;
3267}
3268
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003269static void
3270decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse)
3271{
3272 const struct iphdr *iph = ip_hdr(skb);
Florian Westphal858e5402019-05-16 11:28:16 +02003273 int ihl = iph->ihl;
3274 u8 *xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003275 struct flowi4 *fl4 = &fl->u.ip4;
3276 int oif = 0;
3277
Hangbin Liuc3b4c3a2019-08-22 22:19:49 +08003278 if (skb_dst(skb) && skb_dst(skb)->dev)
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003279 oif = skb_dst(skb)->dev->ifindex;
3280
3281 memset(fl4, 0, sizeof(struct flowi4));
3282 fl4->flowi4_mark = skb->mark;
3283 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
3284
Florian Westphal858e5402019-05-16 11:28:16 +02003285 fl4->flowi4_proto = iph->protocol;
3286 fl4->daddr = reverse ? iph->saddr : iph->daddr;
3287 fl4->saddr = reverse ? iph->daddr : iph->saddr;
3288 fl4->flowi4_tos = iph->tos;
3289
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003290 if (!ip_is_fragment(iph)) {
3291 switch (iph->protocol) {
3292 case IPPROTO_UDP:
3293 case IPPROTO_UDPLITE:
3294 case IPPROTO_TCP:
3295 case IPPROTO_SCTP:
3296 case IPPROTO_DCCP:
3297 if (xprth + 4 < skb->data ||
3298 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3299 __be16 *ports;
3300
Florian Westphal858e5402019-05-16 11:28:16 +02003301 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003302 ports = (__be16 *)xprth;
3303
3304 fl4->fl4_sport = ports[!!reverse];
3305 fl4->fl4_dport = ports[!reverse];
3306 }
3307 break;
3308 case IPPROTO_ICMP:
3309 if (xprth + 2 < skb->data ||
3310 pskb_may_pull(skb, xprth + 2 - skb->data)) {
3311 u8 *icmp;
3312
Florian Westphal858e5402019-05-16 11:28:16 +02003313 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003314 icmp = xprth;
3315
3316 fl4->fl4_icmp_type = icmp[0];
3317 fl4->fl4_icmp_code = icmp[1];
3318 }
3319 break;
3320 case IPPROTO_ESP:
3321 if (xprth + 4 < skb->data ||
3322 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3323 __be32 *ehdr;
3324
Florian Westphal858e5402019-05-16 11:28:16 +02003325 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003326 ehdr = (__be32 *)xprth;
3327
3328 fl4->fl4_ipsec_spi = ehdr[0];
3329 }
3330 break;
3331 case IPPROTO_AH:
3332 if (xprth + 8 < skb->data ||
3333 pskb_may_pull(skb, xprth + 8 - skb->data)) {
3334 __be32 *ah_hdr;
3335
Florian Westphal858e5402019-05-16 11:28:16 +02003336 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003337 ah_hdr = (__be32 *)xprth;
3338
3339 fl4->fl4_ipsec_spi = ah_hdr[1];
3340 }
3341 break;
3342 case IPPROTO_COMP:
3343 if (xprth + 4 < skb->data ||
3344 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3345 __be16 *ipcomp_hdr;
3346
Florian Westphal858e5402019-05-16 11:28:16 +02003347 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003348 ipcomp_hdr = (__be16 *)xprth;
3349
3350 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
3351 }
3352 break;
3353 case IPPROTO_GRE:
3354 if (xprth + 12 < skb->data ||
3355 pskb_may_pull(skb, xprth + 12 - skb->data)) {
3356 __be16 *greflags;
3357 __be32 *gre_hdr;
3358
Florian Westphal858e5402019-05-16 11:28:16 +02003359 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003360 greflags = (__be16 *)xprth;
3361 gre_hdr = (__be32 *)xprth;
3362
3363 if (greflags[0] & GRE_KEY) {
3364 if (greflags[0] & GRE_CSUM)
3365 gre_hdr++;
3366 fl4->fl4_gre_key = gre_hdr[1];
3367 }
3368 }
3369 break;
3370 default:
3371 fl4->fl4_ipsec_spi = 0;
3372 break;
3373 }
3374 }
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003375}
3376
3377#if IS_ENABLED(CONFIG_IPV6)
3378static void
3379decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse)
3380{
3381 struct flowi6 *fl6 = &fl->u.ip6;
3382 int onlyproto = 0;
3383 const struct ipv6hdr *hdr = ipv6_hdr(skb);
3384 u32 offset = sizeof(*hdr);
3385 struct ipv6_opt_hdr *exthdr;
3386 const unsigned char *nh = skb_network_header(skb);
3387 u16 nhoff = IP6CB(skb)->nhoff;
3388 int oif = 0;
3389 u8 nexthdr;
3390
3391 if (!nhoff)
3392 nhoff = offsetof(struct ipv6hdr, nexthdr);
3393
3394 nexthdr = nh[nhoff];
3395
Hangbin Liuc3b4c3a2019-08-22 22:19:49 +08003396 if (skb_dst(skb) && skb_dst(skb)->dev)
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003397 oif = skb_dst(skb)->dev->ifindex;
3398
3399 memset(fl6, 0, sizeof(struct flowi6));
3400 fl6->flowi6_mark = skb->mark;
3401 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
3402
3403 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
3404 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
3405
3406 while (nh + offset + sizeof(*exthdr) < skb->data ||
3407 pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
3408 nh = skb_network_header(skb);
3409 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3410
3411 switch (nexthdr) {
3412 case NEXTHDR_FRAGMENT:
3413 onlyproto = 1;
3414 /* fall through */
3415 case NEXTHDR_ROUTING:
3416 case NEXTHDR_HOP:
3417 case NEXTHDR_DEST:
3418 offset += ipv6_optlen(exthdr);
3419 nexthdr = exthdr->nexthdr;
3420 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3421 break;
3422 case IPPROTO_UDP:
3423 case IPPROTO_UDPLITE:
3424 case IPPROTO_TCP:
3425 case IPPROTO_SCTP:
3426 case IPPROTO_DCCP:
3427 if (!onlyproto && (nh + offset + 4 < skb->data ||
3428 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
3429 __be16 *ports;
3430
3431 nh = skb_network_header(skb);
3432 ports = (__be16 *)(nh + offset);
3433 fl6->fl6_sport = ports[!!reverse];
3434 fl6->fl6_dport = ports[!reverse];
3435 }
3436 fl6->flowi6_proto = nexthdr;
3437 return;
3438 case IPPROTO_ICMPV6:
3439 if (!onlyproto && (nh + offset + 2 < skb->data ||
3440 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
3441 u8 *icmp;
3442
3443 nh = skb_network_header(skb);
3444 icmp = (u8 *)(nh + offset);
3445 fl6->fl6_icmp_type = icmp[0];
3446 fl6->fl6_icmp_code = icmp[1];
3447 }
3448 fl6->flowi6_proto = nexthdr;
3449 return;
3450#if IS_ENABLED(CONFIG_IPV6_MIP6)
3451 case IPPROTO_MH:
3452 offset += ipv6_optlen(exthdr);
3453 if (!onlyproto && (nh + offset + 3 < skb->data ||
3454 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
3455 struct ip6_mh *mh;
3456
3457 nh = skb_network_header(skb);
3458 mh = (struct ip6_mh *)(nh + offset);
3459 fl6->fl6_mh_type = mh->ip6mh_type;
3460 }
3461 fl6->flowi6_proto = nexthdr;
3462 return;
3463#endif
3464 /* XXX Why are there these headers? */
3465 case IPPROTO_AH:
3466 case IPPROTO_ESP:
3467 case IPPROTO_COMP:
3468 default:
3469 fl6->fl6_ipsec_spi = 0;
3470 fl6->flowi6_proto = nexthdr;
3471 return;
3472 }
3473 }
3474}
3475#endif
3476
Herbert Xud5422ef2007-12-12 10:44:16 -08003477int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3478 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003480 switch (family) {
3481 case AF_INET:
3482 decode_session4(skb, fl, reverse);
3483 break;
3484#if IS_ENABLED(CONFIG_IPV6)
3485 case AF_INET6:
3486 decode_session6(skb, fl, reverse);
3487 break;
3488#endif
3489 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return -EAFNOSUPPORT;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003493 return security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
Herbert Xud5422ef2007-12-12 10:44:16 -08003495EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
David S. Miller9a7386e2011-02-24 01:44:12 -05003497static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003500 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003501 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505
3506 return 0;
3507}
3508
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003509int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 unsigned short family)
3511{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003512 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003514 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3515 int npols = 0;
3516 int xfrm_nr;
3517 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003518 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003520 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003521 const struct xfrm_if_cb *ifcb;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003522 struct sec_path *sp;
Benedict Wongbc56b332018-07-19 10:50:44 -07003523 struct xfrm_if *xi;
3524 u32 if_id = 0;
3525
3526 rcu_read_lock();
3527 ifcb = xfrm_if_get_cb();
3528
3529 if (ifcb) {
Martin Willi025c65e2019-03-26 13:20:43 +01003530 xi = ifcb->decode_session(skb, family);
Tobias Brunner660899d2019-02-18 10:49:39 +01003531 if (xi) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003532 if_id = xi->p.if_id;
Tobias Brunner660899d2019-02-18 10:49:39 +01003533 net = xi->net;
3534 }
Benedict Wongbc56b332018-07-19 10:50:44 -07003535 }
3536 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Herbert Xud5422ef2007-12-12 10:44:16 -08003538 reverse = dir & ~XFRM_POLICY_MASK;
3539 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003540
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003541 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003542 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003544 }
3545
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003546 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
3548 /* First, check used SA against their selectors. */
Florian Westphal2294be0f2018-12-18 17:15:20 +01003549 sp = skb_sec_path(skb);
3550 if (sp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 int i;
3552
Florian Westphal2294be0f2018-12-18 17:15:20 +01003553 for (i = sp->len - 1; i >= 0; i--) {
3554 struct xfrm_state *x = sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003555 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003556 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 }
3561
3562 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003563 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003564 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003565 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003566 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003567 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003568 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003569 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003572 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003573 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003575 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003576 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003577 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003578 }
James Morris134b0fc2006-10-05 15:42:27 -05003579
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003580 if (!pol) {
Florian Westphal2294be0f2018-12-18 17:15:20 +01003581 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003582 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003583 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003584 return 0;
3585 }
3586 return 1;
3587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Arnd Bergmann386c5682018-07-11 12:19:13 +02003589 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003591 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003592 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003593#ifdef CONFIG_XFRM_SUB_POLICY
3594 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003595 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003596 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003597 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003598 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003599 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003600 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003601 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003602 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003603 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003604 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003605 }
3606 }
3607#endif
3608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (pol->action == XFRM_POLICY_ALLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003611 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003612 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003613 struct xfrm_tmpl **tpp = tp;
3614 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 int i, k;
3616
Florian Westphal2294be0f2018-12-18 17:15:20 +01003617 sp = skb_sec_path(skb);
3618 if (!sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 sp = &dummy;
3620
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003621 for (pi = 0; pi < npols; pi++) {
3622 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003623 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003624 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003625 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003626 }
3627 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003628 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003629 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003630 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003631 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3632 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3633 }
3634 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003635 if (npols > 1) {
Florian Westphal3aaf3912019-05-03 17:46:17 +02003636 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003637 tpp = stp;
3638 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 /* For each tunnel xfrm, find the first matching tmpl.
3641 * For each tmpl before that, find corresponding xfrm.
3642 * Order is _important_. Later we will implement
3643 * some barriers, but at the moment barriers
3644 * are implied between each two transformations.
3645 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003646 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3647 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003648 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003649 if (k < -1)
3650 /* "-2 - errored_index" returned */
3651 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003652 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 }
3656
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003657 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003658 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003662 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return 1;
3664 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003665 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003668 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003669reject_error:
3670 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return 0;
3672}
3673EXPORT_SYMBOL(__xfrm_policy_check);
3674
3675int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3676{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003677 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003679 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003680 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003682 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003683 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003687 skb_dst_force(skb);
Steffen Klassert9e1437932018-09-11 10:31:15 +02003688 if (!skb_dst(skb)) {
3689 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3690 return 0;
3691 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003692
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003693 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003694 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003695 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003696 dst = NULL;
3697 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003698 skb_dst_set(skb, dst);
3699 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701EXPORT_SYMBOL(__xfrm_route_forward);
3702
David S. Millerd49c73c2006-08-13 18:55:53 -07003703/* Optimize later using cookies and generation ids. */
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3706{
David S. Millerd49c73c2006-08-13 18:55:53 -07003707 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003708 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3709 * get validated by dst_ops->check on every use. We do this
3710 * because when a normal route referenced by an XFRM dst is
3711 * obsoleted we do not go looking around for all parent
3712 * referencing XFRM dsts so that we can invalidate them. It
3713 * is just too much work. Instead we make the checks here on
3714 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003715 *
3716 * XFRM dst A --> IPv4 dst X
3717 *
3718 * X is the "xdst->route" of A (X is also the "dst->path" of A
3719 * in this example). If X is marked obsolete, "A" will not
3720 * notice. That's what we are validating here via the
3721 * stale_bundle() check.
3722 *
Wei Wang52df1572017-06-17 10:42:38 -07003723 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3724 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003725 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003726 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003727 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003728 if (dst->obsolete < 0 && !stale_bundle(dst))
3729 return dst;
3730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return NULL;
3732}
3733
3734static int stale_bundle(struct dst_entry *dst)
3735{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003736 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737}
3738
Herbert Xuaabc9762005-05-03 16:27:10 -07003739void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
David Millerb92cf4a2017-11-28 15:40:22 -05003741 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003742 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003743 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 dev_put(dev);
3745 }
3746}
Herbert Xuaabc9762005-05-03 16:27:10 -07003747EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749static void xfrm_link_failure(struct sk_buff *skb)
3750{
3751 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752}
3753
3754static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3755{
3756 if (dst) {
3757 if (dst->obsolete) {
3758 dst_release(dst);
3759 dst = NULL;
3760 }
3761 }
3762 return dst;
3763}
3764
David Miller54920932017-11-28 15:41:01 -05003765static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
David Miller54920932017-11-28 15:41:01 -05003767 while (nr--) {
3768 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003770 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
David Miller54920932017-11-28 15:41:01 -05003772 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003773 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 xdst->child_mtu_cached = pmtu;
3775
3776 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3777
3778 route_mtu_cached = dst_mtu(xdst->route);
3779 xdst->route_mtu_cached = route_mtu_cached;
3780
3781 if (pmtu > route_mtu_cached)
3782 pmtu = route_mtu_cached;
3783
David S. Millerdefb3512010-12-08 21:16:57 -08003784 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788/* Check that the bundle accepts the flow and its components are
3789 * still valid.
3790 */
3791
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003792static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
David Miller54920932017-11-28 15:41:01 -05003794 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003796 struct xfrm_dst *xdst;
3797 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 u32 mtu;
3799
David Miller0f6c4802017-11-28 15:40:46 -05003800 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 (dst->dev && !netif_running(dst->dev)))
3802 return 0;
3803
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003804 if (dst->flags & DST_XFRM_QUEUE)
3805 return 1;
3806
David Miller54920932017-11-28 15:41:01 -05003807 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 do {
3809 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3812 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003813 if (xdst->xfrm_genid != dst->xfrm->genid)
3814 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003815 if (xdst->num_pols > 0 &&
3816 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
David Miller54920932017-11-28 15:41:01 -05003819 bundle[nr++] = xdst;
3820
David Millerb92cf4a2017-11-28 15:40:22 -05003821 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003823 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 xdst->child_mtu_cached = mtu;
3825 }
3826
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003827 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 return 0;
3829 mtu = dst_mtu(xdst->route);
3830 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003831 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 xdst->route_mtu_cached = mtu;
3833 }
3834
David Millerb92cf4a2017-11-28 15:40:22 -05003835 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 } while (dst->xfrm);
3837
David Miller54920932017-11-28 15:41:01 -05003838 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return 1;
3840
David Miller54920932017-11-28 15:41:01 -05003841 xdst = bundle[start_from - 1];
3842 mtu = xdst->child_mtu_cached;
3843 while (start_from--) {
3844 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
3846 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003847 if (mtu > xdst->route_mtu_cached)
3848 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003849 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003850 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 break;
3852
David Miller54920932017-11-28 15:41:01 -05003853 xdst = bundle[start_from - 1];
3854 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
3856
3857 return 1;
3858}
3859
David S. Miller0dbaee32010-12-13 12:52:14 -08003860static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3861{
David Miller0f6c4802017-11-28 15:40:46 -05003862 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003863}
3864
Steffen Klassertebb762f2011-11-23 02:12:51 +00003865static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003866{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003867 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3868
David Miller0f6c4802017-11-28 15:40:46 -05003869 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003870}
3871
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003872static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3873 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003874{
David Miller0f6c4802017-11-28 15:40:46 -05003875 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003876 const struct xfrm_state *xfrm = dst->xfrm;
3877
Steffen Klassert013cb812018-02-19 07:44:07 +01003878 dst = xfrm_dst_child(dst);
3879
Julian Anastasov63fca652017-02-06 23:14:15 +02003880 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3881 continue;
3882 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3883 daddr = xfrm->coaddr;
3884 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3885 daddr = &xfrm->id.daddr;
3886 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003887 return daddr;
3888}
3889
3890static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3891 struct sk_buff *skb,
3892 const void *daddr)
3893{
David Miller0f6c4802017-11-28 15:40:46 -05003894 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003895
3896 if (!skb)
3897 daddr = xfrm_get_dst_nexthop(dst, daddr);
3898 return path->ops->neigh_lookup(path, skb, daddr);
3899}
3900
3901static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3902{
David Miller0f6c4802017-11-28 15:40:46 -05003903 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003904
3905 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003906 path->ops->confirm_neigh(path, daddr);
3907}
3908
Florian Westphala2817d82017-02-07 15:00:17 +01003909int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
3911 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003912
3913 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003915
Eric Dumazetef8531b2012-08-19 12:31:48 +02003916 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003917 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003918 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 else {
3920 struct dst_ops *dst_ops = afinfo->dst_ops;
3921 if (likely(dst_ops->kmem_cachep == NULL))
3922 dst_ops->kmem_cachep = xfrm_dst_cache;
3923 if (likely(dst_ops->check == NULL))
3924 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003925 if (likely(dst_ops->default_advmss == NULL))
3926 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003927 if (likely(dst_ops->mtu == NULL))
3928 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (likely(dst_ops->negative_advice == NULL))
3930 dst_ops->negative_advice = xfrm_negative_advice;
3931 if (likely(dst_ops->link_failure == NULL))
3932 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003933 if (likely(dst_ops->neigh_lookup == NULL))
3934 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003935 if (likely(!dst_ops->confirm_neigh))
3936 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003937 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003939 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return err;
3942}
3943EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3944
Florian Westphala2817d82017-02-07 15:00:17 +01003945void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Florian Westphal2b619972017-02-07 15:00:15 +01003947 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003948 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003949
Florian Westphala2817d82017-02-07 15:00:17 +01003950 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3951 if (xfrm_policy_afinfo[i] != afinfo)
3952 continue;
3953 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3954 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Florian Westphal2b619972017-02-07 15:00:15 +01003957 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Florian Westphal2b619972017-02-07 15:00:15 +01003959 dst_ops->kmem_cachep = NULL;
3960 dst_ops->check = NULL;
3961 dst_ops->negative_advice = NULL;
3962 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
3964EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3965
Steffen Klassertf203b762018-06-12 14:07:12 +02003966void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3967{
3968 spin_lock(&xfrm_if_cb_lock);
3969 rcu_assign_pointer(xfrm_if_cb, ifcb);
3970 spin_unlock(&xfrm_if_cb_lock);
3971}
3972EXPORT_SYMBOL(xfrm_if_register_cb);
3973
3974void xfrm_if_unregister_cb(void)
3975{
3976 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3977 synchronize_rcu();
3978}
3979EXPORT_SYMBOL(xfrm_if_unregister_cb);
3980
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003981#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003982static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003983{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003984 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003985 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3986 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003987 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003988 rv = xfrm_proc_init(net);
3989 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003990 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003991 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003992}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003993
3994static void xfrm_statistics_fini(struct net *net)
3995{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003996 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003997 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003998}
3999#else
4000static int __net_init xfrm_statistics_init(struct net *net)
4001{
4002 return 0;
4003}
4004
4005static void xfrm_statistics_fini(struct net *net)
4006{
4007}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08004008#endif
4009
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004010static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
David S. Miller2518c7c2006-08-24 04:45:07 -07004012 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01004013 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07004014
Florian Westphal24969fa2018-11-07 23:00:35 +01004015 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004016 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004018 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004019 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01004020 err = rhashtable_init(&xfrm_policy_inexact_table,
4021 &xfrm_pol_inexact_params);
4022 BUG_ON(err);
4023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
David S. Miller2518c7c2006-08-24 04:45:07 -07004025 hmask = 8 - 1;
4026 sz = (hmask+1) * sizeof(struct hlist_head);
4027
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004028 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
4029 if (!net->xfrm.policy_byidx)
4030 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004031 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07004032
Herbert Xu53c2e282014-11-13 17:09:49 +08004033 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07004034 struct xfrm_policy_hash *htab;
4035
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08004036 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08004037 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004038 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07004039
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004040 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07004041 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07004042 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004043 goto out_bydst;
4044 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02004045 htab->dbits4 = 32;
4046 htab->sbits4 = 32;
4047 htab->dbits6 = 128;
4048 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07004049 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004050 net->xfrm.policy_hthresh.lbits4 = 32;
4051 net->xfrm.policy_hthresh.rbits4 = 32;
4052 net->xfrm.policy_hthresh.lbits6 = 128;
4053 net->xfrm.policy_hthresh.rbits6 = 128;
4054
4055 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07004056
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004057 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01004058 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08004059 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004060 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004061 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004062
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004063out_bydst:
4064 for (dir--; dir >= 0; dir--) {
4065 struct xfrm_policy_hash *htab;
4066
4067 htab = &net->xfrm.policy_bydst[dir];
4068 xfrm_hash_free(htab->table, sz);
4069 }
4070 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004071out_byidx:
4072 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004075static void xfrm_policy_fini(struct net *net)
4076{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004077 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004078 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004079 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004080
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004081 flush_work(&net->xfrm.policy_hash_work);
4082#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09004083 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004084#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09004085 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004086
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004087 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004088
Herbert Xu53c2e282014-11-13 17:09:49 +08004089 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004090 struct xfrm_policy_hash *htab;
4091
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004092 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004093
4094 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01004095 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004096 WARN_ON(!hlist_empty(htab->table));
4097 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004098 }
4099
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004100 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004101 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
4102 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01004103
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004104 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4105 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
4106 __xfrm_policy_inexact_prune_bin(b, true);
4107 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004108}
4109
4110static int __net_init xfrm_net_init(struct net *net)
4111{
4112 int rv;
4113
Florian Westphalc2822222017-02-08 11:52:29 +01004114 /* Initialize the per-net locks here */
4115 spin_lock_init(&net->xfrm.xfrm_state_lock);
4116 spin_lock_init(&net->xfrm.xfrm_policy_lock);
4117 mutex_init(&net->xfrm.xfrm_cfg_mutex);
4118
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004119 rv = xfrm_statistics_init(net);
4120 if (rv < 0)
4121 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004122 rv = xfrm_state_init(net);
4123 if (rv < 0)
4124 goto out_state;
4125 rv = xfrm_policy_init(net);
4126 if (rv < 0)
4127 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004128 rv = xfrm_sysctl_init(net);
4129 if (rv < 0)
4130 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08004131
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004132 return 0;
4133
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004134out_sysctl:
4135 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004136out_policy:
4137 xfrm_state_fini(net);
4138out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004139 xfrm_statistics_fini(net);
4140out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004141 return rv;
4142}
4143
4144static void __net_exit xfrm_net_exit(struct net *net)
4145{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004146 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004147 xfrm_policy_fini(net);
4148 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004149 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004150}
4151
4152static struct pernet_operations __net_initdata xfrm_net_ops = {
4153 .init = xfrm_net_init,
4154 .exit = xfrm_net_exit,
4155};
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157void __init xfrm_init(void)
4158{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004159 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03004160 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02004161 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02004163
Sabrina Dubrocae27cca92019-11-25 14:49:02 +01004164#ifdef CONFIG_INET_ESPINTCP
4165 espintcp_init();
4166#endif
4167
Steffen Klassertf203b762018-06-12 14:07:12 +02004168 RCU_INIT_POINTER(xfrm_if_cb, NULL);
4169 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Joy Lattenab5f5e82007-09-17 11:51:22 -07004172#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08004173static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
4174 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004175{
Paul Moore875179f2007-12-01 23:27:18 +11004176 struct xfrm_sec_ctx *ctx = xp->security;
4177 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004178
Paul Moore875179f2007-12-01 23:27:18 +11004179 if (ctx)
4180 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
4181 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
4182
Weilong Chen9b7a7872013-12-24 09:43:46 +08004183 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07004184 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07004185 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004186 if (sel->prefixlen_s != 32)
4187 audit_log_format(audit_buf, " src_prefixlen=%d",
4188 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07004189 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004190 if (sel->prefixlen_d != 32)
4191 audit_log_format(audit_buf, " dst_prefixlen=%d",
4192 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004193 break;
4194 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004195 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004196 if (sel->prefixlen_s != 128)
4197 audit_log_format(audit_buf, " src_prefixlen=%d",
4198 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004199 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004200 if (sel->prefixlen_d != 128)
4201 audit_log_format(audit_buf, " dst_prefixlen=%d",
4202 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004203 break;
4204 }
4205}
4206
Tetsuo Handa2e710292014-04-22 21:48:30 +09004207void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004208{
4209 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004210
Paul Mooreafeb14b2007-12-21 14:58:11 -08004211 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004212 if (audit_buf == NULL)
4213 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004214 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004215 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004216 xfrm_audit_common_policyinfo(xp, audit_buf);
4217 audit_log_end(audit_buf);
4218}
4219EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
4220
Paul Moore68277ac2007-12-20 20:49:33 -08004221void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09004222 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004223{
4224 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004225
Paul Mooreafeb14b2007-12-21 14:58:11 -08004226 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004227 if (audit_buf == NULL)
4228 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004229 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004230 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004231 xfrm_audit_common_policyinfo(xp, audit_buf);
4232 audit_log_end(audit_buf);
4233}
4234EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
4235#endif
4236
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004237#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04004238static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
4239 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004240{
4241 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
4242 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004243 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
4244 sel_cmp->family) &&
4245 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
4246 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004247 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
4248 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004249 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004250 }
4251 } else {
4252 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004253 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004254 }
4255 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04004256 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004257}
4258
Weilong Chen3e94c2d2013-12-24 09:43:47 +08004259static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
4260 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004261{
4262 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004263 struct hlist_head *chain;
4264 u32 priority = ~0U;
4265
Florian Westphal9d0380d2016-08-11 15:17:59 +02004266 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08004267 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004268 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004269 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4270 pol->type == type) {
4271 ret = pol;
4272 priority = ret->priority;
4273 break;
4274 }
4275 }
Fan Du8d549c42013-11-07 17:47:49 +08004276 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01004277 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08004278 if ((pol->priority >= priority) && ret)
4279 break;
4280
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004281 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08004282 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004283 ret = pol;
4284 break;
4285 }
4286 }
4287
Li RongQing586f2eb2015-04-30 17:13:41 +08004288 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004289
Florian Westphal9d0380d2016-08-11 15:17:59 +02004290 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004291
4292 return ret;
4293}
4294
David S. Millerdd701752011-02-24 00:21:08 -05004295static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004296{
4297 int match = 0;
4298
4299 if (t->mode == m->mode && t->id.proto == m->proto &&
4300 (m->reqid == 0 || t->reqid == m->reqid)) {
4301 switch (t->mode) {
4302 case XFRM_MODE_TUNNEL:
4303 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004304 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
4305 m->old_family) &&
4306 xfrm_addr_equal(&t->saddr, &m->old_saddr,
4307 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004308 match = 1;
4309 }
4310 break;
4311 case XFRM_MODE_TRANSPORT:
4312 /* in case of transport mode, template does not store
4313 any IP addresses, hence we just compare mode and
4314 protocol */
4315 match = 1;
4316 break;
4317 default:
4318 break;
4319 }
4320 }
4321 return match;
4322}
4323
4324/* update endpoint address(es) of template(s) */
4325static int xfrm_policy_migrate(struct xfrm_policy *pol,
4326 struct xfrm_migrate *m, int num_migrate)
4327{
4328 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004329 int i, j, n = 0;
4330
4331 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07004332 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004333 /* target policy has been deleted */
4334 write_unlock_bh(&pol->lock);
4335 return -ENOENT;
4336 }
4337
4338 for (i = 0; i < pol->xfrm_nr; i++) {
4339 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4340 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4341 continue;
4342 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07004343 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4344 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004345 continue;
4346 /* update endpoints */
4347 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4348 sizeof(pol->xfrm_vec[i].id.daddr));
4349 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4350 sizeof(pol->xfrm_vec[i].saddr));
4351 pol->xfrm_vec[i].encap_family = mp->new_family;
4352 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00004353 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004354 }
4355 }
4356
4357 write_unlock_bh(&pol->lock);
4358
4359 if (!n)
4360 return -ENODATA;
4361
4362 return 0;
4363}
4364
David S. Millerdd701752011-02-24 00:21:08 -05004365static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004366{
4367 int i, j;
4368
4369 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4370 return -EINVAL;
4371
4372 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004373 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4374 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4375 return -EINVAL;
4376
4377 /* check if there is any duplicated entry */
4378 for (j = i + 1; j < num_migrate; j++) {
4379 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4380 sizeof(m[i].old_daddr)) &&
4381 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4382 sizeof(m[i].old_saddr)) &&
4383 m[i].proto == m[j].proto &&
4384 m[i].mode == m[j].mode &&
4385 m[i].reqid == m[j].reqid &&
4386 m[i].old_family == m[j].old_family)
4387 return -EINVAL;
4388 }
4389 }
4390
4391 return 0;
4392}
4393
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004394int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004395 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004396 struct xfrm_kmaddress *k, struct net *net,
4397 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004398{
4399 int i, err, nx_cur = 0, nx_new = 0;
4400 struct xfrm_policy *pol = NULL;
4401 struct xfrm_state *x, *xc;
4402 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4403 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4404 struct xfrm_migrate *mp;
4405
Vladis Dronov7bab0962017-08-02 19:50:14 +02004406 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004407 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4408 goto out;
4409
Vladis Dronov7bab0962017-08-02 19:50:14 +02004410 if (dir >= XFRM_POLICY_MAX) {
4411 err = -EINVAL;
4412 goto out;
4413 }
4414
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004415 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004416 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004417 err = -ENOENT;
4418 goto out;
4419 }
4420
4421 /* Stage 2 - find and update state(s) */
4422 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004423 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004424 x_cur[nx_cur] = x;
4425 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004426 xc = xfrm_state_migrate(x, mp, encap);
4427 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004428 x_new[nx_new] = xc;
4429 nx_new++;
4430 } else {
4431 err = -ENODATA;
4432 goto restore_state;
4433 }
4434 }
4435 }
4436
4437 /* Stage 3 - update policy */
4438 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4439 goto restore_state;
4440
4441 /* Stage 4 - delete old state(s) */
4442 if (nx_cur) {
4443 xfrm_states_put(x_cur, nx_cur);
4444 xfrm_states_delete(x_cur, nx_cur);
4445 }
4446
4447 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004448 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004449
4450 xfrm_pol_put(pol);
4451
4452 return 0;
4453out:
4454 return err;
4455
4456restore_state:
4457 if (pol)
4458 xfrm_pol_put(pol);
4459 if (nx_cur)
4460 xfrm_states_put(x_cur, nx_cur);
4461 if (nx_new)
4462 xfrm_states_delete(x_new, nx_new);
4463
4464 return err;
4465}
David S. Millere610e672007-02-08 13:29:15 -08004466EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004467#endif