blob: 732a940468b075fabf44df3e891a875cee593986 [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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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
Ahmed S. Darwish77cc278f2020-07-20 17:55:22 +0200125 seqcount_spinlock_t count;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100126 /* 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 Westphal6ac098b2018-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;
Ahmed S. Darwish77cc278f2020-07-20 17:55:22 +0200158static __read_mostly seqcount_mutex_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 Westphale37cc8a2016-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 Westphale37cc8a2016-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;
Ahmed S. Darwish77cc278f2020-07-20 17:55:22 +0200722 seqcount_spinlock_init(&bin->count, &net->xfrm.xfrm_policy_lock);
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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-11-07 23:00:41 +0100927
Florian Westphal769a8072019-08-12 10:32:13 +0200928 node->prefixlen = prefixlen;
929
Florian Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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 Westphal6ac098b2018-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{
Xin Longed17b8d2020-05-25 13:53:37 +08001439 if (policy->mark.v == pol->mark.v &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001440 policy->priority == pol->priority)
1441 return true;
1442
1443 return false;
1444}
1445
Florian Westphal24969fa2018-11-07 23:00:35 +01001446static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1447{
1448 const struct xfrm_pol_inexact_key *k = data;
1449 u32 a = k->type << 24 | k->dir << 16 | k->family;
1450
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001451 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1452 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +01001453}
1454
1455static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1456{
1457 const struct xfrm_pol_inexact_bin *b = data;
1458
1459 return xfrm_pol_bin_key(&b->k, 0, seed);
1460}
1461
1462static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1463 const void *ptr)
1464{
1465 const struct xfrm_pol_inexact_key *key = arg->key;
1466 const struct xfrm_pol_inexact_bin *b = ptr;
1467 int ret;
1468
1469 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1470 return -1;
1471
1472 ret = b->k.dir ^ key->dir;
1473 if (ret)
1474 return ret;
1475
1476 ret = b->k.type ^ key->type;
1477 if (ret)
1478 return ret;
1479
1480 ret = b->k.family ^ key->family;
1481 if (ret)
1482 return ret;
1483
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001484 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +01001485}
1486
1487static const struct rhashtable_params xfrm_pol_inexact_params = {
1488 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1489 .hashfn = xfrm_pol_bin_key,
1490 .obj_hashfn = xfrm_pol_bin_obj,
1491 .obj_cmpfn = xfrm_pol_bin_cmp,
1492 .automatic_shrinking = true,
1493};
1494
1495static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1496 struct xfrm_policy *policy)
1497{
1498 struct xfrm_policy *pol, *delpol = NULL;
1499 struct hlist_node *newpos = NULL;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001500 int i = 0;
Florian Westphal24969fa2018-11-07 23:00:35 +01001501
1502 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1503 if (pol->type == policy->type &&
1504 pol->if_id == policy->if_id &&
1505 !selector_cmp(&pol->selector, &policy->selector) &&
1506 xfrm_policy_mark_match(policy, pol) &&
1507 xfrm_sec_ctx_match(pol->security, policy->security) &&
1508 !WARN_ON(delpol)) {
1509 delpol = pol;
1510 if (policy->priority > pol->priority)
1511 continue;
1512 } else if (policy->priority >= pol->priority) {
1513 newpos = &pol->bydst_inexact_list;
1514 continue;
1515 }
1516 if (delpol)
1517 break;
1518 }
1519
1520 if (newpos)
1521 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1522 else
1523 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001524
1525 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1526 pol->pos = i;
1527 i++;
1528 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001529}
1530
Florian Westphala927d6af2018-11-07 23:00:33 +01001531static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1532 struct xfrm_policy *policy,
1533 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Florian Westphala927d6af2018-11-07 23:00:33 +01001535 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Sasha Levinb67bfe02013-02-27 17:06:00 -08001537 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001538 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001539 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001540 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001541 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001542 xfrm_sec_ctx_match(pol->security, policy->security) &&
1543 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001544 if (excl)
1545 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 delpol = pol;
1547 if (policy->priority > pol->priority)
1548 continue;
1549 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001550 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 continue;
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (delpol)
1554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001558 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001559 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001560 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001561
1562 return delpol;
1563}
1564
1565int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1566{
1567 struct net *net = xp_net(policy);
1568 struct xfrm_policy *delpol;
1569 struct hlist_head *chain;
1570
1571 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1572 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001573 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001574 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001575 else
1576 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001577
1578 if (IS_ERR(delpol)) {
1579 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1580 return PTR_ERR(delpol);
1581 }
1582
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001583 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001584
1585 /* After previous checking, family can either be AF_INET or AF_INET6 */
1586 if (policy->family == AF_INET)
1587 rt_genid_bump_ipv4(net);
1588 else
1589 rt_genid_bump_ipv6(net);
1590
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001591 if (delpol) {
1592 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001593 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001594 }
Fan Due682adf02013-11-07 17:47:48 +08001595 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001596 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001597 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 policy->curlft.use_time = 0;
1599 if (!mod_timer(&policy->timer, jiffies + HZ))
1600 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001601 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
David S. Miller9b78a822005-12-22 07:39:48 -08001603 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001605 else if (xfrm_bydst_should_resize(net, dir, NULL))
1606 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
1609}
1610EXPORT_SYMBOL(xfrm_policy_insert);
1611
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001612static struct xfrm_policy *
1613__xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1614 u8 type, int dir,
1615 struct xfrm_selector *sel,
1616 struct xfrm_sec_ctx *ctx)
1617{
1618 struct xfrm_policy *pol;
1619
1620 if (!chain)
1621 return NULL;
1622
1623 hlist_for_each_entry(pol, chain, bydst) {
1624 if (pol->type == type &&
1625 pol->if_id == if_id &&
1626 (mark & pol->mark.m) == pol->mark.v &&
1627 !selector_cmp(sel, &pol->selector) &&
1628 xfrm_sec_ctx_match(ctx, pol->security))
1629 return pol;
1630 }
1631
1632 return NULL;
1633}
1634
Steffen Klassert7e652642018-06-12 14:07:07 +02001635struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1636 u8 type, int dir,
1637 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001638 struct xfrm_sec_ctx *ctx, int delete,
1639 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Florian Westphal24969fa2018-11-07 23:00:35 +01001641 struct xfrm_pol_inexact_bin *bin = NULL;
1642 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001643 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric Parisef41aaa2007-03-07 15:37:58 -08001645 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001646 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001647 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001648 if (!chain) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001649 struct xfrm_pol_inexact_candidates cand;
1650 int i;
1651
Florian Westphal24969fa2018-11-07 23:00:35 +01001652 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001653 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001654 if (!bin) {
1655 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1656 return NULL;
1657 }
1658
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001659 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1660 &sel->saddr,
1661 &sel->daddr)) {
1662 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1663 return NULL;
1664 }
1665
1666 pol = NULL;
1667 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1668 struct xfrm_policy *tmp;
1669
1670 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1671 if_id, type, dir,
1672 sel, ctx);
Florian Westphal39aa6922018-11-15 02:51:57 +01001673 if (!tmp)
1674 continue;
1675
1676 if (!pol || tmp->pos < pol->pos)
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001677 pol = tmp;
1678 }
1679 } else {
1680 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1681 sel, ctx);
Florian Westphal24969fa2018-11-07 23:00:35 +01001682 }
1683
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001684 if (pol) {
1685 xfrm_pol_hold(pol);
1686 if (delete) {
1687 *err = security_xfrm_policy_delete(pol->security);
1688 if (*err) {
1689 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1690 return pol;
David S. Miller2518c7c2006-08-24 04:45:07 -07001691 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001692 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001694 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001696 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001698 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001699 xfrm_policy_kill(ret);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001700 if (bin && delete)
1701 xfrm_policy_inexact_prune_bin(bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
Trent Jaegerdf718372005-12-13 23:12:27 -08001704EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Steffen Klassert7e652642018-06-12 14:07:07 +02001706struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1707 u8 type, int dir, u32 id, int delete,
1708 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
David S. Miller2518c7c2006-08-24 04:45:07 -07001710 struct xfrm_policy *pol, *ret;
1711 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Herbert Xub5505c62007-05-14 02:15:47 -07001713 *err = -ENOENT;
1714 if (xfrm_policy_id2dir(id) != dir)
1715 return NULL;
1716
Eric Parisef41aaa2007-03-07 15:37:58 -08001717 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001718 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001719 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001720 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001721 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001722 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001723 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001724 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001726 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001727 *err = security_xfrm_policy_delete(
1728 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001729 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001730 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001731 return pol;
1732 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001733 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001734 }
1735 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 break;
1737 }
1738 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001739 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001741 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001742 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745EXPORT_SYMBOL(xfrm_policy_byid);
1746
Joy Latten4aa2e622007-06-04 19:05:57 -04001747#ifdef CONFIG_SECURITY_NETWORK_XFRM
1748static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001749xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Florian Westphalceb159e2018-11-07 23:00:32 +01001751 struct xfrm_policy *pol;
1752 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001753
Florian Westphalceb159e2018-11-07 23:00:32 +01001754 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1755 if (pol->walk.dead ||
1756 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1757 pol->type != type)
1758 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001759
Florian Westphalceb159e2018-11-07 23:00:32 +01001760 err = security_xfrm_policy_delete(pol->security);
1761 if (err) {
1762 xfrm_audit_policy_delete(pol, 0, task_valid);
1763 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001764 }
1765 }
1766 return err;
1767}
1768#else
1769static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001770xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001771{
1772 return 0;
1773}
1774#endif
1775
Tetsuo Handa2e710292014-04-22 21:48:30 +09001776int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001777{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001778 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001779 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Florian Westphal9d0380d2016-08-11 15:17:59 +02001781 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001782
Tetsuo Handa2e710292014-04-22 21:48:30 +09001783 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001784 if (err)
1785 goto out;
1786
Florian Westphalceb159e2018-11-07 23:00:32 +01001787again:
1788 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1789 dir = xfrm_policy_id2dir(pol->index);
1790 if (pol->walk.dead ||
1791 dir >= XFRM_POLICY_MAX ||
1792 pol->type != type)
1793 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001794
Florian Westphalceb159e2018-11-07 23:00:32 +01001795 __xfrm_policy_unlink(pol, dir);
1796 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1797 cnt++;
1798 xfrm_audit_policy_delete(pol, 1, task_valid);
1799 xfrm_policy_kill(pol);
1800 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1801 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001803 if (cnt)
1804 __xfrm_policy_inexact_flush(net);
1805 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001806 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001807out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001808 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001809 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811EXPORT_SYMBOL(xfrm_policy_flush);
1812
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001813int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001814 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 void *data)
1816{
Herbert Xu12a169e2008-10-01 07:03:24 -07001817 struct xfrm_policy *pol;
1818 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001819 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Timo Teras4c563f72008-02-28 21:31:08 -08001821 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1822 walk->type != XFRM_POLICY_TYPE_ANY)
1823 return -EINVAL;
1824
Herbert Xu12a169e2008-10-01 07:03:24 -07001825 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001826 return 0;
1827
Florian Westphal9d0380d2016-08-11 15:17:59 +02001828 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001829 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001830 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001831 else
Li RongQing80077702015-04-22 17:09:54 +08001832 x = list_first_entry(&walk->walk.all,
1833 struct xfrm_policy_walk_entry, all);
1834
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001835 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001836 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001837 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001838 pol = container_of(x, struct xfrm_policy, walk);
1839 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1840 walk->type != pol->type)
1841 continue;
1842 error = func(pol, xfrm_policy_id2dir(pol->index),
1843 walk->seq, data);
1844 if (error) {
1845 list_move_tail(&walk->walk.all, &x->all);
1846 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001847 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001848 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001850 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001851 error = -ENOENT;
1852 goto out;
1853 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001854 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001856 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return error;
1858}
1859EXPORT_SYMBOL(xfrm_policy_walk);
1860
Herbert Xu12a169e2008-10-01 07:03:24 -07001861void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1862{
1863 INIT_LIST_HEAD(&walk->walk.all);
1864 walk->walk.dead = 1;
1865 walk->type = type;
1866 walk->seq = 0;
1867}
1868EXPORT_SYMBOL(xfrm_policy_walk_init);
1869
Fan Du283bc9f2013-11-07 17:47:50 +08001870void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001871{
1872 if (list_empty(&walk->walk.all))
1873 return;
1874
Florian Westphal9d0380d2016-08-11 15:17:59 +02001875 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001876 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001877 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001878}
1879EXPORT_SYMBOL(xfrm_policy_walk_done);
1880
James Morris134b0fc2006-10-05 15:42:27 -05001881/*
1882 * Find policy to apply to this flow.
1883 *
1884 * Returns 0 if policy found, else an -errno.
1885 */
David S. Millerf299d552011-02-24 01:23:30 -05001886static int xfrm_policy_match(const struct xfrm_policy *pol,
1887 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001888 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001889{
David S. Millerf299d552011-02-24 01:23:30 -05001890 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001891 int ret = -ESRCH;
1892 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001893
1894 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001895 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001896 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001897 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001898 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001899
1900 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001901 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001902 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001903 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001904 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001905}
1906
Florian Westphal9cf545e2018-11-07 23:00:38 +01001907static struct xfrm_pol_inexact_node *
1908xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
Ahmed S. Darwish77cc278f2020-07-20 17:55:22 +02001909 seqcount_spinlock_t *count,
Florian Westphal9cf545e2018-11-07 23:00:38 +01001910 const xfrm_address_t *addr, u16 family)
1911{
1912 const struct rb_node *parent;
1913 int seq;
1914
1915again:
1916 seq = read_seqcount_begin(count);
1917
1918 parent = rcu_dereference_raw(r->rb_node);
1919 while (parent) {
1920 struct xfrm_pol_inexact_node *node;
1921 int delta;
1922
1923 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1924
1925 delta = xfrm_policy_addr_delta(addr, &node->addr,
1926 node->prefixlen, family);
1927 if (delta < 0) {
1928 parent = rcu_dereference_raw(parent->rb_left);
1929 continue;
1930 } else if (delta > 0) {
1931 parent = rcu_dereference_raw(parent->rb_right);
1932 continue;
1933 }
1934
1935 return node;
1936 }
1937
1938 if (read_seqcount_retry(count, seq))
1939 goto again;
1940
1941 return NULL;
1942}
1943
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001944static bool
1945xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1946 struct xfrm_pol_inexact_bin *b,
1947 const xfrm_address_t *saddr,
1948 const xfrm_address_t *daddr)
1949{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001950 struct xfrm_pol_inexact_node *n;
1951 u16 family;
1952
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001953 if (!b)
1954 return false;
1955
Florian Westphal9cf545e2018-11-07 23:00:38 +01001956 family = b->k.family;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001957 memset(cand, 0, sizeof(*cand));
1958 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001959
1960 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1961 family);
Florian Westphal6ac098b2018-11-07 23:00:41 +01001962 if (n) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001963 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
Florian Westphal6ac098b2018-11-07 23:00:41 +01001964 n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr,
1965 family);
1966 if (n)
1967 cand->res[XFRM_POL_CAND_BOTH] = &n->hhead;
1968 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001969
Florian Westphal64a09a72018-11-07 23:00:40 +01001970 n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
1971 family);
1972 if (n)
1973 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
1974
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001975 return true;
1976}
1977
Florian Westphal24969fa2018-11-07 23:00:35 +01001978static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001979xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1980 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001981{
1982 struct xfrm_pol_inexact_key k = {
1983 .family = family,
1984 .type = type,
1985 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001986 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001987 };
1988
1989 write_pnet(&k.net, net);
1990
1991 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1992 xfrm_pol_inexact_params);
1993}
1994
1995static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001996xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1997 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001998{
1999 struct xfrm_pol_inexact_bin *bin;
2000
2001 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
2002
2003 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002004 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01002005 rcu_read_unlock();
2006
2007 return bin;
2008}
2009
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002010static struct xfrm_policy *
2011__xfrm_policy_eval_candidates(struct hlist_head *chain,
2012 struct xfrm_policy *prefer,
2013 const struct flowi *fl,
2014 u8 type, u16 family, int dir, u32 if_id)
2015{
2016 u32 priority = prefer ? prefer->priority : ~0u;
2017 struct xfrm_policy *pol;
2018
2019 if (!chain)
2020 return NULL;
2021
2022 hlist_for_each_entry_rcu(pol, chain, bydst) {
2023 int err;
2024
2025 if (pol->priority > priority)
2026 break;
2027
2028 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
2029 if (err) {
2030 if (err != -ESRCH)
2031 return ERR_PTR(err);
2032
2033 continue;
2034 }
2035
2036 if (prefer) {
2037 /* matches. Is it older than *prefer? */
2038 if (pol->priority == priority &&
2039 prefer->pos < pol->pos)
2040 return prefer;
2041 }
2042
2043 return pol;
2044 }
2045
2046 return NULL;
2047}
2048
2049static struct xfrm_policy *
2050xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
2051 struct xfrm_policy *prefer,
2052 const struct flowi *fl,
2053 u8 type, u16 family, int dir, u32 if_id)
2054{
2055 struct xfrm_policy *tmp;
2056 int i;
2057
2058 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
2059 tmp = __xfrm_policy_eval_candidates(cand->res[i],
2060 prefer,
2061 fl, type, family, dir,
2062 if_id);
2063 if (!tmp)
2064 continue;
2065
2066 if (IS_ERR(tmp))
2067 return tmp;
2068 prefer = tmp;
2069 }
2070
2071 return prefer;
2072}
2073
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002074static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08002075 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07002076 u16 family, u8 dir,
2077 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002079 struct xfrm_pol_inexact_candidates cand;
David S. Miller0b597e72011-02-24 01:22:48 -05002080 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01002081 struct xfrm_pol_inexact_bin *bin;
2082 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07002083 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02002084 unsigned int sequence;
Florian Westphal24969fa2018-11-07 23:00:35 +01002085 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07002086
2087 daddr = xfrm_flowi_daddr(fl, family);
2088 saddr = xfrm_flowi_saddr(fl, family);
2089 if (unlikely(!daddr || !saddr))
2090 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Florian Westphala7c442472016-08-11 15:17:56 +02002092 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02002093 retry:
2094 do {
2095 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
2096 chain = policy_hash_direct(net, daddr, saddr, family, dir);
2097 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
2098
David S. Miller2518c7c2006-08-24 04:45:07 -07002099 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02002100 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07002101 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05002102 if (err) {
2103 if (err == -ESRCH)
2104 continue;
2105 else {
2106 ret = ERR_PTR(err);
2107 goto fail;
2108 }
2109 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07002110 ret = pol;
David S. Milleracba48e2006-08-25 15:46:46 -07002111 break;
2112 }
2113 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002114 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002115 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
2116 daddr))
Florian Westphal24969fa2018-11-07 23:00:35 +01002117 goto skip_inexact;
Li RongQing8faf4912015-05-14 11:16:59 +08002118
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002119 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
2120 family, dir, if_id);
2121 if (pol) {
2122 ret = pol;
2123 if (IS_ERR(pol))
2124 goto fail;
David S. Miller2518c7c2006-08-24 04:45:07 -07002125 }
Li RongQing586f2eb2015-04-30 17:13:41 +08002126
Florian Westphal24969fa2018-11-07 23:00:35 +01002127skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02002128 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
2129 goto retry;
2130
Florian Westphale37cc8a2016-08-11 15:17:55 +02002131 if (ret && !xfrm_pol_hold_rcu(ret))
2132 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05002133fail:
Florian Westphala7c442472016-08-11 15:17:56 +02002134 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002135
David S. Miller2518c7c2006-08-24 04:45:07 -07002136 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002137}
2138
Benedict Wongbc56b332018-07-19 10:50:44 -07002139static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
2140 const struct flowi *fl,
2141 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002142{
2143#ifdef CONFIG_XFRM_SUB_POLICY
2144 struct xfrm_policy *pol;
2145
Benedict Wongbc56b332018-07-19 10:50:44 -07002146 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2147 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002148 if (pol != NULL)
2149 return pol;
2150#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07002151 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2152 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002153}
2154
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002155static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07002156 const struct flowi *fl,
2157 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct xfrm_policy *pol;
2160
Eric Dumazetd188ba82015-12-08 07:22:02 -08002161 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02002162 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002163 pol = rcu_dereference(sk->sk_policy[dir]);
2164 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01002165 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002166 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08002167
Steffen Klassertddc47e42017-11-29 06:53:55 +01002168 if (pol->family != family) {
2169 pol = NULL;
2170 goto out;
2171 }
2172
2173 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002174 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02002175 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07002176 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002177 pol = NULL;
2178 goto out;
2179 }
Paul Moore03e1ad72008-04-12 19:07:52 -07002180 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05002181 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02002182 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01002183 if (!err) {
2184 if (!xfrm_pol_hold_rcu(pol))
2185 goto again;
2186 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002187 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01002188 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002189 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01002190 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002191 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 pol = NULL;
2193 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002194out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002195 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return pol;
2197}
2198
2199static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2200{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002201 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002202
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002203 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002204 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 xfrm_pol_hold(pol);
2206}
2207
2208static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2209 int dir)
2210{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002211 struct net *net = xp_net(pol);
2212
Herbert Xu53c2e282014-11-13 17:09:49 +08002213 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07002214 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Herbert Xu53c2e282014-11-13 17:09:49 +08002216 /* Socket policies are not hashed. */
2217 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02002218 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01002219 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08002220 hlist_del(&pol->byidx);
2221 }
2222
2223 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002224 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07002225
2226 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
Herbert Xu53c2e282014-11-13 17:09:49 +08002229static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2230{
2231 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2232}
2233
2234static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2235{
2236 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2237}
2238
Herbert Xu4666faa2005-06-18 22:43:22 -07002239int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Fan Du283bc9f2013-11-07 17:47:50 +08002241 struct net *net = xp_net(pol);
2242
Florian Westphal9d0380d2016-08-11 15:17:59 +02002243 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002245 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07002248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Herbert Xu4666faa2005-06-18 22:43:22 -07002250 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
David S. Millera70fcb02006-03-20 19:18:52 -08002252EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2255{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002256 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 struct xfrm_policy *old_pol;
2258
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002259#ifdef CONFIG_XFRM_SUB_POLICY
2260 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2261 return -EINVAL;
2262#endif
2263
Florian Westphal9d0380d2016-08-11 15:17:59 +02002264 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08002265 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2266 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02002268 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08002269 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08002270 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08002272 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002273 if (old_pol) {
2274 if (pol)
2275 xfrm_policy_requeue(old_pol, pol);
2276
Timo Teräsea2dea9d2010-03-31 00:17:05 +00002277 /* Unlinking succeeds always. This is the only function
2278 * allowed to delete or replace socket policy.
2279 */
Herbert Xu53c2e282014-11-13 17:09:49 +08002280 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002281 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02002282 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 if (old_pol) {
2285 xfrm_policy_kill(old_pol);
2286 }
2287 return 0;
2288}
2289
David S. Millerd3e40a92011-02-24 01:25:41 -05002290static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002292 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08002293 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 if (newp) {
2296 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07002297 if (security_xfrm_policy_clone(old->security,
2298 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08002299 kfree(newp);
2300 return NULL; /* ENOMEM */
2301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 newp->lft = old->lft;
2303 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08002304 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02002305 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 newp->action = old->action;
2307 newp->flags = old->flags;
2308 newp->xfrm_nr = old->xfrm_nr;
2309 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002310 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11002311 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 memcpy(newp->xfrm_vec, old->xfrm_vec,
2313 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02002314 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08002315 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002316 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 xfrm_pol_put(newp);
2318 }
2319 return newp;
2320}
2321
Eric Dumazetd188ba82015-12-08 07:22:02 -08002322int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Eric Dumazetd188ba82015-12-08 07:22:02 -08002324 const struct xfrm_policy *p;
2325 struct xfrm_policy *np;
2326 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Eric Dumazetd188ba82015-12-08 07:22:02 -08002328 rcu_read_lock();
2329 for (i = 0; i < 2; i++) {
2330 p = rcu_dereference(osk->sk_policy[i]);
2331 if (p) {
2332 np = clone_policy(p, i);
2333 if (unlikely(!np)) {
2334 ret = -ENOMEM;
2335 break;
2336 }
2337 rcu_assign_pointer(sk->sk_policy[i], np);
2338 }
2339 }
2340 rcu_read_unlock();
2341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002344static int
David Ahern42a7b322015-08-10 16:58:11 -06002345xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002346 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002347{
2348 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01002349 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002350
2351 if (unlikely(afinfo == NULL))
2352 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002353 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002354 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002355 return err;
2356}
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358/* Resolve list of templates for the flow, given policy. */
2359
2360static int
David S. Millera6c2e612011-02-22 18:35:39 -08002361xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2362 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08002364 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 int nx;
2366 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01002367 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2368 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002369 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Weilong Chen9b7a7872013-12-24 09:43:46 +08002371 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01002373 xfrm_address_t *remote = daddr;
2374 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2376
Steffen Klassert94802152017-11-15 06:40:57 +01002377 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2378 tmpl->mode == XFRM_MODE_BEET) {
2379 remote = &tmpl->id.daddr;
2380 local = &tmpl->saddr;
2381 if (xfrm_addr_any(local, tmpl->encap_family)) {
2382 error = xfrm_get_saddr(net, fl->flowi_oif,
2383 &tmp, remote,
2384 tmpl->encap_family, 0);
2385 if (error)
2386 goto fail;
2387 local = &tmp;
2388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
Benedict Wongbc56b332018-07-19 10:50:44 -07002391 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2392 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 if (x && x->km.state == XFRM_STATE_VALID) {
2395 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01002396 daddr = remote;
2397 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 continue;
2399 }
2400 if (x) {
2401 error = (x->km.state == XFRM_STATE_ERROR ?
2402 -EINVAL : -EAGAIN);
2403 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08002404 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00002405 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 if (!tmpl->optional)
2409 goto fail;
2410 }
2411 return nx;
2412
2413fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002414 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 xfrm_state_put(xfrm[nx]);
2416 return error;
2417}
2418
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002419static int
David S. Millera6c2e612011-02-22 18:35:39 -08002420xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2421 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002422{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002423 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2424 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002425 int cnx = 0;
2426 int error;
2427 int ret;
2428 int i;
2429
2430 for (i = 0; i < npols; i++) {
2431 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2432 error = -ENOBUFS;
2433 goto fail;
2434 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002435
2436 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002437 if (ret < 0) {
2438 error = ret;
2439 goto fail;
2440 } else
2441 cnx += ret;
2442 }
2443
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002444 /* found states are sorted for outbound processing */
2445 if (npols > 1)
2446 xfrm_state_sort(xfrm, tpp, cnx, family);
2447
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002448 return cnx;
2449
2450 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002451 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002452 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002453 return error;
2454
2455}
2456
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002457static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002458{
Florian Westphalf24ea5282019-04-16 16:44:37 +02002459 if (family == AF_INET)
2460 return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08002461
Florian Westphalf24ea5282019-04-16 16:44:37 +02002462 return 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002463}
2464
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002465static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002466{
Florian Westphal37b10382017-02-07 15:00:19 +01002467 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002468 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08002469 struct xfrm_dst *xdst;
2470
2471 if (!afinfo)
2472 return ERR_PTR(-EINVAL);
2473
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002474 switch (family) {
2475 case AF_INET:
2476 dst_ops = &net->xfrm.xfrm4_dst_ops;
2477 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002478#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002479 case AF_INET6:
2480 dst_ops = &net->xfrm.xfrm6_dst_ops;
2481 break;
2482#endif
2483 default:
2484 BUG();
2485 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07002486 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08002487
Madalin Bucurd4cae562011-09-26 07:04:36 +00002488 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00002489 struct dst_entry *dst = &xdst->u.dst;
2490
2491 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00002492 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08002493 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00002494
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002495 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00002496
Herbert Xu25ee3282007-12-11 09:32:34 -08002497 return xdst;
2498}
2499
Florian Westphal2e8b4aa2019-04-16 16:44:38 +02002500static void xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2501 int nfheader_len)
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002502{
Florian Westphal2e8b4aa2019-04-16 16:44:38 +02002503 if (dst->ops->family == AF_INET6) {
2504 struct rt6_info *rt = (struct rt6_info *)dst;
2505 path->path_cookie = rt6_get_cookie(rt);
2506 path->u.rt6.rt6i_nfheader_len = nfheader_len;
2507 }
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002508}
2509
Herbert Xu87c1e122010-03-02 02:51:56 +00002510static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08002511 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08002512{
Florian Westphal37b10382017-02-07 15:00:19 +01002513 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08002514 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2515 int err;
2516
2517 if (!afinfo)
2518 return -EINVAL;
2519
Herbert Xu87c1e122010-03-02 02:51:56 +00002520 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002521
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002522 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002523
2524 return err;
2525}
2526
Timo Teräs80c802f2010-04-07 00:30:05 +00002527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528/* Allocate chain of dst_entry's, attach known xfrm's, calculate
2529 * all the metrics... Shortly, bundle a bundle.
2530 */
2531
Herbert Xu25ee3282007-12-11 09:32:34 -08002532static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05002533 struct xfrm_state **xfrm,
2534 struct xfrm_dst **bundle,
2535 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08002536 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08002537 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Florian Westphal733a5fa2019-03-29 21:16:30 +01002539 const struct xfrm_state_afinfo *afinfo;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002540 const struct xfrm_mode *inner_mode;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002541 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08002542 unsigned long now = jiffies;
2543 struct net_device *dev;
David Miller45b018be2017-11-28 15:40:28 -05002544 struct xfrm_dst *xdst_prev = NULL;
2545 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08002546 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08002548 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002549 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002550 int trailer_len = 0;
2551 int tos;
2552 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09002553 xfrm_address_t saddr, daddr;
2554
2555 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002556
2557 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002558
2559 dst_hold(dst);
2560
2561 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002562 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002563 struct dst_entry *dst1 = &xdst->u.dst;
2564
2565 err = PTR_ERR(xdst);
2566 if (IS_ERR(xdst)) {
2567 dst_release(dst);
2568 goto put_states;
2569 }
2570
David Miller54920932017-11-28 15:41:01 -05002571 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05002572 if (!xdst_prev)
2573 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07002574 else
2575 /* Ref count is taken during xfrm_alloc_dst()
2576 * No need to do dst_clone() on dst1
2577 */
David Miller45b018be2017-11-28 15:40:28 -05002578 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07002579
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002580 if (xfrm[i]->sel.family == AF_UNSPEC) {
2581 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2582 xfrm_af2proto(family));
2583 if (!inner_mode) {
2584 err = -EAFNOSUPPORT;
2585 dst_release(dst);
2586 goto put_states;
2587 }
2588 } else
Florian Westphalc9500d72019-03-29 21:16:32 +01002589 inner_mode = &xfrm[i]->inner_mode;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002590
Herbert Xu25ee3282007-12-11 09:32:34 -08002591 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08002592 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08002593
2594 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Benedict Wonge2612cd2019-01-14 11:24:38 -08002595 __u32 mark = 0;
2596
2597 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
2598 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002599
Herbert Xu25ee3282007-12-11 09:32:34 -08002600 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06002601 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002602 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08002603 err = PTR_ERR(dst);
2604 if (IS_ERR(dst))
2605 goto put_states;
2606 } else
2607 dst_hold(dst);
2608
2609 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00002610 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08002611
David S. Millerf5b0a872012-07-19 12:31:33 -07002612 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08002613 dst1->lastuse = now;
2614
2615 dst1->input = dst_discard;
Florian Westphal733a5fa2019-03-29 21:16:30 +01002616
2617 rcu_read_lock();
2618 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
2619 if (likely(afinfo))
2620 dst1->output = afinfo->output;
2621 else
2622 dst1->output = dst_discard_out;
2623 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002624
David Miller45b018be2017-11-28 15:40:28 -05002625 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002626
2627 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002628 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2629 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002630 trailer_len += xfrm[i]->props.trailer_len;
2631 }
2632
David Miller45b018be2017-11-28 15:40:28 -05002633 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002634 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002635
2636 err = -ENODEV;
2637 dev = dst->dev;
2638 if (!dev)
2639 goto free_dst;
2640
David Miller45b018be2017-11-28 15:40:28 -05002641 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002642 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002643
David Miller45b018be2017-11-28 15:40:28 -05002644 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2645 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2646 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002647 if (err)
2648 goto free_dst;
2649
David Miller45b018be2017-11-28 15:40:28 -05002650 xdst_prev->u.dst.header_len = header_len;
2651 xdst_prev->u.dst.trailer_len = trailer_len;
2652 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2653 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002654 }
2655
David Miller45b018be2017-11-28 15:40:28 -05002656 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002657
2658put_states:
2659 for (; i < nx; i++)
2660 xfrm_state_put(xfrm[i]);
2661free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002662 if (xdst0)
2663 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002664
2665 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
David S. Miller73ff93c2011-02-22 18:33:42 -08002668static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002669 struct xfrm_policy **pols,
2670 int *num_pols, int *num_xfrms)
2671{
2672 int i;
2673
2674 if (*num_pols == 0 || !pols[0]) {
2675 *num_pols = 0;
2676 *num_xfrms = 0;
2677 return 0;
2678 }
2679 if (IS_ERR(pols[0]))
2680 return PTR_ERR(pols[0]);
2681
2682 *num_xfrms = pols[0]->xfrm_nr;
2683
2684#ifdef CONFIG_XFRM_SUB_POLICY
2685 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2686 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2687 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2688 XFRM_POLICY_TYPE_MAIN,
2689 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002690 XFRM_POLICY_OUT,
2691 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002692 if (pols[1]) {
2693 if (IS_ERR(pols[1])) {
2694 xfrm_pols_put(pols, *num_pols);
2695 return PTR_ERR(pols[1]);
2696 }
Weilong Chen02d08922013-12-24 09:43:48 +08002697 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002698 (*num_xfrms) += pols[1]->xfrm_nr;
2699 }
2700 }
2701#endif
2702 for (i = 0; i < *num_pols; i++) {
2703 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2704 *num_xfrms = -1;
2705 break;
2706 }
2707 }
2708
2709 return 0;
2710
2711}
2712
2713static struct xfrm_dst *
2714xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002715 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002716 struct dst_entry *dst_orig)
2717{
2718 struct net *net = xp_net(pols[0]);
2719 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002720 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002721 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002722 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002723 int err;
2724
2725 /* Try to instantiate a bundle */
2726 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002727 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002728 if (err == 0)
2729 return NULL;
2730
2731 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002732 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2733 return ERR_PTR(err);
2734 }
2735
David Miller54920932017-11-28 15:41:01 -05002736 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002737 if (IS_ERR(dst)) {
2738 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2739 return ERR_CAST(dst);
2740 }
2741
2742 xdst = (struct xfrm_dst *)dst;
2743 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002744 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002745 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002746 xdst->policy_genid = atomic_read(&pols[0]->genid);
2747
2748 return xdst;
2749}
2750
Kees Cookc3aed702017-10-16 17:28:56 -07002751static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002752{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002753 struct sk_buff *skb;
2754 struct sock *sk;
2755 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002756 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002757 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002758 struct xfrm_policy_queue *pq = &pol->polq;
2759 struct flowi fl;
2760 struct sk_buff_head list;
2761
2762 spin_lock(&pq->hold_queue.lock);
2763 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002764 if (!skb) {
2765 spin_unlock(&pq->hold_queue.lock);
2766 goto out;
2767 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002768 dst = skb_dst(skb);
2769 sk = skb->sk;
2770 xfrm_decode_session(skb, &fl, dst->ops->family);
2771 spin_unlock(&pq->hold_queue.lock);
2772
David Miller0f6c4802017-11-28 15:40:46 -05002773 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c9812018-02-01 11:26:12 +01002774 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002775 if (IS_ERR(dst))
2776 goto purge_queue;
2777
2778 if (dst->flags & DST_XFRM_QUEUE) {
2779 dst_release(dst);
2780
2781 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2782 goto purge_queue;
2783
2784 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002785 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2786 xfrm_pol_hold(pol);
Colin Ian King7759d6a2018-11-13 14:28:42 +00002787 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002788 }
2789
2790 dst_release(dst);
2791
2792 __skb_queue_head_init(&list);
2793
2794 spin_lock(&pq->hold_queue.lock);
2795 pq->timeout = 0;
2796 skb_queue_splice_init(&pq->hold_queue, &list);
2797 spin_unlock(&pq->hold_queue.lock);
2798
2799 while (!skb_queue_empty(&list)) {
2800 skb = __skb_dequeue(&list);
2801
2802 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002803 dst_hold(xfrm_dst_path(skb_dst(skb)));
2804 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002805 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002806 kfree_skb(skb);
2807 continue;
2808 }
2809
Florian Westphal895b5c92019-09-29 20:54:03 +02002810 nf_reset_ct(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002811 skb_dst_drop(skb);
2812 skb_dst_set(skb, dst);
2813
Eric W. Biederman13206b62015-10-07 16:48:35 -05002814 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002815 }
2816
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002817out:
2818 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002819 return;
2820
2821purge_queue:
2822 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002823 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002824 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002825}
2826
Eric W. Biedermanede20592015-10-07 16:48:47 -05002827static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002828{
2829 unsigned long sched_next;
2830 struct dst_entry *dst = skb_dst(skb);
2831 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002832 struct xfrm_policy *pol = xdst->pols[0];
2833 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002834
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002835 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002836 kfree_skb(skb);
2837 return 0;
2838 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002839
2840 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2841 kfree_skb(skb);
2842 return -EAGAIN;
2843 }
2844
2845 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002846
2847 spin_lock_bh(&pq->hold_queue.lock);
2848
2849 if (!pq->timeout)
2850 pq->timeout = XFRM_QUEUE_TMO_MIN;
2851
2852 sched_next = jiffies + pq->timeout;
2853
2854 if (del_timer(&pq->hold_timer)) {
2855 if (time_before(pq->hold_timer.expires, sched_next))
2856 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002857 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002858 }
2859
2860 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002861 if (!mod_timer(&pq->hold_timer, sched_next))
2862 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002863
2864 spin_unlock_bh(&pq->hold_queue.lock);
2865
2866 return 0;
2867}
2868
2869static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002870 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002871 const struct flowi *fl,
2872 int num_xfrms,
2873 u16 family)
2874{
2875 int err;
2876 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002877 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002878 struct dst_entry *dst1;
2879 struct xfrm_dst *xdst;
2880
2881 xdst = xfrm_alloc_dst(net, family);
2882 if (IS_ERR(xdst))
2883 return xdst;
2884
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002885 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2886 net->xfrm.sysctl_larval_drop ||
2887 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002888 return xdst;
2889
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002890 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002891 dst1 = &xdst->u.dst;
2892 dst_hold(dst);
2893 xdst->route = dst;
2894
2895 dst_copy_metrics(dst1, dst);
2896
2897 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
David Laightaf13b3c2020-03-23 14:31:19 +00002898 dst1->flags |= DST_XFRM_QUEUE;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002899 dst1->lastuse = jiffies;
2900
2901 dst1->input = dst_discard;
2902 dst1->output = xdst_queue_output;
2903
2904 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002905 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002906 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002907
2908 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2909
2910 err = -ENODEV;
2911 dev = dst->dev;
2912 if (!dev)
2913 goto free_dst;
2914
2915 err = xfrm_fill_dst(xdst, dev, fl);
2916 if (err)
2917 goto free_dst;
2918
2919out:
2920 return xdst;
2921
2922free_dst:
2923 dst_release(dst1);
2924 xdst = ERR_PTR(err);
2925 goto out;
2926}
2927
Benedict Wongbc56b332018-07-19 10:50:44 -07002928static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2929 const struct flowi *fl,
2930 u16 family, u8 dir,
2931 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002932{
Timo Teräs80c802f2010-04-07 00:30:05 +00002933 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002934 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002935 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002936
Timo Teräs80c802f2010-04-07 00:30:05 +00002937 /* Resolve policies to use if we couldn't get them from
2938 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002939 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002940 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002941 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002942 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002943 if (err < 0)
2944 goto inc_error;
2945 if (num_pols == 0)
2946 return NULL;
2947 if (num_xfrms <= 0)
2948 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002949
Florian Westphalbd45c532017-07-17 13:57:25 +02002950 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002951 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002952 if (IS_ERR(xdst)) {
2953 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002954 if (err == -EREMOTE) {
2955 xfrm_pols_put(pols, num_pols);
2956 return NULL;
2957 }
2958
Timo Teräs80c802f2010-04-07 00:30:05 +00002959 if (err != -EAGAIN)
2960 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002961 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002962 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002963 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002964 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002965 }
2966
Florian Westphalbd45c532017-07-17 13:57:25 +02002967 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002968
2969make_dummy_bundle:
2970 /* We found policies, but there's no bundles to instantiate:
2971 * either because the policy blocks, has no transformations or
2972 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002973 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002974 if (IS_ERR(xdst)) {
2975 xfrm_pols_put(pols, num_pols);
2976 return ERR_CAST(xdst);
2977 }
2978 xdst->num_pols = num_pols;
2979 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002980 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002981
Florian Westphalbd45c532017-07-17 13:57:25 +02002982 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002983
2984inc_error:
2985 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2986error:
Florian Westphal855dad92017-07-17 13:57:22 +02002987 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002988 return ERR_PTR(err);
2989}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
David S. Miller2774c132011-03-01 14:59:04 -08002991static struct dst_entry *make_blackhole(struct net *net, u16 family,
2992 struct dst_entry *dst_orig)
2993{
Florian Westphal37b10382017-02-07 15:00:19 +01002994 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002995 struct dst_entry *ret;
2996
2997 if (!afinfo) {
2998 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002999 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08003000 } else {
3001 ret = afinfo->blackhole_route(net, dst_orig);
3002 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003003 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08003004
3005 return ret;
3006}
3007
Benedict Wongbc56b332018-07-19 10:50:44 -07003008/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 *
3010 * At the moment we eat a raw IP route. Mostly to speed up lookups
3011 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07003012 *
3013 * xfrm_lookup uses an if_id of 0 by default, and is provided for
3014 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 */
Benedict Wongbc56b332018-07-19 10:50:44 -07003016struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
3017 struct dst_entry *dst_orig,
3018 const struct flowi *fl,
3019 const struct sock *sk,
3020 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003022 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00003023 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08003024 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00003025 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02003026 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00003027 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003028
Timo Teräs80c802f2010-04-07 00:30:05 +00003029 dst = NULL;
3030 xdst = NULL;
3031 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003032
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003033 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07003034 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003035 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003036 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
3037 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00003038 err = xfrm_expand_policies(fl, family, pols,
3039 &num_pols, &num_xfrms);
3040 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08003041 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00003042
3043 if (num_pols) {
3044 if (num_xfrms <= 0) {
3045 drop_pols = num_pols;
3046 goto no_transform;
3047 }
3048
3049 xdst = xfrm_resolve_and_create_bundle(
3050 pols, num_pols, fl,
3051 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01003052
Timo Teräs80c802f2010-04-07 00:30:05 +00003053 if (IS_ERR(xdst)) {
3054 xfrm_pols_put(pols, num_pols);
3055 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02003056 if (err == -EREMOTE)
3057 goto nopol;
3058
Timo Teräs80c802f2010-04-07 00:30:05 +00003059 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00003060 } else if (xdst == NULL) {
3061 num_xfrms = 0;
3062 drop_pols = num_pols;
3063 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00003064 }
3065
Timo Teräs80c802f2010-04-07 00:30:05 +00003066 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003067 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Timo Teräs80c802f2010-04-07 00:30:05 +00003070 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003071 struct xfrm_flo xflo;
3072
3073 xflo.dst_orig = dst_orig;
3074 xflo.flags = flags;
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07003077 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08003078 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08003079 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Benedict Wongbc56b332018-07-19 10:50:44 -07003081 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02003082 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00003083 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02003084 if (IS_ERR(xdst)) {
3085 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003086 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08003087 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003088
3089 num_pols = xdst->num_pols;
3090 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003091 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00003092 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094
Timo Teräs80c802f2010-04-07 00:30:05 +00003095 dst = &xdst->u.dst;
3096 if (route == NULL && num_xfrms > 0) {
3097 /* The only case when xfrm_bundle_lookup() returns a
3098 * bundle with null route, is when the template could
3099 * not be resolved. It means policies are there, but
3100 * bundle could not be created, since we don't yet
3101 * have the xfrm_state's. We need to wait for KM to
3102 * negotiate new SA's or bail out with error.*/
3103 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003104 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01003105 err = -EREMOTE;
3106 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003107 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003108
Steffen Klassert5b8ef342013-08-27 13:43:30 +02003109 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00003110
3111 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3112 goto error;
3113 }
3114
3115no_transform:
3116 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08003117 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Timo Teräs80c802f2010-04-07 00:30:05 +00003119 if ((flags & XFRM_LOOKUP_ICMP) &&
3120 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3121 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08003122 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003123 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08003124
Timo Teräs80c802f2010-04-07 00:30:05 +00003125 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02003126 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08003127
Timo Teräs80c802f2010-04-07 00:30:05 +00003128 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003130 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07003131 err = -EPERM;
3132 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003133 } else if (num_xfrms > 0) {
3134 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00003135 dst_release(dst_orig);
3136 } else {
3137 /* Flow passes untransformed */
3138 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003139 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003141ok:
3142 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003143 if (dst && dst->xfrm &&
3144 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3145 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003146 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Timo Teräs80c802f2010-04-07 00:30:05 +00003148nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003149 if (!(flags & XFRM_LOOKUP_ICMP)) {
3150 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003151 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003152 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003153 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003155 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003156dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003157 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3158 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003159 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003160 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
Benedict Wongbc56b332018-07-19 10:50:44 -07003162EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3163
3164/* Main function: finds/creates a bundle for given flow.
3165 *
3166 * At the moment we eat a raw IP route. Mostly to speed up lookups
3167 * on interfaces with disabled IPsec.
3168 */
3169struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3170 const struct flowi *fl, const struct sock *sk,
3171 int flags)
3172{
3173 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175EXPORT_SYMBOL(xfrm_lookup);
3176
Steffen Klassertf92ee612014-09-16 10:08:40 +02003177/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3178 * Otherwise we may send out blackholed packets.
3179 */
3180struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3181 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003182 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003183{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003184 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003185 flags | XFRM_LOOKUP_QUEUE |
3186 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003187
Masahiro Yamada45586c72020-02-03 17:37:45 -08003188 if (PTR_ERR(dst) == -EREMOTE)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003189 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3190
Tommi Rantala8cc88772018-06-21 09:30:47 +03003191 if (IS_ERR(dst))
3192 dst_release(dst_orig);
3193
Steffen Klassertf92ee612014-09-16 10:08:40 +02003194 return dst;
3195}
3196EXPORT_SYMBOL(xfrm_lookup_route);
3197
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003198static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003199xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003200{
Florian Westphal2294be0f2018-12-18 17:15:20 +01003201 struct sec_path *sp = skb_sec_path(skb);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003202 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003203
Florian Westphal2294be0f2018-12-18 17:15:20 +01003204 if (!sp || idx < 0 || idx >= sp->len)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003205 return 0;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003206 x = sp->xvec[idx];
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003207 if (!x->type->reject)
3208 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003209 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003210}
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212/* When skb is transformed back to its "native" form, we have to
3213 * check policy restrictions. At the moment we make this in maximally
3214 * stupid way. Shame on me. :-) Of course, connected sockets must
3215 * have policy cached at them.
3216 */
3217
3218static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003219xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 unsigned short family)
3221{
3222 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003223 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return x->id.proto == tmpl->id.proto &&
3225 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3226 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3227 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003228 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003229 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003230 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3231 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003234/*
3235 * 0 or more than 0 is returned when validation is succeeded (either bypass
3236 * because of optional transport mode, or next index of the mathced secpath
3237 * state with the template.
3238 * -1 is returned when no matching template is found.
3239 * Otherwise "-2 - errored_index" is returned.
3240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003242xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 unsigned short family)
3244{
3245 int idx = start;
3246
3247 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003248 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return start;
3250 } else
3251 start = -1;
3252 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003253 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003255 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3256 if (start == -1)
3257 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
3261 return start;
3262}
3263
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003264static void
3265decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse)
3266{
3267 const struct iphdr *iph = ip_hdr(skb);
Florian Westphal858e5402019-05-16 11:28:16 +02003268 int ihl = iph->ihl;
3269 u8 *xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003270 struct flowi4 *fl4 = &fl->u.ip4;
3271 int oif = 0;
3272
Hangbin Liuc3b4c3a2019-08-22 22:19:49 +08003273 if (skb_dst(skb) && skb_dst(skb)->dev)
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003274 oif = skb_dst(skb)->dev->ifindex;
3275
3276 memset(fl4, 0, sizeof(struct flowi4));
3277 fl4->flowi4_mark = skb->mark;
3278 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
3279
Florian Westphal858e5402019-05-16 11:28:16 +02003280 fl4->flowi4_proto = iph->protocol;
3281 fl4->daddr = reverse ? iph->saddr : iph->daddr;
3282 fl4->saddr = reverse ? iph->daddr : iph->saddr;
3283 fl4->flowi4_tos = iph->tos;
3284
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003285 if (!ip_is_fragment(iph)) {
3286 switch (iph->protocol) {
3287 case IPPROTO_UDP:
3288 case IPPROTO_UDPLITE:
3289 case IPPROTO_TCP:
3290 case IPPROTO_SCTP:
3291 case IPPROTO_DCCP:
3292 if (xprth + 4 < skb->data ||
3293 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3294 __be16 *ports;
3295
Florian Westphal858e5402019-05-16 11:28:16 +02003296 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003297 ports = (__be16 *)xprth;
3298
3299 fl4->fl4_sport = ports[!!reverse];
3300 fl4->fl4_dport = ports[!reverse];
3301 }
3302 break;
3303 case IPPROTO_ICMP:
3304 if (xprth + 2 < skb->data ||
3305 pskb_may_pull(skb, xprth + 2 - skb->data)) {
3306 u8 *icmp;
3307
Florian Westphal858e5402019-05-16 11:28:16 +02003308 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003309 icmp = xprth;
3310
3311 fl4->fl4_icmp_type = icmp[0];
3312 fl4->fl4_icmp_code = icmp[1];
3313 }
3314 break;
3315 case IPPROTO_ESP:
3316 if (xprth + 4 < skb->data ||
3317 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3318 __be32 *ehdr;
3319
Florian Westphal858e5402019-05-16 11:28:16 +02003320 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003321 ehdr = (__be32 *)xprth;
3322
3323 fl4->fl4_ipsec_spi = ehdr[0];
3324 }
3325 break;
3326 case IPPROTO_AH:
3327 if (xprth + 8 < skb->data ||
3328 pskb_may_pull(skb, xprth + 8 - skb->data)) {
3329 __be32 *ah_hdr;
3330
Florian Westphal858e5402019-05-16 11:28:16 +02003331 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003332 ah_hdr = (__be32 *)xprth;
3333
3334 fl4->fl4_ipsec_spi = ah_hdr[1];
3335 }
3336 break;
3337 case IPPROTO_COMP:
3338 if (xprth + 4 < skb->data ||
3339 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3340 __be16 *ipcomp_hdr;
3341
Florian Westphal858e5402019-05-16 11:28:16 +02003342 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003343 ipcomp_hdr = (__be16 *)xprth;
3344
3345 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
3346 }
3347 break;
3348 case IPPROTO_GRE:
3349 if (xprth + 12 < skb->data ||
3350 pskb_may_pull(skb, xprth + 12 - skb->data)) {
3351 __be16 *greflags;
3352 __be32 *gre_hdr;
3353
Florian Westphal858e5402019-05-16 11:28:16 +02003354 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003355 greflags = (__be16 *)xprth;
3356 gre_hdr = (__be32 *)xprth;
3357
3358 if (greflags[0] & GRE_KEY) {
3359 if (greflags[0] & GRE_CSUM)
3360 gre_hdr++;
3361 fl4->fl4_gre_key = gre_hdr[1];
3362 }
3363 }
3364 break;
3365 default:
3366 fl4->fl4_ipsec_spi = 0;
3367 break;
3368 }
3369 }
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003370}
3371
3372#if IS_ENABLED(CONFIG_IPV6)
3373static void
3374decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse)
3375{
3376 struct flowi6 *fl6 = &fl->u.ip6;
3377 int onlyproto = 0;
3378 const struct ipv6hdr *hdr = ipv6_hdr(skb);
3379 u32 offset = sizeof(*hdr);
3380 struct ipv6_opt_hdr *exthdr;
3381 const unsigned char *nh = skb_network_header(skb);
3382 u16 nhoff = IP6CB(skb)->nhoff;
3383 int oif = 0;
3384 u8 nexthdr;
3385
3386 if (!nhoff)
3387 nhoff = offsetof(struct ipv6hdr, nexthdr);
3388
3389 nexthdr = nh[nhoff];
3390
Hangbin Liuc3b4c3a2019-08-22 22:19:49 +08003391 if (skb_dst(skb) && skb_dst(skb)->dev)
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003392 oif = skb_dst(skb)->dev->ifindex;
3393
3394 memset(fl6, 0, sizeof(struct flowi6));
3395 fl6->flowi6_mark = skb->mark;
3396 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
3397
3398 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
3399 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
3400
3401 while (nh + offset + sizeof(*exthdr) < skb->data ||
3402 pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
3403 nh = skb_network_header(skb);
3404 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3405
3406 switch (nexthdr) {
3407 case NEXTHDR_FRAGMENT:
3408 onlyproto = 1;
3409 /* fall through */
3410 case NEXTHDR_ROUTING:
3411 case NEXTHDR_HOP:
3412 case NEXTHDR_DEST:
3413 offset += ipv6_optlen(exthdr);
3414 nexthdr = exthdr->nexthdr;
3415 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3416 break;
3417 case IPPROTO_UDP:
3418 case IPPROTO_UDPLITE:
3419 case IPPROTO_TCP:
3420 case IPPROTO_SCTP:
3421 case IPPROTO_DCCP:
3422 if (!onlyproto && (nh + offset + 4 < skb->data ||
3423 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
3424 __be16 *ports;
3425
3426 nh = skb_network_header(skb);
3427 ports = (__be16 *)(nh + offset);
3428 fl6->fl6_sport = ports[!!reverse];
3429 fl6->fl6_dport = ports[!reverse];
3430 }
3431 fl6->flowi6_proto = nexthdr;
3432 return;
3433 case IPPROTO_ICMPV6:
3434 if (!onlyproto && (nh + offset + 2 < skb->data ||
3435 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
3436 u8 *icmp;
3437
3438 nh = skb_network_header(skb);
3439 icmp = (u8 *)(nh + offset);
3440 fl6->fl6_icmp_type = icmp[0];
3441 fl6->fl6_icmp_code = icmp[1];
3442 }
3443 fl6->flowi6_proto = nexthdr;
3444 return;
3445#if IS_ENABLED(CONFIG_IPV6_MIP6)
3446 case IPPROTO_MH:
3447 offset += ipv6_optlen(exthdr);
3448 if (!onlyproto && (nh + offset + 3 < skb->data ||
3449 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
3450 struct ip6_mh *mh;
3451
3452 nh = skb_network_header(skb);
3453 mh = (struct ip6_mh *)(nh + offset);
3454 fl6->fl6_mh_type = mh->ip6mh_type;
3455 }
3456 fl6->flowi6_proto = nexthdr;
3457 return;
3458#endif
3459 /* XXX Why are there these headers? */
3460 case IPPROTO_AH:
3461 case IPPROTO_ESP:
3462 case IPPROTO_COMP:
3463 default:
3464 fl6->fl6_ipsec_spi = 0;
3465 fl6->flowi6_proto = nexthdr;
3466 return;
3467 }
3468 }
3469}
3470#endif
3471
Herbert Xud5422ef2007-12-12 10:44:16 -08003472int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3473 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003475 switch (family) {
3476 case AF_INET:
3477 decode_session4(skb, fl, reverse);
3478 break;
3479#if IS_ENABLED(CONFIG_IPV6)
3480 case AF_INET6:
3481 decode_session6(skb, fl, reverse);
3482 break;
3483#endif
3484 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return -EAFNOSUPPORT;
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Florian Westphalc53ac41e2019-04-16 16:44:39 +02003488 return security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
Herbert Xud5422ef2007-12-12 10:44:16 -08003490EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
David S. Miller9a7386e2011-02-24 01:44:12 -05003492static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003495 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003496 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 }
3500
3501 return 0;
3502}
3503
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003504int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 unsigned short family)
3506{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003507 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003509 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3510 int npols = 0;
3511 int xfrm_nr;
3512 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003513 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003515 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003516 const struct xfrm_if_cb *ifcb;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003517 struct sec_path *sp;
Benedict Wongbc56b332018-07-19 10:50:44 -07003518 struct xfrm_if *xi;
3519 u32 if_id = 0;
3520
3521 rcu_read_lock();
3522 ifcb = xfrm_if_get_cb();
3523
3524 if (ifcb) {
Martin Willi025c65e2019-03-26 13:20:43 +01003525 xi = ifcb->decode_session(skb, family);
Tobias Brunner660899d2019-02-18 10:49:39 +01003526 if (xi) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003527 if_id = xi->p.if_id;
Tobias Brunner660899d2019-02-18 10:49:39 +01003528 net = xi->net;
3529 }
Benedict Wongbc56b332018-07-19 10:50:44 -07003530 }
3531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Herbert Xud5422ef2007-12-12 10:44:16 -08003533 reverse = dir & ~XFRM_POLICY_MASK;
3534 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003535
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003536 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003537 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003539 }
3540
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003541 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
3543 /* First, check used SA against their selectors. */
Florian Westphal2294be0f2018-12-18 17:15:20 +01003544 sp = skb_sec_path(skb);
3545 if (sp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 int i;
3547
Florian Westphal2294be0f2018-12-18 17:15:20 +01003548 for (i = sp->len - 1; i >= 0; i--) {
3549 struct xfrm_state *x = sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003550 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003551 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555 }
3556
3557 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003558 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003559 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003560 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003561 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003562 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003563 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003564 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003567 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003568 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003570 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003571 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003572 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003573 }
James Morris134b0fc2006-10-05 15:42:27 -05003574
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003575 if (!pol) {
Florian Westphal2294be0f2018-12-18 17:15:20 +01003576 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003577 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003578 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003579 return 0;
3580 }
3581 return 1;
3582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Arnd Bergmann386c5682018-07-11 12:19:13 +02003584 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003586 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003587 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003588#ifdef CONFIG_XFRM_SUB_POLICY
3589 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003590 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003591 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003592 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003593 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003594 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003595 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003596 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003597 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003598 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003599 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003600 }
3601 }
3602#endif
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 if (pol->action == XFRM_POLICY_ALLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003606 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003607 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003608 struct xfrm_tmpl **tpp = tp;
3609 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 int i, k;
3611
Florian Westphal2294be0f2018-12-18 17:15:20 +01003612 sp = skb_sec_path(skb);
3613 if (!sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 sp = &dummy;
3615
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003616 for (pi = 0; pi < npols; pi++) {
3617 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003618 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003619 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003620 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003621 }
3622 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003623 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003624 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003625 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003626 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3627 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3628 }
3629 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003630 if (npols > 1) {
Florian Westphal3aaf3912019-05-03 17:46:17 +02003631 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003632 tpp = stp;
3633 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 /* For each tunnel xfrm, find the first matching tmpl.
3636 * For each tmpl before that, find corresponding xfrm.
3637 * Order is _important_. Later we will implement
3638 * some barriers, but at the moment barriers
3639 * are implied between each two transformations.
3640 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003641 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3642 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003643 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003644 if (k < -1)
3645 /* "-2 - errored_index" returned */
3646 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003647 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 }
3651
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003652 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003653 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003657 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return 1;
3659 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003660 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003663 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003664reject_error:
3665 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return 0;
3667}
3668EXPORT_SYMBOL(__xfrm_policy_check);
3669
3670int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3671{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003672 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003674 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003675 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003677 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003678 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003682 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02003683 if (!skb_dst(skb)) {
3684 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3685 return 0;
3686 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003687
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003688 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003689 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003690 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003691 dst = NULL;
3692 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003693 skb_dst_set(skb, dst);
3694 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695}
3696EXPORT_SYMBOL(__xfrm_route_forward);
3697
David S. Millerd49c73c2006-08-13 18:55:53 -07003698/* Optimize later using cookies and generation ids. */
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3701{
David S. Millerd49c73c2006-08-13 18:55:53 -07003702 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003703 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3704 * get validated by dst_ops->check on every use. We do this
3705 * because when a normal route referenced by an XFRM dst is
3706 * obsoleted we do not go looking around for all parent
3707 * referencing XFRM dsts so that we can invalidate them. It
3708 * is just too much work. Instead we make the checks here on
3709 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003710 *
3711 * XFRM dst A --> IPv4 dst X
3712 *
3713 * X is the "xdst->route" of A (X is also the "dst->path" of A
3714 * in this example). If X is marked obsolete, "A" will not
3715 * notice. That's what we are validating here via the
3716 * stale_bundle() check.
3717 *
Wei Wang52df1572017-06-17 10:42:38 -07003718 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3719 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003720 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003721 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003722 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003723 if (dst->obsolete < 0 && !stale_bundle(dst))
3724 return dst;
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return NULL;
3727}
3728
3729static int stale_bundle(struct dst_entry *dst)
3730{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003731 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732}
3733
Herbert Xuaabc9762005-05-03 16:27:10 -07003734void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735{
David Millerb92cf4a2017-11-28 15:40:22 -05003736 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003737 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003738 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 dev_put(dev);
3740 }
3741}
Herbert Xuaabc9762005-05-03 16:27:10 -07003742EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744static void xfrm_link_failure(struct sk_buff *skb)
3745{
3746 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
3749static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3750{
3751 if (dst) {
3752 if (dst->obsolete) {
3753 dst_release(dst);
3754 dst = NULL;
3755 }
3756 }
3757 return dst;
3758}
3759
David Miller54920932017-11-28 15:41:01 -05003760static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
David Miller54920932017-11-28 15:41:01 -05003762 while (nr--) {
3763 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003765 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
David Miller54920932017-11-28 15:41:01 -05003767 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003768 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 xdst->child_mtu_cached = pmtu;
3770
3771 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3772
3773 route_mtu_cached = dst_mtu(xdst->route);
3774 xdst->route_mtu_cached = route_mtu_cached;
3775
3776 if (pmtu > route_mtu_cached)
3777 pmtu = route_mtu_cached;
3778
David S. Millerdefb3512010-12-08 21:16:57 -08003779 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783/* Check that the bundle accepts the flow and its components are
3784 * still valid.
3785 */
3786
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003787static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
David Miller54920932017-11-28 15:41:01 -05003789 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003791 struct xfrm_dst *xdst;
3792 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 u32 mtu;
3794
David Miller0f6c4802017-11-28 15:40:46 -05003795 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 (dst->dev && !netif_running(dst->dev)))
3797 return 0;
3798
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003799 if (dst->flags & DST_XFRM_QUEUE)
3800 return 1;
3801
David Miller54920932017-11-28 15:41:01 -05003802 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 do {
3804 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3807 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003808 if (xdst->xfrm_genid != dst->xfrm->genid)
3809 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003810 if (xdst->num_pols > 0 &&
3811 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
David Miller54920932017-11-28 15:41:01 -05003814 bundle[nr++] = xdst;
3815
David Millerb92cf4a2017-11-28 15:40:22 -05003816 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003818 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 xdst->child_mtu_cached = mtu;
3820 }
3821
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003822 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return 0;
3824 mtu = dst_mtu(xdst->route);
3825 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003826 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 xdst->route_mtu_cached = mtu;
3828 }
3829
David Millerb92cf4a2017-11-28 15:40:22 -05003830 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 } while (dst->xfrm);
3832
David Miller54920932017-11-28 15:41:01 -05003833 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return 1;
3835
David Miller54920932017-11-28 15:41:01 -05003836 xdst = bundle[start_from - 1];
3837 mtu = xdst->child_mtu_cached;
3838 while (start_from--) {
3839 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
3841 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003842 if (mtu > xdst->route_mtu_cached)
3843 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003844 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003845 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 break;
3847
David Miller54920932017-11-28 15:41:01 -05003848 xdst = bundle[start_from - 1];
3849 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851
3852 return 1;
3853}
3854
David S. Miller0dbaee32010-12-13 12:52:14 -08003855static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3856{
David Miller0f6c4802017-11-28 15:40:46 -05003857 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003858}
3859
Steffen Klassertebb762f2011-11-23 02:12:51 +00003860static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003861{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003862 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3863
David Miller0f6c4802017-11-28 15:40:46 -05003864 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003865}
3866
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003867static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3868 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003869{
David Miller0f6c4802017-11-28 15:40:46 -05003870 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003871 const struct xfrm_state *xfrm = dst->xfrm;
3872
Steffen Klassert013cb812018-02-19 07:44:07 +01003873 dst = xfrm_dst_child(dst);
3874
Julian Anastasov63fca652017-02-06 23:14:15 +02003875 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3876 continue;
3877 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3878 daddr = xfrm->coaddr;
3879 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3880 daddr = &xfrm->id.daddr;
3881 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003882 return daddr;
3883}
3884
3885static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3886 struct sk_buff *skb,
3887 const void *daddr)
3888{
David Miller0f6c4802017-11-28 15:40:46 -05003889 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003890
3891 if (!skb)
3892 daddr = xfrm_get_dst_nexthop(dst, daddr);
3893 return path->ops->neigh_lookup(path, skb, daddr);
3894}
3895
3896static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3897{
David Miller0f6c4802017-11-28 15:40:46 -05003898 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003899
3900 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003901 path->ops->confirm_neigh(path, daddr);
3902}
3903
Florian Westphala2817d82017-02-07 15:00:17 +01003904int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
3906 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003907
3908 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003910
Eric Dumazetef8531b2012-08-19 12:31:48 +02003911 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003912 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003913 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 else {
3915 struct dst_ops *dst_ops = afinfo->dst_ops;
3916 if (likely(dst_ops->kmem_cachep == NULL))
3917 dst_ops->kmem_cachep = xfrm_dst_cache;
3918 if (likely(dst_ops->check == NULL))
3919 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003920 if (likely(dst_ops->default_advmss == NULL))
3921 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003922 if (likely(dst_ops->mtu == NULL))
3923 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 if (likely(dst_ops->negative_advice == NULL))
3925 dst_ops->negative_advice = xfrm_negative_advice;
3926 if (likely(dst_ops->link_failure == NULL))
3927 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003928 if (likely(dst_ops->neigh_lookup == NULL))
3929 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003930 if (likely(!dst_ops->confirm_neigh))
3931 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003932 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003934 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return err;
3937}
3938EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3939
Florian Westphala2817d82017-02-07 15:00:17 +01003940void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Florian Westphal2b619972017-02-07 15:00:15 +01003942 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003943 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003944
Florian Westphala2817d82017-02-07 15:00:17 +01003945 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3946 if (xfrm_policy_afinfo[i] != afinfo)
3947 continue;
3948 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3949 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Florian Westphal2b619972017-02-07 15:00:15 +01003952 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Florian Westphal2b619972017-02-07 15:00:15 +01003954 dst_ops->kmem_cachep = NULL;
3955 dst_ops->check = NULL;
3956 dst_ops->negative_advice = NULL;
3957 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3960
Steffen Klassertf203b762018-06-12 14:07:12 +02003961void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3962{
3963 spin_lock(&xfrm_if_cb_lock);
3964 rcu_assign_pointer(xfrm_if_cb, ifcb);
3965 spin_unlock(&xfrm_if_cb_lock);
3966}
3967EXPORT_SYMBOL(xfrm_if_register_cb);
3968
3969void xfrm_if_unregister_cb(void)
3970{
3971 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3972 synchronize_rcu();
3973}
3974EXPORT_SYMBOL(xfrm_if_unregister_cb);
3975
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003976#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003977static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003978{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003979 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003980 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3981 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003982 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003983 rv = xfrm_proc_init(net);
3984 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003985 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003986 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003987}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003988
3989static void xfrm_statistics_fini(struct net *net)
3990{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003991 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003992 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003993}
3994#else
3995static int __net_init xfrm_statistics_init(struct net *net)
3996{
3997 return 0;
3998}
3999
4000static void xfrm_statistics_fini(struct net *net)
4001{
4002}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08004003#endif
4004
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004005static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
David S. Miller2518c7c2006-08-24 04:45:07 -07004007 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01004008 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07004009
Florian Westphal24969fa2018-11-07 23:00:35 +01004010 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004011 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004013 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004014 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01004015 err = rhashtable_init(&xfrm_policy_inexact_table,
4016 &xfrm_pol_inexact_params);
4017 BUG_ON(err);
4018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
David S. Miller2518c7c2006-08-24 04:45:07 -07004020 hmask = 8 - 1;
4021 sz = (hmask+1) * sizeof(struct hlist_head);
4022
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004023 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
4024 if (!net->xfrm.policy_byidx)
4025 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004026 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07004027
Herbert Xu53c2e282014-11-13 17:09:49 +08004028 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07004029 struct xfrm_policy_hash *htab;
4030
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08004031 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08004032 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004033 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07004034
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004035 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07004036 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07004037 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004038 goto out_bydst;
4039 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02004040 htab->dbits4 = 32;
4041 htab->sbits4 = 32;
4042 htab->dbits6 = 128;
4043 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07004044 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004045 net->xfrm.policy_hthresh.lbits4 = 32;
4046 net->xfrm.policy_hthresh.rbits4 = 32;
4047 net->xfrm.policy_hthresh.lbits6 = 128;
4048 net->xfrm.policy_hthresh.rbits6 = 128;
4049
4050 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07004051
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004052 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01004053 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08004054 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004055 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004056 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004057
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004058out_bydst:
4059 for (dir--; dir >= 0; dir--) {
4060 struct xfrm_policy_hash *htab;
4061
4062 htab = &net->xfrm.policy_bydst[dir];
4063 xfrm_hash_free(htab->table, sz);
4064 }
4065 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004066out_byidx:
4067 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004070static void xfrm_policy_fini(struct net *net)
4071{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004072 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004073 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004074 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004075
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004076 flush_work(&net->xfrm.policy_hash_work);
4077#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09004078 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004079#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09004080 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004081
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004082 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004083
Herbert Xu53c2e282014-11-13 17:09:49 +08004084 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004085 struct xfrm_policy_hash *htab;
4086
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004087 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004088
4089 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01004090 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004091 WARN_ON(!hlist_empty(htab->table));
4092 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004093 }
4094
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004095 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004096 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
4097 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01004098
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004099 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4100 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
4101 __xfrm_policy_inexact_prune_bin(b, true);
4102 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004103}
4104
4105static int __net_init xfrm_net_init(struct net *net)
4106{
4107 int rv;
4108
Florian Westphalc2822222017-02-08 11:52:29 +01004109 /* Initialize the per-net locks here */
4110 spin_lock_init(&net->xfrm.xfrm_state_lock);
4111 spin_lock_init(&net->xfrm.xfrm_policy_lock);
4112 mutex_init(&net->xfrm.xfrm_cfg_mutex);
4113
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004114 rv = xfrm_statistics_init(net);
4115 if (rv < 0)
4116 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004117 rv = xfrm_state_init(net);
4118 if (rv < 0)
4119 goto out_state;
4120 rv = xfrm_policy_init(net);
4121 if (rv < 0)
4122 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004123 rv = xfrm_sysctl_init(net);
4124 if (rv < 0)
4125 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08004126
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004127 return 0;
4128
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004129out_sysctl:
4130 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004131out_policy:
4132 xfrm_state_fini(net);
4133out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004134 xfrm_statistics_fini(net);
4135out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004136 return rv;
4137}
4138
4139static void __net_exit xfrm_net_exit(struct net *net)
4140{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004141 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004142 xfrm_policy_fini(net);
4143 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004144 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004145}
4146
4147static struct pernet_operations __net_initdata xfrm_net_ops = {
4148 .init = xfrm_net_init,
4149 .exit = xfrm_net_exit,
4150};
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152void __init xfrm_init(void)
4153{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004154 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03004155 xfrm_dev_init();
Ahmed S. Darwish77cc278f2020-07-20 17:55:22 +02004156 seqcount_mutex_init(&xfrm_policy_hash_generation, &hash_resize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02004158
Sabrina Dubrocae27cca92019-11-25 14:49:02 +01004159#ifdef CONFIG_INET_ESPINTCP
4160 espintcp_init();
4161#endif
4162
Steffen Klassertf203b762018-06-12 14:07:12 +02004163 RCU_INIT_POINTER(xfrm_if_cb, NULL);
4164 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Joy Lattenab5f5e82007-09-17 11:51:22 -07004167#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08004168static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
4169 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004170{
Paul Moore875179f2007-12-01 23:27:18 +11004171 struct xfrm_sec_ctx *ctx = xp->security;
4172 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004173
Paul Moore875179f2007-12-01 23:27:18 +11004174 if (ctx)
4175 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
4176 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
4177
Weilong Chen9b7a7872013-12-24 09:43:46 +08004178 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07004179 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07004180 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004181 if (sel->prefixlen_s != 32)
4182 audit_log_format(audit_buf, " src_prefixlen=%d",
4183 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07004184 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004185 if (sel->prefixlen_d != 32)
4186 audit_log_format(audit_buf, " dst_prefixlen=%d",
4187 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004188 break;
4189 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004190 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004191 if (sel->prefixlen_s != 128)
4192 audit_log_format(audit_buf, " src_prefixlen=%d",
4193 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004194 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004195 if (sel->prefixlen_d != 128)
4196 audit_log_format(audit_buf, " dst_prefixlen=%d",
4197 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004198 break;
4199 }
4200}
4201
Tetsuo Handa2e710292014-04-22 21:48:30 +09004202void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004203{
4204 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004205
Paul Mooreafeb14b2007-12-21 14:58:11 -08004206 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004207 if (audit_buf == NULL)
4208 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004209 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004210 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004211 xfrm_audit_common_policyinfo(xp, audit_buf);
4212 audit_log_end(audit_buf);
4213}
4214EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
4215
Paul Moore68277ac2007-12-20 20:49:33 -08004216void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09004217 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004218{
4219 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004220
Paul Mooreafeb14b2007-12-21 14:58:11 -08004221 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004222 if (audit_buf == NULL)
4223 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004224 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004225 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004226 xfrm_audit_common_policyinfo(xp, audit_buf);
4227 audit_log_end(audit_buf);
4228}
4229EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
4230#endif
4231
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004232#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04004233static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
4234 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004235{
4236 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
4237 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004238 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
4239 sel_cmp->family) &&
4240 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
4241 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004242 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
4243 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004244 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004245 }
4246 } else {
4247 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004248 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004249 }
4250 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04004251 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004252}
4253
Weilong Chen3e94c2d2013-12-24 09:43:47 +08004254static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
4255 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004256{
4257 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004258 struct hlist_head *chain;
4259 u32 priority = ~0U;
4260
Florian Westphal9d0380d2016-08-11 15:17:59 +02004261 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08004262 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004263 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004264 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4265 pol->type == type) {
4266 ret = pol;
4267 priority = ret->priority;
4268 break;
4269 }
4270 }
Fan Du8d549c42013-11-07 17:47:49 +08004271 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01004272 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08004273 if ((pol->priority >= priority) && ret)
4274 break;
4275
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004276 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08004277 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004278 ret = pol;
4279 break;
4280 }
4281 }
4282
Li RongQing586f2eb2015-04-30 17:13:41 +08004283 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004284
Florian Westphal9d0380d2016-08-11 15:17:59 +02004285 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004286
4287 return ret;
4288}
4289
David S. Millerdd701752011-02-24 00:21:08 -05004290static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004291{
4292 int match = 0;
4293
4294 if (t->mode == m->mode && t->id.proto == m->proto &&
4295 (m->reqid == 0 || t->reqid == m->reqid)) {
4296 switch (t->mode) {
4297 case XFRM_MODE_TUNNEL:
4298 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004299 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
4300 m->old_family) &&
4301 xfrm_addr_equal(&t->saddr, &m->old_saddr,
4302 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004303 match = 1;
4304 }
4305 break;
4306 case XFRM_MODE_TRANSPORT:
4307 /* in case of transport mode, template does not store
4308 any IP addresses, hence we just compare mode and
4309 protocol */
4310 match = 1;
4311 break;
4312 default:
4313 break;
4314 }
4315 }
4316 return match;
4317}
4318
4319/* update endpoint address(es) of template(s) */
4320static int xfrm_policy_migrate(struct xfrm_policy *pol,
4321 struct xfrm_migrate *m, int num_migrate)
4322{
4323 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004324 int i, j, n = 0;
4325
4326 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07004327 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004328 /* target policy has been deleted */
4329 write_unlock_bh(&pol->lock);
4330 return -ENOENT;
4331 }
4332
4333 for (i = 0; i < pol->xfrm_nr; i++) {
4334 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4335 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4336 continue;
4337 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07004338 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4339 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004340 continue;
4341 /* update endpoints */
4342 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4343 sizeof(pol->xfrm_vec[i].id.daddr));
4344 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4345 sizeof(pol->xfrm_vec[i].saddr));
4346 pol->xfrm_vec[i].encap_family = mp->new_family;
4347 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00004348 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004349 }
4350 }
4351
4352 write_unlock_bh(&pol->lock);
4353
4354 if (!n)
4355 return -ENODATA;
4356
4357 return 0;
4358}
4359
David S. Millerdd701752011-02-24 00:21:08 -05004360static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004361{
4362 int i, j;
4363
4364 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4365 return -EINVAL;
4366
4367 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004368 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4369 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4370 return -EINVAL;
4371
4372 /* check if there is any duplicated entry */
4373 for (j = i + 1; j < num_migrate; j++) {
4374 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4375 sizeof(m[i].old_daddr)) &&
4376 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4377 sizeof(m[i].old_saddr)) &&
4378 m[i].proto == m[j].proto &&
4379 m[i].mode == m[j].mode &&
4380 m[i].reqid == m[j].reqid &&
4381 m[i].old_family == m[j].old_family)
4382 return -EINVAL;
4383 }
4384 }
4385
4386 return 0;
4387}
4388
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004389int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004390 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004391 struct xfrm_kmaddress *k, struct net *net,
4392 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004393{
4394 int i, err, nx_cur = 0, nx_new = 0;
4395 struct xfrm_policy *pol = NULL;
4396 struct xfrm_state *x, *xc;
4397 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4398 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4399 struct xfrm_migrate *mp;
4400
Vladis Dronov7bab0962017-08-02 19:50:14 +02004401 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004402 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4403 goto out;
4404
Vladis Dronov7bab0962017-08-02 19:50:14 +02004405 if (dir >= XFRM_POLICY_MAX) {
4406 err = -EINVAL;
4407 goto out;
4408 }
4409
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004410 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004411 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004412 err = -ENOENT;
4413 goto out;
4414 }
4415
4416 /* Stage 2 - find and update state(s) */
4417 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004418 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004419 x_cur[nx_cur] = x;
4420 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004421 xc = xfrm_state_migrate(x, mp, encap);
4422 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004423 x_new[nx_new] = xc;
4424 nx_new++;
4425 } else {
4426 err = -ENODATA;
4427 goto restore_state;
4428 }
4429 }
4430 }
4431
4432 /* Stage 3 - update policy */
4433 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4434 goto restore_state;
4435
4436 /* Stage 4 - delete old state(s) */
4437 if (nx_cur) {
4438 xfrm_states_put(x_cur, nx_cur);
4439 xfrm_states_delete(x_cur, nx_cur);
4440 }
4441
4442 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004443 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004444
4445 xfrm_pol_put(pol);
4446
4447 return 0;
4448out:
4449 return err;
4450
4451restore_state:
4452 if (pol)
4453 xfrm_pol_put(pol);
4454 if (nx_cur)
4455 xfrm_states_put(x_cur, nx_cur);
4456 if (nx_new)
4457 xfrm_states_delete(x_new, nx_new);
4458
4459 return err;
4460}
David S. Millere610e672007-02-08 13:29:15 -08004461EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004462#endif