blob: f2d1e573ea55154eb2ee4fc3dbdd47313d969b98 [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 Westphalc53ac412019-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 Westphalc53ac412019-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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
David S. Miller44e36b42006-08-24 04:50:50 -070043#include "xfrm_hash.h"
44
Steffen Klasserta0073fe2013-02-05 12:52:55 +010045#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
46#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
47#define XFRM_MAX_QUEUE_LEN 100
48
Steffen Klassertb8c203b2014-09-16 10:08:49 +020049struct xfrm_flo {
50 struct dst_entry *dst_orig;
51 u8 flags;
52};
53
Florian Westphal6be3b0d2018-11-07 23:00:37 +010054/* prefixes smaller than this are stored in lists, not trees. */
55#define INEXACT_PREFIXLEN_IPV4 16
56#define INEXACT_PREFIXLEN_IPV6 48
Florian Westphal9cf545e2018-11-07 23:00:38 +010057
58struct xfrm_pol_inexact_node {
59 struct rb_node node;
60 union {
61 xfrm_address_t addr;
62 struct rcu_head rcu;
63 };
64 u8 prefixlen;
65
Florian Westphal6ac098b2018-11-07 23:00:41 +010066 struct rb_root root;
67
Florian Westphal9cf545e2018-11-07 23:00:38 +010068 /* the policies matching this node, can be empty list */
69 struct hlist_head hhead;
70};
71
72/* xfrm inexact policy search tree:
73 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
74 * |
75 * +---- root_d: sorted by daddr:prefix
76 * | |
77 * | xfrm_pol_inexact_node
78 * | |
Florian Westphal6ac098b2018-11-07 23:00:41 +010079 * | +- root: sorted by saddr/prefix
80 * | | |
81 * | | xfrm_pol_inexact_node
82 * | | |
83 * | | + root: unused
84 * | | |
85 * | | + hhead: saddr:daddr policies
86 * | |
Florian Westphal9cf545e2018-11-07 23:00:38 +010087 * | +- coarse policies and all any:daddr policies
88 * |
Florian Westphal64a09a72018-11-07 23:00:40 +010089 * +---- root_s: sorted by saddr:prefix
90 * | |
91 * | xfrm_pol_inexact_node
92 * | |
93 * | + root: unused
94 * | |
95 * | + hhead: saddr:any policies
96 * |
Florian Westphal9cf545e2018-11-07 23:00:38 +010097 * +---- coarse policies and all any:any policies
98 *
Florian Westphal6ac098b2018-11-07 23:00:41 +010099 * Lookups return four candidate lists:
Florian Westphal9cf545e2018-11-07 23:00:38 +0100100 * 1. any:any list from top-level xfrm_pol_inexact_bin
101 * 2. any:daddr list from daddr tree
Florian Westphal6ac098b2018-11-07 23:00:41 +0100102 * 3. saddr:daddr list from 2nd level daddr tree
103 * 4. saddr:any list from saddr tree
Florian Westphal9cf545e2018-11-07 23:00:38 +0100104 *
105 * This result set then needs to be searched for the policy with
106 * the lowest priority. If two results have same prio, youngest one wins.
107 */
108
Florian Westphal24969fa2018-11-07 23:00:35 +0100109struct xfrm_pol_inexact_key {
110 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100111 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +0100112 u16 family;
113 u8 dir, type;
114};
115
116struct xfrm_pol_inexact_bin {
117 struct xfrm_pol_inexact_key k;
118 struct rhash_head head;
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100119 /* list containing '*:*' policies */
Florian Westphal24969fa2018-11-07 23:00:35 +0100120 struct hlist_head hhead;
121
Florian Westphal9cf545e2018-11-07 23:00:38 +0100122 seqcount_t count;
123 /* tree sorted by daddr/prefix */
124 struct rb_root root_d;
125
Florian Westphal64a09a72018-11-07 23:00:40 +0100126 /* tree sorted by saddr/prefix */
127 struct rb_root root_s;
128
Florian Westphal24969fa2018-11-07 23:00:35 +0100129 /* slow path below */
130 struct list_head inexact_bins;
131 struct rcu_head rcu;
132};
133
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100134enum xfrm_pol_inexact_candidate_type {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100135 XFRM_POL_CAND_BOTH,
Florian Westphal64a09a72018-11-07 23:00:40 +0100136 XFRM_POL_CAND_SADDR,
Florian Westphal9cf545e2018-11-07 23:00:38 +0100137 XFRM_POL_CAND_DADDR,
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100138 XFRM_POL_CAND_ANY,
139
140 XFRM_POL_CAND_MAX,
141};
142
143struct xfrm_pol_inexact_candidates {
144 struct hlist_head *res[XFRM_POL_CAND_MAX];
145};
146
Steffen Klassertf203b762018-06-12 14:07:12 +0200147static DEFINE_SPINLOCK(xfrm_if_cb_lock);
148static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
149
Priyanka Jain418a99a2012-08-12 21:22:29 +0000150static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +0100151static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +0000152 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +0300154static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +0200155static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Florian Westphal24969fa2018-11-07 23:00:35 +0100157static struct rhashtable xfrm_policy_inexact_table;
158static const struct rhashtable_params xfrm_pol_inexact_params;
159
David Miller54920932017-11-28 15:41:01 -0500160static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +0000161static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +0000162static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -0700163static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800165static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800166static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
167 int dir);
168
Florian Westphal24969fa2018-11-07 23:00:35 +0100169static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100170xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
171 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100172
173static struct xfrm_pol_inexact_bin *
174xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100175 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100176static struct xfrm_policy *
177xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
178 bool excl);
179static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
180 struct xfrm_policy *policy);
181
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100182static bool
183xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
184 struct xfrm_pol_inexact_bin *b,
185 const xfrm_address_t *saddr,
186 const xfrm_address_t *daddr);
187
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200188static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
189{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300190 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200191}
192
David S. Millerbc9b35a2012-05-15 15:04:57 -0400193static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500194__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800195{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500196 const struct flowi4 *fl4 = &fl->u.ip4;
197
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000198 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
199 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500200 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
201 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
202 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
203 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800204}
205
David S. Millerbc9b35a2012-05-15 15:04:57 -0400206static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500207__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800208{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500209 const struct flowi6 *fl6 = &fl->u.ip6;
210
211 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
212 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
213 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
214 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
215 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
216 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800217}
218
David S. Millerbc9b35a2012-05-15 15:04:57 -0400219bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
220 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800221{
222 switch (family) {
223 case AF_INET:
224 return __xfrm4_selector_match(sel, fl);
225 case AF_INET6:
226 return __xfrm6_selector_match(sel, fl);
227 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400228 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800229}
230
Florian Westphala2817d82017-02-07 15:00:17 +0100231static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200232{
Florian Westphala2817d82017-02-07 15:00:17 +0100233 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200234
Florian Westphala2817d82017-02-07 15:00:17 +0100235 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200236 return NULL;
237 rcu_read_lock();
238 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
239 if (unlikely(!afinfo))
240 rcu_read_unlock();
241 return afinfo;
242}
243
Steffen Klassertf203b762018-06-12 14:07:12 +0200244/* Called with rcu_read_lock(). */
245static const struct xfrm_if_cb *xfrm_if_get_cb(void)
246{
247 return rcu_dereference(xfrm_if_cb);
248}
249
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200250struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
251 const xfrm_address_t *saddr,
252 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900253 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Florian Westphal37b10382017-02-07 15:00:19 +0100255 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800256 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Herbert Xu25ee3282007-12-11 09:32:34 -0800258 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800260 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900262 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900263
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100264 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900265
266 return dst;
267}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200268EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900269
David Ahern42a7b322015-08-10 16:58:11 -0600270static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
271 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900272 xfrm_address_t *prev_saddr,
273 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900274 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900275{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800276 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900277 xfrm_address_t *saddr = &x->props.saddr;
278 xfrm_address_t *daddr = &x->id.daddr;
279 struct dst_entry *dst;
280
281 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
282 saddr = x->coaddr;
283 daddr = prev_daddr;
284 }
285 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
286 saddr = prev_saddr;
287 daddr = x->coaddr;
288 }
289
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900290 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900291
292 if (!IS_ERR(dst)) {
293 if (prev_saddr != saddr)
294 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
295 if (prev_daddr != daddr)
296 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
297 }
298
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800299 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static inline unsigned long make_jiffies(long secs)
303{
304 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
305 return MAX_SCHEDULE_TIMEOUT-1;
306 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900307 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Kees Cookc3aed702017-10-16 17:28:56 -0700310static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Kees Cookc3aed702017-10-16 17:28:56 -0700312 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200313 time64_t now = ktime_get_real_seconds();
314 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int warn = 0;
316 int dir;
317
318 read_lock(&xp->lock);
319
Timo Teräsea2dea92010-03-31 00:17:05 +0000320 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 goto out;
322
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700323 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200326 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xp->curlft.add_time - now;
328 if (tmo <= 0)
329 goto expired;
330 if (tmo < next)
331 next = tmo;
332 }
333 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200334 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
336 if (tmo <= 0)
337 goto expired;
338 if (tmo < next)
339 next = tmo;
340 }
341 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200342 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 xp->curlft.add_time - now;
344 if (tmo <= 0) {
345 warn = 1;
346 tmo = XFRM_KM_TIMEOUT;
347 }
348 if (tmo < next)
349 next = tmo;
350 }
351 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200352 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
354 if (tmo <= 0) {
355 warn = 1;
356 tmo = XFRM_KM_TIMEOUT;
357 }
358 if (tmo < next)
359 next = tmo;
360 }
361
362 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800363 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200364 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
366 xfrm_pol_hold(xp);
367
368out:
369 read_unlock(&xp->lock);
370 xfrm_pol_put(xp);
371 return;
372
373expired:
374 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700375 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800376 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfrm_pol_put(xp);
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
381 * SPD calls.
382 */
383
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800384struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct xfrm_policy *policy;
387
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700388 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800391 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700392 INIT_LIST_HEAD(&policy->walk.all);
Florian Westphal24969fa2018-11-07 23:00:35 +0100393 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700394 INIT_HLIST_NODE(&policy->bydst);
395 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300397 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100398 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700399 timer_setup(&policy->timer, xfrm_policy_timer, 0);
400 timer_setup(&policy->polq.hold_timer,
401 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return policy;
404}
405EXPORT_SYMBOL(xfrm_policy_alloc);
406
Eric Dumazet56f04732015-12-08 07:22:01 -0800407static void xfrm_policy_destroy_rcu(struct rcu_head *head)
408{
409 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
410
411 security_xfrm_policy_free(policy->security);
412 kfree(policy);
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* Destroy xfrm_policy: descendant resources must be released to this moment. */
416
WANG Cong64c31b32008-01-07 22:34:29 -0800417void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Herbert Xu12a169e2008-10-01 07:03:24 -0700419 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Fan Du0659eea2013-08-01 18:08:36 +0800421 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 BUG();
423
Eric Dumazet56f04732015-12-08 07:22:01 -0800424 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
WANG Cong64c31b32008-01-07 22:34:29 -0800426EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100428/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * entry dead. The rule must be unlinked from lists to the moment.
430 */
431
432static void xfrm_policy_kill(struct xfrm_policy *policy)
433{
Herbert Xu12a169e2008-10-01 07:03:24 -0700434 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Timo Teräs285ead12010-04-07 00:30:06 +0000436 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200438 if (del_timer(&policy->polq.hold_timer))
439 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800440 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100441
Timo Teräs285ead12010-04-07 00:30:06 +0000442 if (del_timer(&policy->timer))
443 xfrm_pol_put(policy);
444
445 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
David S. Miller2518c7c2006-08-24 04:45:07 -0700448static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
449
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800450static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700451{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800452 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700453}
454
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200455/* calculate policy hash thresholds */
456static void __get_hash_thresh(struct net *net,
457 unsigned short family, int dir,
458 u8 *dbits, u8 *sbits)
459{
460 switch (family) {
461 case AF_INET:
462 *dbits = net->xfrm.policy_bydst[dir].dbits4;
463 *sbits = net->xfrm.policy_bydst[dir].sbits4;
464 break;
465
466 case AF_INET6:
467 *dbits = net->xfrm.policy_bydst[dir].dbits6;
468 *sbits = net->xfrm.policy_bydst[dir].sbits6;
469 break;
470
471 default:
472 *dbits = 0;
473 *sbits = 0;
474 }
475}
476
David S. Miller5f803b52011-02-24 00:33:19 -0500477static struct hlist_head *policy_hash_bysel(struct net *net,
478 const struct xfrm_selector *sel,
479 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700480{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800481 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200482 unsigned int hash;
483 u8 dbits;
484 u8 sbits;
485
486 __get_hash_thresh(net, family, dir, &dbits, &sbits);
487 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700488
Florian Westphale1e551b2016-08-11 15:17:53 +0200489 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100490 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200491
492 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
493 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700494}
495
David S. Miller5f803b52011-02-24 00:33:19 -0500496static struct hlist_head *policy_hash_direct(struct net *net,
497 const xfrm_address_t *daddr,
498 const xfrm_address_t *saddr,
499 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700500{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800501 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200502 unsigned int hash;
503 u8 dbits;
504 u8 sbits;
505
506 __get_hash_thresh(net, family, dir, &dbits, &sbits);
507 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700508
Florian Westphale1e551b2016-08-11 15:17:53 +0200509 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
510 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700511}
512
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200513static void xfrm_dst_hash_transfer(struct net *net,
514 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700515 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200516 unsigned int nhashmask,
517 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700518{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800519 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700520 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800521 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200522 u8 dbits;
523 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700524
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800525redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800526 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700527 unsigned int h;
528
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200529 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700530 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200531 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800532 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200533 hlist_del_rcu(&pol->bydst);
534 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800535 h0 = h;
536 } else {
537 if (h != h0)
538 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200539 hlist_del_rcu(&pol->bydst);
540 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800541 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800542 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800543 }
544 if (!hlist_empty(list)) {
545 entry0 = NULL;
546 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700547 }
548}
549
550static void xfrm_idx_hash_transfer(struct hlist_head *list,
551 struct hlist_head *nidxtable,
552 unsigned int nhashmask)
553{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800554 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700555 struct xfrm_policy *pol;
556
Sasha Levinb67bfe02013-02-27 17:06:00 -0800557 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700558 unsigned int h;
559
560 h = __idx_hash(pol->index, nhashmask);
561 hlist_add_head(&pol->byidx, nidxtable+h);
562 }
563}
564
565static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
566{
567 return ((old_hmask + 1) << 1) - 1;
568}
569
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800570static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700571{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800572 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700573 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
574 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700575 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200576 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700577 int i;
578
579 if (!ndst)
580 return;
581
Florian Westphal9d0380d2016-08-11 15:17:59 +0200582 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200583 write_seqcount_begin(&xfrm_policy_hash_generation);
584
585 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
586 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700587
588 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200589 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700590
Florian Westphale1e551b2016-08-11 15:17:53 +0200591 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800592 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700593
Florian Westphal30846092016-08-11 15:17:54 +0200594 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200595 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700596
Florian Westphale1e551b2016-08-11 15:17:53 +0200597 synchronize_rcu();
598
David S. Miller44e36b42006-08-24 04:50:50 -0700599 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700600}
601
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800602static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700603{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800604 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700605 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
606 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800607 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700608 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700609 int i;
610
611 if (!nidx)
612 return;
613
Florian Westphal9d0380d2016-08-11 15:17:59 +0200614 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700615
616 for (i = hmask; i >= 0; i--)
617 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
618
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800619 net->xfrm.policy_byidx = nidx;
620 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700621
Florian Westphal9d0380d2016-08-11 15:17:59 +0200622 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700623
David S. Miller44e36b42006-08-24 04:50:50 -0700624 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700625}
626
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800627static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700628{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800629 unsigned int cnt = net->xfrm.policy_count[dir];
630 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700631
632 if (total)
633 *total += cnt;
634
635 if ((hmask + 1) < xfrm_policy_hashmax &&
636 cnt > hmask)
637 return 1;
638
639 return 0;
640}
641
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800642static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700643{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800644 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700645
646 if ((hmask + 1) < xfrm_policy_hashmax &&
647 total > hmask)
648 return 1;
649
650 return 0;
651}
652
Alexey Dobriyane0710412010-01-23 13:37:10 +0000653void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700654{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000655 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
656 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
657 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
658 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
659 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
660 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
661 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700662 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700663}
664EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700665
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700666static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800667static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700668{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800669 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700670 int dir, total;
671
672 mutex_lock(&hash_resize_mutex);
673
674 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800675 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800676 if (xfrm_bydst_should_resize(net, dir, &total))
677 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700678 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800679 if (xfrm_byidx_should_resize(net, total))
680 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700681
682 mutex_unlock(&hash_resize_mutex);
683}
684
Florian Westphal24969fa2018-11-07 23:00:35 +0100685/* Make sure *pol can be inserted into fastbin.
686 * Useful to check that later insert requests will be sucessful
687 * (provided xfrm_policy_lock is held throughout).
688 */
689static struct xfrm_pol_inexact_bin *
690xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
691{
692 struct xfrm_pol_inexact_bin *bin, *prev;
693 struct xfrm_pol_inexact_key k = {
694 .family = pol->family,
695 .type = pol->type,
696 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100697 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100698 };
699 struct net *net = xp_net(pol);
700
701 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
702
703 write_pnet(&k.net, net);
704 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
705 xfrm_pol_inexact_params);
706 if (bin)
707 return bin;
708
709 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
710 if (!bin)
711 return NULL;
712
713 bin->k = k;
714 INIT_HLIST_HEAD(&bin->hhead);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100715 bin->root_d = RB_ROOT;
Florian Westphal64a09a72018-11-07 23:00:40 +0100716 bin->root_s = RB_ROOT;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100717 seqcount_init(&bin->count);
Florian Westphal24969fa2018-11-07 23:00:35 +0100718
719 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
720 &bin->k, &bin->head,
721 xfrm_pol_inexact_params);
722 if (!prev) {
723 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
724 return bin;
725 }
726
727 kfree(bin);
728
729 return IS_ERR(prev) ? NULL : prev;
730}
731
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100732static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
733 int family, u8 prefixlen)
Florian Westphal24969fa2018-11-07 23:00:35 +0100734{
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100735 if (xfrm_addr_any(addr, family))
736 return true;
Florian Westphal24969fa2018-11-07 23:00:35 +0100737
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100738 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
739 return true;
740
741 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
742 return true;
743
744 return false;
745}
746
747static bool
748xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
749{
750 const xfrm_address_t *addr;
751 bool saddr_any, daddr_any;
752 u8 prefixlen;
753
754 addr = &policy->selector.saddr;
755 prefixlen = policy->selector.prefixlen_s;
756
757 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
758 policy->family,
759 prefixlen);
760 addr = &policy->selector.daddr;
761 prefixlen = policy->selector.prefixlen_d;
762 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
763 policy->family,
764 prefixlen);
765 return saddr_any && daddr_any;
766}
767
Florian Westphal9cf545e2018-11-07 23:00:38 +0100768static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
769 const xfrm_address_t *addr, u8 prefixlen)
770{
771 node->addr = *addr;
772 node->prefixlen = prefixlen;
773}
774
775static struct xfrm_pol_inexact_node *
776xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
777{
778 struct xfrm_pol_inexact_node *node;
779
780 node = kzalloc(sizeof(*node), GFP_ATOMIC);
781 if (node)
782 xfrm_pol_inexact_node_init(node, addr, prefixlen);
783
784 return node;
785}
786
787static int xfrm_policy_addr_delta(const xfrm_address_t *a,
788 const xfrm_address_t *b,
789 u8 prefixlen, u16 family)
790{
791 unsigned int pdw, pbi;
792 int delta = 0;
793
794 switch (family) {
795 case AF_INET:
796 if (sizeof(long) == 4 && prefixlen == 0)
797 return ntohl(a->a4) - ntohl(b->a4);
798 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
799 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
800 case AF_INET6:
801 pdw = prefixlen >> 5;
802 pbi = prefixlen & 0x1f;
803
804 if (pdw) {
805 delta = memcmp(a->a6, b->a6, pdw << 2);
806 if (delta)
807 return delta;
808 }
809 if (pbi) {
810 u32 mask = ~0u << (32 - pbi);
811
812 delta = (ntohl(a->a6[pdw]) & mask) -
813 (ntohl(b->a6[pdw]) & mask);
814 }
815 break;
816 default:
817 break;
818 }
819
820 return delta;
821}
822
823static void xfrm_policy_inexact_list_reinsert(struct net *net,
824 struct xfrm_pol_inexact_node *n,
825 u16 family)
826{
Florian Westphale901cbc2018-11-07 23:00:39 +0100827 unsigned int matched_s, matched_d;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100828 struct xfrm_policy *policy, *p;
829
Florian Westphale901cbc2018-11-07 23:00:39 +0100830 matched_s = 0;
831 matched_d = 0;
832
Florian Westphal9cf545e2018-11-07 23:00:38 +0100833 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal1d389002019-01-04 14:17:03 +0100834 struct hlist_node *newpos = NULL;
Florian Westphale901cbc2018-11-07 23:00:39 +0100835 bool matches_s, matches_d;
836
Florian Westphal9cf545e2018-11-07 23:00:38 +0100837 if (!policy->bydst_reinsert)
838 continue;
839
840 WARN_ON_ONCE(policy->family != family);
841
842 policy->bydst_reinsert = false;
843 hlist_for_each_entry(p, &n->hhead, bydst) {
Florian Westphal1d389002019-01-04 14:17:03 +0100844 if (policy->priority > p->priority)
845 newpos = &p->bydst;
846 else if (policy->priority == p->priority &&
847 policy->pos > p->pos)
Florian Westphal9cf545e2018-11-07 23:00:38 +0100848 newpos = &p->bydst;
849 else
850 break;
851 }
852
853 if (newpos)
Florian Westphal355b00d2019-01-04 14:17:00 +0100854 hlist_add_behind_rcu(&policy->bydst, newpos);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100855 else
Florian Westphal355b00d2019-01-04 14:17:00 +0100856 hlist_add_head_rcu(&policy->bydst, &n->hhead);
Florian Westphale901cbc2018-11-07 23:00:39 +0100857
858 /* paranoia checks follow.
859 * Check that the reinserted policy matches at least
860 * saddr or daddr for current node prefix.
861 *
862 * Matching both is fine, matching saddr in one policy
863 * (but not daddr) and then matching only daddr in another
864 * is a bug.
865 */
866 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
867 &n->addr,
868 n->prefixlen,
869 family) == 0;
870 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
871 &n->addr,
872 n->prefixlen,
873 family) == 0;
874 if (matches_s && matches_d)
875 continue;
876
877 WARN_ON_ONCE(!matches_s && !matches_d);
878 if (matches_s)
879 matched_s++;
880 if (matches_d)
881 matched_d++;
882 WARN_ON_ONCE(matched_s && matched_d);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100883 }
884}
885
Florian Westphal6ac098b2018-11-07 23:00:41 +0100886static void xfrm_policy_inexact_node_reinsert(struct net *net,
887 struct xfrm_pol_inexact_node *n,
888 struct rb_root *new,
889 u16 family)
890{
Florian Westphal6ac098b2018-11-07 23:00:41 +0100891 struct xfrm_pol_inexact_node *node;
Florian Westphal12750ab2019-01-04 14:17:05 +0100892 struct rb_node **p, *parent;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100893
894 /* we should not have another subtree here */
895 WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root));
Florian Westphal12750ab2019-01-04 14:17:05 +0100896restart:
897 parent = NULL;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100898 p = &new->rb_node;
899 while (*p) {
900 u8 prefixlen;
901 int delta;
902
903 parent = *p;
904 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
905
906 prefixlen = min(node->prefixlen, n->prefixlen);
907
908 delta = xfrm_policy_addr_delta(&n->addr, &node->addr,
909 prefixlen, family);
910 if (delta < 0) {
911 p = &parent->rb_left;
912 } else if (delta > 0) {
913 p = &parent->rb_right;
914 } else {
Florian Westphal769a8072019-08-12 10:32:13 +0200915 bool same_prefixlen = node->prefixlen == n->prefixlen;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100916 struct xfrm_policy *tmp;
917
Florian Westphal12750ab2019-01-04 14:17:05 +0100918 hlist_for_each_entry(tmp, &n->hhead, bydst) {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100919 tmp->bydst_reinsert = true;
Florian Westphal12750ab2019-01-04 14:17:05 +0100920 hlist_del_rcu(&tmp->bydst);
921 }
Florian Westphal6ac098b2018-11-07 23:00:41 +0100922
Florian Westphal769a8072019-08-12 10:32:13 +0200923 node->prefixlen = prefixlen;
924
Florian Westphal6ac098b2018-11-07 23:00:41 +0100925 xfrm_policy_inexact_list_reinsert(net, node, family);
926
Florian Westphal769a8072019-08-12 10:32:13 +0200927 if (same_prefixlen) {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100928 kfree_rcu(n, rcu);
929 return;
930 }
931
932 rb_erase(*p, new);
933 kfree_rcu(n, rcu);
934 n = node;
Florian Westphal12750ab2019-01-04 14:17:05 +0100935 goto restart;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100936 }
937 }
938
939 rb_link_node_rcu(&n->node, parent, p);
940 rb_insert_color(&n->node, new);
941}
942
Florian Westphal9cf545e2018-11-07 23:00:38 +0100943/* merge nodes v and n */
944static void xfrm_policy_inexact_node_merge(struct net *net,
945 struct xfrm_pol_inexact_node *v,
946 struct xfrm_pol_inexact_node *n,
947 u16 family)
948{
Florian Westphal6ac098b2018-11-07 23:00:41 +0100949 struct xfrm_pol_inexact_node *node;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100950 struct xfrm_policy *tmp;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100951 struct rb_node *rnode;
952
953 /* To-be-merged node v has a subtree.
954 *
955 * Dismantle it and insert its nodes to n->root.
956 */
957 while ((rnode = rb_first(&v->root)) != NULL) {
958 node = rb_entry(rnode, struct xfrm_pol_inexact_node, node);
959 rb_erase(&node->node, &v->root);
960 xfrm_policy_inexact_node_reinsert(net, node, &n->root,
961 family);
962 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100963
Florian Westphal1d389002019-01-04 14:17:03 +0100964 hlist_for_each_entry(tmp, &v->hhead, bydst) {
Florian Westphal9cf545e2018-11-07 23:00:38 +0100965 tmp->bydst_reinsert = true;
Florian Westphal1d389002019-01-04 14:17:03 +0100966 hlist_del_rcu(&tmp->bydst);
967 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100968
Florian Westphal9cf545e2018-11-07 23:00:38 +0100969 xfrm_policy_inexact_list_reinsert(net, n, family);
970}
971
972static struct xfrm_pol_inexact_node *
973xfrm_policy_inexact_insert_node(struct net *net,
974 struct rb_root *root,
975 xfrm_address_t *addr,
976 u16 family, u8 prefixlen, u8 dir)
977{
978 struct xfrm_pol_inexact_node *cached = NULL;
979 struct rb_node **p, *parent = NULL;
980 struct xfrm_pol_inexact_node *node;
981
982 p = &root->rb_node;
983 while (*p) {
984 int delta;
985
986 parent = *p;
987 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
988
989 delta = xfrm_policy_addr_delta(addr, &node->addr,
990 node->prefixlen,
991 family);
992 if (delta == 0 && prefixlen >= node->prefixlen) {
993 WARN_ON_ONCE(cached); /* ipsec policies got lost */
994 return node;
995 }
996
997 if (delta < 0)
998 p = &parent->rb_left;
999 else
1000 p = &parent->rb_right;
1001
1002 if (prefixlen < node->prefixlen) {
1003 delta = xfrm_policy_addr_delta(addr, &node->addr,
1004 prefixlen,
1005 family);
1006 if (delta)
1007 continue;
1008
1009 /* This node is a subnet of the new prefix. It needs
1010 * to be removed and re-inserted with the smaller
1011 * prefix and all nodes that are now also covered
1012 * by the reduced prefixlen.
1013 */
1014 rb_erase(&node->node, root);
1015
1016 if (!cached) {
1017 xfrm_pol_inexact_node_init(node, addr,
1018 prefixlen);
1019 cached = node;
1020 } else {
1021 /* This node also falls within the new
1022 * prefixlen. Merge the to-be-reinserted
1023 * node and this one.
1024 */
1025 xfrm_policy_inexact_node_merge(net, node,
1026 cached, family);
1027 kfree_rcu(node, rcu);
1028 }
1029
1030 /* restart */
1031 p = &root->rb_node;
1032 parent = NULL;
1033 }
1034 }
1035
1036 node = cached;
1037 if (!node) {
1038 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
1039 if (!node)
1040 return NULL;
1041 }
1042
1043 rb_link_node_rcu(&node->node, parent, p);
1044 rb_insert_color(&node->node, root);
1045
1046 return node;
1047}
1048
1049static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
1050{
1051 struct xfrm_pol_inexact_node *node;
1052 struct rb_node *rn = rb_first(r);
1053
1054 while (rn) {
1055 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
1056
Florian Westphal6ac098b2018-11-07 23:00:41 +01001057 xfrm_policy_inexact_gc_tree(&node->root, rm);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001058 rn = rb_next(rn);
1059
Florian Westphal6ac098b2018-11-07 23:00:41 +01001060 if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001061 WARN_ON_ONCE(rm);
1062 continue;
1063 }
1064
1065 rb_erase(&node->node, r);
1066 kfree_rcu(node, rcu);
1067 }
1068}
1069
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001070static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
1071{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001072 write_seqcount_begin(&b->count);
1073 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
Florian Westphal64a09a72018-11-07 23:00:40 +01001074 xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001075 write_seqcount_end(&b->count);
1076
Florian Westphal64a09a72018-11-07 23:00:40 +01001077 if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
Florian Westphal9cf545e2018-11-07 23:00:38 +01001078 !hlist_empty(&b->hhead)) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001079 WARN_ON_ONCE(net_exit);
Florian Westphal24969fa2018-11-07 23:00:35 +01001080 return;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001081 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001082
1083 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
1084 xfrm_pol_inexact_params) == 0) {
1085 list_del(&b->inexact_bins);
1086 kfree_rcu(b, rcu);
1087 }
1088}
1089
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001090static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
1091{
1092 struct net *net = read_pnet(&b->k.net);
1093
1094 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1095 __xfrm_policy_inexact_prune_bin(b, false);
1096 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1097}
1098
Florian Westphal24969fa2018-11-07 23:00:35 +01001099static void __xfrm_policy_inexact_flush(struct net *net)
1100{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001101 struct xfrm_pol_inexact_bin *bin, *t;
Florian Westphal24969fa2018-11-07 23:00:35 +01001102
1103 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1104
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001105 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1106 __xfrm_policy_inexact_prune_bin(bin, false);
Florian Westphal24969fa2018-11-07 23:00:35 +01001107}
1108
Florian Westphal9cf545e2018-11-07 23:00:38 +01001109static struct hlist_head *
1110xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1111 struct xfrm_policy *policy, u8 dir)
1112{
1113 struct xfrm_pol_inexact_node *n;
1114 struct net *net;
1115
1116 net = xp_net(policy);
1117 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1118
1119 if (xfrm_policy_inexact_insert_use_any_list(policy))
1120 return &bin->hhead;
1121
1122 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1123 policy->family,
Florian Westphal64a09a72018-11-07 23:00:40 +01001124 policy->selector.prefixlen_d)) {
1125 write_seqcount_begin(&bin->count);
1126 n = xfrm_policy_inexact_insert_node(net,
1127 &bin->root_s,
1128 &policy->selector.saddr,
1129 policy->family,
1130 policy->selector.prefixlen_s,
1131 dir);
1132 write_seqcount_end(&bin->count);
1133 if (!n)
1134 return NULL;
1135
1136 return &n->hhead;
1137 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001138
1139 /* daddr is fixed */
1140 write_seqcount_begin(&bin->count);
1141 n = xfrm_policy_inexact_insert_node(net,
1142 &bin->root_d,
1143 &policy->selector.daddr,
1144 policy->family,
1145 policy->selector.prefixlen_d, dir);
1146 write_seqcount_end(&bin->count);
1147 if (!n)
1148 return NULL;
Florian Westphal6ac098b2018-11-07 23:00:41 +01001149
1150 /* saddr is wildcard */
1151 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
1152 policy->family,
1153 policy->selector.prefixlen_s))
1154 return &n->hhead;
1155
1156 write_seqcount_begin(&bin->count);
1157 n = xfrm_policy_inexact_insert_node(net,
1158 &n->root,
1159 &policy->selector.saddr,
1160 policy->family,
1161 policy->selector.prefixlen_s, dir);
1162 write_seqcount_end(&bin->count);
1163 if (!n)
1164 return NULL;
1165
Florian Westphal9cf545e2018-11-07 23:00:38 +01001166 return &n->hhead;
1167}
1168
Florian Westphal24969fa2018-11-07 23:00:35 +01001169static struct xfrm_policy *
1170xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1171{
1172 struct xfrm_pol_inexact_bin *bin;
1173 struct xfrm_policy *delpol;
1174 struct hlist_head *chain;
1175 struct net *net;
1176
1177 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1178 if (!bin)
1179 return ERR_PTR(-ENOMEM);
1180
Florian Westphal24969fa2018-11-07 23:00:35 +01001181 net = xp_net(policy);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001182 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1183
Florian Westphal9cf545e2018-11-07 23:00:38 +01001184 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1185 if (!chain) {
1186 __xfrm_policy_inexact_prune_bin(bin, false);
1187 return ERR_PTR(-ENOMEM);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001188 }
1189
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001190 delpol = xfrm_policy_insert_list(chain, policy, excl);
1191 if (delpol && excl) {
1192 __xfrm_policy_inexact_prune_bin(bin, false);
1193 return ERR_PTR(-EEXIST);
1194 }
1195
Florian Westphal24969fa2018-11-07 23:00:35 +01001196 chain = &net->xfrm.policy_inexact[dir];
1197 xfrm_policy_insert_inexact_list(chain, policy);
1198
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001199 if (delpol)
1200 __xfrm_policy_inexact_prune_bin(bin, false);
1201
Florian Westphal24969fa2018-11-07 23:00:35 +01001202 return delpol;
1203}
1204
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001205static void xfrm_hash_rebuild(struct work_struct *work)
1206{
1207 struct net *net = container_of(work, struct net,
1208 xfrm.policy_hthresh.work);
1209 unsigned int hmask;
1210 struct xfrm_policy *pol;
1211 struct xfrm_policy *policy;
1212 struct hlist_head *chain;
1213 struct hlist_head *odst;
1214 struct hlist_node *newpos;
1215 int i;
1216 int dir;
1217 unsigned seq;
1218 u8 lbits4, rbits4, lbits6, rbits6;
1219
1220 mutex_lock(&hash_resize_mutex);
1221
1222 /* read selector prefixlen thresholds */
1223 do {
1224 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1225
1226 lbits4 = net->xfrm.policy_hthresh.lbits4;
1227 rbits4 = net->xfrm.policy_hthresh.rbits4;
1228 lbits6 = net->xfrm.policy_hthresh.lbits6;
1229 rbits6 = net->xfrm.policy_hthresh.rbits6;
1230 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1231
Florian Westphal9d0380d2016-08-11 15:17:59 +02001232 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal7a474c32019-01-04 14:17:01 +01001233 write_seqcount_begin(&xfrm_policy_hash_generation);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001234
Florian Westphal24969fa2018-11-07 23:00:35 +01001235 /* make sure that we can insert the indirect policies again before
1236 * we start with destructive action.
1237 */
1238 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001239 struct xfrm_pol_inexact_bin *bin;
Florian Westphal24969fa2018-11-07 23:00:35 +01001240 u8 dbits, sbits;
1241
1242 dir = xfrm_policy_id2dir(policy->index);
1243 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1244 continue;
1245
1246 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1247 if (policy->family == AF_INET) {
1248 dbits = rbits4;
1249 sbits = lbits4;
1250 } else {
1251 dbits = rbits6;
1252 sbits = lbits6;
1253 }
1254 } else {
1255 if (policy->family == AF_INET) {
1256 dbits = lbits4;
1257 sbits = rbits4;
1258 } else {
1259 dbits = lbits6;
1260 sbits = rbits6;
1261 }
1262 }
1263
1264 if (policy->selector.prefixlen_d < dbits ||
1265 policy->selector.prefixlen_s < sbits)
1266 continue;
1267
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001268 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1269 if (!bin)
Florian Westphal24969fa2018-11-07 23:00:35 +01001270 goto out_unlock;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001271
1272 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1273 goto out_unlock;
Florian Westphal24969fa2018-11-07 23:00:35 +01001274 }
1275
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001276 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +08001277 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Florian Westphal1548bc42019-01-04 14:17:02 +01001278 struct hlist_node *n;
1279
1280 hlist_for_each_entry_safe(policy, n,
1281 &net->xfrm.policy_inexact[dir],
Florian Westphalfd709722019-07-02 12:46:00 +02001282 bydst_inexact_list) {
1283 hlist_del_rcu(&policy->bydst);
Florian Westphal1548bc42019-01-04 14:17:02 +01001284 hlist_del_init(&policy->bydst_inexact_list);
Florian Westphalfd709722019-07-02 12:46:00 +02001285 }
Florian Westphal1548bc42019-01-04 14:17:02 +01001286
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001287 hmask = net->xfrm.policy_bydst[dir].hmask;
1288 odst = net->xfrm.policy_bydst[dir].table;
Florian Westphalfd709722019-07-02 12:46:00 +02001289 for (i = hmask; i >= 0; i--) {
1290 hlist_for_each_entry_safe(policy, n, odst + i, bydst)
1291 hlist_del_rcu(&policy->bydst);
1292 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001293 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1294 /* dir out => dst = remote, src = local */
1295 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1296 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1297 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1298 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1299 } else {
1300 /* dir in/fwd => dst = local, src = remote */
1301 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1302 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1303 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1304 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1305 }
1306 }
1307
1308 /* re-insert all policies by order of creation */
1309 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal88584c32018-11-27 13:28:54 +01001310 if (policy->walk.dead)
1311 continue;
1312 dir = xfrm_policy_id2dir(policy->index);
1313 if (dir >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +02001314 /* skip socket policies */
1315 continue;
1316 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001317 newpos = NULL;
1318 chain = policy_hash_bysel(net, &policy->selector,
Florian Westphal88584c32018-11-27 13:28:54 +01001319 policy->family, dir);
Florian Westphal1548bc42019-01-04 14:17:02 +01001320
Florian Westphal24969fa2018-11-07 23:00:35 +01001321 if (!chain) {
1322 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1323
1324 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1325 continue;
1326 }
1327
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001328 hlist_for_each_entry(pol, chain, bydst) {
1329 if (policy->priority >= pol->priority)
1330 newpos = &pol->bydst;
1331 else
1332 break;
1333 }
1334 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +02001335 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001336 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001337 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001338 }
1339
Florian Westphal24969fa2018-11-07 23:00:35 +01001340out_unlock:
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001341 __xfrm_policy_inexact_flush(net);
Florian Westphal7a474c32019-01-04 14:17:01 +01001342 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001343 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001344
1345 mutex_unlock(&hash_resize_mutex);
1346}
1347
1348void xfrm_policy_hash_rebuild(struct net *net)
1349{
1350 schedule_work(&net->xfrm.policy_hthresh.work);
1351}
1352EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354/* Generate new index... KAME seems to generate them ordered by cost
1355 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +08001356static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 static u32 idx_generator;
1359
1360 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001361 struct hlist_head *list;
1362 struct xfrm_policy *p;
1363 u32 idx;
1364 int found;
1365
Fan Due682adf02013-11-07 17:47:48 +08001366 if (!index) {
1367 idx = (idx_generator | dir);
1368 idx_generator += 8;
1369 } else {
1370 idx = index;
1371 index = 0;
1372 }
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (idx == 0)
1375 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -08001376 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -07001377 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001378 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001379 if (p->index == idx) {
1380 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 break;
David S. Miller2518c7c2006-08-24 04:45:07 -07001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001384 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return idx;
1386 }
1387}
1388
David S. Miller2518c7c2006-08-24 04:45:07 -07001389static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1390{
1391 u32 *p1 = (u32 *) s1;
1392 u32 *p2 = (u32 *) s2;
1393 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1394 int i;
1395
1396 for (i = 0; i < len; i++) {
1397 if (p1[i] != p2[i])
1398 return 1;
1399 }
1400
1401 return 0;
1402}
1403
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001404static void xfrm_policy_requeue(struct xfrm_policy *old,
1405 struct xfrm_policy *new)
1406{
1407 struct xfrm_policy_queue *pq = &old->polq;
1408 struct sk_buff_head list;
1409
Li RongQingde2ad482015-04-30 17:25:19 +08001410 if (skb_queue_empty(&pq->hold_queue))
1411 return;
1412
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001413 __skb_queue_head_init(&list);
1414
1415 spin_lock_bh(&pq->hold_queue.lock);
1416 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001417 if (del_timer(&pq->hold_timer))
1418 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001419 spin_unlock_bh(&pq->hold_queue.lock);
1420
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001421 pq = &new->polq;
1422
1423 spin_lock_bh(&pq->hold_queue.lock);
1424 skb_queue_splice(&list, &pq->hold_queue);
1425 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001426 if (!mod_timer(&pq->hold_timer, jiffies))
1427 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001428 spin_unlock_bh(&pq->hold_queue.lock);
1429}
1430
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001431static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1432 struct xfrm_policy *pol)
1433{
1434 u32 mark = policy->mark.v & policy->mark.m;
1435
1436 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1437 return true;
1438
1439 if ((mark & pol->mark.m) == pol->mark.v &&
1440 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,
1909 seqcount_t *count,
1910 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 Westphale37cc8ad2016-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äsea2dea92010-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 Westphalf24ea522019-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 Westphalf24ea522019-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->flags |= DST_HOST;
2614 dst1->lastuse = now;
2615
2616 dst1->input = dst_discard;
Florian Westphal733a5fa2019-03-29 21:16:30 +01002617
2618 rcu_read_lock();
2619 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
2620 if (likely(afinfo))
2621 dst1->output = afinfo->output;
2622 else
2623 dst1->output = dst_discard_out;
2624 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002625
David Miller45b018be2017-11-28 15:40:28 -05002626 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002627
2628 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002629 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2630 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002631 trailer_len += xfrm[i]->props.trailer_len;
2632 }
2633
David Miller45b018be2017-11-28 15:40:28 -05002634 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002635 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002636
2637 err = -ENODEV;
2638 dev = dst->dev;
2639 if (!dev)
2640 goto free_dst;
2641
David Miller45b018be2017-11-28 15:40:28 -05002642 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002643 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002644
David Miller45b018be2017-11-28 15:40:28 -05002645 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2646 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2647 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002648 if (err)
2649 goto free_dst;
2650
David Miller45b018be2017-11-28 15:40:28 -05002651 xdst_prev->u.dst.header_len = header_len;
2652 xdst_prev->u.dst.trailer_len = trailer_len;
2653 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2654 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002655 }
2656
David Miller45b018be2017-11-28 15:40:28 -05002657 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002658
2659put_states:
2660 for (; i < nx; i++)
2661 xfrm_state_put(xfrm[i]);
2662free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002663 if (xdst0)
2664 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002665
2666 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
David S. Miller73ff93c2011-02-22 18:33:42 -08002669static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002670 struct xfrm_policy **pols,
2671 int *num_pols, int *num_xfrms)
2672{
2673 int i;
2674
2675 if (*num_pols == 0 || !pols[0]) {
2676 *num_pols = 0;
2677 *num_xfrms = 0;
2678 return 0;
2679 }
2680 if (IS_ERR(pols[0]))
2681 return PTR_ERR(pols[0]);
2682
2683 *num_xfrms = pols[0]->xfrm_nr;
2684
2685#ifdef CONFIG_XFRM_SUB_POLICY
2686 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2687 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2688 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2689 XFRM_POLICY_TYPE_MAIN,
2690 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002691 XFRM_POLICY_OUT,
2692 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002693 if (pols[1]) {
2694 if (IS_ERR(pols[1])) {
2695 xfrm_pols_put(pols, *num_pols);
2696 return PTR_ERR(pols[1]);
2697 }
Weilong Chen02d08922013-12-24 09:43:48 +08002698 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002699 (*num_xfrms) += pols[1]->xfrm_nr;
2700 }
2701 }
2702#endif
2703 for (i = 0; i < *num_pols; i++) {
2704 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2705 *num_xfrms = -1;
2706 break;
2707 }
2708 }
2709
2710 return 0;
2711
2712}
2713
2714static struct xfrm_dst *
2715xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002716 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002717 struct dst_entry *dst_orig)
2718{
2719 struct net *net = xp_net(pols[0]);
2720 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002721 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002722 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002723 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002724 int err;
2725
2726 /* Try to instantiate a bundle */
2727 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002728 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002729 if (err == 0)
2730 return NULL;
2731
2732 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002733 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2734 return ERR_PTR(err);
2735 }
2736
David Miller54920932017-11-28 15:41:01 -05002737 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002738 if (IS_ERR(dst)) {
2739 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2740 return ERR_CAST(dst);
2741 }
2742
2743 xdst = (struct xfrm_dst *)dst;
2744 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002745 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002746 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002747 xdst->policy_genid = atomic_read(&pols[0]->genid);
2748
2749 return xdst;
2750}
2751
Kees Cookc3aed702017-10-16 17:28:56 -07002752static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002753{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002754 struct sk_buff *skb;
2755 struct sock *sk;
2756 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002757 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002758 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002759 struct xfrm_policy_queue *pq = &pol->polq;
2760 struct flowi fl;
2761 struct sk_buff_head list;
2762
2763 spin_lock(&pq->hold_queue.lock);
2764 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002765 if (!skb) {
2766 spin_unlock(&pq->hold_queue.lock);
2767 goto out;
2768 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002769 dst = skb_dst(skb);
2770 sk = skb->sk;
2771 xfrm_decode_session(skb, &fl, dst->ops->family);
2772 spin_unlock(&pq->hold_queue.lock);
2773
David Miller0f6c4802017-11-28 15:40:46 -05002774 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c982018-02-01 11:26:12 +01002775 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002776 if (IS_ERR(dst))
2777 goto purge_queue;
2778
2779 if (dst->flags & DST_XFRM_QUEUE) {
2780 dst_release(dst);
2781
2782 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2783 goto purge_queue;
2784
2785 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002786 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2787 xfrm_pol_hold(pol);
Colin Ian King7759d6a2018-11-13 14:28:42 +00002788 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002789 }
2790
2791 dst_release(dst);
2792
2793 __skb_queue_head_init(&list);
2794
2795 spin_lock(&pq->hold_queue.lock);
2796 pq->timeout = 0;
2797 skb_queue_splice_init(&pq->hold_queue, &list);
2798 spin_unlock(&pq->hold_queue.lock);
2799
2800 while (!skb_queue_empty(&list)) {
2801 skb = __skb_dequeue(&list);
2802
2803 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002804 dst_hold(xfrm_dst_path(skb_dst(skb)));
2805 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002806 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002807 kfree_skb(skb);
2808 continue;
2809 }
2810
Florian Westphal895b5c92019-09-29 20:54:03 +02002811 nf_reset_ct(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002812 skb_dst_drop(skb);
2813 skb_dst_set(skb, dst);
2814
Eric W. Biederman13206b62015-10-07 16:48:35 -05002815 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002816 }
2817
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002818out:
2819 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002820 return;
2821
2822purge_queue:
2823 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002824 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002825 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002826}
2827
Eric W. Biedermanede20592015-10-07 16:48:47 -05002828static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002829{
2830 unsigned long sched_next;
2831 struct dst_entry *dst = skb_dst(skb);
2832 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002833 struct xfrm_policy *pol = xdst->pols[0];
2834 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002835
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002836 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002837 kfree_skb(skb);
2838 return 0;
2839 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002840
2841 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2842 kfree_skb(skb);
2843 return -EAGAIN;
2844 }
2845
2846 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002847
2848 spin_lock_bh(&pq->hold_queue.lock);
2849
2850 if (!pq->timeout)
2851 pq->timeout = XFRM_QUEUE_TMO_MIN;
2852
2853 sched_next = jiffies + pq->timeout;
2854
2855 if (del_timer(&pq->hold_timer)) {
2856 if (time_before(pq->hold_timer.expires, sched_next))
2857 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002858 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002859 }
2860
2861 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002862 if (!mod_timer(&pq->hold_timer, sched_next))
2863 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002864
2865 spin_unlock_bh(&pq->hold_queue.lock);
2866
2867 return 0;
2868}
2869
2870static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002871 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002872 const struct flowi *fl,
2873 int num_xfrms,
2874 u16 family)
2875{
2876 int err;
2877 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002878 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002879 struct dst_entry *dst1;
2880 struct xfrm_dst *xdst;
2881
2882 xdst = xfrm_alloc_dst(net, family);
2883 if (IS_ERR(xdst))
2884 return xdst;
2885
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002886 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2887 net->xfrm.sysctl_larval_drop ||
2888 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002889 return xdst;
2890
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002891 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002892 dst1 = &xdst->u.dst;
2893 dst_hold(dst);
2894 xdst->route = dst;
2895
2896 dst_copy_metrics(dst1, dst);
2897
2898 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2899 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2900 dst1->lastuse = jiffies;
2901
2902 dst1->input = dst_discard;
2903 dst1->output = xdst_queue_output;
2904
2905 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002906 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002907 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002908
2909 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2910
2911 err = -ENODEV;
2912 dev = dst->dev;
2913 if (!dev)
2914 goto free_dst;
2915
2916 err = xfrm_fill_dst(xdst, dev, fl);
2917 if (err)
2918 goto free_dst;
2919
2920out:
2921 return xdst;
2922
2923free_dst:
2924 dst_release(dst1);
2925 xdst = ERR_PTR(err);
2926 goto out;
2927}
2928
Benedict Wongbc56b332018-07-19 10:50:44 -07002929static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2930 const struct flowi *fl,
2931 u16 family, u8 dir,
2932 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002933{
Timo Teräs80c802f2010-04-07 00:30:05 +00002934 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002935 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002936 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002937
Timo Teräs80c802f2010-04-07 00:30:05 +00002938 /* Resolve policies to use if we couldn't get them from
2939 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002940 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002941 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002942 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002943 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002944 if (err < 0)
2945 goto inc_error;
2946 if (num_pols == 0)
2947 return NULL;
2948 if (num_xfrms <= 0)
2949 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002950
Florian Westphalbd45c532017-07-17 13:57:25 +02002951 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002952 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002953 if (IS_ERR(xdst)) {
2954 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002955 if (err == -EREMOTE) {
2956 xfrm_pols_put(pols, num_pols);
2957 return NULL;
2958 }
2959
Timo Teräs80c802f2010-04-07 00:30:05 +00002960 if (err != -EAGAIN)
2961 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002962 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002963 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002964 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002965 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002966 }
2967
Florian Westphalbd45c532017-07-17 13:57:25 +02002968 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002969
2970make_dummy_bundle:
2971 /* We found policies, but there's no bundles to instantiate:
2972 * either because the policy blocks, has no transformations or
2973 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002974 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002975 if (IS_ERR(xdst)) {
2976 xfrm_pols_put(pols, num_pols);
2977 return ERR_CAST(xdst);
2978 }
2979 xdst->num_pols = num_pols;
2980 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002981 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002982
Florian Westphalbd45c532017-07-17 13:57:25 +02002983 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002984
2985inc_error:
2986 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2987error:
Florian Westphal855dad92017-07-17 13:57:22 +02002988 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002989 return ERR_PTR(err);
2990}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
David S. Miller2774c132011-03-01 14:59:04 -08002992static struct dst_entry *make_blackhole(struct net *net, u16 family,
2993 struct dst_entry *dst_orig)
2994{
Florian Westphal37b10382017-02-07 15:00:19 +01002995 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002996 struct dst_entry *ret;
2997
2998 if (!afinfo) {
2999 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00003000 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08003001 } else {
3002 ret = afinfo->blackhole_route(net, dst_orig);
3003 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003004 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08003005
3006 return ret;
3007}
3008
Benedict Wongbc56b332018-07-19 10:50:44 -07003009/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 *
3011 * At the moment we eat a raw IP route. Mostly to speed up lookups
3012 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07003013 *
3014 * xfrm_lookup uses an if_id of 0 by default, and is provided for
3015 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 */
Benedict Wongbc56b332018-07-19 10:50:44 -07003017struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
3018 struct dst_entry *dst_orig,
3019 const struct flowi *fl,
3020 const struct sock *sk,
3021 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003023 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00003024 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08003025 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00003026 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02003027 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00003028 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003029
Timo Teräs80c802f2010-04-07 00:30:05 +00003030 dst = NULL;
3031 xdst = NULL;
3032 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003033
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003034 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07003035 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003036 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003037 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
3038 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00003039 err = xfrm_expand_policies(fl, family, pols,
3040 &num_pols, &num_xfrms);
3041 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08003042 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00003043
3044 if (num_pols) {
3045 if (num_xfrms <= 0) {
3046 drop_pols = num_pols;
3047 goto no_transform;
3048 }
3049
3050 xdst = xfrm_resolve_and_create_bundle(
3051 pols, num_pols, fl,
3052 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01003053
Timo Teräs80c802f2010-04-07 00:30:05 +00003054 if (IS_ERR(xdst)) {
3055 xfrm_pols_put(pols, num_pols);
3056 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02003057 if (err == -EREMOTE)
3058 goto nopol;
3059
Timo Teräs80c802f2010-04-07 00:30:05 +00003060 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00003061 } else if (xdst == NULL) {
3062 num_xfrms = 0;
3063 drop_pols = num_pols;
3064 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00003065 }
3066
Timo Teräs80c802f2010-04-07 00:30:05 +00003067 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003068 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Timo Teräs80c802f2010-04-07 00:30:05 +00003071 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003072 struct xfrm_flo xflo;
3073
3074 xflo.dst_orig = dst_orig;
3075 xflo.flags = flags;
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07003078 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08003079 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08003080 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Benedict Wongbc56b332018-07-19 10:50:44 -07003082 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02003083 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00003084 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02003085 if (IS_ERR(xdst)) {
3086 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003087 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08003088 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003089
3090 num_pols = xdst->num_pols;
3091 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003092 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00003093 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095
Timo Teräs80c802f2010-04-07 00:30:05 +00003096 dst = &xdst->u.dst;
3097 if (route == NULL && num_xfrms > 0) {
3098 /* The only case when xfrm_bundle_lookup() returns a
3099 * bundle with null route, is when the template could
3100 * not be resolved. It means policies are there, but
3101 * bundle could not be created, since we don't yet
3102 * have the xfrm_state's. We need to wait for KM to
3103 * negotiate new SA's or bail out with error.*/
3104 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003105 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01003106 err = -EREMOTE;
3107 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003108 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003109
Steffen Klassert5b8ef342013-08-27 13:43:30 +02003110 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00003111
3112 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3113 goto error;
3114 }
3115
3116no_transform:
3117 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08003118 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Timo Teräs80c802f2010-04-07 00:30:05 +00003120 if ((flags & XFRM_LOOKUP_ICMP) &&
3121 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3122 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08003123 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003124 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08003125
Timo Teräs80c802f2010-04-07 00:30:05 +00003126 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02003127 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08003128
Timo Teräs80c802f2010-04-07 00:30:05 +00003129 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003131 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07003132 err = -EPERM;
3133 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003134 } else if (num_xfrms > 0) {
3135 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00003136 dst_release(dst_orig);
3137 } else {
3138 /* Flow passes untransformed */
3139 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003140 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003142ok:
3143 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003144 if (dst && dst->xfrm &&
3145 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3146 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003147 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Timo Teräs80c802f2010-04-07 00:30:05 +00003149nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003150 if (!(flags & XFRM_LOOKUP_ICMP)) {
3151 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003152 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003153 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003154 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003156 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003157dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003158 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3159 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003160 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003161 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
Benedict Wongbc56b332018-07-19 10:50:44 -07003163EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3164
3165/* Main function: finds/creates a bundle for given flow.
3166 *
3167 * At the moment we eat a raw IP route. Mostly to speed up lookups
3168 * on interfaces with disabled IPsec.
3169 */
3170struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3171 const struct flowi *fl, const struct sock *sk,
3172 int flags)
3173{
3174 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176EXPORT_SYMBOL(xfrm_lookup);
3177
Steffen Klassertf92ee612014-09-16 10:08:40 +02003178/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3179 * Otherwise we may send out blackholed packets.
3180 */
3181struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3182 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003183 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003184{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003185 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003186 flags | XFRM_LOOKUP_QUEUE |
3187 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003188
3189 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
3190 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3191
Tommi Rantala8cc88772018-06-21 09:30:47 +03003192 if (IS_ERR(dst))
3193 dst_release(dst_orig);
3194
Steffen Klassertf92ee612014-09-16 10:08:40 +02003195 return dst;
3196}
3197EXPORT_SYMBOL(xfrm_lookup_route);
3198
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003199static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003200xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003201{
Florian Westphal2294be0f2018-12-18 17:15:20 +01003202 struct sec_path *sp = skb_sec_path(skb);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003203 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003204
Florian Westphal2294be0f2018-12-18 17:15:20 +01003205 if (!sp || idx < 0 || idx >= sp->len)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003206 return 0;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003207 x = sp->xvec[idx];
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003208 if (!x->type->reject)
3209 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003210 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003211}
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213/* When skb is transformed back to its "native" form, we have to
3214 * check policy restrictions. At the moment we make this in maximally
3215 * stupid way. Shame on me. :-) Of course, connected sockets must
3216 * have policy cached at them.
3217 */
3218
3219static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003220xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 unsigned short family)
3222{
3223 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003224 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return x->id.proto == tmpl->id.proto &&
3226 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3227 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3228 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003229 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003230 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003231 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3232 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003235/*
3236 * 0 or more than 0 is returned when validation is succeeded (either bypass
3237 * because of optional transport mode, or next index of the mathced secpath
3238 * state with the template.
3239 * -1 is returned when no matching template is found.
3240 * Otherwise "-2 - errored_index" is returned.
3241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003243xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 unsigned short family)
3245{
3246 int idx = start;
3247
3248 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003249 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return start;
3251 } else
3252 start = -1;
3253 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003254 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003256 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3257 if (start == -1)
3258 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
3262 return start;
3263}
3264
Florian Westphalc53ac412019-04-16 16:44:39 +02003265static void
3266decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse)
3267{
3268 const struct iphdr *iph = ip_hdr(skb);
Florian Westphal858e5402019-05-16 11:28:16 +02003269 int ihl = iph->ihl;
3270 u8 *xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003271 struct flowi4 *fl4 = &fl->u.ip4;
3272 int oif = 0;
3273
Hangbin Liuc3b4c3a2019-08-22 22:19:49 +08003274 if (skb_dst(skb) && skb_dst(skb)->dev)
Florian Westphalc53ac412019-04-16 16:44:39 +02003275 oif = skb_dst(skb)->dev->ifindex;
3276
3277 memset(fl4, 0, sizeof(struct flowi4));
3278 fl4->flowi4_mark = skb->mark;
3279 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
3280
Florian Westphal858e5402019-05-16 11:28:16 +02003281 fl4->flowi4_proto = iph->protocol;
3282 fl4->daddr = reverse ? iph->saddr : iph->daddr;
3283 fl4->saddr = reverse ? iph->daddr : iph->saddr;
3284 fl4->flowi4_tos = iph->tos;
3285
Florian Westphalc53ac412019-04-16 16:44:39 +02003286 if (!ip_is_fragment(iph)) {
3287 switch (iph->protocol) {
3288 case IPPROTO_UDP:
3289 case IPPROTO_UDPLITE:
3290 case IPPROTO_TCP:
3291 case IPPROTO_SCTP:
3292 case IPPROTO_DCCP:
3293 if (xprth + 4 < skb->data ||
3294 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3295 __be16 *ports;
3296
Florian Westphal858e5402019-05-16 11:28:16 +02003297 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003298 ports = (__be16 *)xprth;
3299
3300 fl4->fl4_sport = ports[!!reverse];
3301 fl4->fl4_dport = ports[!reverse];
3302 }
3303 break;
3304 case IPPROTO_ICMP:
3305 if (xprth + 2 < skb->data ||
3306 pskb_may_pull(skb, xprth + 2 - skb->data)) {
3307 u8 *icmp;
3308
Florian Westphal858e5402019-05-16 11:28:16 +02003309 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003310 icmp = xprth;
3311
3312 fl4->fl4_icmp_type = icmp[0];
3313 fl4->fl4_icmp_code = icmp[1];
3314 }
3315 break;
3316 case IPPROTO_ESP:
3317 if (xprth + 4 < skb->data ||
3318 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3319 __be32 *ehdr;
3320
Florian Westphal858e5402019-05-16 11:28:16 +02003321 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003322 ehdr = (__be32 *)xprth;
3323
3324 fl4->fl4_ipsec_spi = ehdr[0];
3325 }
3326 break;
3327 case IPPROTO_AH:
3328 if (xprth + 8 < skb->data ||
3329 pskb_may_pull(skb, xprth + 8 - skb->data)) {
3330 __be32 *ah_hdr;
3331
Florian Westphal858e5402019-05-16 11:28:16 +02003332 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003333 ah_hdr = (__be32 *)xprth;
3334
3335 fl4->fl4_ipsec_spi = ah_hdr[1];
3336 }
3337 break;
3338 case IPPROTO_COMP:
3339 if (xprth + 4 < skb->data ||
3340 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3341 __be16 *ipcomp_hdr;
3342
Florian Westphal858e5402019-05-16 11:28:16 +02003343 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003344 ipcomp_hdr = (__be16 *)xprth;
3345
3346 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
3347 }
3348 break;
3349 case IPPROTO_GRE:
3350 if (xprth + 12 < skb->data ||
3351 pskb_may_pull(skb, xprth + 12 - skb->data)) {
3352 __be16 *greflags;
3353 __be32 *gre_hdr;
3354
Florian Westphal858e5402019-05-16 11:28:16 +02003355 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003356 greflags = (__be16 *)xprth;
3357 gre_hdr = (__be32 *)xprth;
3358
3359 if (greflags[0] & GRE_KEY) {
3360 if (greflags[0] & GRE_CSUM)
3361 gre_hdr++;
3362 fl4->fl4_gre_key = gre_hdr[1];
3363 }
3364 }
3365 break;
3366 default:
3367 fl4->fl4_ipsec_spi = 0;
3368 break;
3369 }
3370 }
Florian Westphalc53ac412019-04-16 16:44:39 +02003371}
3372
3373#if IS_ENABLED(CONFIG_IPV6)
3374static void
3375decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse)
3376{
3377 struct flowi6 *fl6 = &fl->u.ip6;
3378 int onlyproto = 0;
3379 const struct ipv6hdr *hdr = ipv6_hdr(skb);
3380 u32 offset = sizeof(*hdr);
3381 struct ipv6_opt_hdr *exthdr;
3382 const unsigned char *nh = skb_network_header(skb);
3383 u16 nhoff = IP6CB(skb)->nhoff;
3384 int oif = 0;
3385 u8 nexthdr;
3386
3387 if (!nhoff)
3388 nhoff = offsetof(struct ipv6hdr, nexthdr);
3389
3390 nexthdr = nh[nhoff];
3391
Hangbin Liuc3b4c3a2019-08-22 22:19:49 +08003392 if (skb_dst(skb) && skb_dst(skb)->dev)
Florian Westphalc53ac412019-04-16 16:44:39 +02003393 oif = skb_dst(skb)->dev->ifindex;
3394
3395 memset(fl6, 0, sizeof(struct flowi6));
3396 fl6->flowi6_mark = skb->mark;
3397 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
3398
3399 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
3400 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
3401
3402 while (nh + offset + sizeof(*exthdr) < skb->data ||
3403 pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
3404 nh = skb_network_header(skb);
3405 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3406
3407 switch (nexthdr) {
3408 case NEXTHDR_FRAGMENT:
3409 onlyproto = 1;
3410 /* fall through */
3411 case NEXTHDR_ROUTING:
3412 case NEXTHDR_HOP:
3413 case NEXTHDR_DEST:
3414 offset += ipv6_optlen(exthdr);
3415 nexthdr = exthdr->nexthdr;
3416 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3417 break;
3418 case IPPROTO_UDP:
3419 case IPPROTO_UDPLITE:
3420 case IPPROTO_TCP:
3421 case IPPROTO_SCTP:
3422 case IPPROTO_DCCP:
3423 if (!onlyproto && (nh + offset + 4 < skb->data ||
3424 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
3425 __be16 *ports;
3426
3427 nh = skb_network_header(skb);
3428 ports = (__be16 *)(nh + offset);
3429 fl6->fl6_sport = ports[!!reverse];
3430 fl6->fl6_dport = ports[!reverse];
3431 }
3432 fl6->flowi6_proto = nexthdr;
3433 return;
3434 case IPPROTO_ICMPV6:
3435 if (!onlyproto && (nh + offset + 2 < skb->data ||
3436 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
3437 u8 *icmp;
3438
3439 nh = skb_network_header(skb);
3440 icmp = (u8 *)(nh + offset);
3441 fl6->fl6_icmp_type = icmp[0];
3442 fl6->fl6_icmp_code = icmp[1];
3443 }
3444 fl6->flowi6_proto = nexthdr;
3445 return;
3446#if IS_ENABLED(CONFIG_IPV6_MIP6)
3447 case IPPROTO_MH:
3448 offset += ipv6_optlen(exthdr);
3449 if (!onlyproto && (nh + offset + 3 < skb->data ||
3450 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
3451 struct ip6_mh *mh;
3452
3453 nh = skb_network_header(skb);
3454 mh = (struct ip6_mh *)(nh + offset);
3455 fl6->fl6_mh_type = mh->ip6mh_type;
3456 }
3457 fl6->flowi6_proto = nexthdr;
3458 return;
3459#endif
3460 /* XXX Why are there these headers? */
3461 case IPPROTO_AH:
3462 case IPPROTO_ESP:
3463 case IPPROTO_COMP:
3464 default:
3465 fl6->fl6_ipsec_spi = 0;
3466 fl6->flowi6_proto = nexthdr;
3467 return;
3468 }
3469 }
3470}
3471#endif
3472
Herbert Xud5422ef2007-12-12 10:44:16 -08003473int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3474 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475{
Florian Westphalc53ac412019-04-16 16:44:39 +02003476 switch (family) {
3477 case AF_INET:
3478 decode_session4(skb, fl, reverse);
3479 break;
3480#if IS_ENABLED(CONFIG_IPV6)
3481 case AF_INET6:
3482 decode_session6(skb, fl, reverse);
3483 break;
3484#endif
3485 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return -EAFNOSUPPORT;
Florian Westphalc53ac412019-04-16 16:44:39 +02003487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Florian Westphalc53ac412019-04-16 16:44:39 +02003489 return security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490}
Herbert Xud5422ef2007-12-12 10:44:16 -08003491EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
David S. Miller9a7386e2011-02-24 01:44:12 -05003493static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
3495 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003496 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003497 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501
3502 return 0;
3503}
3504
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003505int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 unsigned short family)
3507{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003508 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003510 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3511 int npols = 0;
3512 int xfrm_nr;
3513 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003514 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003516 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003517 const struct xfrm_if_cb *ifcb;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003518 struct sec_path *sp;
Benedict Wongbc56b332018-07-19 10:50:44 -07003519 struct xfrm_if *xi;
3520 u32 if_id = 0;
3521
3522 rcu_read_lock();
3523 ifcb = xfrm_if_get_cb();
3524
3525 if (ifcb) {
Martin Willi025c65e2019-03-26 13:20:43 +01003526 xi = ifcb->decode_session(skb, family);
Tobias Brunner660899d2019-02-18 10:49:39 +01003527 if (xi) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003528 if_id = xi->p.if_id;
Tobias Brunner660899d2019-02-18 10:49:39 +01003529 net = xi->net;
3530 }
Benedict Wongbc56b332018-07-19 10:50:44 -07003531 }
3532 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Herbert Xud5422ef2007-12-12 10:44:16 -08003534 reverse = dir & ~XFRM_POLICY_MASK;
3535 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003536
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003537 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003538 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003540 }
3541
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003542 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544 /* First, check used SA against their selectors. */
Florian Westphal2294be0f2018-12-18 17:15:20 +01003545 sp = skb_sec_path(skb);
3546 if (sp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 int i;
3548
Florian Westphal2294be0f2018-12-18 17:15:20 +01003549 for (i = sp->len - 1; i >= 0; i--) {
3550 struct xfrm_state *x = sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003551 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003552 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 }
3556 }
3557
3558 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003559 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003560 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003561 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003562 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003563 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003564 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003565 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003568 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003569 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003571 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003572 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003573 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003574 }
James Morris134b0fc2006-10-05 15:42:27 -05003575
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003576 if (!pol) {
Florian Westphal2294be0f2018-12-18 17:15:20 +01003577 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003578 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003579 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003580 return 0;
3581 }
3582 return 1;
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Arnd Bergmann386c5682018-07-11 12:19:13 +02003585 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003587 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003588 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003589#ifdef CONFIG_XFRM_SUB_POLICY
3590 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003591 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003592 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003593 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003594 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003595 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003596 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003597 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003598 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003599 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003600 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003601 }
3602 }
3603#endif
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 if (pol->action == XFRM_POLICY_ALLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003607 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003608 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003609 struct xfrm_tmpl **tpp = tp;
3610 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 int i, k;
3612
Florian Westphal2294be0f2018-12-18 17:15:20 +01003613 sp = skb_sec_path(skb);
3614 if (!sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 sp = &dummy;
3616
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003617 for (pi = 0; pi < npols; pi++) {
3618 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003619 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003620 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003621 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003622 }
3623 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003624 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003625 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003626 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003627 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3628 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3629 }
3630 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003631 if (npols > 1) {
Florian Westphal3aaf3912019-05-03 17:46:17 +02003632 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003633 tpp = stp;
3634 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /* For each tunnel xfrm, find the first matching tmpl.
3637 * For each tmpl before that, find corresponding xfrm.
3638 * Order is _important_. Later we will implement
3639 * some barriers, but at the moment barriers
3640 * are implied between each two transformations.
3641 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003642 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3643 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003644 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003645 if (k < -1)
3646 /* "-2 - errored_index" returned */
3647 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003648 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 }
3652
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003653 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003654 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003658 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return 1;
3660 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003661 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003664 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003665reject_error:
3666 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return 0;
3668}
3669EXPORT_SYMBOL(__xfrm_policy_check);
3670
3671int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3672{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003673 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003675 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003676 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003678 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003679 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003683 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02003684 if (!skb_dst(skb)) {
3685 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3686 return 0;
3687 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003688
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003689 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003690 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003691 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003692 dst = NULL;
3693 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003694 skb_dst_set(skb, dst);
3695 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697EXPORT_SYMBOL(__xfrm_route_forward);
3698
David S. Millerd49c73c2006-08-13 18:55:53 -07003699/* Optimize later using cookies and generation ids. */
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3702{
David S. Millerd49c73c2006-08-13 18:55:53 -07003703 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003704 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3705 * get validated by dst_ops->check on every use. We do this
3706 * because when a normal route referenced by an XFRM dst is
3707 * obsoleted we do not go looking around for all parent
3708 * referencing XFRM dsts so that we can invalidate them. It
3709 * is just too much work. Instead we make the checks here on
3710 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003711 *
3712 * XFRM dst A --> IPv4 dst X
3713 *
3714 * X is the "xdst->route" of A (X is also the "dst->path" of A
3715 * in this example). If X is marked obsolete, "A" will not
3716 * notice. That's what we are validating here via the
3717 * stale_bundle() check.
3718 *
Wei Wang52df1572017-06-17 10:42:38 -07003719 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3720 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003721 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003722 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003723 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003724 if (dst->obsolete < 0 && !stale_bundle(dst))
3725 return dst;
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return NULL;
3728}
3729
3730static int stale_bundle(struct dst_entry *dst)
3731{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003732 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Herbert Xuaabc9762005-05-03 16:27:10 -07003735void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
David Millerb92cf4a2017-11-28 15:40:22 -05003737 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003738 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003739 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 dev_put(dev);
3741 }
3742}
Herbert Xuaabc9762005-05-03 16:27:10 -07003743EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
3745static void xfrm_link_failure(struct sk_buff *skb)
3746{
3747 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
3750static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3751{
3752 if (dst) {
3753 if (dst->obsolete) {
3754 dst_release(dst);
3755 dst = NULL;
3756 }
3757 }
3758 return dst;
3759}
3760
David Miller54920932017-11-28 15:41:01 -05003761static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
David Miller54920932017-11-28 15:41:01 -05003763 while (nr--) {
3764 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003766 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
David Miller54920932017-11-28 15:41:01 -05003768 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003769 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 xdst->child_mtu_cached = pmtu;
3771
3772 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3773
3774 route_mtu_cached = dst_mtu(xdst->route);
3775 xdst->route_mtu_cached = route_mtu_cached;
3776
3777 if (pmtu > route_mtu_cached)
3778 pmtu = route_mtu_cached;
3779
David S. Millerdefb3512010-12-08 21:16:57 -08003780 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784/* Check that the bundle accepts the flow and its components are
3785 * still valid.
3786 */
3787
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003788static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
David Miller54920932017-11-28 15:41:01 -05003790 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003792 struct xfrm_dst *xdst;
3793 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 u32 mtu;
3795
David Miller0f6c4802017-11-28 15:40:46 -05003796 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 (dst->dev && !netif_running(dst->dev)))
3798 return 0;
3799
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003800 if (dst->flags & DST_XFRM_QUEUE)
3801 return 1;
3802
David Miller54920932017-11-28 15:41:01 -05003803 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 do {
3805 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3808 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003809 if (xdst->xfrm_genid != dst->xfrm->genid)
3810 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003811 if (xdst->num_pols > 0 &&
3812 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
David Miller54920932017-11-28 15:41:01 -05003815 bundle[nr++] = xdst;
3816
David Millerb92cf4a2017-11-28 15:40:22 -05003817 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003819 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 xdst->child_mtu_cached = mtu;
3821 }
3822
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003823 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return 0;
3825 mtu = dst_mtu(xdst->route);
3826 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003827 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 xdst->route_mtu_cached = mtu;
3829 }
3830
David Millerb92cf4a2017-11-28 15:40:22 -05003831 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 } while (dst->xfrm);
3833
David Miller54920932017-11-28 15:41:01 -05003834 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 return 1;
3836
David Miller54920932017-11-28 15:41:01 -05003837 xdst = bundle[start_from - 1];
3838 mtu = xdst->child_mtu_cached;
3839 while (start_from--) {
3840 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003843 if (mtu > xdst->route_mtu_cached)
3844 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003845 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003846 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 break;
3848
David Miller54920932017-11-28 15:41:01 -05003849 xdst = bundle[start_from - 1];
3850 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
3852
3853 return 1;
3854}
3855
David S. Miller0dbaee32010-12-13 12:52:14 -08003856static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3857{
David Miller0f6c4802017-11-28 15:40:46 -05003858 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003859}
3860
Steffen Klassertebb762f2011-11-23 02:12:51 +00003861static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003862{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003863 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3864
David Miller0f6c4802017-11-28 15:40:46 -05003865 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003866}
3867
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003868static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3869 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003870{
David Miller0f6c4802017-11-28 15:40:46 -05003871 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003872 const struct xfrm_state *xfrm = dst->xfrm;
3873
Steffen Klassert013cb812018-02-19 07:44:07 +01003874 dst = xfrm_dst_child(dst);
3875
Julian Anastasov63fca652017-02-06 23:14:15 +02003876 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3877 continue;
3878 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3879 daddr = xfrm->coaddr;
3880 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3881 daddr = &xfrm->id.daddr;
3882 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003883 return daddr;
3884}
3885
3886static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3887 struct sk_buff *skb,
3888 const void *daddr)
3889{
David Miller0f6c4802017-11-28 15:40:46 -05003890 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003891
3892 if (!skb)
3893 daddr = xfrm_get_dst_nexthop(dst, daddr);
3894 return path->ops->neigh_lookup(path, skb, daddr);
3895}
3896
3897static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3898{
David Miller0f6c4802017-11-28 15:40:46 -05003899 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003900
3901 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003902 path->ops->confirm_neigh(path, daddr);
3903}
3904
Florian Westphala2817d82017-02-07 15:00:17 +01003905int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
3907 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003908
3909 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003911
Eric Dumazetef8531b2012-08-19 12:31:48 +02003912 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003913 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003914 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 else {
3916 struct dst_ops *dst_ops = afinfo->dst_ops;
3917 if (likely(dst_ops->kmem_cachep == NULL))
3918 dst_ops->kmem_cachep = xfrm_dst_cache;
3919 if (likely(dst_ops->check == NULL))
3920 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003921 if (likely(dst_ops->default_advmss == NULL))
3922 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003923 if (likely(dst_ops->mtu == NULL))
3924 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 if (likely(dst_ops->negative_advice == NULL))
3926 dst_ops->negative_advice = xfrm_negative_advice;
3927 if (likely(dst_ops->link_failure == NULL))
3928 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003929 if (likely(dst_ops->neigh_lookup == NULL))
3930 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003931 if (likely(!dst_ops->confirm_neigh))
3932 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003933 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003935 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 return err;
3938}
3939EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3940
Florian Westphala2817d82017-02-07 15:00:17 +01003941void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
Florian Westphal2b619972017-02-07 15:00:15 +01003943 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003944 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003945
Florian Westphala2817d82017-02-07 15:00:17 +01003946 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3947 if (xfrm_policy_afinfo[i] != afinfo)
3948 continue;
3949 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3950 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Florian Westphal2b619972017-02-07 15:00:15 +01003953 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Florian Westphal2b619972017-02-07 15:00:15 +01003955 dst_ops->kmem_cachep = NULL;
3956 dst_ops->check = NULL;
3957 dst_ops->negative_advice = NULL;
3958 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3961
Steffen Klassertf203b762018-06-12 14:07:12 +02003962void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3963{
3964 spin_lock(&xfrm_if_cb_lock);
3965 rcu_assign_pointer(xfrm_if_cb, ifcb);
3966 spin_unlock(&xfrm_if_cb_lock);
3967}
3968EXPORT_SYMBOL(xfrm_if_register_cb);
3969
3970void xfrm_if_unregister_cb(void)
3971{
3972 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3973 synchronize_rcu();
3974}
3975EXPORT_SYMBOL(xfrm_if_unregister_cb);
3976
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003977#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003978static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003979{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003980 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003981 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3982 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003983 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003984 rv = xfrm_proc_init(net);
3985 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003986 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003987 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003988}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003989
3990static void xfrm_statistics_fini(struct net *net)
3991{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003992 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003993 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003994}
3995#else
3996static int __net_init xfrm_statistics_init(struct net *net)
3997{
3998 return 0;
3999}
4000
4001static void xfrm_statistics_fini(struct net *net)
4002{
4003}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08004004#endif
4005
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004006static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
David S. Miller2518c7c2006-08-24 04:45:07 -07004008 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01004009 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07004010
Florian Westphal24969fa2018-11-07 23:00:35 +01004011 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004012 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004014 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004015 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01004016 err = rhashtable_init(&xfrm_policy_inexact_table,
4017 &xfrm_pol_inexact_params);
4018 BUG_ON(err);
4019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
David S. Miller2518c7c2006-08-24 04:45:07 -07004021 hmask = 8 - 1;
4022 sz = (hmask+1) * sizeof(struct hlist_head);
4023
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004024 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
4025 if (!net->xfrm.policy_byidx)
4026 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004027 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07004028
Herbert Xu53c2e282014-11-13 17:09:49 +08004029 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07004030 struct xfrm_policy_hash *htab;
4031
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08004032 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08004033 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004034 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07004035
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004036 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07004037 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07004038 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004039 goto out_bydst;
4040 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02004041 htab->dbits4 = 32;
4042 htab->sbits4 = 32;
4043 htab->dbits6 = 128;
4044 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07004045 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004046 net->xfrm.policy_hthresh.lbits4 = 32;
4047 net->xfrm.policy_hthresh.rbits4 = 32;
4048 net->xfrm.policy_hthresh.lbits6 = 128;
4049 net->xfrm.policy_hthresh.rbits6 = 128;
4050
4051 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07004052
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004053 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01004054 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08004055 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004056 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004057 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004058
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004059out_bydst:
4060 for (dir--; dir >= 0; dir--) {
4061 struct xfrm_policy_hash *htab;
4062
4063 htab = &net->xfrm.policy_bydst[dir];
4064 xfrm_hash_free(htab->table, sz);
4065 }
4066 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004067out_byidx:
4068 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004071static void xfrm_policy_fini(struct net *net)
4072{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004073 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004074 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004075 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004076
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004077 flush_work(&net->xfrm.policy_hash_work);
4078#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09004079 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004080#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09004081 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004082
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004083 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004084
Herbert Xu53c2e282014-11-13 17:09:49 +08004085 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004086 struct xfrm_policy_hash *htab;
4087
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004088 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004089
4090 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01004091 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004092 WARN_ON(!hlist_empty(htab->table));
4093 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004094 }
4095
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004096 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004097 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
4098 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01004099
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004100 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4101 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
4102 __xfrm_policy_inexact_prune_bin(b, true);
4103 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004104}
4105
4106static int __net_init xfrm_net_init(struct net *net)
4107{
4108 int rv;
4109
Florian Westphalc2822222017-02-08 11:52:29 +01004110 /* Initialize the per-net locks here */
4111 spin_lock_init(&net->xfrm.xfrm_state_lock);
4112 spin_lock_init(&net->xfrm.xfrm_policy_lock);
4113 mutex_init(&net->xfrm.xfrm_cfg_mutex);
4114
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004115 rv = xfrm_statistics_init(net);
4116 if (rv < 0)
4117 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004118 rv = xfrm_state_init(net);
4119 if (rv < 0)
4120 goto out_state;
4121 rv = xfrm_policy_init(net);
4122 if (rv < 0)
4123 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004124 rv = xfrm_sysctl_init(net);
4125 if (rv < 0)
4126 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08004127
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004128 return 0;
4129
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004130out_sysctl:
4131 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004132out_policy:
4133 xfrm_state_fini(net);
4134out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004135 xfrm_statistics_fini(net);
4136out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004137 return rv;
4138}
4139
4140static void __net_exit xfrm_net_exit(struct net *net)
4141{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004142 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004143 xfrm_policy_fini(net);
4144 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004145 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004146}
4147
4148static struct pernet_operations __net_initdata xfrm_net_ops = {
4149 .init = xfrm_net_init,
4150 .exit = xfrm_net_exit,
4151};
4152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153void __init xfrm_init(void)
4154{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004155 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03004156 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02004157 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02004159
4160 RCU_INIT_POINTER(xfrm_if_cb, NULL);
4161 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162}
4163
Joy Lattenab5f5e82007-09-17 11:51:22 -07004164#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08004165static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
4166 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004167{
Paul Moore875179f2007-12-01 23:27:18 +11004168 struct xfrm_sec_ctx *ctx = xp->security;
4169 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004170
Paul Moore875179f2007-12-01 23:27:18 +11004171 if (ctx)
4172 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
4173 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
4174
Weilong Chen9b7a7872013-12-24 09:43:46 +08004175 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07004176 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07004177 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004178 if (sel->prefixlen_s != 32)
4179 audit_log_format(audit_buf, " src_prefixlen=%d",
4180 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07004181 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004182 if (sel->prefixlen_d != 32)
4183 audit_log_format(audit_buf, " dst_prefixlen=%d",
4184 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004185 break;
4186 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004187 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004188 if (sel->prefixlen_s != 128)
4189 audit_log_format(audit_buf, " src_prefixlen=%d",
4190 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004191 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004192 if (sel->prefixlen_d != 128)
4193 audit_log_format(audit_buf, " dst_prefixlen=%d",
4194 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004195 break;
4196 }
4197}
4198
Tetsuo Handa2e710292014-04-22 21:48:30 +09004199void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004200{
4201 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004202
Paul Mooreafeb14b2007-12-21 14:58:11 -08004203 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004204 if (audit_buf == NULL)
4205 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004206 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004207 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004208 xfrm_audit_common_policyinfo(xp, audit_buf);
4209 audit_log_end(audit_buf);
4210}
4211EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
4212
Paul Moore68277ac2007-12-20 20:49:33 -08004213void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09004214 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004215{
4216 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004217
Paul Mooreafeb14b2007-12-21 14:58:11 -08004218 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004219 if (audit_buf == NULL)
4220 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004221 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004222 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004223 xfrm_audit_common_policyinfo(xp, audit_buf);
4224 audit_log_end(audit_buf);
4225}
4226EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
4227#endif
4228
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004229#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04004230static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
4231 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004232{
4233 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
4234 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004235 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
4236 sel_cmp->family) &&
4237 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
4238 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004239 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
4240 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004241 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004242 }
4243 } else {
4244 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004245 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004246 }
4247 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04004248 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004249}
4250
Weilong Chen3e94c2d2013-12-24 09:43:47 +08004251static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
4252 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004253{
4254 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004255 struct hlist_head *chain;
4256 u32 priority = ~0U;
4257
Florian Westphal9d0380d2016-08-11 15:17:59 +02004258 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08004259 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004260 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004261 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4262 pol->type == type) {
4263 ret = pol;
4264 priority = ret->priority;
4265 break;
4266 }
4267 }
Fan Du8d549c42013-11-07 17:47:49 +08004268 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01004269 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08004270 if ((pol->priority >= priority) && ret)
4271 break;
4272
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004273 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08004274 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004275 ret = pol;
4276 break;
4277 }
4278 }
4279
Li RongQing586f2eb2015-04-30 17:13:41 +08004280 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004281
Florian Westphal9d0380d2016-08-11 15:17:59 +02004282 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004283
4284 return ret;
4285}
4286
David S. Millerdd701752011-02-24 00:21:08 -05004287static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004288{
4289 int match = 0;
4290
4291 if (t->mode == m->mode && t->id.proto == m->proto &&
4292 (m->reqid == 0 || t->reqid == m->reqid)) {
4293 switch (t->mode) {
4294 case XFRM_MODE_TUNNEL:
4295 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004296 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
4297 m->old_family) &&
4298 xfrm_addr_equal(&t->saddr, &m->old_saddr,
4299 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004300 match = 1;
4301 }
4302 break;
4303 case XFRM_MODE_TRANSPORT:
4304 /* in case of transport mode, template does not store
4305 any IP addresses, hence we just compare mode and
4306 protocol */
4307 match = 1;
4308 break;
4309 default:
4310 break;
4311 }
4312 }
4313 return match;
4314}
4315
4316/* update endpoint address(es) of template(s) */
4317static int xfrm_policy_migrate(struct xfrm_policy *pol,
4318 struct xfrm_migrate *m, int num_migrate)
4319{
4320 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004321 int i, j, n = 0;
4322
4323 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07004324 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004325 /* target policy has been deleted */
4326 write_unlock_bh(&pol->lock);
4327 return -ENOENT;
4328 }
4329
4330 for (i = 0; i < pol->xfrm_nr; i++) {
4331 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4332 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4333 continue;
4334 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07004335 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4336 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004337 continue;
4338 /* update endpoints */
4339 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4340 sizeof(pol->xfrm_vec[i].id.daddr));
4341 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4342 sizeof(pol->xfrm_vec[i].saddr));
4343 pol->xfrm_vec[i].encap_family = mp->new_family;
4344 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00004345 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004346 }
4347 }
4348
4349 write_unlock_bh(&pol->lock);
4350
4351 if (!n)
4352 return -ENODATA;
4353
4354 return 0;
4355}
4356
David S. Millerdd701752011-02-24 00:21:08 -05004357static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004358{
4359 int i, j;
4360
4361 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4362 return -EINVAL;
4363
4364 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004365 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4366 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4367 return -EINVAL;
4368
4369 /* check if there is any duplicated entry */
4370 for (j = i + 1; j < num_migrate; j++) {
4371 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4372 sizeof(m[i].old_daddr)) &&
4373 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4374 sizeof(m[i].old_saddr)) &&
4375 m[i].proto == m[j].proto &&
4376 m[i].mode == m[j].mode &&
4377 m[i].reqid == m[j].reqid &&
4378 m[i].old_family == m[j].old_family)
4379 return -EINVAL;
4380 }
4381 }
4382
4383 return 0;
4384}
4385
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004386int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004387 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004388 struct xfrm_kmaddress *k, struct net *net,
4389 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004390{
4391 int i, err, nx_cur = 0, nx_new = 0;
4392 struct xfrm_policy *pol = NULL;
4393 struct xfrm_state *x, *xc;
4394 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4395 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4396 struct xfrm_migrate *mp;
4397
Vladis Dronov7bab0962017-08-02 19:50:14 +02004398 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004399 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4400 goto out;
4401
Vladis Dronov7bab0962017-08-02 19:50:14 +02004402 if (dir >= XFRM_POLICY_MAX) {
4403 err = -EINVAL;
4404 goto out;
4405 }
4406
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004407 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004408 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004409 err = -ENOENT;
4410 goto out;
4411 }
4412
4413 /* Stage 2 - find and update state(s) */
4414 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004415 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004416 x_cur[nx_cur] = x;
4417 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004418 xc = xfrm_state_migrate(x, mp, encap);
4419 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004420 x_new[nx_new] = xc;
4421 nx_new++;
4422 } else {
4423 err = -ENODATA;
4424 goto restore_state;
4425 }
4426 }
4427 }
4428
4429 /* Stage 3 - update policy */
4430 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4431 goto restore_state;
4432
4433 /* Stage 4 - delete old state(s) */
4434 if (nx_cur) {
4435 xfrm_states_put(x_cur, nx_cur);
4436 xfrm_states_delete(x_cur, nx_cur);
4437 }
4438
4439 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004440 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004441
4442 xfrm_pol_put(pol);
4443
4444 return 0;
4445out:
4446 return err;
4447
4448restore_state:
4449 if (pol)
4450 xfrm_pol_put(pol);
4451 if (nx_cur)
4452 xfrm_states_put(x_cur, nx_cur);
4453 if (nx_new)
4454 xfrm_states_delete(x_new, nx_new);
4455
4456 return err;
4457}
David S. Millere610e672007-02-08 13:29:15 -08004458EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004459#endif