blob: 38e33326c85675029f2253994fbbc69f756e3d6e [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Florian Westphalec30d782017-07-17 13:57:27 +020027#include <linux/cpu.h>
Paul Moore68277ac2007-12-20 20:49:33 -080028#include <linux/audit.h>
Florian Westphal24969fa2018-11-07 23:00:35 +010029#include <linux/rhashtable.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080030#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050031#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/xfrm.h>
33#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080034#ifdef CONFIG_XFRM_STATISTICS
35#include <net/snmp.h>
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David S. Miller44e36b42006-08-24 04:50:50 -070038#include "xfrm_hash.h"
39
Steffen Klasserta0073fe2013-02-05 12:52:55 +010040#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
41#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
42#define XFRM_MAX_QUEUE_LEN 100
43
Steffen Klassertb8c203b2014-09-16 10:08:49 +020044struct xfrm_flo {
45 struct dst_entry *dst_orig;
46 u8 flags;
47};
48
Florian Westphal6be3b0d2018-11-07 23:00:37 +010049/* prefixes smaller than this are stored in lists, not trees. */
50#define INEXACT_PREFIXLEN_IPV4 16
51#define INEXACT_PREFIXLEN_IPV6 48
Florian Westphal9cf545e2018-11-07 23:00:38 +010052
53struct xfrm_pol_inexact_node {
54 struct rb_node node;
55 union {
56 xfrm_address_t addr;
57 struct rcu_head rcu;
58 };
59 u8 prefixlen;
60
61 /* the policies matching this node, can be empty list */
62 struct hlist_head hhead;
63};
64
65/* xfrm inexact policy search tree:
66 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
67 * |
68 * +---- root_d: sorted by daddr:prefix
69 * | |
70 * | xfrm_pol_inexact_node
71 * | |
72 * | +- coarse policies and all any:daddr policies
73 * |
Florian Westphal64a09a72018-11-07 23:00:40 +010074 * +---- root_s: sorted by saddr:prefix
75 * | |
76 * | xfrm_pol_inexact_node
77 * | |
78 * | + root: unused
79 * | |
80 * | + hhead: saddr:any policies
81 * |
Florian Westphal9cf545e2018-11-07 23:00:38 +010082 * +---- coarse policies and all any:any policies
83 *
Florian Westphal64a09a72018-11-07 23:00:40 +010084 * Lookups return three candidate lists:
Florian Westphal9cf545e2018-11-07 23:00:38 +010085 * 1. any:any list from top-level xfrm_pol_inexact_bin
86 * 2. any:daddr list from daddr tree
Florian Westphal64a09a72018-11-07 23:00:40 +010087 * 2. saddr:any list from saddr tree
Florian Westphal9cf545e2018-11-07 23:00:38 +010088 *
89 * This result set then needs to be searched for the policy with
90 * the lowest priority. If two results have same prio, youngest one wins.
91 */
92
Florian Westphal24969fa2018-11-07 23:00:35 +010093struct xfrm_pol_inexact_key {
94 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +010095 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +010096 u16 family;
97 u8 dir, type;
98};
99
100struct xfrm_pol_inexact_bin {
101 struct xfrm_pol_inexact_key k;
102 struct rhash_head head;
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100103 /* list containing '*:*' policies */
Florian Westphal24969fa2018-11-07 23:00:35 +0100104 struct hlist_head hhead;
105
Florian Westphal9cf545e2018-11-07 23:00:38 +0100106 seqcount_t count;
107 /* tree sorted by daddr/prefix */
108 struct rb_root root_d;
109
Florian Westphal64a09a72018-11-07 23:00:40 +0100110 /* tree sorted by saddr/prefix */
111 struct rb_root root_s;
112
Florian Westphal24969fa2018-11-07 23:00:35 +0100113 /* slow path below */
114 struct list_head inexact_bins;
115 struct rcu_head rcu;
116};
117
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100118enum xfrm_pol_inexact_candidate_type {
Florian Westphal64a09a72018-11-07 23:00:40 +0100119 XFRM_POL_CAND_SADDR,
Florian Westphal9cf545e2018-11-07 23:00:38 +0100120 XFRM_POL_CAND_DADDR,
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100121 XFRM_POL_CAND_ANY,
122
123 XFRM_POL_CAND_MAX,
124};
125
126struct xfrm_pol_inexact_candidates {
127 struct hlist_head *res[XFRM_POL_CAND_MAX];
128};
129
Steffen Klassertf203b762018-06-12 14:07:12 +0200130static DEFINE_SPINLOCK(xfrm_if_cb_lock);
131static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
132
Priyanka Jain418a99a2012-08-12 21:22:29 +0000133static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +0100134static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +0000135 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +0300137static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +0200138static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Florian Westphal24969fa2018-11-07 23:00:35 +0100140static struct rhashtable xfrm_policy_inexact_table;
141static const struct rhashtable_params xfrm_pol_inexact_params;
142
David Miller54920932017-11-28 15:41:01 -0500143static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +0000144static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +0000145static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -0700146static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800148static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800149static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
150 int dir);
151
Florian Westphal24969fa2018-11-07 23:00:35 +0100152static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100153xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
154 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100155
156static struct xfrm_pol_inexact_bin *
157xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100158 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100159static struct xfrm_policy *
160xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
161 bool excl);
162static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
163 struct xfrm_policy *policy);
164
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100165static bool
166xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
167 struct xfrm_pol_inexact_bin *b,
168 const xfrm_address_t *saddr,
169 const xfrm_address_t *daddr);
170
Florian Westphale37cc8a2016-08-11 15:17:55 +0200171static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
172{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300173 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8a2016-08-11 15:17:55 +0200174}
175
David S. Millerbc9b35a2012-05-15 15:04:57 -0400176static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500177__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800178{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500179 const struct flowi4 *fl4 = &fl->u.ip4;
180
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000181 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
182 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500183 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
184 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
185 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
186 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800187}
188
David S. Millerbc9b35a2012-05-15 15:04:57 -0400189static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500190__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800191{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500192 const struct flowi6 *fl6 = &fl->u.ip6;
193
194 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
195 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
196 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
197 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
198 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
199 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800200}
201
David S. Millerbc9b35a2012-05-15 15:04:57 -0400202bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
203 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800204{
205 switch (family) {
206 case AF_INET:
207 return __xfrm4_selector_match(sel, fl);
208 case AF_INET6:
209 return __xfrm6_selector_match(sel, fl);
210 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400211 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800212}
213
Florian Westphala2817d82017-02-07 15:00:17 +0100214static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200215{
Florian Westphala2817d82017-02-07 15:00:17 +0100216 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200217
Florian Westphala2817d82017-02-07 15:00:17 +0100218 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200219 return NULL;
220 rcu_read_lock();
221 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
222 if (unlikely(!afinfo))
223 rcu_read_unlock();
224 return afinfo;
225}
226
Steffen Klassertf203b762018-06-12 14:07:12 +0200227/* Called with rcu_read_lock(). */
228static const struct xfrm_if_cb *xfrm_if_get_cb(void)
229{
230 return rcu_dereference(xfrm_if_cb);
231}
232
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200233struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
234 const xfrm_address_t *saddr,
235 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900236 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Florian Westphal37b10382017-02-07 15:00:19 +0100238 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800239 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Herbert Xu25ee3282007-12-11 09:32:34 -0800241 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800243 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900245 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900246
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100247 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900248
249 return dst;
250}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200251EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900252
David Ahern42a7b322015-08-10 16:58:11 -0600253static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
254 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900255 xfrm_address_t *prev_saddr,
256 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900257 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900258{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800259 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900260 xfrm_address_t *saddr = &x->props.saddr;
261 xfrm_address_t *daddr = &x->id.daddr;
262 struct dst_entry *dst;
263
264 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
265 saddr = x->coaddr;
266 daddr = prev_daddr;
267 }
268 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
269 saddr = prev_saddr;
270 daddr = x->coaddr;
271 }
272
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900273 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900274
275 if (!IS_ERR(dst)) {
276 if (prev_saddr != saddr)
277 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
278 if (prev_daddr != daddr)
279 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
280 }
281
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800282 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static inline unsigned long make_jiffies(long secs)
286{
287 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
288 return MAX_SCHEDULE_TIMEOUT-1;
289 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900290 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Kees Cookc3aed702017-10-16 17:28:56 -0700293static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Kees Cookc3aed702017-10-16 17:28:56 -0700295 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200296 time64_t now = ktime_get_real_seconds();
297 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int warn = 0;
299 int dir;
300
301 read_lock(&xp->lock);
302
Timo Teräsea2dea92010-03-31 00:17:05 +0000303 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 goto out;
305
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700306 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200309 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 xp->curlft.add_time - now;
311 if (tmo <= 0)
312 goto expired;
313 if (tmo < next)
314 next = tmo;
315 }
316 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200317 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
319 if (tmo <= 0)
320 goto expired;
321 if (tmo < next)
322 next = tmo;
323 }
324 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200325 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xp->curlft.add_time - now;
327 if (tmo <= 0) {
328 warn = 1;
329 tmo = XFRM_KM_TIMEOUT;
330 }
331 if (tmo < next)
332 next = tmo;
333 }
334 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200335 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
337 if (tmo <= 0) {
338 warn = 1;
339 tmo = XFRM_KM_TIMEOUT;
340 }
341 if (tmo < next)
342 next = tmo;
343 }
344
345 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800346 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200347 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
349 xfrm_pol_hold(xp);
350
351out:
352 read_unlock(&xp->lock);
353 xfrm_pol_put(xp);
354 return;
355
356expired:
357 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700358 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800359 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 xfrm_pol_put(xp);
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
364 * SPD calls.
365 */
366
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800367struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct xfrm_policy *policy;
370
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700371 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800374 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700375 INIT_LIST_HEAD(&policy->walk.all);
Florian Westphal24969fa2018-11-07 23:00:35 +0100376 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700377 INIT_HLIST_NODE(&policy->bydst);
378 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300380 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100381 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700382 timer_setup(&policy->timer, xfrm_policy_timer, 0);
383 timer_setup(&policy->polq.hold_timer,
384 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 return policy;
387}
388EXPORT_SYMBOL(xfrm_policy_alloc);
389
Eric Dumazet56f04732015-12-08 07:22:01 -0800390static void xfrm_policy_destroy_rcu(struct rcu_head *head)
391{
392 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
393
394 security_xfrm_policy_free(policy->security);
395 kfree(policy);
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/* Destroy xfrm_policy: descendant resources must be released to this moment. */
399
WANG Cong64c31b32008-01-07 22:34:29 -0800400void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Herbert Xu12a169e2008-10-01 07:03:24 -0700402 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Fan Du0659eea2013-08-01 18:08:36 +0800404 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 BUG();
406
Eric Dumazet56f04732015-12-08 07:22:01 -0800407 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
WANG Cong64c31b32008-01-07 22:34:29 -0800409EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100411/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * entry dead. The rule must be unlinked from lists to the moment.
413 */
414
415static void xfrm_policy_kill(struct xfrm_policy *policy)
416{
Herbert Xu12a169e2008-10-01 07:03:24 -0700417 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Timo Teräs285ead12010-04-07 00:30:06 +0000419 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200421 if (del_timer(&policy->polq.hold_timer))
422 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800423 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100424
Timo Teräs285ead12010-04-07 00:30:06 +0000425 if (del_timer(&policy->timer))
426 xfrm_pol_put(policy);
427
428 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
David S. Miller2518c7c2006-08-24 04:45:07 -0700431static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
432
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800433static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700434{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800435 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700436}
437
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200438/* calculate policy hash thresholds */
439static void __get_hash_thresh(struct net *net,
440 unsigned short family, int dir,
441 u8 *dbits, u8 *sbits)
442{
443 switch (family) {
444 case AF_INET:
445 *dbits = net->xfrm.policy_bydst[dir].dbits4;
446 *sbits = net->xfrm.policy_bydst[dir].sbits4;
447 break;
448
449 case AF_INET6:
450 *dbits = net->xfrm.policy_bydst[dir].dbits6;
451 *sbits = net->xfrm.policy_bydst[dir].sbits6;
452 break;
453
454 default:
455 *dbits = 0;
456 *sbits = 0;
457 }
458}
459
David S. Miller5f803b52011-02-24 00:33:19 -0500460static struct hlist_head *policy_hash_bysel(struct net *net,
461 const struct xfrm_selector *sel,
462 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700463{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800464 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200465 unsigned int hash;
466 u8 dbits;
467 u8 sbits;
468
469 __get_hash_thresh(net, family, dir, &dbits, &sbits);
470 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700471
Florian Westphale1e551b2016-08-11 15:17:53 +0200472 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100473 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200474
475 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
476 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700477}
478
David S. Miller5f803b52011-02-24 00:33:19 -0500479static struct hlist_head *policy_hash_direct(struct net *net,
480 const xfrm_address_t *daddr,
481 const xfrm_address_t *saddr,
482 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700483{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800484 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200485 unsigned int hash;
486 u8 dbits;
487 u8 sbits;
488
489 __get_hash_thresh(net, family, dir, &dbits, &sbits);
490 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700491
Florian Westphale1e551b2016-08-11 15:17:53 +0200492 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
493 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700494}
495
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200496static void xfrm_dst_hash_transfer(struct net *net,
497 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700498 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200499 unsigned int nhashmask,
500 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700501{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800502 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700503 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800504 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200505 u8 dbits;
506 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700507
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800508redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800509 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700510 unsigned int h;
511
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200512 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700513 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200514 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800515 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200516 hlist_del_rcu(&pol->bydst);
517 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800518 h0 = h;
519 } else {
520 if (h != h0)
521 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200522 hlist_del_rcu(&pol->bydst);
523 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800524 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800525 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800526 }
527 if (!hlist_empty(list)) {
528 entry0 = NULL;
529 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700530 }
531}
532
533static void xfrm_idx_hash_transfer(struct hlist_head *list,
534 struct hlist_head *nidxtable,
535 unsigned int nhashmask)
536{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700538 struct xfrm_policy *pol;
539
Sasha Levinb67bfe02013-02-27 17:06:00 -0800540 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700541 unsigned int h;
542
543 h = __idx_hash(pol->index, nhashmask);
544 hlist_add_head(&pol->byidx, nidxtable+h);
545 }
546}
547
548static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
549{
550 return ((old_hmask + 1) << 1) - 1;
551}
552
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800553static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700554{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800555 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700556 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
557 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700558 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200559 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 int i;
561
562 if (!ndst)
563 return;
564
Florian Westphal9d0380d2016-08-11 15:17:59 +0200565 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200566 write_seqcount_begin(&xfrm_policy_hash_generation);
567
568 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
569 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700570
Florian Westphale1e551b2016-08-11 15:17:53 +0200571 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
572 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
573
David S. Miller2518c7c2006-08-24 04:45:07 -0700574 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200575 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700576
Florian Westphale1e551b2016-08-11 15:17:53 +0200577 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800578 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700579
Florian Westphal30846092016-08-11 15:17:54 +0200580 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200581 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700582
Florian Westphale1e551b2016-08-11 15:17:53 +0200583 synchronize_rcu();
584
David S. Miller44e36b42006-08-24 04:50:50 -0700585 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700586}
587
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800588static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700589{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800590 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700591 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
592 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800593 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700594 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700595 int i;
596
597 if (!nidx)
598 return;
599
Florian Westphal9d0380d2016-08-11 15:17:59 +0200600 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700601
602 for (i = hmask; i >= 0; i--)
603 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
604
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800605 net->xfrm.policy_byidx = nidx;
606 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700607
Florian Westphal9d0380d2016-08-11 15:17:59 +0200608 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700609
David S. Miller44e36b42006-08-24 04:50:50 -0700610 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700611}
612
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800613static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700614{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800615 unsigned int cnt = net->xfrm.policy_count[dir];
616 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700617
618 if (total)
619 *total += cnt;
620
621 if ((hmask + 1) < xfrm_policy_hashmax &&
622 cnt > hmask)
623 return 1;
624
625 return 0;
626}
627
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800628static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700629{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800630 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700631
632 if ((hmask + 1) < xfrm_policy_hashmax &&
633 total > hmask)
634 return 1;
635
636 return 0;
637}
638
Alexey Dobriyane0710412010-01-23 13:37:10 +0000639void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700640{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000641 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
642 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
643 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
644 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
645 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
646 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
647 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700648 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700649}
650EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700651
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700652static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800653static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700654{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800655 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700656 int dir, total;
657
658 mutex_lock(&hash_resize_mutex);
659
660 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800661 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800662 if (xfrm_bydst_should_resize(net, dir, &total))
663 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700664 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800665 if (xfrm_byidx_should_resize(net, total))
666 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700667
668 mutex_unlock(&hash_resize_mutex);
669}
670
Florian Westphal24969fa2018-11-07 23:00:35 +0100671static void xfrm_hash_reset_inexact_table(struct net *net)
672{
673 struct xfrm_pol_inexact_bin *b;
674
675 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
676
677 list_for_each_entry(b, &net->xfrm.inexact_bins, inexact_bins)
678 INIT_HLIST_HEAD(&b->hhead);
679}
680
681/* Make sure *pol can be inserted into fastbin.
682 * Useful to check that later insert requests will be sucessful
683 * (provided xfrm_policy_lock is held throughout).
684 */
685static struct xfrm_pol_inexact_bin *
686xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
687{
688 struct xfrm_pol_inexact_bin *bin, *prev;
689 struct xfrm_pol_inexact_key k = {
690 .family = pol->family,
691 .type = pol->type,
692 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100693 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100694 };
695 struct net *net = xp_net(pol);
696
697 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
698
699 write_pnet(&k.net, net);
700 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
701 xfrm_pol_inexact_params);
702 if (bin)
703 return bin;
704
705 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
706 if (!bin)
707 return NULL;
708
709 bin->k = k;
710 INIT_HLIST_HEAD(&bin->hhead);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100711 bin->root_d = RB_ROOT;
Florian Westphal64a09a72018-11-07 23:00:40 +0100712 bin->root_s = RB_ROOT;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100713 seqcount_init(&bin->count);
Florian Westphal24969fa2018-11-07 23:00:35 +0100714
715 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
716 &bin->k, &bin->head,
717 xfrm_pol_inexact_params);
718 if (!prev) {
719 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
720 return bin;
721 }
722
723 kfree(bin);
724
725 return IS_ERR(prev) ? NULL : prev;
726}
727
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100728static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
729 int family, u8 prefixlen)
Florian Westphal24969fa2018-11-07 23:00:35 +0100730{
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100731 if (xfrm_addr_any(addr, family))
732 return true;
Florian Westphal24969fa2018-11-07 23:00:35 +0100733
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100734 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
735 return true;
736
737 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
738 return true;
739
740 return false;
741}
742
743static bool
744xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
745{
746 const xfrm_address_t *addr;
747 bool saddr_any, daddr_any;
748 u8 prefixlen;
749
750 addr = &policy->selector.saddr;
751 prefixlen = policy->selector.prefixlen_s;
752
753 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
754 policy->family,
755 prefixlen);
756 addr = &policy->selector.daddr;
757 prefixlen = policy->selector.prefixlen_d;
758 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
759 policy->family,
760 prefixlen);
761 return saddr_any && daddr_any;
762}
763
Florian Westphal9cf545e2018-11-07 23:00:38 +0100764static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
765 const xfrm_address_t *addr, u8 prefixlen)
766{
767 node->addr = *addr;
768 node->prefixlen = prefixlen;
769}
770
771static struct xfrm_pol_inexact_node *
772xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
773{
774 struct xfrm_pol_inexact_node *node;
775
776 node = kzalloc(sizeof(*node), GFP_ATOMIC);
777 if (node)
778 xfrm_pol_inexact_node_init(node, addr, prefixlen);
779
780 return node;
781}
782
783static int xfrm_policy_addr_delta(const xfrm_address_t *a,
784 const xfrm_address_t *b,
785 u8 prefixlen, u16 family)
786{
787 unsigned int pdw, pbi;
788 int delta = 0;
789
790 switch (family) {
791 case AF_INET:
792 if (sizeof(long) == 4 && prefixlen == 0)
793 return ntohl(a->a4) - ntohl(b->a4);
794 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
795 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
796 case AF_INET6:
797 pdw = prefixlen >> 5;
798 pbi = prefixlen & 0x1f;
799
800 if (pdw) {
801 delta = memcmp(a->a6, b->a6, pdw << 2);
802 if (delta)
803 return delta;
804 }
805 if (pbi) {
806 u32 mask = ~0u << (32 - pbi);
807
808 delta = (ntohl(a->a6[pdw]) & mask) -
809 (ntohl(b->a6[pdw]) & mask);
810 }
811 break;
812 default:
813 break;
814 }
815
816 return delta;
817}
818
819static void xfrm_policy_inexact_list_reinsert(struct net *net,
820 struct xfrm_pol_inexact_node *n,
821 u16 family)
822{
Florian Westphale901cbc2018-11-07 23:00:39 +0100823 unsigned int matched_s, matched_d;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100824 struct hlist_node *newpos = NULL;
825 struct xfrm_policy *policy, *p;
826
Florian Westphale901cbc2018-11-07 23:00:39 +0100827 matched_s = 0;
828 matched_d = 0;
829
Florian Westphal9cf545e2018-11-07 23:00:38 +0100830 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphale901cbc2018-11-07 23:00:39 +0100831 bool matches_s, matches_d;
832
Florian Westphal9cf545e2018-11-07 23:00:38 +0100833 if (!policy->bydst_reinsert)
834 continue;
835
836 WARN_ON_ONCE(policy->family != family);
837
838 policy->bydst_reinsert = false;
839 hlist_for_each_entry(p, &n->hhead, bydst) {
840 if (policy->priority >= p->priority)
841 newpos = &p->bydst;
842 else
843 break;
844 }
845
846 if (newpos)
847 hlist_add_behind(&policy->bydst, newpos);
848 else
849 hlist_add_head(&policy->bydst, &n->hhead);
Florian Westphale901cbc2018-11-07 23:00:39 +0100850
851 /* paranoia checks follow.
852 * Check that the reinserted policy matches at least
853 * saddr or daddr for current node prefix.
854 *
855 * Matching both is fine, matching saddr in one policy
856 * (but not daddr) and then matching only daddr in another
857 * is a bug.
858 */
859 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
860 &n->addr,
861 n->prefixlen,
862 family) == 0;
863 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
864 &n->addr,
865 n->prefixlen,
866 family) == 0;
867 if (matches_s && matches_d)
868 continue;
869
870 WARN_ON_ONCE(!matches_s && !matches_d);
871 if (matches_s)
872 matched_s++;
873 if (matches_d)
874 matched_d++;
875 WARN_ON_ONCE(matched_s && matched_d);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100876 }
877}
878
879/* merge nodes v and n */
880static void xfrm_policy_inexact_node_merge(struct net *net,
881 struct xfrm_pol_inexact_node *v,
882 struct xfrm_pol_inexact_node *n,
883 u16 family)
884{
885 struct xfrm_policy *tmp;
886
887 hlist_for_each_entry(tmp, &v->hhead, bydst)
888 tmp->bydst_reinsert = true;
889 hlist_for_each_entry(tmp, &n->hhead, bydst)
890 tmp->bydst_reinsert = true;
891
892 INIT_HLIST_HEAD(&n->hhead);
893 xfrm_policy_inexact_list_reinsert(net, n, family);
894}
895
896static struct xfrm_pol_inexact_node *
897xfrm_policy_inexact_insert_node(struct net *net,
898 struct rb_root *root,
899 xfrm_address_t *addr,
900 u16 family, u8 prefixlen, u8 dir)
901{
902 struct xfrm_pol_inexact_node *cached = NULL;
903 struct rb_node **p, *parent = NULL;
904 struct xfrm_pol_inexact_node *node;
905
906 p = &root->rb_node;
907 while (*p) {
908 int delta;
909
910 parent = *p;
911 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
912
913 delta = xfrm_policy_addr_delta(addr, &node->addr,
914 node->prefixlen,
915 family);
916 if (delta == 0 && prefixlen >= node->prefixlen) {
917 WARN_ON_ONCE(cached); /* ipsec policies got lost */
918 return node;
919 }
920
921 if (delta < 0)
922 p = &parent->rb_left;
923 else
924 p = &parent->rb_right;
925
926 if (prefixlen < node->prefixlen) {
927 delta = xfrm_policy_addr_delta(addr, &node->addr,
928 prefixlen,
929 family);
930 if (delta)
931 continue;
932
933 /* This node is a subnet of the new prefix. It needs
934 * to be removed and re-inserted with the smaller
935 * prefix and all nodes that are now also covered
936 * by the reduced prefixlen.
937 */
938 rb_erase(&node->node, root);
939
940 if (!cached) {
941 xfrm_pol_inexact_node_init(node, addr,
942 prefixlen);
943 cached = node;
944 } else {
945 /* This node also falls within the new
946 * prefixlen. Merge the to-be-reinserted
947 * node and this one.
948 */
949 xfrm_policy_inexact_node_merge(net, node,
950 cached, family);
951 kfree_rcu(node, rcu);
952 }
953
954 /* restart */
955 p = &root->rb_node;
956 parent = NULL;
957 }
958 }
959
960 node = cached;
961 if (!node) {
962 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
963 if (!node)
964 return NULL;
965 }
966
967 rb_link_node_rcu(&node->node, parent, p);
968 rb_insert_color(&node->node, root);
969
970 return node;
971}
972
973static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
974{
975 struct xfrm_pol_inexact_node *node;
976 struct rb_node *rn = rb_first(r);
977
978 while (rn) {
979 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
980
981 rn = rb_next(rn);
982
983 if (!hlist_empty(&node->hhead)) {
984 WARN_ON_ONCE(rm);
985 continue;
986 }
987
988 rb_erase(&node->node, r);
989 kfree_rcu(node, rcu);
990 }
991}
992
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100993static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
994{
Florian Westphal9cf545e2018-11-07 23:00:38 +0100995 write_seqcount_begin(&b->count);
996 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
Florian Westphal64a09a72018-11-07 23:00:40 +0100997 xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100998 write_seqcount_end(&b->count);
999
Florian Westphal64a09a72018-11-07 23:00:40 +01001000 if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
Florian Westphal9cf545e2018-11-07 23:00:38 +01001001 !hlist_empty(&b->hhead)) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001002 WARN_ON_ONCE(net_exit);
Florian Westphal24969fa2018-11-07 23:00:35 +01001003 return;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001004 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001005
1006 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
1007 xfrm_pol_inexact_params) == 0) {
1008 list_del(&b->inexact_bins);
1009 kfree_rcu(b, rcu);
1010 }
1011}
1012
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001013static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
1014{
1015 struct net *net = read_pnet(&b->k.net);
1016
1017 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1018 __xfrm_policy_inexact_prune_bin(b, false);
1019 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1020}
1021
Florian Westphal24969fa2018-11-07 23:00:35 +01001022static void __xfrm_policy_inexact_flush(struct net *net)
1023{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001024 struct xfrm_pol_inexact_bin *bin, *t;
Florian Westphal24969fa2018-11-07 23:00:35 +01001025
1026 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1027
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001028 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1029 __xfrm_policy_inexact_prune_bin(bin, false);
Florian Westphal24969fa2018-11-07 23:00:35 +01001030}
1031
Florian Westphal9cf545e2018-11-07 23:00:38 +01001032static struct hlist_head *
1033xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1034 struct xfrm_policy *policy, u8 dir)
1035{
1036 struct xfrm_pol_inexact_node *n;
1037 struct net *net;
1038
1039 net = xp_net(policy);
1040 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1041
1042 if (xfrm_policy_inexact_insert_use_any_list(policy))
1043 return &bin->hhead;
1044
Florian Westphal64a09a72018-11-07 23:00:40 +01001045 /* saddr is wildcard */
1046 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
1047 policy->family,
1048 policy->selector.prefixlen_s))
1049 return &bin->hhead;
1050
Florian Westphal9cf545e2018-11-07 23:00:38 +01001051 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1052 policy->family,
Florian Westphal64a09a72018-11-07 23:00:40 +01001053 policy->selector.prefixlen_d)) {
1054 write_seqcount_begin(&bin->count);
1055 n = xfrm_policy_inexact_insert_node(net,
1056 &bin->root_s,
1057 &policy->selector.saddr,
1058 policy->family,
1059 policy->selector.prefixlen_s,
1060 dir);
1061 write_seqcount_end(&bin->count);
1062 if (!n)
1063 return NULL;
1064
1065 return &n->hhead;
1066 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001067
1068 /* daddr is fixed */
1069 write_seqcount_begin(&bin->count);
1070 n = xfrm_policy_inexact_insert_node(net,
1071 &bin->root_d,
1072 &policy->selector.daddr,
1073 policy->family,
1074 policy->selector.prefixlen_d, dir);
1075 write_seqcount_end(&bin->count);
1076 if (!n)
1077 return NULL;
1078 return &n->hhead;
1079}
1080
Florian Westphal24969fa2018-11-07 23:00:35 +01001081static struct xfrm_policy *
1082xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1083{
1084 struct xfrm_pol_inexact_bin *bin;
1085 struct xfrm_policy *delpol;
1086 struct hlist_head *chain;
1087 struct net *net;
1088
1089 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1090 if (!bin)
1091 return ERR_PTR(-ENOMEM);
1092
Florian Westphal24969fa2018-11-07 23:00:35 +01001093 net = xp_net(policy);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001094 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1095
Florian Westphal9cf545e2018-11-07 23:00:38 +01001096 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1097 if (!chain) {
1098 __xfrm_policy_inexact_prune_bin(bin, false);
1099 return ERR_PTR(-ENOMEM);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001100 }
1101
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001102 delpol = xfrm_policy_insert_list(chain, policy, excl);
1103 if (delpol && excl) {
1104 __xfrm_policy_inexact_prune_bin(bin, false);
1105 return ERR_PTR(-EEXIST);
1106 }
1107
Florian Westphal24969fa2018-11-07 23:00:35 +01001108 chain = &net->xfrm.policy_inexact[dir];
1109 xfrm_policy_insert_inexact_list(chain, policy);
1110
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001111 if (delpol)
1112 __xfrm_policy_inexact_prune_bin(bin, false);
1113
Florian Westphal24969fa2018-11-07 23:00:35 +01001114 return delpol;
1115}
1116
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001117static void xfrm_hash_rebuild(struct work_struct *work)
1118{
1119 struct net *net = container_of(work, struct net,
1120 xfrm.policy_hthresh.work);
1121 unsigned int hmask;
1122 struct xfrm_policy *pol;
1123 struct xfrm_policy *policy;
1124 struct hlist_head *chain;
1125 struct hlist_head *odst;
1126 struct hlist_node *newpos;
1127 int i;
1128 int dir;
1129 unsigned seq;
1130 u8 lbits4, rbits4, lbits6, rbits6;
1131
1132 mutex_lock(&hash_resize_mutex);
1133
1134 /* read selector prefixlen thresholds */
1135 do {
1136 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1137
1138 lbits4 = net->xfrm.policy_hthresh.lbits4;
1139 rbits4 = net->xfrm.policy_hthresh.rbits4;
1140 lbits6 = net->xfrm.policy_hthresh.lbits6;
1141 rbits6 = net->xfrm.policy_hthresh.rbits6;
1142 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1143
Florian Westphal9d0380d2016-08-11 15:17:59 +02001144 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001145
Florian Westphal24969fa2018-11-07 23:00:35 +01001146 /* make sure that we can insert the indirect policies again before
1147 * we start with destructive action.
1148 */
1149 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001150 struct xfrm_pol_inexact_bin *bin;
Florian Westphal24969fa2018-11-07 23:00:35 +01001151 u8 dbits, sbits;
1152
1153 dir = xfrm_policy_id2dir(policy->index);
1154 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1155 continue;
1156
1157 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1158 if (policy->family == AF_INET) {
1159 dbits = rbits4;
1160 sbits = lbits4;
1161 } else {
1162 dbits = rbits6;
1163 sbits = lbits6;
1164 }
1165 } else {
1166 if (policy->family == AF_INET) {
1167 dbits = lbits4;
1168 sbits = rbits4;
1169 } else {
1170 dbits = lbits6;
1171 sbits = rbits6;
1172 }
1173 }
1174
1175 if (policy->selector.prefixlen_d < dbits ||
1176 policy->selector.prefixlen_s < sbits)
1177 continue;
1178
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001179 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1180 if (!bin)
Florian Westphal24969fa2018-11-07 23:00:35 +01001181 goto out_unlock;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001182
1183 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1184 goto out_unlock;
Florian Westphal24969fa2018-11-07 23:00:35 +01001185 }
1186
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001187 /* reset the bydst and inexact table in all directions */
Florian Westphal24969fa2018-11-07 23:00:35 +01001188 xfrm_hash_reset_inexact_table(net);
1189
Herbert Xu53c2e282014-11-13 17:09:49 +08001190 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001191 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
1192 hmask = net->xfrm.policy_bydst[dir].hmask;
1193 odst = net->xfrm.policy_bydst[dir].table;
1194 for (i = hmask; i >= 0; i--)
1195 INIT_HLIST_HEAD(odst + i);
1196 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1197 /* dir out => dst = remote, src = local */
1198 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1199 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1200 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1201 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1202 } else {
1203 /* dir in/fwd => dst = local, src = remote */
1204 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1205 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1206 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1207 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1208 }
1209 }
1210
1211 /* re-insert all policies by order of creation */
1212 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal862591b2017-12-27 23:25:45 +01001213 if (policy->walk.dead ||
1214 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +02001215 /* skip socket policies */
1216 continue;
1217 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001218 newpos = NULL;
1219 chain = policy_hash_bysel(net, &policy->selector,
1220 policy->family,
1221 xfrm_policy_id2dir(policy->index));
Florian Westphal24969fa2018-11-07 23:00:35 +01001222 if (!chain) {
1223 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1224
1225 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1226 continue;
1227 }
1228
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001229 hlist_for_each_entry(pol, chain, bydst) {
1230 if (policy->priority >= pol->priority)
1231 newpos = &pol->bydst;
1232 else
1233 break;
1234 }
1235 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +02001236 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001237 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001238 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001239 }
1240
Florian Westphal24969fa2018-11-07 23:00:35 +01001241out_unlock:
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001242 __xfrm_policy_inexact_flush(net);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001243 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001244
1245 mutex_unlock(&hash_resize_mutex);
1246}
1247
1248void xfrm_policy_hash_rebuild(struct net *net)
1249{
1250 schedule_work(&net->xfrm.policy_hthresh.work);
1251}
1252EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254/* Generate new index... KAME seems to generate them ordered by cost
1255 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +08001256static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 static u32 idx_generator;
1259
1260 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001261 struct hlist_head *list;
1262 struct xfrm_policy *p;
1263 u32 idx;
1264 int found;
1265
Fan Due682adf02013-11-07 17:47:48 +08001266 if (!index) {
1267 idx = (idx_generator | dir);
1268 idx_generator += 8;
1269 } else {
1270 idx = index;
1271 index = 0;
1272 }
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (idx == 0)
1275 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -08001276 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -07001277 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001278 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001279 if (p->index == idx) {
1280 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
David S. Miller2518c7c2006-08-24 04:45:07 -07001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001284 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return idx;
1286 }
1287}
1288
David S. Miller2518c7c2006-08-24 04:45:07 -07001289static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1290{
1291 u32 *p1 = (u32 *) s1;
1292 u32 *p2 = (u32 *) s2;
1293 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1294 int i;
1295
1296 for (i = 0; i < len; i++) {
1297 if (p1[i] != p2[i])
1298 return 1;
1299 }
1300
1301 return 0;
1302}
1303
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001304static void xfrm_policy_requeue(struct xfrm_policy *old,
1305 struct xfrm_policy *new)
1306{
1307 struct xfrm_policy_queue *pq = &old->polq;
1308 struct sk_buff_head list;
1309
Li RongQingde2ad482015-04-30 17:25:19 +08001310 if (skb_queue_empty(&pq->hold_queue))
1311 return;
1312
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001313 __skb_queue_head_init(&list);
1314
1315 spin_lock_bh(&pq->hold_queue.lock);
1316 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001317 if (del_timer(&pq->hold_timer))
1318 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001319 spin_unlock_bh(&pq->hold_queue.lock);
1320
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001321 pq = &new->polq;
1322
1323 spin_lock_bh(&pq->hold_queue.lock);
1324 skb_queue_splice(&list, &pq->hold_queue);
1325 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001326 if (!mod_timer(&pq->hold_timer, jiffies))
1327 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001328 spin_unlock_bh(&pq->hold_queue.lock);
1329}
1330
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001331static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1332 struct xfrm_policy *pol)
1333{
1334 u32 mark = policy->mark.v & policy->mark.m;
1335
1336 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1337 return true;
1338
1339 if ((mark & pol->mark.m) == pol->mark.v &&
1340 policy->priority == pol->priority)
1341 return true;
1342
1343 return false;
1344}
1345
Florian Westphal24969fa2018-11-07 23:00:35 +01001346static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1347{
1348 const struct xfrm_pol_inexact_key *k = data;
1349 u32 a = k->type << 24 | k->dir << 16 | k->family;
1350
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001351 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1352 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +01001353}
1354
1355static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1356{
1357 const struct xfrm_pol_inexact_bin *b = data;
1358
1359 return xfrm_pol_bin_key(&b->k, 0, seed);
1360}
1361
1362static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1363 const void *ptr)
1364{
1365 const struct xfrm_pol_inexact_key *key = arg->key;
1366 const struct xfrm_pol_inexact_bin *b = ptr;
1367 int ret;
1368
1369 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1370 return -1;
1371
1372 ret = b->k.dir ^ key->dir;
1373 if (ret)
1374 return ret;
1375
1376 ret = b->k.type ^ key->type;
1377 if (ret)
1378 return ret;
1379
1380 ret = b->k.family ^ key->family;
1381 if (ret)
1382 return ret;
1383
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001384 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +01001385}
1386
1387static const struct rhashtable_params xfrm_pol_inexact_params = {
1388 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1389 .hashfn = xfrm_pol_bin_key,
1390 .obj_hashfn = xfrm_pol_bin_obj,
1391 .obj_cmpfn = xfrm_pol_bin_cmp,
1392 .automatic_shrinking = true,
1393};
1394
1395static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1396 struct xfrm_policy *policy)
1397{
1398 struct xfrm_policy *pol, *delpol = NULL;
1399 struct hlist_node *newpos = NULL;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001400 int i = 0;
Florian Westphal24969fa2018-11-07 23:00:35 +01001401
1402 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1403 if (pol->type == policy->type &&
1404 pol->if_id == policy->if_id &&
1405 !selector_cmp(&pol->selector, &policy->selector) &&
1406 xfrm_policy_mark_match(policy, pol) &&
1407 xfrm_sec_ctx_match(pol->security, policy->security) &&
1408 !WARN_ON(delpol)) {
1409 delpol = pol;
1410 if (policy->priority > pol->priority)
1411 continue;
1412 } else if (policy->priority >= pol->priority) {
1413 newpos = &pol->bydst_inexact_list;
1414 continue;
1415 }
1416 if (delpol)
1417 break;
1418 }
1419
1420 if (newpos)
1421 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1422 else
1423 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001424
1425 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1426 pol->pos = i;
1427 i++;
1428 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001429}
1430
Florian Westphala927d6af2018-11-07 23:00:33 +01001431static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1432 struct xfrm_policy *policy,
1433 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Florian Westphala927d6af2018-11-07 23:00:33 +01001435 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Sasha Levinb67bfe02013-02-27 17:06:00 -08001437 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001438 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001439 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001440 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001441 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001442 xfrm_sec_ctx_match(pol->security, policy->security) &&
1443 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001444 if (excl)
1445 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 delpol = pol;
1447 if (policy->priority > pol->priority)
1448 continue;
1449 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001450 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 continue;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (delpol)
1454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001458 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001459 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001460 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001461
1462 return delpol;
1463}
1464
1465int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1466{
1467 struct net *net = xp_net(policy);
1468 struct xfrm_policy *delpol;
1469 struct hlist_head *chain;
1470
1471 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1472 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001473 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001474 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001475 else
1476 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001477
1478 if (IS_ERR(delpol)) {
1479 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1480 return PTR_ERR(delpol);
1481 }
1482
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001483 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001484
1485 /* After previous checking, family can either be AF_INET or AF_INET6 */
1486 if (policy->family == AF_INET)
1487 rt_genid_bump_ipv4(net);
1488 else
1489 rt_genid_bump_ipv6(net);
1490
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001491 if (delpol) {
1492 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001493 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001494 }
Fan Due682adf02013-11-07 17:47:48 +08001495 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001496 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001497 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 policy->curlft.use_time = 0;
1499 if (!mod_timer(&policy->timer, jiffies + HZ))
1500 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001501 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
David S. Miller9b78a822005-12-22 07:39:48 -08001503 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001505 else if (xfrm_bydst_should_resize(net, dir, NULL))
1506 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509}
1510EXPORT_SYMBOL(xfrm_policy_insert);
1511
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001512static struct xfrm_policy *
1513__xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1514 u8 type, int dir,
1515 struct xfrm_selector *sel,
1516 struct xfrm_sec_ctx *ctx)
1517{
1518 struct xfrm_policy *pol;
1519
1520 if (!chain)
1521 return NULL;
1522
1523 hlist_for_each_entry(pol, chain, bydst) {
1524 if (pol->type == type &&
1525 pol->if_id == if_id &&
1526 (mark & pol->mark.m) == pol->mark.v &&
1527 !selector_cmp(sel, &pol->selector) &&
1528 xfrm_sec_ctx_match(ctx, pol->security))
1529 return pol;
1530 }
1531
1532 return NULL;
1533}
1534
Steffen Klassert7e652642018-06-12 14:07:07 +02001535struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1536 u8 type, int dir,
1537 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001538 struct xfrm_sec_ctx *ctx, int delete,
1539 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Florian Westphal24969fa2018-11-07 23:00:35 +01001541 struct xfrm_pol_inexact_bin *bin = NULL;
1542 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001543 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Eric Parisef41aaa2007-03-07 15:37:58 -08001545 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001546 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001547 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001548 if (!chain) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001549 struct xfrm_pol_inexact_candidates cand;
1550 int i;
1551
Florian Westphal24969fa2018-11-07 23:00:35 +01001552 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001553 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001554 if (!bin) {
1555 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1556 return NULL;
1557 }
1558
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001559 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1560 &sel->saddr,
1561 &sel->daddr)) {
1562 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1563 return NULL;
1564 }
1565
1566 pol = NULL;
1567 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1568 struct xfrm_policy *tmp;
1569
1570 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1571 if_id, type, dir,
1572 sel, ctx);
1573 if (tmp && pol && tmp->pos < pol->pos)
1574 pol = tmp;
1575 }
1576 } else {
1577 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1578 sel, ctx);
Florian Westphal24969fa2018-11-07 23:00:35 +01001579 }
1580
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001581 if (pol) {
1582 xfrm_pol_hold(pol);
1583 if (delete) {
1584 *err = security_xfrm_policy_delete(pol->security);
1585 if (*err) {
1586 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1587 return pol;
David S. Miller2518c7c2006-08-24 04:45:07 -07001588 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001589 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001591 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001593 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001595 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001596 xfrm_policy_kill(ret);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001597 if (bin && delete)
1598 xfrm_policy_inexact_prune_bin(bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
Trent Jaegerdf718372005-12-13 23:12:27 -08001601EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Steffen Klassert7e652642018-06-12 14:07:07 +02001603struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1604 u8 type, int dir, u32 id, int delete,
1605 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
David S. Miller2518c7c2006-08-24 04:45:07 -07001607 struct xfrm_policy *pol, *ret;
1608 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Herbert Xub5505c62007-05-14 02:15:47 -07001610 *err = -ENOENT;
1611 if (xfrm_policy_id2dir(id) != dir)
1612 return NULL;
1613
Eric Parisef41aaa2007-03-07 15:37:58 -08001614 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001615 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001616 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001617 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001618 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001619 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001620 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001621 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001623 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001624 *err = security_xfrm_policy_delete(
1625 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001626 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001627 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001628 return pol;
1629 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001630 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001631 }
1632 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 }
1635 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001636 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001638 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001639 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642EXPORT_SYMBOL(xfrm_policy_byid);
1643
Joy Latten4aa2e622007-06-04 19:05:57 -04001644#ifdef CONFIG_SECURITY_NETWORK_XFRM
1645static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001646xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Florian Westphalceb159e2018-11-07 23:00:32 +01001648 struct xfrm_policy *pol;
1649 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001650
Florian Westphalceb159e2018-11-07 23:00:32 +01001651 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1652 if (pol->walk.dead ||
1653 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1654 pol->type != type)
1655 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001656
Florian Westphalceb159e2018-11-07 23:00:32 +01001657 err = security_xfrm_policy_delete(pol->security);
1658 if (err) {
1659 xfrm_audit_policy_delete(pol, 0, task_valid);
1660 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001661 }
1662 }
1663 return err;
1664}
1665#else
1666static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001667xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001668{
1669 return 0;
1670}
1671#endif
1672
Tetsuo Handa2e710292014-04-22 21:48:30 +09001673int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001674{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001675 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001676 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Florian Westphal9d0380d2016-08-11 15:17:59 +02001678 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001679
Tetsuo Handa2e710292014-04-22 21:48:30 +09001680 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001681 if (err)
1682 goto out;
1683
Florian Westphalceb159e2018-11-07 23:00:32 +01001684again:
1685 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1686 dir = xfrm_policy_id2dir(pol->index);
1687 if (pol->walk.dead ||
1688 dir >= XFRM_POLICY_MAX ||
1689 pol->type != type)
1690 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001691
Florian Westphalceb159e2018-11-07 23:00:32 +01001692 __xfrm_policy_unlink(pol, dir);
1693 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1694 cnt++;
1695 xfrm_audit_policy_delete(pol, 1, task_valid);
1696 xfrm_policy_kill(pol);
1697 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1698 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001700 if (cnt)
1701 __xfrm_policy_inexact_flush(net);
1702 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001703 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001704out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001705 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708EXPORT_SYMBOL(xfrm_policy_flush);
1709
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001710int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001711 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 void *data)
1713{
Herbert Xu12a169e2008-10-01 07:03:24 -07001714 struct xfrm_policy *pol;
1715 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001716 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Timo Teras4c563f72008-02-28 21:31:08 -08001718 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1719 walk->type != XFRM_POLICY_TYPE_ANY)
1720 return -EINVAL;
1721
Herbert Xu12a169e2008-10-01 07:03:24 -07001722 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001723 return 0;
1724
Florian Westphal9d0380d2016-08-11 15:17:59 +02001725 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001726 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001727 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001728 else
Li RongQing80077702015-04-22 17:09:54 +08001729 x = list_first_entry(&walk->walk.all,
1730 struct xfrm_policy_walk_entry, all);
1731
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001732 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001733 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001734 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001735 pol = container_of(x, struct xfrm_policy, walk);
1736 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1737 walk->type != pol->type)
1738 continue;
1739 error = func(pol, xfrm_policy_id2dir(pol->index),
1740 walk->seq, data);
1741 if (error) {
1742 list_move_tail(&walk->walk.all, &x->all);
1743 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001744 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001745 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001747 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001748 error = -ENOENT;
1749 goto out;
1750 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001751 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001753 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return error;
1755}
1756EXPORT_SYMBOL(xfrm_policy_walk);
1757
Herbert Xu12a169e2008-10-01 07:03:24 -07001758void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1759{
1760 INIT_LIST_HEAD(&walk->walk.all);
1761 walk->walk.dead = 1;
1762 walk->type = type;
1763 walk->seq = 0;
1764}
1765EXPORT_SYMBOL(xfrm_policy_walk_init);
1766
Fan Du283bc9f2013-11-07 17:47:50 +08001767void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001768{
1769 if (list_empty(&walk->walk.all))
1770 return;
1771
Florian Westphal9d0380d2016-08-11 15:17:59 +02001772 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001773 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001774 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001775}
1776EXPORT_SYMBOL(xfrm_policy_walk_done);
1777
James Morris134b0fc2006-10-05 15:42:27 -05001778/*
1779 * Find policy to apply to this flow.
1780 *
1781 * Returns 0 if policy found, else an -errno.
1782 */
David S. Millerf299d552011-02-24 01:23:30 -05001783static int xfrm_policy_match(const struct xfrm_policy *pol,
1784 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001785 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001786{
David S. Millerf299d552011-02-24 01:23:30 -05001787 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001788 int ret = -ESRCH;
1789 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001790
1791 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001792 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001793 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001794 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001795 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001796
1797 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001798 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001799 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001800 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001801 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001802}
1803
Florian Westphal9cf545e2018-11-07 23:00:38 +01001804static struct xfrm_pol_inexact_node *
1805xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
1806 seqcount_t *count,
1807 const xfrm_address_t *addr, u16 family)
1808{
1809 const struct rb_node *parent;
1810 int seq;
1811
1812again:
1813 seq = read_seqcount_begin(count);
1814
1815 parent = rcu_dereference_raw(r->rb_node);
1816 while (parent) {
1817 struct xfrm_pol_inexact_node *node;
1818 int delta;
1819
1820 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1821
1822 delta = xfrm_policy_addr_delta(addr, &node->addr,
1823 node->prefixlen, family);
1824 if (delta < 0) {
1825 parent = rcu_dereference_raw(parent->rb_left);
1826 continue;
1827 } else if (delta > 0) {
1828 parent = rcu_dereference_raw(parent->rb_right);
1829 continue;
1830 }
1831
1832 return node;
1833 }
1834
1835 if (read_seqcount_retry(count, seq))
1836 goto again;
1837
1838 return NULL;
1839}
1840
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001841static bool
1842xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1843 struct xfrm_pol_inexact_bin *b,
1844 const xfrm_address_t *saddr,
1845 const xfrm_address_t *daddr)
1846{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001847 struct xfrm_pol_inexact_node *n;
1848 u16 family;
1849
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001850 if (!b)
1851 return false;
1852
Florian Westphal9cf545e2018-11-07 23:00:38 +01001853 family = b->k.family;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001854 memset(cand, 0, sizeof(*cand));
1855 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001856
1857 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1858 family);
1859 if (n)
1860 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
1861
Florian Westphal64a09a72018-11-07 23:00:40 +01001862 n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
1863 family);
1864 if (n)
1865 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
1866
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001867 return true;
1868}
1869
Florian Westphal24969fa2018-11-07 23:00:35 +01001870static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001871xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1872 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001873{
1874 struct xfrm_pol_inexact_key k = {
1875 .family = family,
1876 .type = type,
1877 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001878 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001879 };
1880
1881 write_pnet(&k.net, net);
1882
1883 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1884 xfrm_pol_inexact_params);
1885}
1886
1887static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001888xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1889 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001890{
1891 struct xfrm_pol_inexact_bin *bin;
1892
1893 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1894
1895 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001896 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001897 rcu_read_unlock();
1898
1899 return bin;
1900}
1901
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001902static struct xfrm_policy *
1903__xfrm_policy_eval_candidates(struct hlist_head *chain,
1904 struct xfrm_policy *prefer,
1905 const struct flowi *fl,
1906 u8 type, u16 family, int dir, u32 if_id)
1907{
1908 u32 priority = prefer ? prefer->priority : ~0u;
1909 struct xfrm_policy *pol;
1910
1911 if (!chain)
1912 return NULL;
1913
1914 hlist_for_each_entry_rcu(pol, chain, bydst) {
1915 int err;
1916
1917 if (pol->priority > priority)
1918 break;
1919
1920 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
1921 if (err) {
1922 if (err != -ESRCH)
1923 return ERR_PTR(err);
1924
1925 continue;
1926 }
1927
1928 if (prefer) {
1929 /* matches. Is it older than *prefer? */
1930 if (pol->priority == priority &&
1931 prefer->pos < pol->pos)
1932 return prefer;
1933 }
1934
1935 return pol;
1936 }
1937
1938 return NULL;
1939}
1940
1941static struct xfrm_policy *
1942xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
1943 struct xfrm_policy *prefer,
1944 const struct flowi *fl,
1945 u8 type, u16 family, int dir, u32 if_id)
1946{
1947 struct xfrm_policy *tmp;
1948 int i;
1949
1950 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
1951 tmp = __xfrm_policy_eval_candidates(cand->res[i],
1952 prefer,
1953 fl, type, family, dir,
1954 if_id);
1955 if (!tmp)
1956 continue;
1957
1958 if (IS_ERR(tmp))
1959 return tmp;
1960 prefer = tmp;
1961 }
1962
1963 return prefer;
1964}
1965
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001966static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001967 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001968 u16 family, u8 dir,
1969 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001971 struct xfrm_pol_inexact_candidates cand;
David S. Miller0b597e72011-02-24 01:22:48 -05001972 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01001973 struct xfrm_pol_inexact_bin *bin;
1974 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001975 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001976 unsigned int sequence;
1977 u32 priority;
Florian Westphal24969fa2018-11-07 23:00:35 +01001978 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001979
1980 daddr = xfrm_flowi_daddr(fl, family);
1981 saddr = xfrm_flowi_saddr(fl, family);
1982 if (unlikely(!daddr || !saddr))
1983 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Florian Westphala7c442472016-08-11 15:17:56 +02001985 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001986 retry:
1987 do {
1988 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1989 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1990 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1991
1992 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001993 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001994 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07001995 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05001996 if (err) {
1997 if (err == -ESRCH)
1998 continue;
1999 else {
2000 ret = ERR_PTR(err);
2001 goto fail;
2002 }
2003 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07002004 ret = pol;
2005 priority = ret->priority;
2006 break;
2007 }
2008 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002009 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002010 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
2011 daddr))
Florian Westphal24969fa2018-11-07 23:00:35 +01002012 goto skip_inexact;
Li RongQing8faf4912015-05-14 11:16:59 +08002013
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002014 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
2015 family, dir, if_id);
2016 if (pol) {
2017 ret = pol;
2018 if (IS_ERR(pol))
2019 goto fail;
David S. Miller2518c7c2006-08-24 04:45:07 -07002020 }
Li RongQing586f2eb2015-04-30 17:13:41 +08002021
Florian Westphal24969fa2018-11-07 23:00:35 +01002022skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02002023 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
2024 goto retry;
2025
Florian Westphale37cc8a2016-08-11 15:17:55 +02002026 if (ret && !xfrm_pol_hold_rcu(ret))
2027 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05002028fail:
Florian Westphala7c442472016-08-11 15:17:56 +02002029 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002030
David S. Miller2518c7c2006-08-24 04:45:07 -07002031 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002032}
2033
Benedict Wongbc56b332018-07-19 10:50:44 -07002034static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
2035 const struct flowi *fl,
2036 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002037{
2038#ifdef CONFIG_XFRM_SUB_POLICY
2039 struct xfrm_policy *pol;
2040
Benedict Wongbc56b332018-07-19 10:50:44 -07002041 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2042 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002043 if (pol != NULL)
2044 return pol;
2045#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07002046 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2047 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002048}
2049
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002050static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07002051 const struct flowi *fl,
2052 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct xfrm_policy *pol;
2055
Eric Dumazetd188ba82015-12-08 07:22:02 -08002056 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02002057 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002058 pol = rcu_dereference(sk->sk_policy[dir]);
2059 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01002060 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002061 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08002062
Steffen Klassertddc47e42017-11-29 06:53:55 +01002063 if (pol->family != family) {
2064 pol = NULL;
2065 goto out;
2066 }
2067
2068 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002069 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02002070 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07002071 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002072 pol = NULL;
2073 goto out;
2074 }
Paul Moore03e1ad72008-04-12 19:07:52 -07002075 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05002076 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02002077 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01002078 if (!err) {
2079 if (!xfrm_pol_hold_rcu(pol))
2080 goto again;
2081 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002082 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01002083 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002084 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01002085 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002086 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 pol = NULL;
2088 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002089out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002090 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return pol;
2092}
2093
2094static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2095{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002096 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002097
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002098 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002099 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 xfrm_pol_hold(pol);
2101}
2102
2103static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2104 int dir)
2105{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002106 struct net *net = xp_net(pol);
2107
Herbert Xu53c2e282014-11-13 17:09:49 +08002108 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07002109 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Herbert Xu53c2e282014-11-13 17:09:49 +08002111 /* Socket policies are not hashed. */
2112 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02002113 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01002114 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08002115 hlist_del(&pol->byidx);
2116 }
2117
2118 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002119 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07002120
2121 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Herbert Xu53c2e282014-11-13 17:09:49 +08002124static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2125{
2126 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2127}
2128
2129static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2130{
2131 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2132}
2133
Herbert Xu4666faa2005-06-18 22:43:22 -07002134int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Fan Du283bc9f2013-11-07 17:47:50 +08002136 struct net *net = xp_net(pol);
2137
Florian Westphal9d0380d2016-08-11 15:17:59 +02002138 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002140 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07002143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Herbert Xu4666faa2005-06-18 22:43:22 -07002145 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
David S. Millera70fcb02006-03-20 19:18:52 -08002147EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2150{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002151 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 struct xfrm_policy *old_pol;
2153
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002154#ifdef CONFIG_XFRM_SUB_POLICY
2155 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2156 return -EINVAL;
2157#endif
2158
Florian Westphal9d0380d2016-08-11 15:17:59 +02002159 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08002160 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2161 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02002163 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08002164 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08002165 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08002167 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002168 if (old_pol) {
2169 if (pol)
2170 xfrm_policy_requeue(old_pol, pol);
2171
Timo Teräsea2dea92010-03-31 00:17:05 +00002172 /* Unlinking succeeds always. This is the only function
2173 * allowed to delete or replace socket policy.
2174 */
Herbert Xu53c2e282014-11-13 17:09:49 +08002175 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002176 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02002177 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 if (old_pol) {
2180 xfrm_policy_kill(old_pol);
2181 }
2182 return 0;
2183}
2184
David S. Millerd3e40a92011-02-24 01:25:41 -05002185static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002187 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08002188 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 if (newp) {
2191 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07002192 if (security_xfrm_policy_clone(old->security,
2193 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08002194 kfree(newp);
2195 return NULL; /* ENOMEM */
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 newp->lft = old->lft;
2198 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08002199 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02002200 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 newp->action = old->action;
2202 newp->flags = old->flags;
2203 newp->xfrm_nr = old->xfrm_nr;
2204 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002205 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11002206 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 memcpy(newp->xfrm_vec, old->xfrm_vec,
2208 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02002209 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08002210 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002211 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 xfrm_pol_put(newp);
2213 }
2214 return newp;
2215}
2216
Eric Dumazetd188ba82015-12-08 07:22:02 -08002217int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Eric Dumazetd188ba82015-12-08 07:22:02 -08002219 const struct xfrm_policy *p;
2220 struct xfrm_policy *np;
2221 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Eric Dumazetd188ba82015-12-08 07:22:02 -08002223 rcu_read_lock();
2224 for (i = 0; i < 2; i++) {
2225 p = rcu_dereference(osk->sk_policy[i]);
2226 if (p) {
2227 np = clone_policy(p, i);
2228 if (unlikely(!np)) {
2229 ret = -ENOMEM;
2230 break;
2231 }
2232 rcu_assign_pointer(sk->sk_policy[i], np);
2233 }
2234 }
2235 rcu_read_unlock();
2236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002239static int
David Ahern42a7b322015-08-10 16:58:11 -06002240xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002241 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002242{
2243 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01002244 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002245
2246 if (unlikely(afinfo == NULL))
2247 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002248 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002249 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002250 return err;
2251}
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253/* Resolve list of templates for the flow, given policy. */
2254
2255static int
David S. Millera6c2e612011-02-22 18:35:39 -08002256xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2257 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08002259 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 int nx;
2261 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01002262 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2263 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002264 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Weilong Chen9b7a7872013-12-24 09:43:46 +08002266 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01002268 xfrm_address_t *remote = daddr;
2269 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2271
Steffen Klassert94802152017-11-15 06:40:57 +01002272 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2273 tmpl->mode == XFRM_MODE_BEET) {
2274 remote = &tmpl->id.daddr;
2275 local = &tmpl->saddr;
2276 if (xfrm_addr_any(local, tmpl->encap_family)) {
2277 error = xfrm_get_saddr(net, fl->flowi_oif,
2278 &tmp, remote,
2279 tmpl->encap_family, 0);
2280 if (error)
2281 goto fail;
2282 local = &tmp;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285
Benedict Wongbc56b332018-07-19 10:50:44 -07002286 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2287 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (x && x->km.state == XFRM_STATE_VALID) {
2290 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01002291 daddr = remote;
2292 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 continue;
2294 }
2295 if (x) {
2296 error = (x->km.state == XFRM_STATE_ERROR ?
2297 -EINVAL : -EAGAIN);
2298 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08002299 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00002300 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (!tmpl->optional)
2304 goto fail;
2305 }
2306 return nx;
2307
2308fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002309 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 xfrm_state_put(xfrm[nx]);
2311 return error;
2312}
2313
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002314static int
David S. Millera6c2e612011-02-22 18:35:39 -08002315xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2316 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002317{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002318 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2319 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002320 int cnx = 0;
2321 int error;
2322 int ret;
2323 int i;
2324
2325 for (i = 0; i < npols; i++) {
2326 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2327 error = -ENOBUFS;
2328 goto fail;
2329 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002330
2331 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002332 if (ret < 0) {
2333 error = ret;
2334 goto fail;
2335 } else
2336 cnx += ret;
2337 }
2338
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002339 /* found states are sorted for outbound processing */
2340 if (npols > 1)
2341 xfrm_state_sort(xfrm, tpp, cnx, family);
2342
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002343 return cnx;
2344
2345 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002346 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002347 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002348 return error;
2349
2350}
2351
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002352static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002353{
Florian Westphal37b10382017-02-07 15:00:19 +01002354 const struct xfrm_policy_afinfo *afinfo;
Xin Long143a4452018-02-17 15:16:22 +08002355 int tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08002356
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002357 afinfo = xfrm_policy_get_afinfo(family);
Xin Long143a4452018-02-17 15:16:22 +08002358 if (!afinfo)
2359 return 0;
2360
2361 tos = afinfo->get_tos(fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002362
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002363 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002364
2365 return tos;
2366}
2367
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002368static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002369{
Florian Westphal37b10382017-02-07 15:00:19 +01002370 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002371 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08002372 struct xfrm_dst *xdst;
2373
2374 if (!afinfo)
2375 return ERR_PTR(-EINVAL);
2376
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002377 switch (family) {
2378 case AF_INET:
2379 dst_ops = &net->xfrm.xfrm4_dst_ops;
2380 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002381#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002382 case AF_INET6:
2383 dst_ops = &net->xfrm.xfrm6_dst_ops;
2384 break;
2385#endif
2386 default:
2387 BUG();
2388 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07002389 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08002390
Madalin Bucurd4cae562011-09-26 07:04:36 +00002391 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00002392 struct dst_entry *dst = &xdst->u.dst;
2393
2394 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00002395 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08002396 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00002397
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002398 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00002399
Herbert Xu25ee3282007-12-11 09:32:34 -08002400 return xdst;
2401}
2402
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002403static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2404 int nfheader_len)
2405{
Florian Westphal37b10382017-02-07 15:00:19 +01002406 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002407 xfrm_policy_get_afinfo(dst->ops->family);
2408 int err;
2409
2410 if (!afinfo)
2411 return -EINVAL;
2412
2413 err = afinfo->init_path(path, dst, nfheader_len);
2414
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002415 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002416
2417 return err;
2418}
2419
Herbert Xu87c1e122010-03-02 02:51:56 +00002420static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08002421 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08002422{
Florian Westphal37b10382017-02-07 15:00:19 +01002423 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08002424 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2425 int err;
2426
2427 if (!afinfo)
2428 return -EINVAL;
2429
Herbert Xu87c1e122010-03-02 02:51:56 +00002430 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002431
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002432 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002433
2434 return err;
2435}
2436
Timo Teräs80c802f2010-04-07 00:30:05 +00002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/* Allocate chain of dst_entry's, attach known xfrm's, calculate
2439 * all the metrics... Shortly, bundle a bundle.
2440 */
2441
Herbert Xu25ee3282007-12-11 09:32:34 -08002442static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05002443 struct xfrm_state **xfrm,
2444 struct xfrm_dst **bundle,
2445 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08002446 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08002447 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002449 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08002450 unsigned long now = jiffies;
2451 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002452 struct xfrm_mode *inner_mode;
David Miller45b018be2017-11-28 15:40:28 -05002453 struct xfrm_dst *xdst_prev = NULL;
2454 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08002455 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08002457 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002458 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002459 int trailer_len = 0;
2460 int tos;
2461 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09002462 xfrm_address_t saddr, daddr;
2463
2464 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002465
2466 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002467
2468 dst_hold(dst);
2469
2470 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002471 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002472 struct dst_entry *dst1 = &xdst->u.dst;
2473
2474 err = PTR_ERR(xdst);
2475 if (IS_ERR(xdst)) {
2476 dst_release(dst);
2477 goto put_states;
2478 }
2479
David Miller54920932017-11-28 15:41:01 -05002480 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05002481 if (!xdst_prev)
2482 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07002483 else
2484 /* Ref count is taken during xfrm_alloc_dst()
2485 * No need to do dst_clone() on dst1
2486 */
David Miller45b018be2017-11-28 15:40:28 -05002487 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07002488
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002489 if (xfrm[i]->sel.family == AF_UNSPEC) {
2490 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2491 xfrm_af2proto(family));
2492 if (!inner_mode) {
2493 err = -EAFNOSUPPORT;
2494 dst_release(dst);
2495 goto put_states;
2496 }
2497 } else
2498 inner_mode = xfrm[i]->inner_mode;
2499
Herbert Xu25ee3282007-12-11 09:32:34 -08002500 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08002501 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08002502
2503 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002504 __u32 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
2505
Herbert Xu25ee3282007-12-11 09:32:34 -08002506 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06002507 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002508 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08002509 err = PTR_ERR(dst);
2510 if (IS_ERR(dst))
2511 goto put_states;
2512 } else
2513 dst_hold(dst);
2514
2515 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00002516 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08002517
David S. Millerf5b0a872012-07-19 12:31:33 -07002518 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08002519 dst1->flags |= DST_HOST;
2520 dst1->lastuse = now;
2521
2522 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002523 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08002524
David Miller45b018be2017-11-28 15:40:28 -05002525 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002526
2527 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002528 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2529 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002530 trailer_len += xfrm[i]->props.trailer_len;
2531 }
2532
David Miller45b018be2017-11-28 15:40:28 -05002533 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002534 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002535
2536 err = -ENODEV;
2537 dev = dst->dev;
2538 if (!dev)
2539 goto free_dst;
2540
David Miller45b018be2017-11-28 15:40:28 -05002541 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002542 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002543
David Miller45b018be2017-11-28 15:40:28 -05002544 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2545 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2546 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002547 if (err)
2548 goto free_dst;
2549
David Miller45b018be2017-11-28 15:40:28 -05002550 xdst_prev->u.dst.header_len = header_len;
2551 xdst_prev->u.dst.trailer_len = trailer_len;
2552 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2553 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002554 }
2555
David Miller45b018be2017-11-28 15:40:28 -05002556 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002557
2558put_states:
2559 for (; i < nx; i++)
2560 xfrm_state_put(xfrm[i]);
2561free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002562 if (xdst0)
2563 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002564
2565 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
David S. Miller73ff93c2011-02-22 18:33:42 -08002568static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002569 struct xfrm_policy **pols,
2570 int *num_pols, int *num_xfrms)
2571{
2572 int i;
2573
2574 if (*num_pols == 0 || !pols[0]) {
2575 *num_pols = 0;
2576 *num_xfrms = 0;
2577 return 0;
2578 }
2579 if (IS_ERR(pols[0]))
2580 return PTR_ERR(pols[0]);
2581
2582 *num_xfrms = pols[0]->xfrm_nr;
2583
2584#ifdef CONFIG_XFRM_SUB_POLICY
2585 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2586 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2587 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2588 XFRM_POLICY_TYPE_MAIN,
2589 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002590 XFRM_POLICY_OUT,
2591 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002592 if (pols[1]) {
2593 if (IS_ERR(pols[1])) {
2594 xfrm_pols_put(pols, *num_pols);
2595 return PTR_ERR(pols[1]);
2596 }
Weilong Chen02d08922013-12-24 09:43:48 +08002597 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002598 (*num_xfrms) += pols[1]->xfrm_nr;
2599 }
2600 }
2601#endif
2602 for (i = 0; i < *num_pols; i++) {
2603 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2604 *num_xfrms = -1;
2605 break;
2606 }
2607 }
2608
2609 return 0;
2610
2611}
2612
2613static struct xfrm_dst *
2614xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002615 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002616 struct dst_entry *dst_orig)
2617{
2618 struct net *net = xp_net(pols[0]);
2619 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002620 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002621 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002622 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002623 int err;
2624
2625 /* Try to instantiate a bundle */
2626 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002627 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002628 if (err == 0)
2629 return NULL;
2630
2631 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002632 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2633 return ERR_PTR(err);
2634 }
2635
David Miller54920932017-11-28 15:41:01 -05002636 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002637 if (IS_ERR(dst)) {
2638 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2639 return ERR_CAST(dst);
2640 }
2641
2642 xdst = (struct xfrm_dst *)dst;
2643 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002644 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002645 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002646 xdst->policy_genid = atomic_read(&pols[0]->genid);
2647
2648 return xdst;
2649}
2650
Kees Cookc3aed702017-10-16 17:28:56 -07002651static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002652{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002653 struct sk_buff *skb;
2654 struct sock *sk;
2655 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002656 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002657 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002658 struct xfrm_policy_queue *pq = &pol->polq;
2659 struct flowi fl;
2660 struct sk_buff_head list;
2661
2662 spin_lock(&pq->hold_queue.lock);
2663 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002664 if (!skb) {
2665 spin_unlock(&pq->hold_queue.lock);
2666 goto out;
2667 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002668 dst = skb_dst(skb);
2669 sk = skb->sk;
2670 xfrm_decode_session(skb, &fl, dst->ops->family);
2671 spin_unlock(&pq->hold_queue.lock);
2672
David Miller0f6c4802017-11-28 15:40:46 -05002673 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c982018-02-01 11:26:12 +01002674 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002675 if (IS_ERR(dst))
2676 goto purge_queue;
2677
2678 if (dst->flags & DST_XFRM_QUEUE) {
2679 dst_release(dst);
2680
2681 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2682 goto purge_queue;
2683
2684 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002685 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2686 xfrm_pol_hold(pol);
2687 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002688 }
2689
2690 dst_release(dst);
2691
2692 __skb_queue_head_init(&list);
2693
2694 spin_lock(&pq->hold_queue.lock);
2695 pq->timeout = 0;
2696 skb_queue_splice_init(&pq->hold_queue, &list);
2697 spin_unlock(&pq->hold_queue.lock);
2698
2699 while (!skb_queue_empty(&list)) {
2700 skb = __skb_dequeue(&list);
2701
2702 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002703 dst_hold(xfrm_dst_path(skb_dst(skb)));
2704 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002705 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002706 kfree_skb(skb);
2707 continue;
2708 }
2709
2710 nf_reset(skb);
2711 skb_dst_drop(skb);
2712 skb_dst_set(skb, dst);
2713
Eric W. Biederman13206b62015-10-07 16:48:35 -05002714 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002715 }
2716
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002717out:
2718 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002719 return;
2720
2721purge_queue:
2722 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002723 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002724 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002725}
2726
Eric W. Biedermanede20592015-10-07 16:48:47 -05002727static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002728{
2729 unsigned long sched_next;
2730 struct dst_entry *dst = skb_dst(skb);
2731 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002732 struct xfrm_policy *pol = xdst->pols[0];
2733 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002734
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002735 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002736 kfree_skb(skb);
2737 return 0;
2738 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002739
2740 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2741 kfree_skb(skb);
2742 return -EAGAIN;
2743 }
2744
2745 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002746
2747 spin_lock_bh(&pq->hold_queue.lock);
2748
2749 if (!pq->timeout)
2750 pq->timeout = XFRM_QUEUE_TMO_MIN;
2751
2752 sched_next = jiffies + pq->timeout;
2753
2754 if (del_timer(&pq->hold_timer)) {
2755 if (time_before(pq->hold_timer.expires, sched_next))
2756 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002757 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002758 }
2759
2760 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002761 if (!mod_timer(&pq->hold_timer, sched_next))
2762 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002763
2764 spin_unlock_bh(&pq->hold_queue.lock);
2765
2766 return 0;
2767}
2768
2769static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002770 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002771 const struct flowi *fl,
2772 int num_xfrms,
2773 u16 family)
2774{
2775 int err;
2776 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002777 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002778 struct dst_entry *dst1;
2779 struct xfrm_dst *xdst;
2780
2781 xdst = xfrm_alloc_dst(net, family);
2782 if (IS_ERR(xdst))
2783 return xdst;
2784
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002785 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2786 net->xfrm.sysctl_larval_drop ||
2787 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002788 return xdst;
2789
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002790 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002791 dst1 = &xdst->u.dst;
2792 dst_hold(dst);
2793 xdst->route = dst;
2794
2795 dst_copy_metrics(dst1, dst);
2796
2797 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2798 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2799 dst1->lastuse = jiffies;
2800
2801 dst1->input = dst_discard;
2802 dst1->output = xdst_queue_output;
2803
2804 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002805 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002806 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002807
2808 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2809
2810 err = -ENODEV;
2811 dev = dst->dev;
2812 if (!dev)
2813 goto free_dst;
2814
2815 err = xfrm_fill_dst(xdst, dev, fl);
2816 if (err)
2817 goto free_dst;
2818
2819out:
2820 return xdst;
2821
2822free_dst:
2823 dst_release(dst1);
2824 xdst = ERR_PTR(err);
2825 goto out;
2826}
2827
Benedict Wongbc56b332018-07-19 10:50:44 -07002828static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2829 const struct flowi *fl,
2830 u16 family, u8 dir,
2831 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002832{
Timo Teräs80c802f2010-04-07 00:30:05 +00002833 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002834 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002835 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002836
Timo Teräs80c802f2010-04-07 00:30:05 +00002837 /* Resolve policies to use if we couldn't get them from
2838 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002839 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002840 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002841 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002842 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002843 if (err < 0)
2844 goto inc_error;
2845 if (num_pols == 0)
2846 return NULL;
2847 if (num_xfrms <= 0)
2848 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002849
Florian Westphalbd45c532017-07-17 13:57:25 +02002850 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002851 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002852 if (IS_ERR(xdst)) {
2853 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002854 if (err == -EREMOTE) {
2855 xfrm_pols_put(pols, num_pols);
2856 return NULL;
2857 }
2858
Timo Teräs80c802f2010-04-07 00:30:05 +00002859 if (err != -EAGAIN)
2860 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002861 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002862 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002863 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002864 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002865 }
2866
Florian Westphalbd45c532017-07-17 13:57:25 +02002867 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002868
2869make_dummy_bundle:
2870 /* We found policies, but there's no bundles to instantiate:
2871 * either because the policy blocks, has no transformations or
2872 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002873 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002874 if (IS_ERR(xdst)) {
2875 xfrm_pols_put(pols, num_pols);
2876 return ERR_CAST(xdst);
2877 }
2878 xdst->num_pols = num_pols;
2879 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002880 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002881
Florian Westphalbd45c532017-07-17 13:57:25 +02002882 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002883
2884inc_error:
2885 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2886error:
Florian Westphal855dad92017-07-17 13:57:22 +02002887 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002888 return ERR_PTR(err);
2889}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
David S. Miller2774c132011-03-01 14:59:04 -08002891static struct dst_entry *make_blackhole(struct net *net, u16 family,
2892 struct dst_entry *dst_orig)
2893{
Florian Westphal37b10382017-02-07 15:00:19 +01002894 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002895 struct dst_entry *ret;
2896
2897 if (!afinfo) {
2898 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002899 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002900 } else {
2901 ret = afinfo->blackhole_route(net, dst_orig);
2902 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002903 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08002904
2905 return ret;
2906}
2907
Benedict Wongbc56b332018-07-19 10:50:44 -07002908/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 *
2910 * At the moment we eat a raw IP route. Mostly to speed up lookups
2911 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07002912 *
2913 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2914 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 */
Benedict Wongbc56b332018-07-19 10:50:44 -07002916struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
2917 struct dst_entry *dst_orig,
2918 const struct flowi *fl,
2919 const struct sock *sk,
2920 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002922 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002923 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002924 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002925 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02002926 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00002927 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002928
Timo Teräs80c802f2010-04-07 00:30:05 +00002929 dst = NULL;
2930 xdst = NULL;
2931 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002932
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002933 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002934 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002935 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002936 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
2937 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002938 err = xfrm_expand_policies(fl, family, pols,
2939 &num_pols, &num_xfrms);
2940 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002941 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002942
2943 if (num_pols) {
2944 if (num_xfrms <= 0) {
2945 drop_pols = num_pols;
2946 goto no_transform;
2947 }
2948
2949 xdst = xfrm_resolve_and_create_bundle(
2950 pols, num_pols, fl,
2951 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01002952
Timo Teräs80c802f2010-04-07 00:30:05 +00002953 if (IS_ERR(xdst)) {
2954 xfrm_pols_put(pols, num_pols);
2955 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002956 if (err == -EREMOTE)
2957 goto nopol;
2958
Timo Teräs80c802f2010-04-07 00:30:05 +00002959 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002960 } else if (xdst == NULL) {
2961 num_xfrms = 0;
2962 drop_pols = num_pols;
2963 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002964 }
2965
Timo Teräs80c802f2010-04-07 00:30:05 +00002966 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002967 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Timo Teräs80c802f2010-04-07 00:30:05 +00002970 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002971 struct xfrm_flo xflo;
2972
2973 xflo.dst_orig = dst_orig;
2974 xflo.flags = flags;
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002977 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002978 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002979 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Benedict Wongbc56b332018-07-19 10:50:44 -07002981 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02002982 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00002983 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02002984 if (IS_ERR(xdst)) {
2985 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002986 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002987 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002988
2989 num_pols = xdst->num_pols;
2990 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002991 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002992 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
2994
Timo Teräs80c802f2010-04-07 00:30:05 +00002995 dst = &xdst->u.dst;
2996 if (route == NULL && num_xfrms > 0) {
2997 /* The only case when xfrm_bundle_lookup() returns a
2998 * bundle with null route, is when the template could
2999 * not be resolved. It means policies are there, but
3000 * bundle could not be created, since we don't yet
3001 * have the xfrm_state's. We need to wait for KM to
3002 * negotiate new SA's or bail out with error.*/
3003 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003004 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01003005 err = -EREMOTE;
3006 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003007 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003008
Steffen Klassert5b8ef342013-08-27 13:43:30 +02003009 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00003010
3011 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3012 goto error;
3013 }
3014
3015no_transform:
3016 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08003017 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Timo Teräs80c802f2010-04-07 00:30:05 +00003019 if ((flags & XFRM_LOOKUP_ICMP) &&
3020 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3021 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08003022 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003023 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08003024
Timo Teräs80c802f2010-04-07 00:30:05 +00003025 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02003026 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08003027
Timo Teräs80c802f2010-04-07 00:30:05 +00003028 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003030 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07003031 err = -EPERM;
3032 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003033 } else if (num_xfrms > 0) {
3034 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00003035 dst_release(dst_orig);
3036 } else {
3037 /* Flow passes untransformed */
3038 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003039 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003041ok:
3042 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003043 if (dst && dst->xfrm &&
3044 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3045 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003046 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Timo Teräs80c802f2010-04-07 00:30:05 +00003048nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003049 if (!(flags & XFRM_LOOKUP_ICMP)) {
3050 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003051 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003052 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003053 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003055 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003056dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003057 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3058 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003059 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003060 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
Benedict Wongbc56b332018-07-19 10:50:44 -07003062EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3063
3064/* Main function: finds/creates a bundle for given flow.
3065 *
3066 * At the moment we eat a raw IP route. Mostly to speed up lookups
3067 * on interfaces with disabled IPsec.
3068 */
3069struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3070 const struct flowi *fl, const struct sock *sk,
3071 int flags)
3072{
3073 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075EXPORT_SYMBOL(xfrm_lookup);
3076
Steffen Klassertf92ee612014-09-16 10:08:40 +02003077/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3078 * Otherwise we may send out blackholed packets.
3079 */
3080struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3081 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003082 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003083{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003084 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003085 flags | XFRM_LOOKUP_QUEUE |
3086 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003087
3088 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
3089 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3090
Tommi Rantala8cc88772018-06-21 09:30:47 +03003091 if (IS_ERR(dst))
3092 dst_release(dst_orig);
3093
Steffen Klassertf92ee612014-09-16 10:08:40 +02003094 return dst;
3095}
3096EXPORT_SYMBOL(xfrm_lookup_route);
3097
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003098static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003099xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003100{
3101 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003102
3103 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
3104 return 0;
3105 x = skb->sp->xvec[idx];
3106 if (!x->type->reject)
3107 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003108 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003109}
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111/* When skb is transformed back to its "native" form, we have to
3112 * check policy restrictions. At the moment we make this in maximally
3113 * stupid way. Shame on me. :-) Of course, connected sockets must
3114 * have policy cached at them.
3115 */
3116
3117static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003118xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 unsigned short family)
3120{
3121 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003122 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return x->id.proto == tmpl->id.proto &&
3124 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3125 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3126 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003127 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003128 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003129 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3130 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003133/*
3134 * 0 or more than 0 is returned when validation is succeeded (either bypass
3135 * because of optional transport mode, or next index of the mathced secpath
3136 * state with the template.
3137 * -1 is returned when no matching template is found.
3138 * Otherwise "-2 - errored_index" is returned.
3139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003141xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 unsigned short family)
3143{
3144 int idx = start;
3145
3146 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003147 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return start;
3149 } else
3150 start = -1;
3151 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003152 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003154 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3155 if (start == -1)
3156 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160 return start;
3161}
3162
Herbert Xud5422ef2007-12-12 10:44:16 -08003163int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3164 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Florian Westphal37b10382017-02-07 15:00:19 +01003166 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003167 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 if (unlikely(afinfo == NULL))
3170 return -EAFNOSUPPORT;
3171
Herbert Xud5422ef2007-12-12 10:44:16 -08003172 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf203b762018-06-12 14:07:12 +02003173
David S. Miller1d28f422011-03-12 00:29:39 -05003174 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003175 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003176 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
Herbert Xud5422ef2007-12-12 10:44:16 -08003178EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
David S. Miller9a7386e2011-02-24 01:44:12 -05003180static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
3182 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003183 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003184 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188
3189 return 0;
3190}
3191
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003192int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 unsigned short family)
3194{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003195 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003197 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3198 int npols = 0;
3199 int xfrm_nr;
3200 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003201 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003203 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003204 const struct xfrm_if_cb *ifcb;
3205 struct xfrm_if *xi;
3206 u32 if_id = 0;
3207
3208 rcu_read_lock();
3209 ifcb = xfrm_if_get_cb();
3210
3211 if (ifcb) {
3212 xi = ifcb->decode_session(skb);
3213 if (xi)
3214 if_id = xi->p.if_id;
3215 }
3216 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Herbert Xud5422ef2007-12-12 10:44:16 -08003218 reverse = dir & ~XFRM_POLICY_MASK;
3219 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003220
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003221 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003222 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003224 }
3225
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003226 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 /* First, check used SA against their selectors. */
3229 if (skb->sp) {
3230 int i;
3231
Weilong Chen9b7a7872013-12-24 09:43:46 +08003232 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003233 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003234 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003235 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
3239 }
3240
3241 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003242 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003243 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003244 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003245 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003246 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003247 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003248 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003251 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003252 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003254 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003255 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003256 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003257 }
James Morris134b0fc2006-10-05 15:42:27 -05003258
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003259 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003260 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003261 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003262 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003263 return 0;
3264 }
3265 return 1;
3266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Arnd Bergmann386c5682018-07-11 12:19:13 +02003268 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003270 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003271 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003272#ifdef CONFIG_XFRM_SUB_POLICY
3273 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003274 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003275 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003276 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003277 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003278 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003279 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003280 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003281 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003282 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003283 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003284 }
3285 }
3286#endif
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (pol->action == XFRM_POLICY_ALLOW) {
3289 struct sec_path *sp;
3290 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003291 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003292 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003293 struct xfrm_tmpl **tpp = tp;
3294 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 int i, k;
3296
3297 if ((sp = skb->sp) == NULL)
3298 sp = &dummy;
3299
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003300 for (pi = 0; pi < npols; pi++) {
3301 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003302 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003303 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003304 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003305 }
3306 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003307 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003308 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003309 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003310 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3311 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3312 }
3313 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003314 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08003315 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003316 tpp = stp;
3317 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 /* For each tunnel xfrm, find the first matching tmpl.
3320 * For each tmpl before that, find corresponding xfrm.
3321 * Order is _important_. Later we will implement
3322 * some barriers, but at the moment barriers
3323 * are implied between each two transformations.
3324 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003325 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3326 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003327 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003328 if (k < -1)
3329 /* "-2 - errored_index" returned */
3330 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003331 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
3335
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003336 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003337 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003341 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return 1;
3343 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003344 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
3346reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003347 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003348reject_error:
3349 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return 0;
3351}
3352EXPORT_SYMBOL(__xfrm_policy_check);
3353
3354int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3355{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003356 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003358 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003359 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003361 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003362 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003366 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02003367 if (!skb_dst(skb)) {
3368 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3369 return 0;
3370 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003371
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003372 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003373 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003374 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003375 dst = NULL;
3376 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003377 skb_dst_set(skb, dst);
3378 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380EXPORT_SYMBOL(__xfrm_route_forward);
3381
David S. Millerd49c73c2006-08-13 18:55:53 -07003382/* Optimize later using cookies and generation ids. */
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3385{
David S. Millerd49c73c2006-08-13 18:55:53 -07003386 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003387 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3388 * get validated by dst_ops->check on every use. We do this
3389 * because when a normal route referenced by an XFRM dst is
3390 * obsoleted we do not go looking around for all parent
3391 * referencing XFRM dsts so that we can invalidate them. It
3392 * is just too much work. Instead we make the checks here on
3393 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003394 *
3395 * XFRM dst A --> IPv4 dst X
3396 *
3397 * X is the "xdst->route" of A (X is also the "dst->path" of A
3398 * in this example). If X is marked obsolete, "A" will not
3399 * notice. That's what we are validating here via the
3400 * stale_bundle() check.
3401 *
Wei Wang52df1572017-06-17 10:42:38 -07003402 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3403 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003404 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003405 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003406 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003407 if (dst->obsolete < 0 && !stale_bundle(dst))
3408 return dst;
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return NULL;
3411}
3412
3413static int stale_bundle(struct dst_entry *dst)
3414{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003415 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Herbert Xuaabc9762005-05-03 16:27:10 -07003418void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
David Millerb92cf4a2017-11-28 15:40:22 -05003420 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003421 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003422 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 dev_put(dev);
3424 }
3425}
Herbert Xuaabc9762005-05-03 16:27:10 -07003426EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428static void xfrm_link_failure(struct sk_buff *skb)
3429{
3430 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
3433static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3434{
3435 if (dst) {
3436 if (dst->obsolete) {
3437 dst_release(dst);
3438 dst = NULL;
3439 }
3440 }
3441 return dst;
3442}
3443
David Miller54920932017-11-28 15:41:01 -05003444static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
David Miller54920932017-11-28 15:41:01 -05003446 while (nr--) {
3447 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003449 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
David Miller54920932017-11-28 15:41:01 -05003451 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003452 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 xdst->child_mtu_cached = pmtu;
3454
3455 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3456
3457 route_mtu_cached = dst_mtu(xdst->route);
3458 xdst->route_mtu_cached = route_mtu_cached;
3459
3460 if (pmtu > route_mtu_cached)
3461 pmtu = route_mtu_cached;
3462
David S. Millerdefb3512010-12-08 21:16:57 -08003463 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467/* Check that the bundle accepts the flow and its components are
3468 * still valid.
3469 */
3470
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003471static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
David Miller54920932017-11-28 15:41:01 -05003473 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003475 struct xfrm_dst *xdst;
3476 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 u32 mtu;
3478
David Miller0f6c4802017-11-28 15:40:46 -05003479 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 (dst->dev && !netif_running(dst->dev)))
3481 return 0;
3482
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003483 if (dst->flags & DST_XFRM_QUEUE)
3484 return 1;
3485
David Miller54920932017-11-28 15:41:01 -05003486 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 do {
3488 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3491 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003492 if (xdst->xfrm_genid != dst->xfrm->genid)
3493 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003494 if (xdst->num_pols > 0 &&
3495 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
David Miller54920932017-11-28 15:41:01 -05003498 bundle[nr++] = xdst;
3499
David Millerb92cf4a2017-11-28 15:40:22 -05003500 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003502 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 xdst->child_mtu_cached = mtu;
3504 }
3505
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003506 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return 0;
3508 mtu = dst_mtu(xdst->route);
3509 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003510 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 xdst->route_mtu_cached = mtu;
3512 }
3513
David Millerb92cf4a2017-11-28 15:40:22 -05003514 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 } while (dst->xfrm);
3516
David Miller54920932017-11-28 15:41:01 -05003517 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return 1;
3519
David Miller54920932017-11-28 15:41:01 -05003520 xdst = bundle[start_from - 1];
3521 mtu = xdst->child_mtu_cached;
3522 while (start_from--) {
3523 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003526 if (mtu > xdst->route_mtu_cached)
3527 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003528 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003529 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 break;
3531
David Miller54920932017-11-28 15:41:01 -05003532 xdst = bundle[start_from - 1];
3533 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 }
3535
3536 return 1;
3537}
3538
David S. Miller0dbaee32010-12-13 12:52:14 -08003539static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3540{
David Miller0f6c4802017-11-28 15:40:46 -05003541 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003542}
3543
Steffen Klassertebb762f2011-11-23 02:12:51 +00003544static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003545{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003546 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3547
David Miller0f6c4802017-11-28 15:40:46 -05003548 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003549}
3550
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003551static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3552 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003553{
David Miller0f6c4802017-11-28 15:40:46 -05003554 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003555 const struct xfrm_state *xfrm = dst->xfrm;
3556
Steffen Klassert013cb812018-02-19 07:44:07 +01003557 dst = xfrm_dst_child(dst);
3558
Julian Anastasov63fca652017-02-06 23:14:15 +02003559 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3560 continue;
3561 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3562 daddr = xfrm->coaddr;
3563 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3564 daddr = &xfrm->id.daddr;
3565 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003566 return daddr;
3567}
3568
3569static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3570 struct sk_buff *skb,
3571 const void *daddr)
3572{
David Miller0f6c4802017-11-28 15:40:46 -05003573 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003574
3575 if (!skb)
3576 daddr = xfrm_get_dst_nexthop(dst, daddr);
3577 return path->ops->neigh_lookup(path, skb, daddr);
3578}
3579
3580static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3581{
David Miller0f6c4802017-11-28 15:40:46 -05003582 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003583
3584 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003585 path->ops->confirm_neigh(path, daddr);
3586}
3587
Florian Westphala2817d82017-02-07 15:00:17 +01003588int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
3590 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003591
3592 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003594
Eric Dumazetef8531b2012-08-19 12:31:48 +02003595 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003596 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003597 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 else {
3599 struct dst_ops *dst_ops = afinfo->dst_ops;
3600 if (likely(dst_ops->kmem_cachep == NULL))
3601 dst_ops->kmem_cachep = xfrm_dst_cache;
3602 if (likely(dst_ops->check == NULL))
3603 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003604 if (likely(dst_ops->default_advmss == NULL))
3605 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003606 if (likely(dst_ops->mtu == NULL))
3607 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 if (likely(dst_ops->negative_advice == NULL))
3609 dst_ops->negative_advice = xfrm_negative_advice;
3610 if (likely(dst_ops->link_failure == NULL))
3611 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003612 if (likely(dst_ops->neigh_lookup == NULL))
3613 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003614 if (likely(!dst_ops->confirm_neigh))
3615 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003616 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003618 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 return err;
3621}
3622EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3623
Florian Westphala2817d82017-02-07 15:00:17 +01003624void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
Florian Westphal2b619972017-02-07 15:00:15 +01003626 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003627 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003628
Florian Westphala2817d82017-02-07 15:00:17 +01003629 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3630 if (xfrm_policy_afinfo[i] != afinfo)
3631 continue;
3632 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3633 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Florian Westphal2b619972017-02-07 15:00:15 +01003636 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Florian Westphal2b619972017-02-07 15:00:15 +01003638 dst_ops->kmem_cachep = NULL;
3639 dst_ops->check = NULL;
3640 dst_ops->negative_advice = NULL;
3641 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642}
3643EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3644
Steffen Klassertf203b762018-06-12 14:07:12 +02003645void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3646{
3647 spin_lock(&xfrm_if_cb_lock);
3648 rcu_assign_pointer(xfrm_if_cb, ifcb);
3649 spin_unlock(&xfrm_if_cb_lock);
3650}
3651EXPORT_SYMBOL(xfrm_if_register_cb);
3652
3653void xfrm_if_unregister_cb(void)
3654{
3655 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3656 synchronize_rcu();
3657}
3658EXPORT_SYMBOL(xfrm_if_unregister_cb);
3659
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003660#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003661static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003662{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003663 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003664 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3665 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003666 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003667 rv = xfrm_proc_init(net);
3668 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003669 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003670 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003671}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003672
3673static void xfrm_statistics_fini(struct net *net)
3674{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003675 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003676 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003677}
3678#else
3679static int __net_init xfrm_statistics_init(struct net *net)
3680{
3681 return 0;
3682}
3683
3684static void xfrm_statistics_fini(struct net *net)
3685{
3686}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003687#endif
3688
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003689static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690{
David S. Miller2518c7c2006-08-24 04:45:07 -07003691 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01003692 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07003693
Florian Westphal24969fa2018-11-07 23:00:35 +01003694 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003695 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003697 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003698 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01003699 err = rhashtable_init(&xfrm_policy_inexact_table,
3700 &xfrm_pol_inexact_params);
3701 BUG_ON(err);
3702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
David S. Miller2518c7c2006-08-24 04:45:07 -07003704 hmask = 8 - 1;
3705 sz = (hmask+1) * sizeof(struct hlist_head);
3706
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003707 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3708 if (!net->xfrm.policy_byidx)
3709 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003710 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07003711
Herbert Xu53c2e282014-11-13 17:09:49 +08003712 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07003713 struct xfrm_policy_hash *htab;
3714
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08003715 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08003716 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003717 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07003718
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003719 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07003720 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07003721 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003722 goto out_bydst;
3723 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02003724 htab->dbits4 = 32;
3725 htab->sbits4 = 32;
3726 htab->dbits6 = 128;
3727 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003728 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003729 net->xfrm.policy_hthresh.lbits4 = 32;
3730 net->xfrm.policy_hthresh.rbits4 = 32;
3731 net->xfrm.policy_hthresh.lbits6 = 128;
3732 net->xfrm.policy_hthresh.rbits6 = 128;
3733
3734 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003735
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003736 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01003737 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003738 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003739 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003740 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003741
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003742out_bydst:
3743 for (dir--; dir >= 0; dir--) {
3744 struct xfrm_policy_hash *htab;
3745
3746 htab = &net->xfrm.policy_bydst[dir];
3747 xfrm_hash_free(htab->table, sz);
3748 }
3749 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003750out_byidx:
3751 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752}
3753
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003754static void xfrm_policy_fini(struct net *net)
3755{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01003756 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003757 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003758 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003759
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003760 flush_work(&net->xfrm.policy_hash_work);
3761#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003762 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003763#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003764 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003765
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003766 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003767
Herbert Xu53c2e282014-11-13 17:09:49 +08003768 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003769 struct xfrm_policy_hash *htab;
3770
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003771 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003772
3773 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003774 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003775 WARN_ON(!hlist_empty(htab->table));
3776 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003777 }
3778
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003779 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003780 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3781 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01003782
Florian Westphal6be3b0d2018-11-07 23:00:37 +01003783 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
3784 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
3785 __xfrm_policy_inexact_prune_bin(b, true);
3786 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003787}
3788
3789static int __net_init xfrm_net_init(struct net *net)
3790{
3791 int rv;
3792
Florian Westphalc2822222017-02-08 11:52:29 +01003793 /* Initialize the per-net locks here */
3794 spin_lock_init(&net->xfrm.xfrm_state_lock);
3795 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3796 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3797
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003798 rv = xfrm_statistics_init(net);
3799 if (rv < 0)
3800 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003801 rv = xfrm_state_init(net);
3802 if (rv < 0)
3803 goto out_state;
3804 rv = xfrm_policy_init(net);
3805 if (rv < 0)
3806 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003807 rv = xfrm_sysctl_init(net);
3808 if (rv < 0)
3809 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08003810
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003811 return 0;
3812
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003813out_sysctl:
3814 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003815out_policy:
3816 xfrm_state_fini(net);
3817out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003818 xfrm_statistics_fini(net);
3819out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003820 return rv;
3821}
3822
3823static void __net_exit xfrm_net_exit(struct net *net)
3824{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003825 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003826 xfrm_policy_fini(net);
3827 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003828 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003829}
3830
3831static struct pernet_operations __net_initdata xfrm_net_ops = {
3832 .init = xfrm_net_init,
3833 .exit = xfrm_net_exit,
3834};
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836void __init xfrm_init(void)
3837{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003838 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03003839 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02003840 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02003842
3843 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3844 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
Joy Lattenab5f5e82007-09-17 11:51:22 -07003847#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003848static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3849 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003850{
Paul Moore875179f2007-12-01 23:27:18 +11003851 struct xfrm_sec_ctx *ctx = xp->security;
3852 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003853
Paul Moore875179f2007-12-01 23:27:18 +11003854 if (ctx)
3855 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3856 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3857
Weilong Chen9b7a7872013-12-24 09:43:46 +08003858 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003859 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003860 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003861 if (sel->prefixlen_s != 32)
3862 audit_log_format(audit_buf, " src_prefixlen=%d",
3863 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003864 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003865 if (sel->prefixlen_d != 32)
3866 audit_log_format(audit_buf, " dst_prefixlen=%d",
3867 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003868 break;
3869 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003870 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003871 if (sel->prefixlen_s != 128)
3872 audit_log_format(audit_buf, " src_prefixlen=%d",
3873 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003874 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003875 if (sel->prefixlen_d != 128)
3876 audit_log_format(audit_buf, " dst_prefixlen=%d",
3877 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003878 break;
3879 }
3880}
3881
Tetsuo Handa2e710292014-04-22 21:48:30 +09003882void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003883{
3884 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003885
Paul Mooreafeb14b2007-12-21 14:58:11 -08003886 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003887 if (audit_buf == NULL)
3888 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003889 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003890 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003891 xfrm_audit_common_policyinfo(xp, audit_buf);
3892 audit_log_end(audit_buf);
3893}
3894EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3895
Paul Moore68277ac2007-12-20 20:49:33 -08003896void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003897 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003898{
3899 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003900
Paul Mooreafeb14b2007-12-21 14:58:11 -08003901 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003902 if (audit_buf == NULL)
3903 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003904 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003905 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003906 xfrm_audit_common_policyinfo(xp, audit_buf);
3907 audit_log_end(audit_buf);
3908}
3909EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3910#endif
3911
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003912#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003913static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3914 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003915{
3916 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3917 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003918 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3919 sel_cmp->family) &&
3920 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3921 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003922 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3923 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003924 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003925 }
3926 } else {
3927 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003928 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003929 }
3930 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003931 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003932}
3933
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003934static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3935 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003936{
3937 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003938 struct hlist_head *chain;
3939 u32 priority = ~0U;
3940
Florian Westphal9d0380d2016-08-11 15:17:59 +02003941 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003942 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003943 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003944 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3945 pol->type == type) {
3946 ret = pol;
3947 priority = ret->priority;
3948 break;
3949 }
3950 }
Fan Du8d549c42013-11-07 17:47:49 +08003951 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01003952 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08003953 if ((pol->priority >= priority) && ret)
3954 break;
3955
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003956 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003957 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003958 ret = pol;
3959 break;
3960 }
3961 }
3962
Li RongQing586f2eb2015-04-30 17:13:41 +08003963 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003964
Florian Westphal9d0380d2016-08-11 15:17:59 +02003965 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003966
3967 return ret;
3968}
3969
David S. Millerdd701752011-02-24 00:21:08 -05003970static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003971{
3972 int match = 0;
3973
3974 if (t->mode == m->mode && t->id.proto == m->proto &&
3975 (m->reqid == 0 || t->reqid == m->reqid)) {
3976 switch (t->mode) {
3977 case XFRM_MODE_TUNNEL:
3978 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003979 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3980 m->old_family) &&
3981 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3982 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003983 match = 1;
3984 }
3985 break;
3986 case XFRM_MODE_TRANSPORT:
3987 /* in case of transport mode, template does not store
3988 any IP addresses, hence we just compare mode and
3989 protocol */
3990 match = 1;
3991 break;
3992 default:
3993 break;
3994 }
3995 }
3996 return match;
3997}
3998
3999/* update endpoint address(es) of template(s) */
4000static int xfrm_policy_migrate(struct xfrm_policy *pol,
4001 struct xfrm_migrate *m, int num_migrate)
4002{
4003 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004004 int i, j, n = 0;
4005
4006 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07004007 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004008 /* target policy has been deleted */
4009 write_unlock_bh(&pol->lock);
4010 return -ENOENT;
4011 }
4012
4013 for (i = 0; i < pol->xfrm_nr; i++) {
4014 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4015 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4016 continue;
4017 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07004018 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4019 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004020 continue;
4021 /* update endpoints */
4022 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4023 sizeof(pol->xfrm_vec[i].id.daddr));
4024 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4025 sizeof(pol->xfrm_vec[i].saddr));
4026 pol->xfrm_vec[i].encap_family = mp->new_family;
4027 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00004028 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004029 }
4030 }
4031
4032 write_unlock_bh(&pol->lock);
4033
4034 if (!n)
4035 return -ENODATA;
4036
4037 return 0;
4038}
4039
David S. Millerdd701752011-02-24 00:21:08 -05004040static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004041{
4042 int i, j;
4043
4044 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4045 return -EINVAL;
4046
4047 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004048 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4049 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4050 return -EINVAL;
4051
4052 /* check if there is any duplicated entry */
4053 for (j = i + 1; j < num_migrate; j++) {
4054 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4055 sizeof(m[i].old_daddr)) &&
4056 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4057 sizeof(m[i].old_saddr)) &&
4058 m[i].proto == m[j].proto &&
4059 m[i].mode == m[j].mode &&
4060 m[i].reqid == m[j].reqid &&
4061 m[i].old_family == m[j].old_family)
4062 return -EINVAL;
4063 }
4064 }
4065
4066 return 0;
4067}
4068
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004069int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004070 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004071 struct xfrm_kmaddress *k, struct net *net,
4072 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004073{
4074 int i, err, nx_cur = 0, nx_new = 0;
4075 struct xfrm_policy *pol = NULL;
4076 struct xfrm_state *x, *xc;
4077 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4078 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4079 struct xfrm_migrate *mp;
4080
Vladis Dronov7bab0962017-08-02 19:50:14 +02004081 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004082 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4083 goto out;
4084
Vladis Dronov7bab0962017-08-02 19:50:14 +02004085 if (dir >= XFRM_POLICY_MAX) {
4086 err = -EINVAL;
4087 goto out;
4088 }
4089
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004090 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004091 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004092 err = -ENOENT;
4093 goto out;
4094 }
4095
4096 /* Stage 2 - find and update state(s) */
4097 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004098 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004099 x_cur[nx_cur] = x;
4100 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004101 xc = xfrm_state_migrate(x, mp, encap);
4102 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004103 x_new[nx_new] = xc;
4104 nx_new++;
4105 } else {
4106 err = -ENODATA;
4107 goto restore_state;
4108 }
4109 }
4110 }
4111
4112 /* Stage 3 - update policy */
4113 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4114 goto restore_state;
4115
4116 /* Stage 4 - delete old state(s) */
4117 if (nx_cur) {
4118 xfrm_states_put(x_cur, nx_cur);
4119 xfrm_states_delete(x_cur, nx_cur);
4120 }
4121
4122 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004123 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004124
4125 xfrm_pol_put(pol);
4126
4127 return 0;
4128out:
4129 return err;
4130
4131restore_state:
4132 if (pol)
4133 xfrm_pol_put(pol);
4134 if (nx_cur)
4135 xfrm_states_put(x_cur, nx_cur);
4136 if (nx_new)
4137 xfrm_states_delete(x_new, nx_new);
4138
4139 return err;
4140}
David S. Millere610e672007-02-08 13:29:15 -08004141EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004142#endif