blob: c411662141ae9c424bce1e7f405d557bd2ba707a [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Florian Westphalec30d782017-07-17 13:57:27 +020027#include <linux/cpu.h>
Paul Moore68277ac2007-12-20 20:49:33 -080028#include <linux/audit.h>
Florian Westphal24969fa2018-11-07 23:00:35 +010029#include <linux/rhashtable.h>
Florian Westphalc53ac412019-04-16 16:44:39 +020030#include <linux/if_tunnel.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080031#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050032#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <net/xfrm.h>
34#include <net/ip.h>
Florian Westphalc53ac412019-04-16 16:44:39 +020035#if IS_ENABLED(CONFIG_IPV6_MIP6)
36#include <net/mip6.h>
37#endif
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080038#ifdef CONFIG_XFRM_STATISTICS
39#include <net/snmp.h>
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David S. Miller44e36b42006-08-24 04:50:50 -070042#include "xfrm_hash.h"
43
Steffen Klasserta0073fe2013-02-05 12:52:55 +010044#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
45#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
46#define XFRM_MAX_QUEUE_LEN 100
47
Steffen Klassertb8c203b2014-09-16 10:08:49 +020048struct xfrm_flo {
49 struct dst_entry *dst_orig;
50 u8 flags;
51};
52
Florian Westphal6be3b0d2018-11-07 23:00:37 +010053/* prefixes smaller than this are stored in lists, not trees. */
54#define INEXACT_PREFIXLEN_IPV4 16
55#define INEXACT_PREFIXLEN_IPV6 48
Florian Westphal9cf545e2018-11-07 23:00:38 +010056
57struct xfrm_pol_inexact_node {
58 struct rb_node node;
59 union {
60 xfrm_address_t addr;
61 struct rcu_head rcu;
62 };
63 u8 prefixlen;
64
Florian Westphal6ac098b2018-11-07 23:00:41 +010065 struct rb_root root;
66
Florian Westphal9cf545e2018-11-07 23:00:38 +010067 /* the policies matching this node, can be empty list */
68 struct hlist_head hhead;
69};
70
71/* xfrm inexact policy search tree:
72 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
73 * |
74 * +---- root_d: sorted by daddr:prefix
75 * | |
76 * | xfrm_pol_inexact_node
77 * | |
Florian Westphal6ac098b2018-11-07 23:00:41 +010078 * | +- root: sorted by saddr/prefix
79 * | | |
80 * | | xfrm_pol_inexact_node
81 * | | |
82 * | | + root: unused
83 * | | |
84 * | | + hhead: saddr:daddr policies
85 * | |
Florian Westphal9cf545e2018-11-07 23:00:38 +010086 * | +- coarse policies and all any:daddr policies
87 * |
Florian Westphal64a09a72018-11-07 23:00:40 +010088 * +---- root_s: sorted by saddr:prefix
89 * | |
90 * | xfrm_pol_inexact_node
91 * | |
92 * | + root: unused
93 * | |
94 * | + hhead: saddr:any policies
95 * |
Florian Westphal9cf545e2018-11-07 23:00:38 +010096 * +---- coarse policies and all any:any policies
97 *
Florian Westphal6ac098b2018-11-07 23:00:41 +010098 * Lookups return four candidate lists:
Florian Westphal9cf545e2018-11-07 23:00:38 +010099 * 1. any:any list from top-level xfrm_pol_inexact_bin
100 * 2. any:daddr list from daddr tree
Florian Westphal6ac098b2018-11-07 23:00:41 +0100101 * 3. saddr:daddr list from 2nd level daddr tree
102 * 4. saddr:any list from saddr tree
Florian Westphal9cf545e2018-11-07 23:00:38 +0100103 *
104 * This result set then needs to be searched for the policy with
105 * the lowest priority. If two results have same prio, youngest one wins.
106 */
107
Florian Westphal24969fa2018-11-07 23:00:35 +0100108struct xfrm_pol_inexact_key {
109 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100110 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +0100111 u16 family;
112 u8 dir, type;
113};
114
115struct xfrm_pol_inexact_bin {
116 struct xfrm_pol_inexact_key k;
117 struct rhash_head head;
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100118 /* list containing '*:*' policies */
Florian Westphal24969fa2018-11-07 23:00:35 +0100119 struct hlist_head hhead;
120
Florian Westphal9cf545e2018-11-07 23:00:38 +0100121 seqcount_t count;
122 /* tree sorted by daddr/prefix */
123 struct rb_root root_d;
124
Florian Westphal64a09a72018-11-07 23:00:40 +0100125 /* tree sorted by saddr/prefix */
126 struct rb_root root_s;
127
Florian Westphal24969fa2018-11-07 23:00:35 +0100128 /* slow path below */
129 struct list_head inexact_bins;
130 struct rcu_head rcu;
131};
132
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100133enum xfrm_pol_inexact_candidate_type {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100134 XFRM_POL_CAND_BOTH,
Florian Westphal64a09a72018-11-07 23:00:40 +0100135 XFRM_POL_CAND_SADDR,
Florian Westphal9cf545e2018-11-07 23:00:38 +0100136 XFRM_POL_CAND_DADDR,
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100137 XFRM_POL_CAND_ANY,
138
139 XFRM_POL_CAND_MAX,
140};
141
142struct xfrm_pol_inexact_candidates {
143 struct hlist_head *res[XFRM_POL_CAND_MAX];
144};
145
Steffen Klassertf203b762018-06-12 14:07:12 +0200146static DEFINE_SPINLOCK(xfrm_if_cb_lock);
147static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
148
Priyanka Jain418a99a2012-08-12 21:22:29 +0000149static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +0100150static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +0000151 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +0300153static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +0200154static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Florian Westphal24969fa2018-11-07 23:00:35 +0100156static struct rhashtable xfrm_policy_inexact_table;
157static const struct rhashtable_params xfrm_pol_inexact_params;
158
David Miller54920932017-11-28 15:41:01 -0500159static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +0000160static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +0000161static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -0700162static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800164static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800165static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
166 int dir);
167
Florian Westphal24969fa2018-11-07 23:00:35 +0100168static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100169xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
170 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100171
172static struct xfrm_pol_inexact_bin *
173xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100174 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100175static struct xfrm_policy *
176xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
177 bool excl);
178static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
179 struct xfrm_policy *policy);
180
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100181static bool
182xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
183 struct xfrm_pol_inexact_bin *b,
184 const xfrm_address_t *saddr,
185 const xfrm_address_t *daddr);
186
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200187static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
188{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300189 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8ad2016-08-11 15:17:55 +0200190}
191
David S. Millerbc9b35a2012-05-15 15:04:57 -0400192static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500193__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800194{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500195 const struct flowi4 *fl4 = &fl->u.ip4;
196
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000197 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
198 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500199 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
200 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
201 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
202 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800203}
204
David S. Millerbc9b35a2012-05-15 15:04:57 -0400205static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500206__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800207{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500208 const struct flowi6 *fl6 = &fl->u.ip6;
209
210 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
211 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
212 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
213 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
214 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
215 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800216}
217
David S. Millerbc9b35a2012-05-15 15:04:57 -0400218bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
219 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800220{
221 switch (family) {
222 case AF_INET:
223 return __xfrm4_selector_match(sel, fl);
224 case AF_INET6:
225 return __xfrm6_selector_match(sel, fl);
226 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400227 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800228}
229
Florian Westphala2817d82017-02-07 15:00:17 +0100230static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200231{
Florian Westphala2817d82017-02-07 15:00:17 +0100232 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200233
Florian Westphala2817d82017-02-07 15:00:17 +0100234 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200235 return NULL;
236 rcu_read_lock();
237 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
238 if (unlikely(!afinfo))
239 rcu_read_unlock();
240 return afinfo;
241}
242
Steffen Klassertf203b762018-06-12 14:07:12 +0200243/* Called with rcu_read_lock(). */
244static const struct xfrm_if_cb *xfrm_if_get_cb(void)
245{
246 return rcu_dereference(xfrm_if_cb);
247}
248
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200249struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
250 const xfrm_address_t *saddr,
251 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900252 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Florian Westphal37b10382017-02-07 15:00:19 +0100254 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800255 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Herbert Xu25ee3282007-12-11 09:32:34 -0800257 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800259 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900261 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900262
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100263 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900264
265 return dst;
266}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200267EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900268
David Ahern42a7b322015-08-10 16:58:11 -0600269static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
270 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900271 xfrm_address_t *prev_saddr,
272 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900273 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900274{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800275 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900276 xfrm_address_t *saddr = &x->props.saddr;
277 xfrm_address_t *daddr = &x->id.daddr;
278 struct dst_entry *dst;
279
280 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
281 saddr = x->coaddr;
282 daddr = prev_daddr;
283 }
284 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
285 saddr = prev_saddr;
286 daddr = x->coaddr;
287 }
288
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900289 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900290
291 if (!IS_ERR(dst)) {
292 if (prev_saddr != saddr)
293 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
294 if (prev_daddr != daddr)
295 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
296 }
297
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800298 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static inline unsigned long make_jiffies(long secs)
302{
303 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
304 return MAX_SCHEDULE_TIMEOUT-1;
305 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900306 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Kees Cookc3aed702017-10-16 17:28:56 -0700309static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Kees Cookc3aed702017-10-16 17:28:56 -0700311 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200312 time64_t now = ktime_get_real_seconds();
313 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int warn = 0;
315 int dir;
316
317 read_lock(&xp->lock);
318
Timo Teräsea2dea92010-03-31 00:17:05 +0000319 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto out;
321
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700322 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200325 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xp->curlft.add_time - now;
327 if (tmo <= 0)
328 goto expired;
329 if (tmo < next)
330 next = tmo;
331 }
332 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200333 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
335 if (tmo <= 0)
336 goto expired;
337 if (tmo < next)
338 next = tmo;
339 }
340 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200341 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xp->curlft.add_time - now;
343 if (tmo <= 0) {
344 warn = 1;
345 tmo = XFRM_KM_TIMEOUT;
346 }
347 if (tmo < next)
348 next = tmo;
349 }
350 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200351 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
353 if (tmo <= 0) {
354 warn = 1;
355 tmo = XFRM_KM_TIMEOUT;
356 }
357 if (tmo < next)
358 next = tmo;
359 }
360
361 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800362 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200363 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
365 xfrm_pol_hold(xp);
366
367out:
368 read_unlock(&xp->lock);
369 xfrm_pol_put(xp);
370 return;
371
372expired:
373 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700374 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800375 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfrm_pol_put(xp);
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
380 * SPD calls.
381 */
382
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800383struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct xfrm_policy *policy;
386
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700387 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800390 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700391 INIT_LIST_HEAD(&policy->walk.all);
Florian Westphal24969fa2018-11-07 23:00:35 +0100392 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700393 INIT_HLIST_NODE(&policy->bydst);
394 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300396 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100397 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700398 timer_setup(&policy->timer, xfrm_policy_timer, 0);
399 timer_setup(&policy->polq.hold_timer,
400 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 return policy;
403}
404EXPORT_SYMBOL(xfrm_policy_alloc);
405
Eric Dumazet56f04732015-12-08 07:22:01 -0800406static void xfrm_policy_destroy_rcu(struct rcu_head *head)
407{
408 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
409
410 security_xfrm_policy_free(policy->security);
411 kfree(policy);
412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* Destroy xfrm_policy: descendant resources must be released to this moment. */
415
WANG Cong64c31b32008-01-07 22:34:29 -0800416void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Herbert Xu12a169e2008-10-01 07:03:24 -0700418 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Fan Du0659eea2013-08-01 18:08:36 +0800420 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 BUG();
422
Eric Dumazet56f04732015-12-08 07:22:01 -0800423 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
WANG Cong64c31b32008-01-07 22:34:29 -0800425EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100427/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * entry dead. The rule must be unlinked from lists to the moment.
429 */
430
431static void xfrm_policy_kill(struct xfrm_policy *policy)
432{
Herbert Xu12a169e2008-10-01 07:03:24 -0700433 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Timo Teräs285ead12010-04-07 00:30:06 +0000435 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200437 if (del_timer(&policy->polq.hold_timer))
438 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800439 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100440
Timo Teräs285ead12010-04-07 00:30:06 +0000441 if (del_timer(&policy->timer))
442 xfrm_pol_put(policy);
443
444 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
David S. Miller2518c7c2006-08-24 04:45:07 -0700447static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
448
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800449static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700450{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800451 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700452}
453
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200454/* calculate policy hash thresholds */
455static void __get_hash_thresh(struct net *net,
456 unsigned short family, int dir,
457 u8 *dbits, u8 *sbits)
458{
459 switch (family) {
460 case AF_INET:
461 *dbits = net->xfrm.policy_bydst[dir].dbits4;
462 *sbits = net->xfrm.policy_bydst[dir].sbits4;
463 break;
464
465 case AF_INET6:
466 *dbits = net->xfrm.policy_bydst[dir].dbits6;
467 *sbits = net->xfrm.policy_bydst[dir].sbits6;
468 break;
469
470 default:
471 *dbits = 0;
472 *sbits = 0;
473 }
474}
475
David S. Miller5f803b52011-02-24 00:33:19 -0500476static struct hlist_head *policy_hash_bysel(struct net *net,
477 const struct xfrm_selector *sel,
478 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700479{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800480 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200481 unsigned int hash;
482 u8 dbits;
483 u8 sbits;
484
485 __get_hash_thresh(net, family, dir, &dbits, &sbits);
486 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700487
Florian Westphale1e551b2016-08-11 15:17:53 +0200488 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100489 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200490
491 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
492 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700493}
494
David S. Miller5f803b52011-02-24 00:33:19 -0500495static struct hlist_head *policy_hash_direct(struct net *net,
496 const xfrm_address_t *daddr,
497 const xfrm_address_t *saddr,
498 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700499{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800500 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200501 unsigned int hash;
502 u8 dbits;
503 u8 sbits;
504
505 __get_hash_thresh(net, family, dir, &dbits, &sbits);
506 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700507
Florian Westphale1e551b2016-08-11 15:17:53 +0200508 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
509 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700510}
511
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200512static void xfrm_dst_hash_transfer(struct net *net,
513 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700514 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200515 unsigned int nhashmask,
516 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700517{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800518 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700519 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800520 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200521 u8 dbits;
522 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700523
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800524redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800525 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700526 unsigned int h;
527
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200528 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700529 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200530 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800531 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200532 hlist_del_rcu(&pol->bydst);
533 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800534 h0 = h;
535 } else {
536 if (h != h0)
537 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200538 hlist_del_rcu(&pol->bydst);
539 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800540 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800541 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800542 }
543 if (!hlist_empty(list)) {
544 entry0 = NULL;
545 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700546 }
547}
548
549static void xfrm_idx_hash_transfer(struct hlist_head *list,
550 struct hlist_head *nidxtable,
551 unsigned int nhashmask)
552{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800553 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700554 struct xfrm_policy *pol;
555
Sasha Levinb67bfe02013-02-27 17:06:00 -0800556 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 unsigned int h;
558
559 h = __idx_hash(pol->index, nhashmask);
560 hlist_add_head(&pol->byidx, nidxtable+h);
561 }
562}
563
564static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
565{
566 return ((old_hmask + 1) << 1) - 1;
567}
568
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800569static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700570{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800571 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700572 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
573 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700574 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200575 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700576 int i;
577
578 if (!ndst)
579 return;
580
Florian Westphal9d0380d2016-08-11 15:17:59 +0200581 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200582 write_seqcount_begin(&xfrm_policy_hash_generation);
583
584 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
585 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700586
587 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200588 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700589
Florian Westphale1e551b2016-08-11 15:17:53 +0200590 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800591 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700592
Florian Westphal30846092016-08-11 15:17:54 +0200593 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200594 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700595
Florian Westphale1e551b2016-08-11 15:17:53 +0200596 synchronize_rcu();
597
David S. Miller44e36b42006-08-24 04:50:50 -0700598 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700599}
600
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800601static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700602{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800603 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700604 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
605 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800606 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700607 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700608 int i;
609
610 if (!nidx)
611 return;
612
Florian Westphal9d0380d2016-08-11 15:17:59 +0200613 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700614
615 for (i = hmask; i >= 0; i--)
616 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
617
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800618 net->xfrm.policy_byidx = nidx;
619 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700620
Florian Westphal9d0380d2016-08-11 15:17:59 +0200621 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700622
David S. Miller44e36b42006-08-24 04:50:50 -0700623 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700624}
625
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800626static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700627{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800628 unsigned int cnt = net->xfrm.policy_count[dir];
629 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700630
631 if (total)
632 *total += cnt;
633
634 if ((hmask + 1) < xfrm_policy_hashmax &&
635 cnt > hmask)
636 return 1;
637
638 return 0;
639}
640
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800641static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700642{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800643 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700644
645 if ((hmask + 1) < xfrm_policy_hashmax &&
646 total > hmask)
647 return 1;
648
649 return 0;
650}
651
Alexey Dobriyane0710412010-01-23 13:37:10 +0000652void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700653{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000654 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
655 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
656 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
657 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
658 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
659 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
660 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700661 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700662}
663EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700664
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700665static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800666static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700667{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800668 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700669 int dir, total;
670
671 mutex_lock(&hash_resize_mutex);
672
673 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800674 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800675 if (xfrm_bydst_should_resize(net, dir, &total))
676 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800678 if (xfrm_byidx_should_resize(net, total))
679 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700680
681 mutex_unlock(&hash_resize_mutex);
682}
683
Florian Westphal24969fa2018-11-07 23:00:35 +0100684/* Make sure *pol can be inserted into fastbin.
685 * Useful to check that later insert requests will be sucessful
686 * (provided xfrm_policy_lock is held throughout).
687 */
688static struct xfrm_pol_inexact_bin *
689xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
690{
691 struct xfrm_pol_inexact_bin *bin, *prev;
692 struct xfrm_pol_inexact_key k = {
693 .family = pol->family,
694 .type = pol->type,
695 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100696 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100697 };
698 struct net *net = xp_net(pol);
699
700 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
701
702 write_pnet(&k.net, net);
703 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
704 xfrm_pol_inexact_params);
705 if (bin)
706 return bin;
707
708 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
709 if (!bin)
710 return NULL;
711
712 bin->k = k;
713 INIT_HLIST_HEAD(&bin->hhead);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100714 bin->root_d = RB_ROOT;
Florian Westphal64a09a72018-11-07 23:00:40 +0100715 bin->root_s = RB_ROOT;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100716 seqcount_init(&bin->count);
Florian Westphal24969fa2018-11-07 23:00:35 +0100717
718 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
719 &bin->k, &bin->head,
720 xfrm_pol_inexact_params);
721 if (!prev) {
722 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
723 return bin;
724 }
725
726 kfree(bin);
727
728 return IS_ERR(prev) ? NULL : prev;
729}
730
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100731static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
732 int family, u8 prefixlen)
Florian Westphal24969fa2018-11-07 23:00:35 +0100733{
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100734 if (xfrm_addr_any(addr, family))
735 return true;
Florian Westphal24969fa2018-11-07 23:00:35 +0100736
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100737 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
738 return true;
739
740 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
741 return true;
742
743 return false;
744}
745
746static bool
747xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
748{
749 const xfrm_address_t *addr;
750 bool saddr_any, daddr_any;
751 u8 prefixlen;
752
753 addr = &policy->selector.saddr;
754 prefixlen = policy->selector.prefixlen_s;
755
756 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
757 policy->family,
758 prefixlen);
759 addr = &policy->selector.daddr;
760 prefixlen = policy->selector.prefixlen_d;
761 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
762 policy->family,
763 prefixlen);
764 return saddr_any && daddr_any;
765}
766
Florian Westphal9cf545e2018-11-07 23:00:38 +0100767static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
768 const xfrm_address_t *addr, u8 prefixlen)
769{
770 node->addr = *addr;
771 node->prefixlen = prefixlen;
772}
773
774static struct xfrm_pol_inexact_node *
775xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
776{
777 struct xfrm_pol_inexact_node *node;
778
779 node = kzalloc(sizeof(*node), GFP_ATOMIC);
780 if (node)
781 xfrm_pol_inexact_node_init(node, addr, prefixlen);
782
783 return node;
784}
785
786static int xfrm_policy_addr_delta(const xfrm_address_t *a,
787 const xfrm_address_t *b,
788 u8 prefixlen, u16 family)
789{
790 unsigned int pdw, pbi;
791 int delta = 0;
792
793 switch (family) {
794 case AF_INET:
795 if (sizeof(long) == 4 && prefixlen == 0)
796 return ntohl(a->a4) - ntohl(b->a4);
797 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
798 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
799 case AF_INET6:
800 pdw = prefixlen >> 5;
801 pbi = prefixlen & 0x1f;
802
803 if (pdw) {
804 delta = memcmp(a->a6, b->a6, pdw << 2);
805 if (delta)
806 return delta;
807 }
808 if (pbi) {
809 u32 mask = ~0u << (32 - pbi);
810
811 delta = (ntohl(a->a6[pdw]) & mask) -
812 (ntohl(b->a6[pdw]) & mask);
813 }
814 break;
815 default:
816 break;
817 }
818
819 return delta;
820}
821
822static void xfrm_policy_inexact_list_reinsert(struct net *net,
823 struct xfrm_pol_inexact_node *n,
824 u16 family)
825{
Florian Westphale901cbc2018-11-07 23:00:39 +0100826 unsigned int matched_s, matched_d;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100827 struct xfrm_policy *policy, *p;
828
Florian Westphale901cbc2018-11-07 23:00:39 +0100829 matched_s = 0;
830 matched_d = 0;
831
Florian Westphal9cf545e2018-11-07 23:00:38 +0100832 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal1d389002019-01-04 14:17:03 +0100833 struct hlist_node *newpos = NULL;
Florian Westphale901cbc2018-11-07 23:00:39 +0100834 bool matches_s, matches_d;
835
Florian Westphal9cf545e2018-11-07 23:00:38 +0100836 if (!policy->bydst_reinsert)
837 continue;
838
839 WARN_ON_ONCE(policy->family != family);
840
841 policy->bydst_reinsert = false;
842 hlist_for_each_entry(p, &n->hhead, bydst) {
Florian Westphal1d389002019-01-04 14:17:03 +0100843 if (policy->priority > p->priority)
844 newpos = &p->bydst;
845 else if (policy->priority == p->priority &&
846 policy->pos > p->pos)
Florian Westphal9cf545e2018-11-07 23:00:38 +0100847 newpos = &p->bydst;
848 else
849 break;
850 }
851
852 if (newpos)
Florian Westphal355b00d2019-01-04 14:17:00 +0100853 hlist_add_behind_rcu(&policy->bydst, newpos);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100854 else
Florian Westphal355b00d2019-01-04 14:17:00 +0100855 hlist_add_head_rcu(&policy->bydst, &n->hhead);
Florian Westphale901cbc2018-11-07 23:00:39 +0100856
857 /* paranoia checks follow.
858 * Check that the reinserted policy matches at least
859 * saddr or daddr for current node prefix.
860 *
861 * Matching both is fine, matching saddr in one policy
862 * (but not daddr) and then matching only daddr in another
863 * is a bug.
864 */
865 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
866 &n->addr,
867 n->prefixlen,
868 family) == 0;
869 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
870 &n->addr,
871 n->prefixlen,
872 family) == 0;
873 if (matches_s && matches_d)
874 continue;
875
876 WARN_ON_ONCE(!matches_s && !matches_d);
877 if (matches_s)
878 matched_s++;
879 if (matches_d)
880 matched_d++;
881 WARN_ON_ONCE(matched_s && matched_d);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100882 }
883}
884
Florian Westphal6ac098b2018-11-07 23:00:41 +0100885static void xfrm_policy_inexact_node_reinsert(struct net *net,
886 struct xfrm_pol_inexact_node *n,
887 struct rb_root *new,
888 u16 family)
889{
Florian Westphal6ac098b2018-11-07 23:00:41 +0100890 struct xfrm_pol_inexact_node *node;
Florian Westphal12750ab2019-01-04 14:17:05 +0100891 struct rb_node **p, *parent;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100892
893 /* we should not have another subtree here */
894 WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root));
Florian Westphal12750ab2019-01-04 14:17:05 +0100895restart:
896 parent = NULL;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100897 p = &new->rb_node;
898 while (*p) {
899 u8 prefixlen;
900 int delta;
901
902 parent = *p;
903 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
904
905 prefixlen = min(node->prefixlen, n->prefixlen);
906
907 delta = xfrm_policy_addr_delta(&n->addr, &node->addr,
908 prefixlen, family);
909 if (delta < 0) {
910 p = &parent->rb_left;
911 } else if (delta > 0) {
912 p = &parent->rb_right;
913 } else {
914 struct xfrm_policy *tmp;
915
Florian Westphal12750ab2019-01-04 14:17:05 +0100916 hlist_for_each_entry(tmp, &n->hhead, bydst) {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100917 tmp->bydst_reinsert = true;
Florian Westphal12750ab2019-01-04 14:17:05 +0100918 hlist_del_rcu(&tmp->bydst);
919 }
Florian Westphal6ac098b2018-11-07 23:00:41 +0100920
Florian Westphal6ac098b2018-11-07 23:00:41 +0100921 xfrm_policy_inexact_list_reinsert(net, node, family);
922
923 if (node->prefixlen == n->prefixlen) {
924 kfree_rcu(n, rcu);
925 return;
926 }
927
928 rb_erase(*p, new);
929 kfree_rcu(n, rcu);
930 n = node;
931 n->prefixlen = prefixlen;
Florian Westphal12750ab2019-01-04 14:17:05 +0100932 goto restart;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100933 }
934 }
935
936 rb_link_node_rcu(&n->node, parent, p);
937 rb_insert_color(&n->node, new);
938}
939
Florian Westphal9cf545e2018-11-07 23:00:38 +0100940/* merge nodes v and n */
941static void xfrm_policy_inexact_node_merge(struct net *net,
942 struct xfrm_pol_inexact_node *v,
943 struct xfrm_pol_inexact_node *n,
944 u16 family)
945{
Florian Westphal6ac098b2018-11-07 23:00:41 +0100946 struct xfrm_pol_inexact_node *node;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100947 struct xfrm_policy *tmp;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100948 struct rb_node *rnode;
949
950 /* To-be-merged node v has a subtree.
951 *
952 * Dismantle it and insert its nodes to n->root.
953 */
954 while ((rnode = rb_first(&v->root)) != NULL) {
955 node = rb_entry(rnode, struct xfrm_pol_inexact_node, node);
956 rb_erase(&node->node, &v->root);
957 xfrm_policy_inexact_node_reinsert(net, node, &n->root,
958 family);
959 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100960
Florian Westphal1d389002019-01-04 14:17:03 +0100961 hlist_for_each_entry(tmp, &v->hhead, bydst) {
Florian Westphal9cf545e2018-11-07 23:00:38 +0100962 tmp->bydst_reinsert = true;
Florian Westphal1d389002019-01-04 14:17:03 +0100963 hlist_del_rcu(&tmp->bydst);
964 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100965
Florian Westphal9cf545e2018-11-07 23:00:38 +0100966 xfrm_policy_inexact_list_reinsert(net, n, family);
967}
968
969static struct xfrm_pol_inexact_node *
970xfrm_policy_inexact_insert_node(struct net *net,
971 struct rb_root *root,
972 xfrm_address_t *addr,
973 u16 family, u8 prefixlen, u8 dir)
974{
975 struct xfrm_pol_inexact_node *cached = NULL;
976 struct rb_node **p, *parent = NULL;
977 struct xfrm_pol_inexact_node *node;
978
979 p = &root->rb_node;
980 while (*p) {
981 int delta;
982
983 parent = *p;
984 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
985
986 delta = xfrm_policy_addr_delta(addr, &node->addr,
987 node->prefixlen,
988 family);
989 if (delta == 0 && prefixlen >= node->prefixlen) {
990 WARN_ON_ONCE(cached); /* ipsec policies got lost */
991 return node;
992 }
993
994 if (delta < 0)
995 p = &parent->rb_left;
996 else
997 p = &parent->rb_right;
998
999 if (prefixlen < node->prefixlen) {
1000 delta = xfrm_policy_addr_delta(addr, &node->addr,
1001 prefixlen,
1002 family);
1003 if (delta)
1004 continue;
1005
1006 /* This node is a subnet of the new prefix. It needs
1007 * to be removed and re-inserted with the smaller
1008 * prefix and all nodes that are now also covered
1009 * by the reduced prefixlen.
1010 */
1011 rb_erase(&node->node, root);
1012
1013 if (!cached) {
1014 xfrm_pol_inexact_node_init(node, addr,
1015 prefixlen);
1016 cached = node;
1017 } else {
1018 /* This node also falls within the new
1019 * prefixlen. Merge the to-be-reinserted
1020 * node and this one.
1021 */
1022 xfrm_policy_inexact_node_merge(net, node,
1023 cached, family);
1024 kfree_rcu(node, rcu);
1025 }
1026
1027 /* restart */
1028 p = &root->rb_node;
1029 parent = NULL;
1030 }
1031 }
1032
1033 node = cached;
1034 if (!node) {
1035 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
1036 if (!node)
1037 return NULL;
1038 }
1039
1040 rb_link_node_rcu(&node->node, parent, p);
1041 rb_insert_color(&node->node, root);
1042
1043 return node;
1044}
1045
1046static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
1047{
1048 struct xfrm_pol_inexact_node *node;
1049 struct rb_node *rn = rb_first(r);
1050
1051 while (rn) {
1052 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
1053
Florian Westphal6ac098b2018-11-07 23:00:41 +01001054 xfrm_policy_inexact_gc_tree(&node->root, rm);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001055 rn = rb_next(rn);
1056
Florian Westphal6ac098b2018-11-07 23:00:41 +01001057 if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001058 WARN_ON_ONCE(rm);
1059 continue;
1060 }
1061
1062 rb_erase(&node->node, r);
1063 kfree_rcu(node, rcu);
1064 }
1065}
1066
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001067static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
1068{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001069 write_seqcount_begin(&b->count);
1070 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
Florian Westphal64a09a72018-11-07 23:00:40 +01001071 xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001072 write_seqcount_end(&b->count);
1073
Florian Westphal64a09a72018-11-07 23:00:40 +01001074 if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
Florian Westphal9cf545e2018-11-07 23:00:38 +01001075 !hlist_empty(&b->hhead)) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001076 WARN_ON_ONCE(net_exit);
Florian Westphal24969fa2018-11-07 23:00:35 +01001077 return;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001078 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001079
1080 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
1081 xfrm_pol_inexact_params) == 0) {
1082 list_del(&b->inexact_bins);
1083 kfree_rcu(b, rcu);
1084 }
1085}
1086
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001087static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
1088{
1089 struct net *net = read_pnet(&b->k.net);
1090
1091 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1092 __xfrm_policy_inexact_prune_bin(b, false);
1093 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1094}
1095
Florian Westphal24969fa2018-11-07 23:00:35 +01001096static void __xfrm_policy_inexact_flush(struct net *net)
1097{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001098 struct xfrm_pol_inexact_bin *bin, *t;
Florian Westphal24969fa2018-11-07 23:00:35 +01001099
1100 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1101
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001102 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1103 __xfrm_policy_inexact_prune_bin(bin, false);
Florian Westphal24969fa2018-11-07 23:00:35 +01001104}
1105
Florian Westphal9cf545e2018-11-07 23:00:38 +01001106static struct hlist_head *
1107xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1108 struct xfrm_policy *policy, u8 dir)
1109{
1110 struct xfrm_pol_inexact_node *n;
1111 struct net *net;
1112
1113 net = xp_net(policy);
1114 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1115
1116 if (xfrm_policy_inexact_insert_use_any_list(policy))
1117 return &bin->hhead;
1118
1119 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1120 policy->family,
Florian Westphal64a09a72018-11-07 23:00:40 +01001121 policy->selector.prefixlen_d)) {
1122 write_seqcount_begin(&bin->count);
1123 n = xfrm_policy_inexact_insert_node(net,
1124 &bin->root_s,
1125 &policy->selector.saddr,
1126 policy->family,
1127 policy->selector.prefixlen_s,
1128 dir);
1129 write_seqcount_end(&bin->count);
1130 if (!n)
1131 return NULL;
1132
1133 return &n->hhead;
1134 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001135
1136 /* daddr is fixed */
1137 write_seqcount_begin(&bin->count);
1138 n = xfrm_policy_inexact_insert_node(net,
1139 &bin->root_d,
1140 &policy->selector.daddr,
1141 policy->family,
1142 policy->selector.prefixlen_d, dir);
1143 write_seqcount_end(&bin->count);
1144 if (!n)
1145 return NULL;
Florian Westphal6ac098b2018-11-07 23:00:41 +01001146
1147 /* saddr is wildcard */
1148 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
1149 policy->family,
1150 policy->selector.prefixlen_s))
1151 return &n->hhead;
1152
1153 write_seqcount_begin(&bin->count);
1154 n = xfrm_policy_inexact_insert_node(net,
1155 &n->root,
1156 &policy->selector.saddr,
1157 policy->family,
1158 policy->selector.prefixlen_s, dir);
1159 write_seqcount_end(&bin->count);
1160 if (!n)
1161 return NULL;
1162
Florian Westphal9cf545e2018-11-07 23:00:38 +01001163 return &n->hhead;
1164}
1165
Florian Westphal24969fa2018-11-07 23:00:35 +01001166static struct xfrm_policy *
1167xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1168{
1169 struct xfrm_pol_inexact_bin *bin;
1170 struct xfrm_policy *delpol;
1171 struct hlist_head *chain;
1172 struct net *net;
1173
1174 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1175 if (!bin)
1176 return ERR_PTR(-ENOMEM);
1177
Florian Westphal24969fa2018-11-07 23:00:35 +01001178 net = xp_net(policy);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001179 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1180
Florian Westphal9cf545e2018-11-07 23:00:38 +01001181 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1182 if (!chain) {
1183 __xfrm_policy_inexact_prune_bin(bin, false);
1184 return ERR_PTR(-ENOMEM);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001185 }
1186
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001187 delpol = xfrm_policy_insert_list(chain, policy, excl);
1188 if (delpol && excl) {
1189 __xfrm_policy_inexact_prune_bin(bin, false);
1190 return ERR_PTR(-EEXIST);
1191 }
1192
Florian Westphal24969fa2018-11-07 23:00:35 +01001193 chain = &net->xfrm.policy_inexact[dir];
1194 xfrm_policy_insert_inexact_list(chain, policy);
1195
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001196 if (delpol)
1197 __xfrm_policy_inexact_prune_bin(bin, false);
1198
Florian Westphal24969fa2018-11-07 23:00:35 +01001199 return delpol;
1200}
1201
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001202static void xfrm_hash_rebuild(struct work_struct *work)
1203{
1204 struct net *net = container_of(work, struct net,
1205 xfrm.policy_hthresh.work);
1206 unsigned int hmask;
1207 struct xfrm_policy *pol;
1208 struct xfrm_policy *policy;
1209 struct hlist_head *chain;
1210 struct hlist_head *odst;
1211 struct hlist_node *newpos;
1212 int i;
1213 int dir;
1214 unsigned seq;
1215 u8 lbits4, rbits4, lbits6, rbits6;
1216
1217 mutex_lock(&hash_resize_mutex);
1218
1219 /* read selector prefixlen thresholds */
1220 do {
1221 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1222
1223 lbits4 = net->xfrm.policy_hthresh.lbits4;
1224 rbits4 = net->xfrm.policy_hthresh.rbits4;
1225 lbits6 = net->xfrm.policy_hthresh.lbits6;
1226 rbits6 = net->xfrm.policy_hthresh.rbits6;
1227 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1228
Florian Westphal9d0380d2016-08-11 15:17:59 +02001229 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal7a474c32019-01-04 14:17:01 +01001230 write_seqcount_begin(&xfrm_policy_hash_generation);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001231
Florian Westphal24969fa2018-11-07 23:00:35 +01001232 /* make sure that we can insert the indirect policies again before
1233 * we start with destructive action.
1234 */
1235 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001236 struct xfrm_pol_inexact_bin *bin;
Florian Westphal24969fa2018-11-07 23:00:35 +01001237 u8 dbits, sbits;
1238
1239 dir = xfrm_policy_id2dir(policy->index);
1240 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1241 continue;
1242
1243 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1244 if (policy->family == AF_INET) {
1245 dbits = rbits4;
1246 sbits = lbits4;
1247 } else {
1248 dbits = rbits6;
1249 sbits = lbits6;
1250 }
1251 } else {
1252 if (policy->family == AF_INET) {
1253 dbits = lbits4;
1254 sbits = rbits4;
1255 } else {
1256 dbits = lbits6;
1257 sbits = rbits6;
1258 }
1259 }
1260
1261 if (policy->selector.prefixlen_d < dbits ||
1262 policy->selector.prefixlen_s < sbits)
1263 continue;
1264
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001265 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1266 if (!bin)
Florian Westphal24969fa2018-11-07 23:00:35 +01001267 goto out_unlock;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001268
1269 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1270 goto out_unlock;
Florian Westphal24969fa2018-11-07 23:00:35 +01001271 }
1272
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001273 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +08001274 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Florian Westphal1548bc42019-01-04 14:17:02 +01001275 struct hlist_node *n;
1276
1277 hlist_for_each_entry_safe(policy, n,
1278 &net->xfrm.policy_inexact[dir],
Florian Westphalfd709722019-07-02 12:46:00 +02001279 bydst_inexact_list) {
1280 hlist_del_rcu(&policy->bydst);
Florian Westphal1548bc42019-01-04 14:17:02 +01001281 hlist_del_init(&policy->bydst_inexact_list);
Florian Westphalfd709722019-07-02 12:46:00 +02001282 }
Florian Westphal1548bc42019-01-04 14:17:02 +01001283
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001284 hmask = net->xfrm.policy_bydst[dir].hmask;
1285 odst = net->xfrm.policy_bydst[dir].table;
Florian Westphalfd709722019-07-02 12:46:00 +02001286 for (i = hmask; i >= 0; i--) {
1287 hlist_for_each_entry_safe(policy, n, odst + i, bydst)
1288 hlist_del_rcu(&policy->bydst);
1289 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001290 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1291 /* dir out => dst = remote, src = local */
1292 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1293 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1294 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1295 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1296 } else {
1297 /* dir in/fwd => dst = local, src = remote */
1298 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1299 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1300 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1301 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1302 }
1303 }
1304
1305 /* re-insert all policies by order of creation */
1306 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal88584c32018-11-27 13:28:54 +01001307 if (policy->walk.dead)
1308 continue;
1309 dir = xfrm_policy_id2dir(policy->index);
1310 if (dir >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +02001311 /* skip socket policies */
1312 continue;
1313 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001314 newpos = NULL;
1315 chain = policy_hash_bysel(net, &policy->selector,
Florian Westphal88584c32018-11-27 13:28:54 +01001316 policy->family, dir);
Florian Westphal1548bc42019-01-04 14:17:02 +01001317
Florian Westphal24969fa2018-11-07 23:00:35 +01001318 if (!chain) {
1319 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1320
1321 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1322 continue;
1323 }
1324
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001325 hlist_for_each_entry(pol, chain, bydst) {
1326 if (policy->priority >= pol->priority)
1327 newpos = &pol->bydst;
1328 else
1329 break;
1330 }
1331 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +02001332 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001333 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001334 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001335 }
1336
Florian Westphal24969fa2018-11-07 23:00:35 +01001337out_unlock:
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001338 __xfrm_policy_inexact_flush(net);
Florian Westphal7a474c32019-01-04 14:17:01 +01001339 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001340 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001341
1342 mutex_unlock(&hash_resize_mutex);
1343}
1344
1345void xfrm_policy_hash_rebuild(struct net *net)
1346{
1347 schedule_work(&net->xfrm.policy_hthresh.work);
1348}
1349EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/* Generate new index... KAME seems to generate them ordered by cost
1352 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +08001353static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 static u32 idx_generator;
1356
1357 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001358 struct hlist_head *list;
1359 struct xfrm_policy *p;
1360 u32 idx;
1361 int found;
1362
Fan Due682adf02013-11-07 17:47:48 +08001363 if (!index) {
1364 idx = (idx_generator | dir);
1365 idx_generator += 8;
1366 } else {
1367 idx = index;
1368 index = 0;
1369 }
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (idx == 0)
1372 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -08001373 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -07001374 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001375 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001376 if (p->index == idx) {
1377 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 break;
David S. Miller2518c7c2006-08-24 04:45:07 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001381 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return idx;
1383 }
1384}
1385
David S. Miller2518c7c2006-08-24 04:45:07 -07001386static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1387{
1388 u32 *p1 = (u32 *) s1;
1389 u32 *p2 = (u32 *) s2;
1390 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1391 int i;
1392
1393 for (i = 0; i < len; i++) {
1394 if (p1[i] != p2[i])
1395 return 1;
1396 }
1397
1398 return 0;
1399}
1400
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001401static void xfrm_policy_requeue(struct xfrm_policy *old,
1402 struct xfrm_policy *new)
1403{
1404 struct xfrm_policy_queue *pq = &old->polq;
1405 struct sk_buff_head list;
1406
Li RongQingde2ad482015-04-30 17:25:19 +08001407 if (skb_queue_empty(&pq->hold_queue))
1408 return;
1409
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001410 __skb_queue_head_init(&list);
1411
1412 spin_lock_bh(&pq->hold_queue.lock);
1413 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001414 if (del_timer(&pq->hold_timer))
1415 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001416 spin_unlock_bh(&pq->hold_queue.lock);
1417
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001418 pq = &new->polq;
1419
1420 spin_lock_bh(&pq->hold_queue.lock);
1421 skb_queue_splice(&list, &pq->hold_queue);
1422 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001423 if (!mod_timer(&pq->hold_timer, jiffies))
1424 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001425 spin_unlock_bh(&pq->hold_queue.lock);
1426}
1427
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001428static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1429 struct xfrm_policy *pol)
1430{
1431 u32 mark = policy->mark.v & policy->mark.m;
1432
1433 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1434 return true;
1435
1436 if ((mark & pol->mark.m) == pol->mark.v &&
1437 policy->priority == pol->priority)
1438 return true;
1439
1440 return false;
1441}
1442
Florian Westphal24969fa2018-11-07 23:00:35 +01001443static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1444{
1445 const struct xfrm_pol_inexact_key *k = data;
1446 u32 a = k->type << 24 | k->dir << 16 | k->family;
1447
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001448 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1449 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +01001450}
1451
1452static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1453{
1454 const struct xfrm_pol_inexact_bin *b = data;
1455
1456 return xfrm_pol_bin_key(&b->k, 0, seed);
1457}
1458
1459static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1460 const void *ptr)
1461{
1462 const struct xfrm_pol_inexact_key *key = arg->key;
1463 const struct xfrm_pol_inexact_bin *b = ptr;
1464 int ret;
1465
1466 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1467 return -1;
1468
1469 ret = b->k.dir ^ key->dir;
1470 if (ret)
1471 return ret;
1472
1473 ret = b->k.type ^ key->type;
1474 if (ret)
1475 return ret;
1476
1477 ret = b->k.family ^ key->family;
1478 if (ret)
1479 return ret;
1480
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001481 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +01001482}
1483
1484static const struct rhashtable_params xfrm_pol_inexact_params = {
1485 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1486 .hashfn = xfrm_pol_bin_key,
1487 .obj_hashfn = xfrm_pol_bin_obj,
1488 .obj_cmpfn = xfrm_pol_bin_cmp,
1489 .automatic_shrinking = true,
1490};
1491
1492static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1493 struct xfrm_policy *policy)
1494{
1495 struct xfrm_policy *pol, *delpol = NULL;
1496 struct hlist_node *newpos = NULL;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001497 int i = 0;
Florian Westphal24969fa2018-11-07 23:00:35 +01001498
1499 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1500 if (pol->type == policy->type &&
1501 pol->if_id == policy->if_id &&
1502 !selector_cmp(&pol->selector, &policy->selector) &&
1503 xfrm_policy_mark_match(policy, pol) &&
1504 xfrm_sec_ctx_match(pol->security, policy->security) &&
1505 !WARN_ON(delpol)) {
1506 delpol = pol;
1507 if (policy->priority > pol->priority)
1508 continue;
1509 } else if (policy->priority >= pol->priority) {
1510 newpos = &pol->bydst_inexact_list;
1511 continue;
1512 }
1513 if (delpol)
1514 break;
1515 }
1516
1517 if (newpos)
1518 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1519 else
1520 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001521
1522 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1523 pol->pos = i;
1524 i++;
1525 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001526}
1527
Florian Westphala927d6af2018-11-07 23:00:33 +01001528static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1529 struct xfrm_policy *policy,
1530 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Florian Westphala927d6af2018-11-07 23:00:33 +01001532 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Sasha Levinb67bfe02013-02-27 17:06:00 -08001534 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001535 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001536 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001537 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001538 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001539 xfrm_sec_ctx_match(pol->security, policy->security) &&
1540 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001541 if (excl)
1542 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 delpol = pol;
1544 if (policy->priority > pol->priority)
1545 continue;
1546 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001547 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 continue;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (delpol)
1551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001555 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001556 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001557 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001558
1559 return delpol;
1560}
1561
1562int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1563{
1564 struct net *net = xp_net(policy);
1565 struct xfrm_policy *delpol;
1566 struct hlist_head *chain;
1567
1568 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1569 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001570 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001571 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001572 else
1573 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001574
1575 if (IS_ERR(delpol)) {
1576 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1577 return PTR_ERR(delpol);
1578 }
1579
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001580 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001581
1582 /* After previous checking, family can either be AF_INET or AF_INET6 */
1583 if (policy->family == AF_INET)
1584 rt_genid_bump_ipv4(net);
1585 else
1586 rt_genid_bump_ipv6(net);
1587
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001588 if (delpol) {
1589 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001590 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001591 }
Fan Due682adf02013-11-07 17:47:48 +08001592 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001593 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001594 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 policy->curlft.use_time = 0;
1596 if (!mod_timer(&policy->timer, jiffies + HZ))
1597 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001598 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David S. Miller9b78a822005-12-22 07:39:48 -08001600 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001602 else if (xfrm_bydst_should_resize(net, dir, NULL))
1603 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return 0;
1606}
1607EXPORT_SYMBOL(xfrm_policy_insert);
1608
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001609static struct xfrm_policy *
1610__xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1611 u8 type, int dir,
1612 struct xfrm_selector *sel,
1613 struct xfrm_sec_ctx *ctx)
1614{
1615 struct xfrm_policy *pol;
1616
1617 if (!chain)
1618 return NULL;
1619
1620 hlist_for_each_entry(pol, chain, bydst) {
1621 if (pol->type == type &&
1622 pol->if_id == if_id &&
1623 (mark & pol->mark.m) == pol->mark.v &&
1624 !selector_cmp(sel, &pol->selector) &&
1625 xfrm_sec_ctx_match(ctx, pol->security))
1626 return pol;
1627 }
1628
1629 return NULL;
1630}
1631
Steffen Klassert7e652642018-06-12 14:07:07 +02001632struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1633 u8 type, int dir,
1634 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001635 struct xfrm_sec_ctx *ctx, int delete,
1636 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Florian Westphal24969fa2018-11-07 23:00:35 +01001638 struct xfrm_pol_inexact_bin *bin = NULL;
1639 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001640 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Eric Parisef41aaa2007-03-07 15:37:58 -08001642 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001643 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001644 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001645 if (!chain) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001646 struct xfrm_pol_inexact_candidates cand;
1647 int i;
1648
Florian Westphal24969fa2018-11-07 23:00:35 +01001649 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001650 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001651 if (!bin) {
1652 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1653 return NULL;
1654 }
1655
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001656 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1657 &sel->saddr,
1658 &sel->daddr)) {
1659 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1660 return NULL;
1661 }
1662
1663 pol = NULL;
1664 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1665 struct xfrm_policy *tmp;
1666
1667 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1668 if_id, type, dir,
1669 sel, ctx);
Florian Westphal39aa6922018-11-15 02:51:57 +01001670 if (!tmp)
1671 continue;
1672
1673 if (!pol || tmp->pos < pol->pos)
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001674 pol = tmp;
1675 }
1676 } else {
1677 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1678 sel, ctx);
Florian Westphal24969fa2018-11-07 23:00:35 +01001679 }
1680
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001681 if (pol) {
1682 xfrm_pol_hold(pol);
1683 if (delete) {
1684 *err = security_xfrm_policy_delete(pol->security);
1685 if (*err) {
1686 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1687 return pol;
David S. Miller2518c7c2006-08-24 04:45:07 -07001688 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001689 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001691 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001693 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001695 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001696 xfrm_policy_kill(ret);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001697 if (bin && delete)
1698 xfrm_policy_inexact_prune_bin(bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
Trent Jaegerdf718372005-12-13 23:12:27 -08001701EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Steffen Klassert7e652642018-06-12 14:07:07 +02001703struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1704 u8 type, int dir, u32 id, int delete,
1705 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
David S. Miller2518c7c2006-08-24 04:45:07 -07001707 struct xfrm_policy *pol, *ret;
1708 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Herbert Xub5505c62007-05-14 02:15:47 -07001710 *err = -ENOENT;
1711 if (xfrm_policy_id2dir(id) != dir)
1712 return NULL;
1713
Eric Parisef41aaa2007-03-07 15:37:58 -08001714 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001715 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001716 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001717 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001718 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001719 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001720 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001721 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001723 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001724 *err = security_xfrm_policy_delete(
1725 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001726 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001727 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001728 return pol;
1729 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001730 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001731 }
1732 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 break;
1734 }
1735 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001736 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001738 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001739 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742EXPORT_SYMBOL(xfrm_policy_byid);
1743
Joy Latten4aa2e622007-06-04 19:05:57 -04001744#ifdef CONFIG_SECURITY_NETWORK_XFRM
1745static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001746xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Florian Westphalceb159e2018-11-07 23:00:32 +01001748 struct xfrm_policy *pol;
1749 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001750
Florian Westphalceb159e2018-11-07 23:00:32 +01001751 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1752 if (pol->walk.dead ||
1753 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1754 pol->type != type)
1755 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001756
Florian Westphalceb159e2018-11-07 23:00:32 +01001757 err = security_xfrm_policy_delete(pol->security);
1758 if (err) {
1759 xfrm_audit_policy_delete(pol, 0, task_valid);
1760 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001761 }
1762 }
1763 return err;
1764}
1765#else
1766static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001767xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001768{
1769 return 0;
1770}
1771#endif
1772
Tetsuo Handa2e710292014-04-22 21:48:30 +09001773int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001774{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001775 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001776 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Florian Westphal9d0380d2016-08-11 15:17:59 +02001778 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001779
Tetsuo Handa2e710292014-04-22 21:48:30 +09001780 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001781 if (err)
1782 goto out;
1783
Florian Westphalceb159e2018-11-07 23:00:32 +01001784again:
1785 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1786 dir = xfrm_policy_id2dir(pol->index);
1787 if (pol->walk.dead ||
1788 dir >= XFRM_POLICY_MAX ||
1789 pol->type != type)
1790 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001791
Florian Westphalceb159e2018-11-07 23:00:32 +01001792 __xfrm_policy_unlink(pol, dir);
1793 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1794 cnt++;
1795 xfrm_audit_policy_delete(pol, 1, task_valid);
1796 xfrm_policy_kill(pol);
1797 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1798 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001800 if (cnt)
1801 __xfrm_policy_inexact_flush(net);
1802 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001803 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001804out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001805 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001806 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808EXPORT_SYMBOL(xfrm_policy_flush);
1809
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001810int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001811 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 void *data)
1813{
Herbert Xu12a169e2008-10-01 07:03:24 -07001814 struct xfrm_policy *pol;
1815 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001816 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Timo Teras4c563f72008-02-28 21:31:08 -08001818 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1819 walk->type != XFRM_POLICY_TYPE_ANY)
1820 return -EINVAL;
1821
Herbert Xu12a169e2008-10-01 07:03:24 -07001822 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001823 return 0;
1824
Florian Westphal9d0380d2016-08-11 15:17:59 +02001825 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001826 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001827 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001828 else
Li RongQing80077702015-04-22 17:09:54 +08001829 x = list_first_entry(&walk->walk.all,
1830 struct xfrm_policy_walk_entry, all);
1831
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001832 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001833 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001834 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001835 pol = container_of(x, struct xfrm_policy, walk);
1836 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1837 walk->type != pol->type)
1838 continue;
1839 error = func(pol, xfrm_policy_id2dir(pol->index),
1840 walk->seq, data);
1841 if (error) {
1842 list_move_tail(&walk->walk.all, &x->all);
1843 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001844 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001845 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001847 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001848 error = -ENOENT;
1849 goto out;
1850 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001851 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001853 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return error;
1855}
1856EXPORT_SYMBOL(xfrm_policy_walk);
1857
Herbert Xu12a169e2008-10-01 07:03:24 -07001858void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1859{
1860 INIT_LIST_HEAD(&walk->walk.all);
1861 walk->walk.dead = 1;
1862 walk->type = type;
1863 walk->seq = 0;
1864}
1865EXPORT_SYMBOL(xfrm_policy_walk_init);
1866
Fan Du283bc9f2013-11-07 17:47:50 +08001867void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001868{
1869 if (list_empty(&walk->walk.all))
1870 return;
1871
Florian Westphal9d0380d2016-08-11 15:17:59 +02001872 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001873 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001874 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001875}
1876EXPORT_SYMBOL(xfrm_policy_walk_done);
1877
James Morris134b0fc2006-10-05 15:42:27 -05001878/*
1879 * Find policy to apply to this flow.
1880 *
1881 * Returns 0 if policy found, else an -errno.
1882 */
David S. Millerf299d552011-02-24 01:23:30 -05001883static int xfrm_policy_match(const struct xfrm_policy *pol,
1884 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001885 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001886{
David S. Millerf299d552011-02-24 01:23:30 -05001887 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001888 int ret = -ESRCH;
1889 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001890
1891 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001892 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001893 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001894 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001895 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001896
1897 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001898 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001899 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001900 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001901 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001902}
1903
Florian Westphal9cf545e2018-11-07 23:00:38 +01001904static struct xfrm_pol_inexact_node *
1905xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
1906 seqcount_t *count,
1907 const xfrm_address_t *addr, u16 family)
1908{
1909 const struct rb_node *parent;
1910 int seq;
1911
1912again:
1913 seq = read_seqcount_begin(count);
1914
1915 parent = rcu_dereference_raw(r->rb_node);
1916 while (parent) {
1917 struct xfrm_pol_inexact_node *node;
1918 int delta;
1919
1920 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1921
1922 delta = xfrm_policy_addr_delta(addr, &node->addr,
1923 node->prefixlen, family);
1924 if (delta < 0) {
1925 parent = rcu_dereference_raw(parent->rb_left);
1926 continue;
1927 } else if (delta > 0) {
1928 parent = rcu_dereference_raw(parent->rb_right);
1929 continue;
1930 }
1931
1932 return node;
1933 }
1934
1935 if (read_seqcount_retry(count, seq))
1936 goto again;
1937
1938 return NULL;
1939}
1940
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001941static bool
1942xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1943 struct xfrm_pol_inexact_bin *b,
1944 const xfrm_address_t *saddr,
1945 const xfrm_address_t *daddr)
1946{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001947 struct xfrm_pol_inexact_node *n;
1948 u16 family;
1949
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001950 if (!b)
1951 return false;
1952
Florian Westphal9cf545e2018-11-07 23:00:38 +01001953 family = b->k.family;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001954 memset(cand, 0, sizeof(*cand));
1955 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001956
1957 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1958 family);
Florian Westphal6ac098b2018-11-07 23:00:41 +01001959 if (n) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001960 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
Florian Westphal6ac098b2018-11-07 23:00:41 +01001961 n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr,
1962 family);
1963 if (n)
1964 cand->res[XFRM_POL_CAND_BOTH] = &n->hhead;
1965 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001966
Florian Westphal64a09a72018-11-07 23:00:40 +01001967 n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
1968 family);
1969 if (n)
1970 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
1971
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001972 return true;
1973}
1974
Florian Westphal24969fa2018-11-07 23:00:35 +01001975static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001976xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1977 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001978{
1979 struct xfrm_pol_inexact_key k = {
1980 .family = family,
1981 .type = type,
1982 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001983 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001984 };
1985
1986 write_pnet(&k.net, net);
1987
1988 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1989 xfrm_pol_inexact_params);
1990}
1991
1992static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001993xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1994 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001995{
1996 struct xfrm_pol_inexact_bin *bin;
1997
1998 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1999
2000 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002001 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01002002 rcu_read_unlock();
2003
2004 return bin;
2005}
2006
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002007static struct xfrm_policy *
2008__xfrm_policy_eval_candidates(struct hlist_head *chain,
2009 struct xfrm_policy *prefer,
2010 const struct flowi *fl,
2011 u8 type, u16 family, int dir, u32 if_id)
2012{
2013 u32 priority = prefer ? prefer->priority : ~0u;
2014 struct xfrm_policy *pol;
2015
2016 if (!chain)
2017 return NULL;
2018
2019 hlist_for_each_entry_rcu(pol, chain, bydst) {
2020 int err;
2021
2022 if (pol->priority > priority)
2023 break;
2024
2025 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
2026 if (err) {
2027 if (err != -ESRCH)
2028 return ERR_PTR(err);
2029
2030 continue;
2031 }
2032
2033 if (prefer) {
2034 /* matches. Is it older than *prefer? */
2035 if (pol->priority == priority &&
2036 prefer->pos < pol->pos)
2037 return prefer;
2038 }
2039
2040 return pol;
2041 }
2042
2043 return NULL;
2044}
2045
2046static struct xfrm_policy *
2047xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
2048 struct xfrm_policy *prefer,
2049 const struct flowi *fl,
2050 u8 type, u16 family, int dir, u32 if_id)
2051{
2052 struct xfrm_policy *tmp;
2053 int i;
2054
2055 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
2056 tmp = __xfrm_policy_eval_candidates(cand->res[i],
2057 prefer,
2058 fl, type, family, dir,
2059 if_id);
2060 if (!tmp)
2061 continue;
2062
2063 if (IS_ERR(tmp))
2064 return tmp;
2065 prefer = tmp;
2066 }
2067
2068 return prefer;
2069}
2070
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002071static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08002072 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07002073 u16 family, u8 dir,
2074 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002076 struct xfrm_pol_inexact_candidates cand;
David S. Miller0b597e72011-02-24 01:22:48 -05002077 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01002078 struct xfrm_pol_inexact_bin *bin;
2079 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07002080 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02002081 unsigned int sequence;
Florian Westphal24969fa2018-11-07 23:00:35 +01002082 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07002083
2084 daddr = xfrm_flowi_daddr(fl, family);
2085 saddr = xfrm_flowi_saddr(fl, family);
2086 if (unlikely(!daddr || !saddr))
2087 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Florian Westphala7c442472016-08-11 15:17:56 +02002089 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02002090 retry:
2091 do {
2092 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
2093 chain = policy_hash_direct(net, daddr, saddr, family, dir);
2094 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
2095
David S. Miller2518c7c2006-08-24 04:45:07 -07002096 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02002097 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07002098 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05002099 if (err) {
2100 if (err == -ESRCH)
2101 continue;
2102 else {
2103 ret = ERR_PTR(err);
2104 goto fail;
2105 }
2106 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07002107 ret = pol;
David S. Milleracba48e2006-08-25 15:46:46 -07002108 break;
2109 }
2110 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002111 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002112 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
2113 daddr))
Florian Westphal24969fa2018-11-07 23:00:35 +01002114 goto skip_inexact;
Li RongQing8faf4912015-05-14 11:16:59 +08002115
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002116 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
2117 family, dir, if_id);
2118 if (pol) {
2119 ret = pol;
2120 if (IS_ERR(pol))
2121 goto fail;
David S. Miller2518c7c2006-08-24 04:45:07 -07002122 }
Li RongQing586f2eb2015-04-30 17:13:41 +08002123
Florian Westphal24969fa2018-11-07 23:00:35 +01002124skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02002125 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
2126 goto retry;
2127
Florian Westphale37cc8ad2016-08-11 15:17:55 +02002128 if (ret && !xfrm_pol_hold_rcu(ret))
2129 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05002130fail:
Florian Westphala7c442472016-08-11 15:17:56 +02002131 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002132
David S. Miller2518c7c2006-08-24 04:45:07 -07002133 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002134}
2135
Benedict Wongbc56b332018-07-19 10:50:44 -07002136static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
2137 const struct flowi *fl,
2138 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002139{
2140#ifdef CONFIG_XFRM_SUB_POLICY
2141 struct xfrm_policy *pol;
2142
Benedict Wongbc56b332018-07-19 10:50:44 -07002143 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2144 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002145 if (pol != NULL)
2146 return pol;
2147#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07002148 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2149 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002150}
2151
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002152static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07002153 const struct flowi *fl,
2154 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 struct xfrm_policy *pol;
2157
Eric Dumazetd188ba82015-12-08 07:22:02 -08002158 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02002159 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002160 pol = rcu_dereference(sk->sk_policy[dir]);
2161 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01002162 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002163 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08002164
Steffen Klassertddc47e42017-11-29 06:53:55 +01002165 if (pol->family != family) {
2166 pol = NULL;
2167 goto out;
2168 }
2169
2170 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002171 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02002172 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07002173 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002174 pol = NULL;
2175 goto out;
2176 }
Paul Moore03e1ad72008-04-12 19:07:52 -07002177 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05002178 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02002179 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01002180 if (!err) {
2181 if (!xfrm_pol_hold_rcu(pol))
2182 goto again;
2183 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002184 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01002185 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002186 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01002187 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002188 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 pol = NULL;
2190 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002191out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002192 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return pol;
2194}
2195
2196static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2197{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002198 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002199
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002200 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002201 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 xfrm_pol_hold(pol);
2203}
2204
2205static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2206 int dir)
2207{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002208 struct net *net = xp_net(pol);
2209
Herbert Xu53c2e282014-11-13 17:09:49 +08002210 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07002211 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Herbert Xu53c2e282014-11-13 17:09:49 +08002213 /* Socket policies are not hashed. */
2214 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02002215 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01002216 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08002217 hlist_del(&pol->byidx);
2218 }
2219
2220 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002221 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07002222
2223 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Herbert Xu53c2e282014-11-13 17:09:49 +08002226static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2227{
2228 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2229}
2230
2231static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2232{
2233 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2234}
2235
Herbert Xu4666faa2005-06-18 22:43:22 -07002236int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Fan Du283bc9f2013-11-07 17:47:50 +08002238 struct net *net = xp_net(pol);
2239
Florian Westphal9d0380d2016-08-11 15:17:59 +02002240 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002242 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07002245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Herbert Xu4666faa2005-06-18 22:43:22 -07002247 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
David S. Millera70fcb02006-03-20 19:18:52 -08002249EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2252{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002253 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct xfrm_policy *old_pol;
2255
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002256#ifdef CONFIG_XFRM_SUB_POLICY
2257 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2258 return -EINVAL;
2259#endif
2260
Florian Westphal9d0380d2016-08-11 15:17:59 +02002261 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08002262 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2263 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02002265 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08002266 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08002267 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08002269 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002270 if (old_pol) {
2271 if (pol)
2272 xfrm_policy_requeue(old_pol, pol);
2273
Timo Teräsea2dea92010-03-31 00:17:05 +00002274 /* Unlinking succeeds always. This is the only function
2275 * allowed to delete or replace socket policy.
2276 */
Herbert Xu53c2e282014-11-13 17:09:49 +08002277 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002278 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02002279 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 if (old_pol) {
2282 xfrm_policy_kill(old_pol);
2283 }
2284 return 0;
2285}
2286
David S. Millerd3e40a92011-02-24 01:25:41 -05002287static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002289 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08002290 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 if (newp) {
2293 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07002294 if (security_xfrm_policy_clone(old->security,
2295 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08002296 kfree(newp);
2297 return NULL; /* ENOMEM */
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 newp->lft = old->lft;
2300 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08002301 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02002302 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 newp->action = old->action;
2304 newp->flags = old->flags;
2305 newp->xfrm_nr = old->xfrm_nr;
2306 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002307 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11002308 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 memcpy(newp->xfrm_vec, old->xfrm_vec,
2310 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02002311 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08002312 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002313 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 xfrm_pol_put(newp);
2315 }
2316 return newp;
2317}
2318
Eric Dumazetd188ba82015-12-08 07:22:02 -08002319int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Eric Dumazetd188ba82015-12-08 07:22:02 -08002321 const struct xfrm_policy *p;
2322 struct xfrm_policy *np;
2323 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Eric Dumazetd188ba82015-12-08 07:22:02 -08002325 rcu_read_lock();
2326 for (i = 0; i < 2; i++) {
2327 p = rcu_dereference(osk->sk_policy[i]);
2328 if (p) {
2329 np = clone_policy(p, i);
2330 if (unlikely(!np)) {
2331 ret = -ENOMEM;
2332 break;
2333 }
2334 rcu_assign_pointer(sk->sk_policy[i], np);
2335 }
2336 }
2337 rcu_read_unlock();
2338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002341static int
David Ahern42a7b322015-08-10 16:58:11 -06002342xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002343 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002344{
2345 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01002346 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002347
2348 if (unlikely(afinfo == NULL))
2349 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002350 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002351 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002352 return err;
2353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/* Resolve list of templates for the flow, given policy. */
2356
2357static int
David S. Millera6c2e612011-02-22 18:35:39 -08002358xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2359 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08002361 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int nx;
2363 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01002364 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2365 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002366 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Weilong Chen9b7a7872013-12-24 09:43:46 +08002368 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01002370 xfrm_address_t *remote = daddr;
2371 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2373
Steffen Klassert94802152017-11-15 06:40:57 +01002374 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2375 tmpl->mode == XFRM_MODE_BEET) {
2376 remote = &tmpl->id.daddr;
2377 local = &tmpl->saddr;
2378 if (xfrm_addr_any(local, tmpl->encap_family)) {
2379 error = xfrm_get_saddr(net, fl->flowi_oif,
2380 &tmp, remote,
2381 tmpl->encap_family, 0);
2382 if (error)
2383 goto fail;
2384 local = &tmp;
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Benedict Wongbc56b332018-07-19 10:50:44 -07002388 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2389 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 if (x && x->km.state == XFRM_STATE_VALID) {
2392 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01002393 daddr = remote;
2394 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 continue;
2396 }
2397 if (x) {
2398 error = (x->km.state == XFRM_STATE_ERROR ?
2399 -EINVAL : -EAGAIN);
2400 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08002401 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00002402 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (!tmpl->optional)
2406 goto fail;
2407 }
2408 return nx;
2409
2410fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002411 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 xfrm_state_put(xfrm[nx]);
2413 return error;
2414}
2415
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002416static int
David S. Millera6c2e612011-02-22 18:35:39 -08002417xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2418 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002419{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002420 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2421 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002422 int cnx = 0;
2423 int error;
2424 int ret;
2425 int i;
2426
2427 for (i = 0; i < npols; i++) {
2428 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2429 error = -ENOBUFS;
2430 goto fail;
2431 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002432
2433 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002434 if (ret < 0) {
2435 error = ret;
2436 goto fail;
2437 } else
2438 cnx += ret;
2439 }
2440
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002441 /* found states are sorted for outbound processing */
2442 if (npols > 1)
2443 xfrm_state_sort(xfrm, tpp, cnx, family);
2444
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002445 return cnx;
2446
2447 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002448 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002449 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002450 return error;
2451
2452}
2453
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002454static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002455{
Florian Westphalf24ea522019-04-16 16:44:37 +02002456 if (family == AF_INET)
2457 return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08002458
Florian Westphalf24ea522019-04-16 16:44:37 +02002459 return 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002460}
2461
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002462static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002463{
Florian Westphal37b10382017-02-07 15:00:19 +01002464 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002465 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08002466 struct xfrm_dst *xdst;
2467
2468 if (!afinfo)
2469 return ERR_PTR(-EINVAL);
2470
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002471 switch (family) {
2472 case AF_INET:
2473 dst_ops = &net->xfrm.xfrm4_dst_ops;
2474 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002475#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002476 case AF_INET6:
2477 dst_ops = &net->xfrm.xfrm6_dst_ops;
2478 break;
2479#endif
2480 default:
2481 BUG();
2482 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07002483 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08002484
Madalin Bucurd4cae562011-09-26 07:04:36 +00002485 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00002486 struct dst_entry *dst = &xdst->u.dst;
2487
2488 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00002489 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08002490 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00002491
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002492 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00002493
Herbert Xu25ee3282007-12-11 09:32:34 -08002494 return xdst;
2495}
2496
Florian Westphal2e8b4aa2019-04-16 16:44:38 +02002497static void xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2498 int nfheader_len)
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002499{
Florian Westphal2e8b4aa2019-04-16 16:44:38 +02002500 if (dst->ops->family == AF_INET6) {
2501 struct rt6_info *rt = (struct rt6_info *)dst;
2502 path->path_cookie = rt6_get_cookie(rt);
2503 path->u.rt6.rt6i_nfheader_len = nfheader_len;
2504 }
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002505}
2506
Herbert Xu87c1e122010-03-02 02:51:56 +00002507static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08002508 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08002509{
Florian Westphal37b10382017-02-07 15:00:19 +01002510 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08002511 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2512 int err;
2513
2514 if (!afinfo)
2515 return -EINVAL;
2516
Herbert Xu87c1e122010-03-02 02:51:56 +00002517 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002518
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002519 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002520
2521 return err;
2522}
2523
Timo Teräs80c802f2010-04-07 00:30:05 +00002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525/* Allocate chain of dst_entry's, attach known xfrm's, calculate
2526 * all the metrics... Shortly, bundle a bundle.
2527 */
2528
Herbert Xu25ee3282007-12-11 09:32:34 -08002529static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05002530 struct xfrm_state **xfrm,
2531 struct xfrm_dst **bundle,
2532 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08002533 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08002534 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Florian Westphal733a5fa2019-03-29 21:16:30 +01002536 const struct xfrm_state_afinfo *afinfo;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002537 const struct xfrm_mode *inner_mode;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002538 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08002539 unsigned long now = jiffies;
2540 struct net_device *dev;
David Miller45b018be2017-11-28 15:40:28 -05002541 struct xfrm_dst *xdst_prev = NULL;
2542 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08002543 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08002545 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002546 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002547 int trailer_len = 0;
2548 int tos;
2549 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09002550 xfrm_address_t saddr, daddr;
2551
2552 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002553
2554 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002555
2556 dst_hold(dst);
2557
2558 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002559 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002560 struct dst_entry *dst1 = &xdst->u.dst;
2561
2562 err = PTR_ERR(xdst);
2563 if (IS_ERR(xdst)) {
2564 dst_release(dst);
2565 goto put_states;
2566 }
2567
David Miller54920932017-11-28 15:41:01 -05002568 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05002569 if (!xdst_prev)
2570 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07002571 else
2572 /* Ref count is taken during xfrm_alloc_dst()
2573 * No need to do dst_clone() on dst1
2574 */
David Miller45b018be2017-11-28 15:40:28 -05002575 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07002576
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002577 if (xfrm[i]->sel.family == AF_UNSPEC) {
2578 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2579 xfrm_af2proto(family));
2580 if (!inner_mode) {
2581 err = -EAFNOSUPPORT;
2582 dst_release(dst);
2583 goto put_states;
2584 }
2585 } else
Florian Westphalc9500d72019-03-29 21:16:32 +01002586 inner_mode = &xfrm[i]->inner_mode;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002587
Herbert Xu25ee3282007-12-11 09:32:34 -08002588 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08002589 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08002590
2591 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Benedict Wonge2612cd2019-01-14 11:24:38 -08002592 __u32 mark = 0;
2593
2594 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
2595 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002596
Herbert Xu25ee3282007-12-11 09:32:34 -08002597 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06002598 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002599 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08002600 err = PTR_ERR(dst);
2601 if (IS_ERR(dst))
2602 goto put_states;
2603 } else
2604 dst_hold(dst);
2605
2606 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00002607 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08002608
David S. Millerf5b0a872012-07-19 12:31:33 -07002609 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08002610 dst1->flags |= DST_HOST;
2611 dst1->lastuse = now;
2612
2613 dst1->input = dst_discard;
Florian Westphal733a5fa2019-03-29 21:16:30 +01002614
2615 rcu_read_lock();
2616 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
2617 if (likely(afinfo))
2618 dst1->output = afinfo->output;
2619 else
2620 dst1->output = dst_discard_out;
2621 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002622
David Miller45b018be2017-11-28 15:40:28 -05002623 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002624
2625 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002626 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2627 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002628 trailer_len += xfrm[i]->props.trailer_len;
2629 }
2630
David Miller45b018be2017-11-28 15:40:28 -05002631 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002632 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002633
2634 err = -ENODEV;
2635 dev = dst->dev;
2636 if (!dev)
2637 goto free_dst;
2638
David Miller45b018be2017-11-28 15:40:28 -05002639 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002640 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002641
David Miller45b018be2017-11-28 15:40:28 -05002642 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2643 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2644 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002645 if (err)
2646 goto free_dst;
2647
David Miller45b018be2017-11-28 15:40:28 -05002648 xdst_prev->u.dst.header_len = header_len;
2649 xdst_prev->u.dst.trailer_len = trailer_len;
2650 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2651 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002652 }
2653
David Miller45b018be2017-11-28 15:40:28 -05002654 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002655
2656put_states:
2657 for (; i < nx; i++)
2658 xfrm_state_put(xfrm[i]);
2659free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002660 if (xdst0)
2661 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002662
2663 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
David S. Miller73ff93c2011-02-22 18:33:42 -08002666static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002667 struct xfrm_policy **pols,
2668 int *num_pols, int *num_xfrms)
2669{
2670 int i;
2671
2672 if (*num_pols == 0 || !pols[0]) {
2673 *num_pols = 0;
2674 *num_xfrms = 0;
2675 return 0;
2676 }
2677 if (IS_ERR(pols[0]))
2678 return PTR_ERR(pols[0]);
2679
2680 *num_xfrms = pols[0]->xfrm_nr;
2681
2682#ifdef CONFIG_XFRM_SUB_POLICY
2683 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2684 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2685 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2686 XFRM_POLICY_TYPE_MAIN,
2687 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002688 XFRM_POLICY_OUT,
2689 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002690 if (pols[1]) {
2691 if (IS_ERR(pols[1])) {
2692 xfrm_pols_put(pols, *num_pols);
2693 return PTR_ERR(pols[1]);
2694 }
Weilong Chen02d08922013-12-24 09:43:48 +08002695 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002696 (*num_xfrms) += pols[1]->xfrm_nr;
2697 }
2698 }
2699#endif
2700 for (i = 0; i < *num_pols; i++) {
2701 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2702 *num_xfrms = -1;
2703 break;
2704 }
2705 }
2706
2707 return 0;
2708
2709}
2710
2711static struct xfrm_dst *
2712xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002713 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002714 struct dst_entry *dst_orig)
2715{
2716 struct net *net = xp_net(pols[0]);
2717 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002718 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002719 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002720 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002721 int err;
2722
2723 /* Try to instantiate a bundle */
2724 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002725 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002726 if (err == 0)
2727 return NULL;
2728
2729 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002730 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2731 return ERR_PTR(err);
2732 }
2733
David Miller54920932017-11-28 15:41:01 -05002734 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002735 if (IS_ERR(dst)) {
2736 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2737 return ERR_CAST(dst);
2738 }
2739
2740 xdst = (struct xfrm_dst *)dst;
2741 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002742 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002743 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002744 xdst->policy_genid = atomic_read(&pols[0]->genid);
2745
2746 return xdst;
2747}
2748
Kees Cookc3aed702017-10-16 17:28:56 -07002749static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002750{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002751 struct sk_buff *skb;
2752 struct sock *sk;
2753 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002754 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002755 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002756 struct xfrm_policy_queue *pq = &pol->polq;
2757 struct flowi fl;
2758 struct sk_buff_head list;
2759
2760 spin_lock(&pq->hold_queue.lock);
2761 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002762 if (!skb) {
2763 spin_unlock(&pq->hold_queue.lock);
2764 goto out;
2765 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002766 dst = skb_dst(skb);
2767 sk = skb->sk;
2768 xfrm_decode_session(skb, &fl, dst->ops->family);
2769 spin_unlock(&pq->hold_queue.lock);
2770
David Miller0f6c4802017-11-28 15:40:46 -05002771 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c982018-02-01 11:26:12 +01002772 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002773 if (IS_ERR(dst))
2774 goto purge_queue;
2775
2776 if (dst->flags & DST_XFRM_QUEUE) {
2777 dst_release(dst);
2778
2779 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2780 goto purge_queue;
2781
2782 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002783 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2784 xfrm_pol_hold(pol);
Colin Ian King7759d6a2018-11-13 14:28:42 +00002785 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002786 }
2787
2788 dst_release(dst);
2789
2790 __skb_queue_head_init(&list);
2791
2792 spin_lock(&pq->hold_queue.lock);
2793 pq->timeout = 0;
2794 skb_queue_splice_init(&pq->hold_queue, &list);
2795 spin_unlock(&pq->hold_queue.lock);
2796
2797 while (!skb_queue_empty(&list)) {
2798 skb = __skb_dequeue(&list);
2799
2800 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002801 dst_hold(xfrm_dst_path(skb_dst(skb)));
2802 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002803 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002804 kfree_skb(skb);
2805 continue;
2806 }
2807
2808 nf_reset(skb);
2809 skb_dst_drop(skb);
2810 skb_dst_set(skb, dst);
2811
Eric W. Biederman13206b62015-10-07 16:48:35 -05002812 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002813 }
2814
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002815out:
2816 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002817 return;
2818
2819purge_queue:
2820 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002821 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002822 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002823}
2824
Eric W. Biedermanede20592015-10-07 16:48:47 -05002825static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002826{
2827 unsigned long sched_next;
2828 struct dst_entry *dst = skb_dst(skb);
2829 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002830 struct xfrm_policy *pol = xdst->pols[0];
2831 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002832
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002833 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002834 kfree_skb(skb);
2835 return 0;
2836 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002837
2838 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2839 kfree_skb(skb);
2840 return -EAGAIN;
2841 }
2842
2843 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002844
2845 spin_lock_bh(&pq->hold_queue.lock);
2846
2847 if (!pq->timeout)
2848 pq->timeout = XFRM_QUEUE_TMO_MIN;
2849
2850 sched_next = jiffies + pq->timeout;
2851
2852 if (del_timer(&pq->hold_timer)) {
2853 if (time_before(pq->hold_timer.expires, sched_next))
2854 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002855 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002856 }
2857
2858 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002859 if (!mod_timer(&pq->hold_timer, sched_next))
2860 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002861
2862 spin_unlock_bh(&pq->hold_queue.lock);
2863
2864 return 0;
2865}
2866
2867static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002868 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002869 const struct flowi *fl,
2870 int num_xfrms,
2871 u16 family)
2872{
2873 int err;
2874 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002875 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002876 struct dst_entry *dst1;
2877 struct xfrm_dst *xdst;
2878
2879 xdst = xfrm_alloc_dst(net, family);
2880 if (IS_ERR(xdst))
2881 return xdst;
2882
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002883 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2884 net->xfrm.sysctl_larval_drop ||
2885 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002886 return xdst;
2887
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002888 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002889 dst1 = &xdst->u.dst;
2890 dst_hold(dst);
2891 xdst->route = dst;
2892
2893 dst_copy_metrics(dst1, dst);
2894
2895 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2896 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2897 dst1->lastuse = jiffies;
2898
2899 dst1->input = dst_discard;
2900 dst1->output = xdst_queue_output;
2901
2902 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002903 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002904 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002905
2906 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2907
2908 err = -ENODEV;
2909 dev = dst->dev;
2910 if (!dev)
2911 goto free_dst;
2912
2913 err = xfrm_fill_dst(xdst, dev, fl);
2914 if (err)
2915 goto free_dst;
2916
2917out:
2918 return xdst;
2919
2920free_dst:
2921 dst_release(dst1);
2922 xdst = ERR_PTR(err);
2923 goto out;
2924}
2925
Benedict Wongbc56b332018-07-19 10:50:44 -07002926static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2927 const struct flowi *fl,
2928 u16 family, u8 dir,
2929 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002930{
Timo Teräs80c802f2010-04-07 00:30:05 +00002931 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002932 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002933 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002934
Timo Teräs80c802f2010-04-07 00:30:05 +00002935 /* Resolve policies to use if we couldn't get them from
2936 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002937 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002938 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002939 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002940 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002941 if (err < 0)
2942 goto inc_error;
2943 if (num_pols == 0)
2944 return NULL;
2945 if (num_xfrms <= 0)
2946 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002947
Florian Westphalbd45c532017-07-17 13:57:25 +02002948 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002949 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002950 if (IS_ERR(xdst)) {
2951 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002952 if (err == -EREMOTE) {
2953 xfrm_pols_put(pols, num_pols);
2954 return NULL;
2955 }
2956
Timo Teräs80c802f2010-04-07 00:30:05 +00002957 if (err != -EAGAIN)
2958 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002959 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002960 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002961 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002962 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002963 }
2964
Florian Westphalbd45c532017-07-17 13:57:25 +02002965 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002966
2967make_dummy_bundle:
2968 /* We found policies, but there's no bundles to instantiate:
2969 * either because the policy blocks, has no transformations or
2970 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002971 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002972 if (IS_ERR(xdst)) {
2973 xfrm_pols_put(pols, num_pols);
2974 return ERR_CAST(xdst);
2975 }
2976 xdst->num_pols = num_pols;
2977 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002978 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002979
Florian Westphalbd45c532017-07-17 13:57:25 +02002980 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002981
2982inc_error:
2983 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2984error:
Florian Westphal855dad92017-07-17 13:57:22 +02002985 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002986 return ERR_PTR(err);
2987}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
David S. Miller2774c132011-03-01 14:59:04 -08002989static struct dst_entry *make_blackhole(struct net *net, u16 family,
2990 struct dst_entry *dst_orig)
2991{
Florian Westphal37b10382017-02-07 15:00:19 +01002992 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002993 struct dst_entry *ret;
2994
2995 if (!afinfo) {
2996 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002997 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002998 } else {
2999 ret = afinfo->blackhole_route(net, dst_orig);
3000 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003001 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08003002
3003 return ret;
3004}
3005
Benedict Wongbc56b332018-07-19 10:50:44 -07003006/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 *
3008 * At the moment we eat a raw IP route. Mostly to speed up lookups
3009 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07003010 *
3011 * xfrm_lookup uses an if_id of 0 by default, and is provided for
3012 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
Benedict Wongbc56b332018-07-19 10:50:44 -07003014struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
3015 struct dst_entry *dst_orig,
3016 const struct flowi *fl,
3017 const struct sock *sk,
3018 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003020 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00003021 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08003022 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00003023 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02003024 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00003025 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003026
Timo Teräs80c802f2010-04-07 00:30:05 +00003027 dst = NULL;
3028 xdst = NULL;
3029 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003030
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003031 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07003032 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003033 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003034 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
3035 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00003036 err = xfrm_expand_policies(fl, family, pols,
3037 &num_pols, &num_xfrms);
3038 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08003039 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00003040
3041 if (num_pols) {
3042 if (num_xfrms <= 0) {
3043 drop_pols = num_pols;
3044 goto no_transform;
3045 }
3046
3047 xdst = xfrm_resolve_and_create_bundle(
3048 pols, num_pols, fl,
3049 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01003050
Timo Teräs80c802f2010-04-07 00:30:05 +00003051 if (IS_ERR(xdst)) {
3052 xfrm_pols_put(pols, num_pols);
3053 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02003054 if (err == -EREMOTE)
3055 goto nopol;
3056
Timo Teräs80c802f2010-04-07 00:30:05 +00003057 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00003058 } else if (xdst == NULL) {
3059 num_xfrms = 0;
3060 drop_pols = num_pols;
3061 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00003062 }
3063
Timo Teräs80c802f2010-04-07 00:30:05 +00003064 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003065 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Timo Teräs80c802f2010-04-07 00:30:05 +00003068 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003069 struct xfrm_flo xflo;
3070
3071 xflo.dst_orig = dst_orig;
3072 xflo.flags = flags;
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07003075 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08003076 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08003077 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Benedict Wongbc56b332018-07-19 10:50:44 -07003079 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02003080 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00003081 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02003082 if (IS_ERR(xdst)) {
3083 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003084 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08003085 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003086
3087 num_pols = xdst->num_pols;
3088 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003089 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00003090 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092
Timo Teräs80c802f2010-04-07 00:30:05 +00003093 dst = &xdst->u.dst;
3094 if (route == NULL && num_xfrms > 0) {
3095 /* The only case when xfrm_bundle_lookup() returns a
3096 * bundle with null route, is when the template could
3097 * not be resolved. It means policies are there, but
3098 * bundle could not be created, since we don't yet
3099 * have the xfrm_state's. We need to wait for KM to
3100 * negotiate new SA's or bail out with error.*/
3101 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003102 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01003103 err = -EREMOTE;
3104 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003105 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003106
Steffen Klassert5b8ef342013-08-27 13:43:30 +02003107 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00003108
3109 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3110 goto error;
3111 }
3112
3113no_transform:
3114 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08003115 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Timo Teräs80c802f2010-04-07 00:30:05 +00003117 if ((flags & XFRM_LOOKUP_ICMP) &&
3118 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3119 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08003120 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003121 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08003122
Timo Teräs80c802f2010-04-07 00:30:05 +00003123 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02003124 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08003125
Timo Teräs80c802f2010-04-07 00:30:05 +00003126 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003128 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07003129 err = -EPERM;
3130 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003131 } else if (num_xfrms > 0) {
3132 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00003133 dst_release(dst_orig);
3134 } else {
3135 /* Flow passes untransformed */
3136 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003137 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003139ok:
3140 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003141 if (dst && dst->xfrm &&
3142 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3143 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003144 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Timo Teräs80c802f2010-04-07 00:30:05 +00003146nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003147 if (!(flags & XFRM_LOOKUP_ICMP)) {
3148 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003149 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003150 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003151 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003153 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003154dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003155 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3156 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003157 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003158 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
Benedict Wongbc56b332018-07-19 10:50:44 -07003160EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3161
3162/* Main function: finds/creates a bundle for given flow.
3163 *
3164 * At the moment we eat a raw IP route. Mostly to speed up lookups
3165 * on interfaces with disabled IPsec.
3166 */
3167struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3168 const struct flowi *fl, const struct sock *sk,
3169 int flags)
3170{
3171 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173EXPORT_SYMBOL(xfrm_lookup);
3174
Steffen Klassertf92ee612014-09-16 10:08:40 +02003175/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3176 * Otherwise we may send out blackholed packets.
3177 */
3178struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3179 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003180 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003181{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003182 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003183 flags | XFRM_LOOKUP_QUEUE |
3184 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003185
3186 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
3187 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3188
Tommi Rantala8cc88772018-06-21 09:30:47 +03003189 if (IS_ERR(dst))
3190 dst_release(dst_orig);
3191
Steffen Klassertf92ee612014-09-16 10:08:40 +02003192 return dst;
3193}
3194EXPORT_SYMBOL(xfrm_lookup_route);
3195
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003196static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003197xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003198{
Florian Westphal2294be0f2018-12-18 17:15:20 +01003199 struct sec_path *sp = skb_sec_path(skb);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003200 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003201
Florian Westphal2294be0f2018-12-18 17:15:20 +01003202 if (!sp || idx < 0 || idx >= sp->len)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003203 return 0;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003204 x = sp->xvec[idx];
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003205 if (!x->type->reject)
3206 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003207 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003208}
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210/* When skb is transformed back to its "native" form, we have to
3211 * check policy restrictions. At the moment we make this in maximally
3212 * stupid way. Shame on me. :-) Of course, connected sockets must
3213 * have policy cached at them.
3214 */
3215
3216static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003217xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 unsigned short family)
3219{
3220 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003221 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return x->id.proto == tmpl->id.proto &&
3223 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3224 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3225 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003226 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003227 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003228 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3229 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003232/*
3233 * 0 or more than 0 is returned when validation is succeeded (either bypass
3234 * because of optional transport mode, or next index of the mathced secpath
3235 * state with the template.
3236 * -1 is returned when no matching template is found.
3237 * Otherwise "-2 - errored_index" is returned.
3238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003240xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 unsigned short family)
3242{
3243 int idx = start;
3244
3245 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003246 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return start;
3248 } else
3249 start = -1;
3250 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003251 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003253 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3254 if (start == -1)
3255 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
3259 return start;
3260}
3261
Florian Westphalc53ac412019-04-16 16:44:39 +02003262static void
3263decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse)
3264{
3265 const struct iphdr *iph = ip_hdr(skb);
Florian Westphal858e5402019-05-16 11:28:16 +02003266 int ihl = iph->ihl;
3267 u8 *xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003268 struct flowi4 *fl4 = &fl->u.ip4;
3269 int oif = 0;
3270
3271 if (skb_dst(skb))
3272 oif = skb_dst(skb)->dev->ifindex;
3273
3274 memset(fl4, 0, sizeof(struct flowi4));
3275 fl4->flowi4_mark = skb->mark;
3276 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
3277
Florian Westphal858e5402019-05-16 11:28:16 +02003278 fl4->flowi4_proto = iph->protocol;
3279 fl4->daddr = reverse ? iph->saddr : iph->daddr;
3280 fl4->saddr = reverse ? iph->daddr : iph->saddr;
3281 fl4->flowi4_tos = iph->tos;
3282
Florian Westphalc53ac412019-04-16 16:44:39 +02003283 if (!ip_is_fragment(iph)) {
3284 switch (iph->protocol) {
3285 case IPPROTO_UDP:
3286 case IPPROTO_UDPLITE:
3287 case IPPROTO_TCP:
3288 case IPPROTO_SCTP:
3289 case IPPROTO_DCCP:
3290 if (xprth + 4 < skb->data ||
3291 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3292 __be16 *ports;
3293
Florian Westphal858e5402019-05-16 11:28:16 +02003294 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003295 ports = (__be16 *)xprth;
3296
3297 fl4->fl4_sport = ports[!!reverse];
3298 fl4->fl4_dport = ports[!reverse];
3299 }
3300 break;
3301 case IPPROTO_ICMP:
3302 if (xprth + 2 < skb->data ||
3303 pskb_may_pull(skb, xprth + 2 - skb->data)) {
3304 u8 *icmp;
3305
Florian Westphal858e5402019-05-16 11:28:16 +02003306 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003307 icmp = xprth;
3308
3309 fl4->fl4_icmp_type = icmp[0];
3310 fl4->fl4_icmp_code = icmp[1];
3311 }
3312 break;
3313 case IPPROTO_ESP:
3314 if (xprth + 4 < skb->data ||
3315 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3316 __be32 *ehdr;
3317
Florian Westphal858e5402019-05-16 11:28:16 +02003318 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003319 ehdr = (__be32 *)xprth;
3320
3321 fl4->fl4_ipsec_spi = ehdr[0];
3322 }
3323 break;
3324 case IPPROTO_AH:
3325 if (xprth + 8 < skb->data ||
3326 pskb_may_pull(skb, xprth + 8 - skb->data)) {
3327 __be32 *ah_hdr;
3328
Florian Westphal858e5402019-05-16 11:28:16 +02003329 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003330 ah_hdr = (__be32 *)xprth;
3331
3332 fl4->fl4_ipsec_spi = ah_hdr[1];
3333 }
3334 break;
3335 case IPPROTO_COMP:
3336 if (xprth + 4 < skb->data ||
3337 pskb_may_pull(skb, xprth + 4 - skb->data)) {
3338 __be16 *ipcomp_hdr;
3339
Florian Westphal858e5402019-05-16 11:28:16 +02003340 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003341 ipcomp_hdr = (__be16 *)xprth;
3342
3343 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
3344 }
3345 break;
3346 case IPPROTO_GRE:
3347 if (xprth + 12 < skb->data ||
3348 pskb_may_pull(skb, xprth + 12 - skb->data)) {
3349 __be16 *greflags;
3350 __be32 *gre_hdr;
3351
Florian Westphal858e5402019-05-16 11:28:16 +02003352 xprth = skb_network_header(skb) + ihl * 4;
Florian Westphalc53ac412019-04-16 16:44:39 +02003353 greflags = (__be16 *)xprth;
3354 gre_hdr = (__be32 *)xprth;
3355
3356 if (greflags[0] & GRE_KEY) {
3357 if (greflags[0] & GRE_CSUM)
3358 gre_hdr++;
3359 fl4->fl4_gre_key = gre_hdr[1];
3360 }
3361 }
3362 break;
3363 default:
3364 fl4->fl4_ipsec_spi = 0;
3365 break;
3366 }
3367 }
Florian Westphalc53ac412019-04-16 16:44:39 +02003368}
3369
3370#if IS_ENABLED(CONFIG_IPV6)
3371static void
3372decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse)
3373{
3374 struct flowi6 *fl6 = &fl->u.ip6;
3375 int onlyproto = 0;
3376 const struct ipv6hdr *hdr = ipv6_hdr(skb);
3377 u32 offset = sizeof(*hdr);
3378 struct ipv6_opt_hdr *exthdr;
3379 const unsigned char *nh = skb_network_header(skb);
3380 u16 nhoff = IP6CB(skb)->nhoff;
3381 int oif = 0;
3382 u8 nexthdr;
3383
3384 if (!nhoff)
3385 nhoff = offsetof(struct ipv6hdr, nexthdr);
3386
3387 nexthdr = nh[nhoff];
3388
3389 if (skb_dst(skb))
3390 oif = skb_dst(skb)->dev->ifindex;
3391
3392 memset(fl6, 0, sizeof(struct flowi6));
3393 fl6->flowi6_mark = skb->mark;
3394 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
3395
3396 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
3397 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
3398
3399 while (nh + offset + sizeof(*exthdr) < skb->data ||
3400 pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
3401 nh = skb_network_header(skb);
3402 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3403
3404 switch (nexthdr) {
3405 case NEXTHDR_FRAGMENT:
3406 onlyproto = 1;
3407 /* fall through */
3408 case NEXTHDR_ROUTING:
3409 case NEXTHDR_HOP:
3410 case NEXTHDR_DEST:
3411 offset += ipv6_optlen(exthdr);
3412 nexthdr = exthdr->nexthdr;
3413 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
3414 break;
3415 case IPPROTO_UDP:
3416 case IPPROTO_UDPLITE:
3417 case IPPROTO_TCP:
3418 case IPPROTO_SCTP:
3419 case IPPROTO_DCCP:
3420 if (!onlyproto && (nh + offset + 4 < skb->data ||
3421 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
3422 __be16 *ports;
3423
3424 nh = skb_network_header(skb);
3425 ports = (__be16 *)(nh + offset);
3426 fl6->fl6_sport = ports[!!reverse];
3427 fl6->fl6_dport = ports[!reverse];
3428 }
3429 fl6->flowi6_proto = nexthdr;
3430 return;
3431 case IPPROTO_ICMPV6:
3432 if (!onlyproto && (nh + offset + 2 < skb->data ||
3433 pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
3434 u8 *icmp;
3435
3436 nh = skb_network_header(skb);
3437 icmp = (u8 *)(nh + offset);
3438 fl6->fl6_icmp_type = icmp[0];
3439 fl6->fl6_icmp_code = icmp[1];
3440 }
3441 fl6->flowi6_proto = nexthdr;
3442 return;
3443#if IS_ENABLED(CONFIG_IPV6_MIP6)
3444 case IPPROTO_MH:
3445 offset += ipv6_optlen(exthdr);
3446 if (!onlyproto && (nh + offset + 3 < skb->data ||
3447 pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
3448 struct ip6_mh *mh;
3449
3450 nh = skb_network_header(skb);
3451 mh = (struct ip6_mh *)(nh + offset);
3452 fl6->fl6_mh_type = mh->ip6mh_type;
3453 }
3454 fl6->flowi6_proto = nexthdr;
3455 return;
3456#endif
3457 /* XXX Why are there these headers? */
3458 case IPPROTO_AH:
3459 case IPPROTO_ESP:
3460 case IPPROTO_COMP:
3461 default:
3462 fl6->fl6_ipsec_spi = 0;
3463 fl6->flowi6_proto = nexthdr;
3464 return;
3465 }
3466 }
3467}
3468#endif
3469
Herbert Xud5422ef2007-12-12 10:44:16 -08003470int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3471 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Florian Westphalc53ac412019-04-16 16:44:39 +02003473 switch (family) {
3474 case AF_INET:
3475 decode_session4(skb, fl, reverse);
3476 break;
3477#if IS_ENABLED(CONFIG_IPV6)
3478 case AF_INET6:
3479 decode_session6(skb, fl, reverse);
3480 break;
3481#endif
3482 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 return -EAFNOSUPPORT;
Florian Westphalc53ac412019-04-16 16:44:39 +02003484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Florian Westphalc53ac412019-04-16 16:44:39 +02003486 return security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487}
Herbert Xud5422ef2007-12-12 10:44:16 -08003488EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
David S. Miller9a7386e2011-02-24 01:44:12 -05003490static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003493 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003494 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498
3499 return 0;
3500}
3501
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003502int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 unsigned short family)
3504{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003505 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003507 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3508 int npols = 0;
3509 int xfrm_nr;
3510 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003511 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003513 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003514 const struct xfrm_if_cb *ifcb;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003515 struct sec_path *sp;
Benedict Wongbc56b332018-07-19 10:50:44 -07003516 struct xfrm_if *xi;
3517 u32 if_id = 0;
3518
3519 rcu_read_lock();
3520 ifcb = xfrm_if_get_cb();
3521
3522 if (ifcb) {
Martin Willi025c65e2019-03-26 13:20:43 +01003523 xi = ifcb->decode_session(skb, family);
Tobias Brunner660899d2019-02-18 10:49:39 +01003524 if (xi) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003525 if_id = xi->p.if_id;
Tobias Brunner660899d2019-02-18 10:49:39 +01003526 net = xi->net;
3527 }
Benedict Wongbc56b332018-07-19 10:50:44 -07003528 }
3529 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Herbert Xud5422ef2007-12-12 10:44:16 -08003531 reverse = dir & ~XFRM_POLICY_MASK;
3532 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003533
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003534 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003535 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003537 }
3538
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003539 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 /* First, check used SA against their selectors. */
Florian Westphal2294be0f2018-12-18 17:15:20 +01003542 sp = skb_sec_path(skb);
3543 if (sp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 int i;
3545
Florian Westphal2294be0f2018-12-18 17:15:20 +01003546 for (i = sp->len - 1; i >= 0; i--) {
3547 struct xfrm_state *x = sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003548 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003549 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
3553 }
3554
3555 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003556 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003557 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003558 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003559 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003560 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003561 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003562 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003565 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003566 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003568 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003569 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003570 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003571 }
James Morris134b0fc2006-10-05 15:42:27 -05003572
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003573 if (!pol) {
Florian Westphal2294be0f2018-12-18 17:15:20 +01003574 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003575 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003576 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003577 return 0;
3578 }
3579 return 1;
3580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Arnd Bergmann386c5682018-07-11 12:19:13 +02003582 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003584 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003585 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003586#ifdef CONFIG_XFRM_SUB_POLICY
3587 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003588 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003589 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003590 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003591 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003592 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003593 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003594 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003595 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003596 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003597 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003598 }
3599 }
3600#endif
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 if (pol->action == XFRM_POLICY_ALLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003604 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003605 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003606 struct xfrm_tmpl **tpp = tp;
3607 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 int i, k;
3609
Florian Westphal2294be0f2018-12-18 17:15:20 +01003610 sp = skb_sec_path(skb);
3611 if (!sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 sp = &dummy;
3613
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003614 for (pi = 0; pi < npols; pi++) {
3615 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003616 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003617 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003618 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003619 }
3620 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003621 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003622 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003623 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003624 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3625 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3626 }
3627 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003628 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08003629 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003630 tpp = stp;
3631 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /* For each tunnel xfrm, find the first matching tmpl.
3634 * For each tmpl before that, find corresponding xfrm.
3635 * Order is _important_. Later we will implement
3636 * some barriers, but at the moment barriers
3637 * are implied between each two transformations.
3638 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003639 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3640 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003641 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003642 if (k < -1)
3643 /* "-2 - errored_index" returned */
3644 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003645 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003650 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003651 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003655 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return 1;
3657 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003658 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
3660reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003661 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003662reject_error:
3663 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return 0;
3665}
3666EXPORT_SYMBOL(__xfrm_policy_check);
3667
3668int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3669{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003670 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003672 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003673 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003675 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003676 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003680 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02003681 if (!skb_dst(skb)) {
3682 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3683 return 0;
3684 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003685
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003686 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003687 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003688 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003689 dst = NULL;
3690 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003691 skb_dst_set(skb, dst);
3692 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694EXPORT_SYMBOL(__xfrm_route_forward);
3695
David S. Millerd49c73c2006-08-13 18:55:53 -07003696/* Optimize later using cookies and generation ids. */
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3699{
David S. Millerd49c73c2006-08-13 18:55:53 -07003700 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003701 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3702 * get validated by dst_ops->check on every use. We do this
3703 * because when a normal route referenced by an XFRM dst is
3704 * obsoleted we do not go looking around for all parent
3705 * referencing XFRM dsts so that we can invalidate them. It
3706 * is just too much work. Instead we make the checks here on
3707 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003708 *
3709 * XFRM dst A --> IPv4 dst X
3710 *
3711 * X is the "xdst->route" of A (X is also the "dst->path" of A
3712 * in this example). If X is marked obsolete, "A" will not
3713 * notice. That's what we are validating here via the
3714 * stale_bundle() check.
3715 *
Wei Wang52df1572017-06-17 10:42:38 -07003716 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3717 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003718 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003719 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003720 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003721 if (dst->obsolete < 0 && !stale_bundle(dst))
3722 return dst;
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return NULL;
3725}
3726
3727static int stale_bundle(struct dst_entry *dst)
3728{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003729 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
3731
Herbert Xuaabc9762005-05-03 16:27:10 -07003732void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
David Millerb92cf4a2017-11-28 15:40:22 -05003734 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003735 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003736 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 dev_put(dev);
3738 }
3739}
Herbert Xuaabc9762005-05-03 16:27:10 -07003740EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
3742static void xfrm_link_failure(struct sk_buff *skb)
3743{
3744 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
3747static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3748{
3749 if (dst) {
3750 if (dst->obsolete) {
3751 dst_release(dst);
3752 dst = NULL;
3753 }
3754 }
3755 return dst;
3756}
3757
David Miller54920932017-11-28 15:41:01 -05003758static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
David Miller54920932017-11-28 15:41:01 -05003760 while (nr--) {
3761 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003763 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
David Miller54920932017-11-28 15:41:01 -05003765 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003766 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 xdst->child_mtu_cached = pmtu;
3768
3769 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3770
3771 route_mtu_cached = dst_mtu(xdst->route);
3772 xdst->route_mtu_cached = route_mtu_cached;
3773
3774 if (pmtu > route_mtu_cached)
3775 pmtu = route_mtu_cached;
3776
David S. Millerdefb3512010-12-08 21:16:57 -08003777 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
3780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781/* Check that the bundle accepts the flow and its components are
3782 * still valid.
3783 */
3784
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003785static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
David Miller54920932017-11-28 15:41:01 -05003787 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003789 struct xfrm_dst *xdst;
3790 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 u32 mtu;
3792
David Miller0f6c4802017-11-28 15:40:46 -05003793 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 (dst->dev && !netif_running(dst->dev)))
3795 return 0;
3796
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003797 if (dst->flags & DST_XFRM_QUEUE)
3798 return 1;
3799
David Miller54920932017-11-28 15:41:01 -05003800 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 do {
3802 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3805 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003806 if (xdst->xfrm_genid != dst->xfrm->genid)
3807 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003808 if (xdst->num_pols > 0 &&
3809 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
David Miller54920932017-11-28 15:41:01 -05003812 bundle[nr++] = xdst;
3813
David Millerb92cf4a2017-11-28 15:40:22 -05003814 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003816 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 xdst->child_mtu_cached = mtu;
3818 }
3819
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003820 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return 0;
3822 mtu = dst_mtu(xdst->route);
3823 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003824 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 xdst->route_mtu_cached = mtu;
3826 }
3827
David Millerb92cf4a2017-11-28 15:40:22 -05003828 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 } while (dst->xfrm);
3830
David Miller54920932017-11-28 15:41:01 -05003831 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return 1;
3833
David Miller54920932017-11-28 15:41:01 -05003834 xdst = bundle[start_from - 1];
3835 mtu = xdst->child_mtu_cached;
3836 while (start_from--) {
3837 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003840 if (mtu > xdst->route_mtu_cached)
3841 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003842 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003843 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 break;
3845
David Miller54920932017-11-28 15:41:01 -05003846 xdst = bundle[start_from - 1];
3847 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
3849
3850 return 1;
3851}
3852
David S. Miller0dbaee32010-12-13 12:52:14 -08003853static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3854{
David Miller0f6c4802017-11-28 15:40:46 -05003855 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003856}
3857
Steffen Klassertebb762f2011-11-23 02:12:51 +00003858static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003859{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003860 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3861
David Miller0f6c4802017-11-28 15:40:46 -05003862 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003863}
3864
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003865static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3866 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003867{
David Miller0f6c4802017-11-28 15:40:46 -05003868 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003869 const struct xfrm_state *xfrm = dst->xfrm;
3870
Steffen Klassert013cb812018-02-19 07:44:07 +01003871 dst = xfrm_dst_child(dst);
3872
Julian Anastasov63fca652017-02-06 23:14:15 +02003873 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3874 continue;
3875 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3876 daddr = xfrm->coaddr;
3877 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3878 daddr = &xfrm->id.daddr;
3879 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003880 return daddr;
3881}
3882
3883static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3884 struct sk_buff *skb,
3885 const void *daddr)
3886{
David Miller0f6c4802017-11-28 15:40:46 -05003887 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003888
3889 if (!skb)
3890 daddr = xfrm_get_dst_nexthop(dst, daddr);
3891 return path->ops->neigh_lookup(path, skb, daddr);
3892}
3893
3894static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3895{
David Miller0f6c4802017-11-28 15:40:46 -05003896 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003897
3898 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003899 path->ops->confirm_neigh(path, daddr);
3900}
3901
Florian Westphala2817d82017-02-07 15:00:17 +01003902int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
3904 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003905
3906 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003908
Eric Dumazetef8531b2012-08-19 12:31:48 +02003909 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003910 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003911 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 else {
3913 struct dst_ops *dst_ops = afinfo->dst_ops;
3914 if (likely(dst_ops->kmem_cachep == NULL))
3915 dst_ops->kmem_cachep = xfrm_dst_cache;
3916 if (likely(dst_ops->check == NULL))
3917 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003918 if (likely(dst_ops->default_advmss == NULL))
3919 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003920 if (likely(dst_ops->mtu == NULL))
3921 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 if (likely(dst_ops->negative_advice == NULL))
3923 dst_ops->negative_advice = xfrm_negative_advice;
3924 if (likely(dst_ops->link_failure == NULL))
3925 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003926 if (likely(dst_ops->neigh_lookup == NULL))
3927 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003928 if (likely(!dst_ops->confirm_neigh))
3929 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003930 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003932 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 return err;
3935}
3936EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3937
Florian Westphala2817d82017-02-07 15:00:17 +01003938void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
Florian Westphal2b619972017-02-07 15:00:15 +01003940 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003941 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003942
Florian Westphala2817d82017-02-07 15:00:17 +01003943 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3944 if (xfrm_policy_afinfo[i] != afinfo)
3945 continue;
3946 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3947 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Florian Westphal2b619972017-02-07 15:00:15 +01003950 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
Florian Westphal2b619972017-02-07 15:00:15 +01003952 dst_ops->kmem_cachep = NULL;
3953 dst_ops->check = NULL;
3954 dst_ops->negative_advice = NULL;
3955 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3958
Steffen Klassertf203b762018-06-12 14:07:12 +02003959void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3960{
3961 spin_lock(&xfrm_if_cb_lock);
3962 rcu_assign_pointer(xfrm_if_cb, ifcb);
3963 spin_unlock(&xfrm_if_cb_lock);
3964}
3965EXPORT_SYMBOL(xfrm_if_register_cb);
3966
3967void xfrm_if_unregister_cb(void)
3968{
3969 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3970 synchronize_rcu();
3971}
3972EXPORT_SYMBOL(xfrm_if_unregister_cb);
3973
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003974#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003975static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003976{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003977 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003978 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3979 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003980 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003981 rv = xfrm_proc_init(net);
3982 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003983 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003984 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003985}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003986
3987static void xfrm_statistics_fini(struct net *net)
3988{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003989 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003990 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003991}
3992#else
3993static int __net_init xfrm_statistics_init(struct net *net)
3994{
3995 return 0;
3996}
3997
3998static void xfrm_statistics_fini(struct net *net)
3999{
4000}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08004001#endif
4002
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004003static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
David S. Miller2518c7c2006-08-24 04:45:07 -07004005 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01004006 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07004007
Florian Westphal24969fa2018-11-07 23:00:35 +01004008 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004009 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07004011 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09004012 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01004013 err = rhashtable_init(&xfrm_policy_inexact_table,
4014 &xfrm_pol_inexact_params);
4015 BUG_ON(err);
4016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
David S. Miller2518c7c2006-08-24 04:45:07 -07004018 hmask = 8 - 1;
4019 sz = (hmask+1) * sizeof(struct hlist_head);
4020
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004021 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
4022 if (!net->xfrm.policy_byidx)
4023 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004024 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07004025
Herbert Xu53c2e282014-11-13 17:09:49 +08004026 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07004027 struct xfrm_policy_hash *htab;
4028
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08004029 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08004030 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004031 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07004032
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004033 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07004034 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07004035 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004036 goto out_bydst;
4037 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02004038 htab->dbits4 = 32;
4039 htab->sbits4 = 32;
4040 htab->dbits6 = 128;
4041 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07004042 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004043 net->xfrm.policy_hthresh.lbits4 = 32;
4044 net->xfrm.policy_hthresh.rbits4 = 32;
4045 net->xfrm.policy_hthresh.lbits6 = 128;
4046 net->xfrm.policy_hthresh.rbits6 = 128;
4047
4048 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07004049
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004050 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01004051 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08004052 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02004053 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004054 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004055
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004056out_bydst:
4057 for (dir--; dir >= 0; dir--) {
4058 struct xfrm_policy_hash *htab;
4059
4060 htab = &net->xfrm.policy_bydst[dir];
4061 xfrm_hash_free(htab->table, sz);
4062 }
4063 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004064out_byidx:
4065 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004068static void xfrm_policy_fini(struct net *net)
4069{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004070 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004071 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004072 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004073
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004074 flush_work(&net->xfrm.policy_hash_work);
4075#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09004076 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004077#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09004078 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08004079
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08004080 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004081
Herbert Xu53c2e282014-11-13 17:09:49 +08004082 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004083 struct xfrm_policy_hash *htab;
4084
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004085 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004086
4087 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01004088 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08004089 WARN_ON(!hlist_empty(htab->table));
4090 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08004091 }
4092
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08004093 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08004094 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
4095 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01004096
Florian Westphal6be3b0d2018-11-07 23:00:37 +01004097 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
4098 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
4099 __xfrm_policy_inexact_prune_bin(b, true);
4100 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004101}
4102
4103static int __net_init xfrm_net_init(struct net *net)
4104{
4105 int rv;
4106
Florian Westphalc2822222017-02-08 11:52:29 +01004107 /* Initialize the per-net locks here */
4108 spin_lock_init(&net->xfrm.xfrm_state_lock);
4109 spin_lock_init(&net->xfrm.xfrm_policy_lock);
4110 mutex_init(&net->xfrm.xfrm_cfg_mutex);
4111
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004112 rv = xfrm_statistics_init(net);
4113 if (rv < 0)
4114 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004115 rv = xfrm_state_init(net);
4116 if (rv < 0)
4117 goto out_state;
4118 rv = xfrm_policy_init(net);
4119 if (rv < 0)
4120 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004121 rv = xfrm_sysctl_init(net);
4122 if (rv < 0)
4123 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08004124
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004125 return 0;
4126
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004127out_sysctl:
4128 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004129out_policy:
4130 xfrm_state_fini(net);
4131out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004132 xfrm_statistics_fini(net);
4133out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004134 return rv;
4135}
4136
4137static void __net_exit xfrm_net_exit(struct net *net)
4138{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08004139 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004140 xfrm_policy_fini(net);
4141 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08004142 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004143}
4144
4145static struct pernet_operations __net_initdata xfrm_net_ops = {
4146 .init = xfrm_net_init,
4147 .exit = xfrm_net_exit,
4148};
4149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150void __init xfrm_init(void)
4151{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08004152 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03004153 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02004154 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02004156
4157 RCU_INIT_POINTER(xfrm_if_cb, NULL);
4158 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
Joy Lattenab5f5e82007-09-17 11:51:22 -07004161#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08004162static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
4163 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004164{
Paul Moore875179f2007-12-01 23:27:18 +11004165 struct xfrm_sec_ctx *ctx = xp->security;
4166 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004167
Paul Moore875179f2007-12-01 23:27:18 +11004168 if (ctx)
4169 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
4170 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
4171
Weilong Chen9b7a7872013-12-24 09:43:46 +08004172 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07004173 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07004174 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004175 if (sel->prefixlen_s != 32)
4176 audit_log_format(audit_buf, " src_prefixlen=%d",
4177 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07004178 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11004179 if (sel->prefixlen_d != 32)
4180 audit_log_format(audit_buf, " dst_prefixlen=%d",
4181 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004182 break;
4183 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004184 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004185 if (sel->prefixlen_s != 128)
4186 audit_log_format(audit_buf, " src_prefixlen=%d",
4187 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07004188 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11004189 if (sel->prefixlen_d != 128)
4190 audit_log_format(audit_buf, " dst_prefixlen=%d",
4191 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004192 break;
4193 }
4194}
4195
Tetsuo Handa2e710292014-04-22 21:48:30 +09004196void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004197{
4198 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004199
Paul Mooreafeb14b2007-12-21 14:58:11 -08004200 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004201 if (audit_buf == NULL)
4202 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004203 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004204 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004205 xfrm_audit_common_policyinfo(xp, audit_buf);
4206 audit_log_end(audit_buf);
4207}
4208EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
4209
Paul Moore68277ac2007-12-20 20:49:33 -08004210void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09004211 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004212{
4213 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004214
Paul Mooreafeb14b2007-12-21 14:58:11 -08004215 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004216 if (audit_buf == NULL)
4217 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004218 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004219 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004220 xfrm_audit_common_policyinfo(xp, audit_buf);
4221 audit_log_end(audit_buf);
4222}
4223EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
4224#endif
4225
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004226#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04004227static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
4228 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004229{
4230 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
4231 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004232 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
4233 sel_cmp->family) &&
4234 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
4235 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004236 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
4237 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004238 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004239 }
4240 } else {
4241 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004242 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004243 }
4244 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04004245 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004246}
4247
Weilong Chen3e94c2d2013-12-24 09:43:47 +08004248static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
4249 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004250{
4251 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004252 struct hlist_head *chain;
4253 u32 priority = ~0U;
4254
Florian Westphal9d0380d2016-08-11 15:17:59 +02004255 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08004256 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004257 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004258 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4259 pol->type == type) {
4260 ret = pol;
4261 priority = ret->priority;
4262 break;
4263 }
4264 }
Fan Du8d549c42013-11-07 17:47:49 +08004265 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01004266 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08004267 if ((pol->priority >= priority) && ret)
4268 break;
4269
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004270 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08004271 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004272 ret = pol;
4273 break;
4274 }
4275 }
4276
Li RongQing586f2eb2015-04-30 17:13:41 +08004277 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004278
Florian Westphal9d0380d2016-08-11 15:17:59 +02004279 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004280
4281 return ret;
4282}
4283
David S. Millerdd701752011-02-24 00:21:08 -05004284static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004285{
4286 int match = 0;
4287
4288 if (t->mode == m->mode && t->id.proto == m->proto &&
4289 (m->reqid == 0 || t->reqid == m->reqid)) {
4290 switch (t->mode) {
4291 case XFRM_MODE_TUNNEL:
4292 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004293 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
4294 m->old_family) &&
4295 xfrm_addr_equal(&t->saddr, &m->old_saddr,
4296 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004297 match = 1;
4298 }
4299 break;
4300 case XFRM_MODE_TRANSPORT:
4301 /* in case of transport mode, template does not store
4302 any IP addresses, hence we just compare mode and
4303 protocol */
4304 match = 1;
4305 break;
4306 default:
4307 break;
4308 }
4309 }
4310 return match;
4311}
4312
4313/* update endpoint address(es) of template(s) */
4314static int xfrm_policy_migrate(struct xfrm_policy *pol,
4315 struct xfrm_migrate *m, int num_migrate)
4316{
4317 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004318 int i, j, n = 0;
4319
4320 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07004321 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004322 /* target policy has been deleted */
4323 write_unlock_bh(&pol->lock);
4324 return -ENOENT;
4325 }
4326
4327 for (i = 0; i < pol->xfrm_nr; i++) {
4328 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4329 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4330 continue;
4331 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07004332 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4333 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004334 continue;
4335 /* update endpoints */
4336 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4337 sizeof(pol->xfrm_vec[i].id.daddr));
4338 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4339 sizeof(pol->xfrm_vec[i].saddr));
4340 pol->xfrm_vec[i].encap_family = mp->new_family;
4341 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00004342 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004343 }
4344 }
4345
4346 write_unlock_bh(&pol->lock);
4347
4348 if (!n)
4349 return -ENODATA;
4350
4351 return 0;
4352}
4353
David S. Millerdd701752011-02-24 00:21:08 -05004354static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004355{
4356 int i, j;
4357
4358 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4359 return -EINVAL;
4360
4361 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004362 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4363 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4364 return -EINVAL;
4365
4366 /* check if there is any duplicated entry */
4367 for (j = i + 1; j < num_migrate; j++) {
4368 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4369 sizeof(m[i].old_daddr)) &&
4370 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4371 sizeof(m[i].old_saddr)) &&
4372 m[i].proto == m[j].proto &&
4373 m[i].mode == m[j].mode &&
4374 m[i].reqid == m[j].reqid &&
4375 m[i].old_family == m[j].old_family)
4376 return -EINVAL;
4377 }
4378 }
4379
4380 return 0;
4381}
4382
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004383int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004384 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004385 struct xfrm_kmaddress *k, struct net *net,
4386 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004387{
4388 int i, err, nx_cur = 0, nx_new = 0;
4389 struct xfrm_policy *pol = NULL;
4390 struct xfrm_state *x, *xc;
4391 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4392 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4393 struct xfrm_migrate *mp;
4394
Vladis Dronov7bab0962017-08-02 19:50:14 +02004395 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004396 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4397 goto out;
4398
Vladis Dronov7bab0962017-08-02 19:50:14 +02004399 if (dir >= XFRM_POLICY_MAX) {
4400 err = -EINVAL;
4401 goto out;
4402 }
4403
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004404 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004405 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004406 err = -ENOENT;
4407 goto out;
4408 }
4409
4410 /* Stage 2 - find and update state(s) */
4411 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004412 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004413 x_cur[nx_cur] = x;
4414 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004415 xc = xfrm_state_migrate(x, mp, encap);
4416 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004417 x_new[nx_new] = xc;
4418 nx_new++;
4419 } else {
4420 err = -ENODATA;
4421 goto restore_state;
4422 }
4423 }
4424 }
4425
4426 /* Stage 3 - update policy */
4427 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4428 goto restore_state;
4429
4430 /* Stage 4 - delete old state(s) */
4431 if (nx_cur) {
4432 xfrm_states_put(x_cur, nx_cur);
4433 xfrm_states_delete(x_cur, nx_cur);
4434 }
4435
4436 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004437 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004438
4439 xfrm_pol_put(pol);
4440
4441 return 0;
4442out:
4443 return err;
4444
4445restore_state:
4446 if (pol)
4447 xfrm_pol_put(pol);
4448 if (nx_cur)
4449 xfrm_states_put(x_cur, nx_cur);
4450 if (nx_new)
4451 xfrm_states_delete(x_new, nx_new);
4452
4453 return err;
4454}
David S. Millere610e672007-02-08 13:29:15 -08004455EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004456#endif