blob: 16e70fc547b13670a6a1767169bc063ae1921711 [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
Florian Westphal6ac098b2018-11-07 23:00:41 +010061 struct rb_root root;
62
Florian Westphal9cf545e2018-11-07 23:00:38 +010063 /* the policies matching this node, can be empty list */
64 struct hlist_head hhead;
65};
66
67/* xfrm inexact policy search tree:
68 * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
69 * |
70 * +---- root_d: sorted by daddr:prefix
71 * | |
72 * | xfrm_pol_inexact_node
73 * | |
Florian Westphal6ac098b2018-11-07 23:00:41 +010074 * | +- root: sorted by saddr/prefix
75 * | | |
76 * | | xfrm_pol_inexact_node
77 * | | |
78 * | | + root: unused
79 * | | |
80 * | | + hhead: saddr:daddr policies
81 * | |
Florian Westphal9cf545e2018-11-07 23:00:38 +010082 * | +- coarse policies and all any:daddr policies
83 * |
Florian Westphal64a09a72018-11-07 23:00:40 +010084 * +---- root_s: sorted by saddr:prefix
85 * | |
86 * | xfrm_pol_inexact_node
87 * | |
88 * | + root: unused
89 * | |
90 * | + hhead: saddr:any policies
91 * |
Florian Westphal9cf545e2018-11-07 23:00:38 +010092 * +---- coarse policies and all any:any policies
93 *
Florian Westphal6ac098b2018-11-07 23:00:41 +010094 * Lookups return four candidate lists:
Florian Westphal9cf545e2018-11-07 23:00:38 +010095 * 1. any:any list from top-level xfrm_pol_inexact_bin
96 * 2. any:daddr list from daddr tree
Florian Westphal6ac098b2018-11-07 23:00:41 +010097 * 3. saddr:daddr list from 2nd level daddr tree
98 * 4. saddr:any list from saddr tree
Florian Westphal9cf545e2018-11-07 23:00:38 +010099 *
100 * This result set then needs to be searched for the policy with
101 * the lowest priority. If two results have same prio, youngest one wins.
102 */
103
Florian Westphal24969fa2018-11-07 23:00:35 +0100104struct xfrm_pol_inexact_key {
105 possible_net_t net;
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100106 u32 if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +0100107 u16 family;
108 u8 dir, type;
109};
110
111struct xfrm_pol_inexact_bin {
112 struct xfrm_pol_inexact_key k;
113 struct rhash_head head;
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100114 /* list containing '*:*' policies */
Florian Westphal24969fa2018-11-07 23:00:35 +0100115 struct hlist_head hhead;
116
Florian Westphal9cf545e2018-11-07 23:00:38 +0100117 seqcount_t count;
118 /* tree sorted by daddr/prefix */
119 struct rb_root root_d;
120
Florian Westphal64a09a72018-11-07 23:00:40 +0100121 /* tree sorted by saddr/prefix */
122 struct rb_root root_s;
123
Florian Westphal24969fa2018-11-07 23:00:35 +0100124 /* slow path below */
125 struct list_head inexact_bins;
126 struct rcu_head rcu;
127};
128
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100129enum xfrm_pol_inexact_candidate_type {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100130 XFRM_POL_CAND_BOTH,
Florian Westphal64a09a72018-11-07 23:00:40 +0100131 XFRM_POL_CAND_SADDR,
Florian Westphal9cf545e2018-11-07 23:00:38 +0100132 XFRM_POL_CAND_DADDR,
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100133 XFRM_POL_CAND_ANY,
134
135 XFRM_POL_CAND_MAX,
136};
137
138struct xfrm_pol_inexact_candidates {
139 struct hlist_head *res[XFRM_POL_CAND_MAX];
140};
141
Steffen Klassertf203b762018-06-12 14:07:12 +0200142static DEFINE_SPINLOCK(xfrm_if_cb_lock);
143static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
144
Priyanka Jain418a99a2012-08-12 21:22:29 +0000145static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +0100146static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +0000147 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +0300149static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +0200150static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Florian Westphal24969fa2018-11-07 23:00:35 +0100152static struct rhashtable xfrm_policy_inexact_table;
153static const struct rhashtable_params xfrm_pol_inexact_params;
154
David Miller54920932017-11-28 15:41:01 -0500155static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +0000156static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +0000157static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -0700158static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800160static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800161static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
162 int dir);
163
Florian Westphal24969fa2018-11-07 23:00:35 +0100164static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100165xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
166 u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100167
168static struct xfrm_pol_inexact_bin *
169xfrm_policy_inexact_lookup_rcu(struct net *net,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100170 u8 type, u16 family, u8 dir, u32 if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +0100171static struct xfrm_policy *
172xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
173 bool excl);
174static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
175 struct xfrm_policy *policy);
176
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100177static bool
178xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
179 struct xfrm_pol_inexact_bin *b,
180 const xfrm_address_t *saddr,
181 const xfrm_address_t *daddr);
182
Florian Westphale37cc8a2016-08-11 15:17:55 +0200183static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
184{
Reshetova, Elena850a6212017-07-04 15:53:22 +0300185 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8a2016-08-11 15:17:55 +0200186}
187
David S. Millerbc9b35a2012-05-15 15:04:57 -0400188static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500189__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800190{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500191 const struct flowi4 *fl4 = &fl->u.ip4;
192
Alexey Dobriyan26bff942011-11-22 06:46:02 +0000193 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
194 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500195 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
196 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
197 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
198 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800199}
200
David S. Millerbc9b35a2012-05-15 15:04:57 -0400201static inline bool
David S. Miller200ce962011-02-24 00:12:25 -0500202__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -0800203{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500204 const struct flowi6 *fl6 = &fl->u.ip6;
205
206 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
207 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
208 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
209 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
210 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
211 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -0800212}
213
David S. Millerbc9b35a2012-05-15 15:04:57 -0400214bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
215 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800216{
217 switch (family) {
218 case AF_INET:
219 return __xfrm4_selector_match(sel, fl);
220 case AF_INET6:
221 return __xfrm6_selector_match(sel, fl);
222 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400223 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800224}
225
Florian Westphala2817d82017-02-07 15:00:17 +0100226static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200227{
Florian Westphala2817d82017-02-07 15:00:17 +0100228 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200229
Florian Westphala2817d82017-02-07 15:00:17 +0100230 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200231 return NULL;
232 rcu_read_lock();
233 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
234 if (unlikely(!afinfo))
235 rcu_read_unlock();
236 return afinfo;
237}
238
Steffen Klassertf203b762018-06-12 14:07:12 +0200239/* Called with rcu_read_lock(). */
240static const struct xfrm_if_cb *xfrm_if_get_cb(void)
241{
242 return rcu_dereference(xfrm_if_cb);
243}
244
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200245struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
246 const xfrm_address_t *saddr,
247 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900248 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Florian Westphal37b10382017-02-07 15:00:19 +0100250 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800251 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Herbert Xu25ee3282007-12-11 09:32:34 -0800253 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800255 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900257 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900258
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100259 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900260
261 return dst;
262}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200263EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900264
David Ahern42a7b322015-08-10 16:58:11 -0600265static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
266 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900267 xfrm_address_t *prev_saddr,
268 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900269 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900270{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800271 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900272 xfrm_address_t *saddr = &x->props.saddr;
273 xfrm_address_t *daddr = &x->id.daddr;
274 struct dst_entry *dst;
275
276 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
277 saddr = x->coaddr;
278 daddr = prev_daddr;
279 }
280 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
281 saddr = prev_saddr;
282 daddr = x->coaddr;
283 }
284
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900285 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900286
287 if (!IS_ERR(dst)) {
288 if (prev_saddr != saddr)
289 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
290 if (prev_daddr != daddr)
291 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
292 }
293
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800294 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static inline unsigned long make_jiffies(long secs)
298{
299 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
300 return MAX_SCHEDULE_TIMEOUT-1;
301 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900302 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Kees Cookc3aed702017-10-16 17:28:56 -0700305static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Kees Cookc3aed702017-10-16 17:28:56 -0700307 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200308 time64_t now = ktime_get_real_seconds();
309 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int warn = 0;
311 int dir;
312
313 read_lock(&xp->lock);
314
Timo Teräsea2dea92010-03-31 00:17:05 +0000315 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto out;
317
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700318 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200321 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 xp->curlft.add_time - now;
323 if (tmo <= 0)
324 goto expired;
325 if (tmo < next)
326 next = tmo;
327 }
328 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200329 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
331 if (tmo <= 0)
332 goto expired;
333 if (tmo < next)
334 next = tmo;
335 }
336 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200337 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xp->curlft.add_time - now;
339 if (tmo <= 0) {
340 warn = 1;
341 tmo = XFRM_KM_TIMEOUT;
342 }
343 if (tmo < next)
344 next = tmo;
345 }
346 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200347 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
349 if (tmo <= 0) {
350 warn = 1;
351 tmo = XFRM_KM_TIMEOUT;
352 }
353 if (tmo < next)
354 next = tmo;
355 }
356
357 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800358 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200359 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
361 xfrm_pol_hold(xp);
362
363out:
364 read_unlock(&xp->lock);
365 xfrm_pol_put(xp);
366 return;
367
368expired:
369 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700370 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800371 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xfrm_pol_put(xp);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
376 * SPD calls.
377 */
378
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800379struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct xfrm_policy *policy;
382
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700383 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800386 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700387 INIT_LIST_HEAD(&policy->walk.all);
Florian Westphal24969fa2018-11-07 23:00:35 +0100388 INIT_HLIST_NODE(&policy->bydst_inexact_list);
David S. Miller2518c7c2006-08-24 04:45:07 -0700389 INIT_HLIST_NODE(&policy->bydst);
390 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300392 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100393 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700394 timer_setup(&policy->timer, xfrm_policy_timer, 0);
395 timer_setup(&policy->polq.hold_timer,
396 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 return policy;
399}
400EXPORT_SYMBOL(xfrm_policy_alloc);
401
Eric Dumazet56f04732015-12-08 07:22:01 -0800402static void xfrm_policy_destroy_rcu(struct rcu_head *head)
403{
404 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
405
406 security_xfrm_policy_free(policy->security);
407 kfree(policy);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* Destroy xfrm_policy: descendant resources must be released to this moment. */
411
WANG Cong64c31b32008-01-07 22:34:29 -0800412void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Herbert Xu12a169e2008-10-01 07:03:24 -0700414 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Fan Du0659eea2013-08-01 18:08:36 +0800416 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 BUG();
418
Eric Dumazet56f04732015-12-08 07:22:01 -0800419 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
WANG Cong64c31b32008-01-07 22:34:29 -0800421EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100423/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * entry dead. The rule must be unlinked from lists to the moment.
425 */
426
427static void xfrm_policy_kill(struct xfrm_policy *policy)
428{
Herbert Xu12a169e2008-10-01 07:03:24 -0700429 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Timo Teräs285ead12010-04-07 00:30:06 +0000431 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200433 if (del_timer(&policy->polq.hold_timer))
434 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800435 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100436
Timo Teräs285ead12010-04-07 00:30:06 +0000437 if (del_timer(&policy->timer))
438 xfrm_pol_put(policy);
439
440 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
David S. Miller2518c7c2006-08-24 04:45:07 -0700443static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
444
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800445static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700446{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800447 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700448}
449
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200450/* calculate policy hash thresholds */
451static void __get_hash_thresh(struct net *net,
452 unsigned short family, int dir,
453 u8 *dbits, u8 *sbits)
454{
455 switch (family) {
456 case AF_INET:
457 *dbits = net->xfrm.policy_bydst[dir].dbits4;
458 *sbits = net->xfrm.policy_bydst[dir].sbits4;
459 break;
460
461 case AF_INET6:
462 *dbits = net->xfrm.policy_bydst[dir].dbits6;
463 *sbits = net->xfrm.policy_bydst[dir].sbits6;
464 break;
465
466 default:
467 *dbits = 0;
468 *sbits = 0;
469 }
470}
471
David S. Miller5f803b52011-02-24 00:33:19 -0500472static struct hlist_head *policy_hash_bysel(struct net *net,
473 const struct xfrm_selector *sel,
474 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700475{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800476 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200477 unsigned int hash;
478 u8 dbits;
479 u8 sbits;
480
481 __get_hash_thresh(net, family, dir, &dbits, &sbits);
482 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700483
Florian Westphale1e551b2016-08-11 15:17:53 +0200484 if (hash == hmask + 1)
Florian Westphalcc1bb842018-11-07 23:00:34 +0100485 return NULL;
Florian Westphale1e551b2016-08-11 15:17:53 +0200486
487 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
488 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700489}
490
David S. Miller5f803b52011-02-24 00:33:19 -0500491static struct hlist_head *policy_hash_direct(struct net *net,
492 const xfrm_address_t *daddr,
493 const xfrm_address_t *saddr,
494 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700495{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800496 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200497 unsigned int hash;
498 u8 dbits;
499 u8 sbits;
500
501 __get_hash_thresh(net, family, dir, &dbits, &sbits);
502 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
Florian Westphale1e551b2016-08-11 15:17:53 +0200504 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
505 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700506}
507
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200508static void xfrm_dst_hash_transfer(struct net *net,
509 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700510 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200511 unsigned int nhashmask,
512 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700513{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800514 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700515 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800516 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200517 u8 dbits;
518 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700519
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800520redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800521 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700522 unsigned int h;
523
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200524 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200526 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800527 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200528 hlist_del_rcu(&pol->bydst);
529 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800530 h0 = h;
531 } else {
532 if (h != h0)
533 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200534 hlist_del_rcu(&pol->bydst);
535 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800536 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800538 }
539 if (!hlist_empty(list)) {
540 entry0 = NULL;
541 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700542 }
543}
544
545static void xfrm_idx_hash_transfer(struct hlist_head *list,
546 struct hlist_head *nidxtable,
547 unsigned int nhashmask)
548{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800549 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700550 struct xfrm_policy *pol;
551
Sasha Levinb67bfe02013-02-27 17:06:00 -0800552 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700553 unsigned int h;
554
555 h = __idx_hash(pol->index, nhashmask);
556 hlist_add_head(&pol->byidx, nidxtable+h);
557 }
558}
559
560static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
561{
562 return ((old_hmask + 1) << 1) - 1;
563}
564
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800565static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700566{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800567 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700568 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
569 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700570 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200571 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700572 int i;
573
574 if (!ndst)
575 return;
576
Florian Westphal9d0380d2016-08-11 15:17:59 +0200577 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200578 write_seqcount_begin(&xfrm_policy_hash_generation);
579
580 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
581 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700582
Florian Westphale1e551b2016-08-11 15:17:53 +0200583 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
584 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
585
David S. Miller2518c7c2006-08-24 04:45:07 -0700586 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200587 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700588
Florian Westphale1e551b2016-08-11 15:17:53 +0200589 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800590 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700591
Florian Westphal30846092016-08-11 15:17:54 +0200592 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200593 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700594
Florian Westphale1e551b2016-08-11 15:17:53 +0200595 synchronize_rcu();
596
David S. Miller44e36b42006-08-24 04:50:50 -0700597 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700598}
599
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800600static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700601{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800602 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700603 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
604 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800605 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700606 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700607 int i;
608
609 if (!nidx)
610 return;
611
Florian Westphal9d0380d2016-08-11 15:17:59 +0200612 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700613
614 for (i = hmask; i >= 0; i--)
615 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
616
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800617 net->xfrm.policy_byidx = nidx;
618 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700619
Florian Westphal9d0380d2016-08-11 15:17:59 +0200620 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700621
David S. Miller44e36b42006-08-24 04:50:50 -0700622 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700623}
624
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800625static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700626{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800627 unsigned int cnt = net->xfrm.policy_count[dir];
628 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700629
630 if (total)
631 *total += cnt;
632
633 if ((hmask + 1) < xfrm_policy_hashmax &&
634 cnt > hmask)
635 return 1;
636
637 return 0;
638}
639
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800640static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700641{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800642 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700643
644 if ((hmask + 1) < xfrm_policy_hashmax &&
645 total > hmask)
646 return 1;
647
648 return 0;
649}
650
Alexey Dobriyane0710412010-01-23 13:37:10 +0000651void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700652{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000653 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
654 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
655 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
656 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
657 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
658 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
659 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700660 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700661}
662EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700663
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700664static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800665static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700666{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800667 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700668 int dir, total;
669
670 mutex_lock(&hash_resize_mutex);
671
672 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800673 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800674 if (xfrm_bydst_should_resize(net, dir, &total))
675 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700676 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800677 if (xfrm_byidx_should_resize(net, total))
678 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700679
680 mutex_unlock(&hash_resize_mutex);
681}
682
Florian Westphal24969fa2018-11-07 23:00:35 +0100683/* Make sure *pol can be inserted into fastbin.
684 * Useful to check that later insert requests will be sucessful
685 * (provided xfrm_policy_lock is held throughout).
686 */
687static struct xfrm_pol_inexact_bin *
688xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
689{
690 struct xfrm_pol_inexact_bin *bin, *prev;
691 struct xfrm_pol_inexact_key k = {
692 .family = pol->family,
693 .type = pol->type,
694 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +0100695 .if_id = pol->if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +0100696 };
697 struct net *net = xp_net(pol);
698
699 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
700
701 write_pnet(&k.net, net);
702 bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
703 xfrm_pol_inexact_params);
704 if (bin)
705 return bin;
706
707 bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
708 if (!bin)
709 return NULL;
710
711 bin->k = k;
712 INIT_HLIST_HEAD(&bin->hhead);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100713 bin->root_d = RB_ROOT;
Florian Westphal64a09a72018-11-07 23:00:40 +0100714 bin->root_s = RB_ROOT;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100715 seqcount_init(&bin->count);
Florian Westphal24969fa2018-11-07 23:00:35 +0100716
717 prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
718 &bin->k, &bin->head,
719 xfrm_pol_inexact_params);
720 if (!prev) {
721 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
722 return bin;
723 }
724
725 kfree(bin);
726
727 return IS_ERR(prev) ? NULL : prev;
728}
729
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100730static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
731 int family, u8 prefixlen)
Florian Westphal24969fa2018-11-07 23:00:35 +0100732{
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100733 if (xfrm_addr_any(addr, family))
734 return true;
Florian Westphal24969fa2018-11-07 23:00:35 +0100735
Florian Westphal6be3b0d2018-11-07 23:00:37 +0100736 if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
737 return true;
738
739 if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
740 return true;
741
742 return false;
743}
744
745static bool
746xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
747{
748 const xfrm_address_t *addr;
749 bool saddr_any, daddr_any;
750 u8 prefixlen;
751
752 addr = &policy->selector.saddr;
753 prefixlen = policy->selector.prefixlen_s;
754
755 saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
756 policy->family,
757 prefixlen);
758 addr = &policy->selector.daddr;
759 prefixlen = policy->selector.prefixlen_d;
760 daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
761 policy->family,
762 prefixlen);
763 return saddr_any && daddr_any;
764}
765
Florian Westphal9cf545e2018-11-07 23:00:38 +0100766static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
767 const xfrm_address_t *addr, u8 prefixlen)
768{
769 node->addr = *addr;
770 node->prefixlen = prefixlen;
771}
772
773static struct xfrm_pol_inexact_node *
774xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
775{
776 struct xfrm_pol_inexact_node *node;
777
778 node = kzalloc(sizeof(*node), GFP_ATOMIC);
779 if (node)
780 xfrm_pol_inexact_node_init(node, addr, prefixlen);
781
782 return node;
783}
784
785static int xfrm_policy_addr_delta(const xfrm_address_t *a,
786 const xfrm_address_t *b,
787 u8 prefixlen, u16 family)
788{
789 unsigned int pdw, pbi;
790 int delta = 0;
791
792 switch (family) {
793 case AF_INET:
794 if (sizeof(long) == 4 && prefixlen == 0)
795 return ntohl(a->a4) - ntohl(b->a4);
796 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
797 (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
798 case AF_INET6:
799 pdw = prefixlen >> 5;
800 pbi = prefixlen & 0x1f;
801
802 if (pdw) {
803 delta = memcmp(a->a6, b->a6, pdw << 2);
804 if (delta)
805 return delta;
806 }
807 if (pbi) {
808 u32 mask = ~0u << (32 - pbi);
809
810 delta = (ntohl(a->a6[pdw]) & mask) -
811 (ntohl(b->a6[pdw]) & mask);
812 }
813 break;
814 default:
815 break;
816 }
817
818 return delta;
819}
820
821static void xfrm_policy_inexact_list_reinsert(struct net *net,
822 struct xfrm_pol_inexact_node *n,
823 u16 family)
824{
Florian Westphale901cbc2018-11-07 23:00:39 +0100825 unsigned int matched_s, matched_d;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100826 struct xfrm_policy *policy, *p;
827
Florian Westphale901cbc2018-11-07 23:00:39 +0100828 matched_s = 0;
829 matched_d = 0;
830
Florian Westphal9cf545e2018-11-07 23:00:38 +0100831 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal1d389002019-01-04 14:17:03 +0100832 struct hlist_node *newpos = NULL;
Florian Westphale901cbc2018-11-07 23:00:39 +0100833 bool matches_s, matches_d;
834
Florian Westphal9cf545e2018-11-07 23:00:38 +0100835 if (!policy->bydst_reinsert)
836 continue;
837
838 WARN_ON_ONCE(policy->family != family);
839
840 policy->bydst_reinsert = false;
841 hlist_for_each_entry(p, &n->hhead, bydst) {
Florian Westphal1d389002019-01-04 14:17:03 +0100842 if (policy->priority > p->priority)
843 newpos = &p->bydst;
844 else if (policy->priority == p->priority &&
845 policy->pos > p->pos)
Florian Westphal9cf545e2018-11-07 23:00:38 +0100846 newpos = &p->bydst;
847 else
848 break;
849 }
850
851 if (newpos)
Florian Westphal355b00d2019-01-04 14:17:00 +0100852 hlist_add_behind_rcu(&policy->bydst, newpos);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100853 else
Florian Westphal355b00d2019-01-04 14:17:00 +0100854 hlist_add_head_rcu(&policy->bydst, &n->hhead);
Florian Westphale901cbc2018-11-07 23:00:39 +0100855
856 /* paranoia checks follow.
857 * Check that the reinserted policy matches at least
858 * saddr or daddr for current node prefix.
859 *
860 * Matching both is fine, matching saddr in one policy
861 * (but not daddr) and then matching only daddr in another
862 * is a bug.
863 */
864 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
865 &n->addr,
866 n->prefixlen,
867 family) == 0;
868 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
869 &n->addr,
870 n->prefixlen,
871 family) == 0;
872 if (matches_s && matches_d)
873 continue;
874
875 WARN_ON_ONCE(!matches_s && !matches_d);
876 if (matches_s)
877 matched_s++;
878 if (matches_d)
879 matched_d++;
880 WARN_ON_ONCE(matched_s && matched_d);
Florian Westphal9cf545e2018-11-07 23:00:38 +0100881 }
882}
883
Florian Westphal6ac098b2018-11-07 23:00:41 +0100884static void xfrm_policy_inexact_node_reinsert(struct net *net,
885 struct xfrm_pol_inexact_node *n,
886 struct rb_root *new,
887 u16 family)
888{
Florian Westphal6ac098b2018-11-07 23:00:41 +0100889 struct xfrm_pol_inexact_node *node;
Florian Westphal12750ab2019-01-04 14:17:05 +0100890 struct rb_node **p, *parent;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100891
892 /* we should not have another subtree here */
893 WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root));
Florian Westphal12750ab2019-01-04 14:17:05 +0100894restart:
895 parent = NULL;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100896 p = &new->rb_node;
897 while (*p) {
898 u8 prefixlen;
899 int delta;
900
901 parent = *p;
902 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
903
904 prefixlen = min(node->prefixlen, n->prefixlen);
905
906 delta = xfrm_policy_addr_delta(&n->addr, &node->addr,
907 prefixlen, family);
908 if (delta < 0) {
909 p = &parent->rb_left;
910 } else if (delta > 0) {
911 p = &parent->rb_right;
912 } else {
913 struct xfrm_policy *tmp;
914
Florian Westphal12750ab2019-01-04 14:17:05 +0100915 hlist_for_each_entry(tmp, &n->hhead, bydst) {
Florian Westphal6ac098b2018-11-07 23:00:41 +0100916 tmp->bydst_reinsert = true;
Florian Westphal12750ab2019-01-04 14:17:05 +0100917 hlist_del_rcu(&tmp->bydst);
918 }
Florian Westphal6ac098b2018-11-07 23:00:41 +0100919
Florian Westphal6ac098b2018-11-07 23:00:41 +0100920 xfrm_policy_inexact_list_reinsert(net, node, family);
921
922 if (node->prefixlen == n->prefixlen) {
923 kfree_rcu(n, rcu);
924 return;
925 }
926
927 rb_erase(*p, new);
928 kfree_rcu(n, rcu);
929 n = node;
930 n->prefixlen = prefixlen;
Florian Westphal12750ab2019-01-04 14:17:05 +0100931 goto restart;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100932 }
933 }
934
935 rb_link_node_rcu(&n->node, parent, p);
936 rb_insert_color(&n->node, new);
937}
938
Florian Westphal9cf545e2018-11-07 23:00:38 +0100939/* merge nodes v and n */
940static void xfrm_policy_inexact_node_merge(struct net *net,
941 struct xfrm_pol_inexact_node *v,
942 struct xfrm_pol_inexact_node *n,
943 u16 family)
944{
Florian Westphal6ac098b2018-11-07 23:00:41 +0100945 struct xfrm_pol_inexact_node *node;
Florian Westphal9cf545e2018-11-07 23:00:38 +0100946 struct xfrm_policy *tmp;
Florian Westphal6ac098b2018-11-07 23:00:41 +0100947 struct rb_node *rnode;
948
949 /* To-be-merged node v has a subtree.
950 *
951 * Dismantle it and insert its nodes to n->root.
952 */
953 while ((rnode = rb_first(&v->root)) != NULL) {
954 node = rb_entry(rnode, struct xfrm_pol_inexact_node, node);
955 rb_erase(&node->node, &v->root);
956 xfrm_policy_inexact_node_reinsert(net, node, &n->root,
957 family);
958 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100959
Florian Westphal1d389002019-01-04 14:17:03 +0100960 hlist_for_each_entry(tmp, &v->hhead, bydst) {
Florian Westphal9cf545e2018-11-07 23:00:38 +0100961 tmp->bydst_reinsert = true;
Florian Westphal1d389002019-01-04 14:17:03 +0100962 hlist_del_rcu(&tmp->bydst);
963 }
Florian Westphal9cf545e2018-11-07 23:00:38 +0100964
Florian Westphal9cf545e2018-11-07 23:00:38 +0100965 xfrm_policy_inexact_list_reinsert(net, n, family);
966}
967
968static struct xfrm_pol_inexact_node *
969xfrm_policy_inexact_insert_node(struct net *net,
970 struct rb_root *root,
971 xfrm_address_t *addr,
972 u16 family, u8 prefixlen, u8 dir)
973{
974 struct xfrm_pol_inexact_node *cached = NULL;
975 struct rb_node **p, *parent = NULL;
976 struct xfrm_pol_inexact_node *node;
977
978 p = &root->rb_node;
979 while (*p) {
980 int delta;
981
982 parent = *p;
983 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
984
985 delta = xfrm_policy_addr_delta(addr, &node->addr,
986 node->prefixlen,
987 family);
988 if (delta == 0 && prefixlen >= node->prefixlen) {
989 WARN_ON_ONCE(cached); /* ipsec policies got lost */
990 return node;
991 }
992
993 if (delta < 0)
994 p = &parent->rb_left;
995 else
996 p = &parent->rb_right;
997
998 if (prefixlen < node->prefixlen) {
999 delta = xfrm_policy_addr_delta(addr, &node->addr,
1000 prefixlen,
1001 family);
1002 if (delta)
1003 continue;
1004
1005 /* This node is a subnet of the new prefix. It needs
1006 * to be removed and re-inserted with the smaller
1007 * prefix and all nodes that are now also covered
1008 * by the reduced prefixlen.
1009 */
1010 rb_erase(&node->node, root);
1011
1012 if (!cached) {
1013 xfrm_pol_inexact_node_init(node, addr,
1014 prefixlen);
1015 cached = node;
1016 } else {
1017 /* This node also falls within the new
1018 * prefixlen. Merge the to-be-reinserted
1019 * node and this one.
1020 */
1021 xfrm_policy_inexact_node_merge(net, node,
1022 cached, family);
1023 kfree_rcu(node, rcu);
1024 }
1025
1026 /* restart */
1027 p = &root->rb_node;
1028 parent = NULL;
1029 }
1030 }
1031
1032 node = cached;
1033 if (!node) {
1034 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
1035 if (!node)
1036 return NULL;
1037 }
1038
1039 rb_link_node_rcu(&node->node, parent, p);
1040 rb_insert_color(&node->node, root);
1041
1042 return node;
1043}
1044
1045static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
1046{
1047 struct xfrm_pol_inexact_node *node;
1048 struct rb_node *rn = rb_first(r);
1049
1050 while (rn) {
1051 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
1052
Florian Westphal6ac098b2018-11-07 23:00:41 +01001053 xfrm_policy_inexact_gc_tree(&node->root, rm);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001054 rn = rb_next(rn);
1055
Florian Westphal6ac098b2018-11-07 23:00:41 +01001056 if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001057 WARN_ON_ONCE(rm);
1058 continue;
1059 }
1060
1061 rb_erase(&node->node, r);
1062 kfree_rcu(node, rcu);
1063 }
1064}
1065
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001066static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
1067{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001068 write_seqcount_begin(&b->count);
1069 xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
Florian Westphal64a09a72018-11-07 23:00:40 +01001070 xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
Florian Westphal9cf545e2018-11-07 23:00:38 +01001071 write_seqcount_end(&b->count);
1072
Florian Westphal64a09a72018-11-07 23:00:40 +01001073 if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
Florian Westphal9cf545e2018-11-07 23:00:38 +01001074 !hlist_empty(&b->hhead)) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001075 WARN_ON_ONCE(net_exit);
Florian Westphal24969fa2018-11-07 23:00:35 +01001076 return;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001077 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001078
1079 if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
1080 xfrm_pol_inexact_params) == 0) {
1081 list_del(&b->inexact_bins);
1082 kfree_rcu(b, rcu);
1083 }
1084}
1085
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001086static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
1087{
1088 struct net *net = read_pnet(&b->k.net);
1089
1090 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1091 __xfrm_policy_inexact_prune_bin(b, false);
1092 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1093}
1094
Florian Westphal24969fa2018-11-07 23:00:35 +01001095static void __xfrm_policy_inexact_flush(struct net *net)
1096{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001097 struct xfrm_pol_inexact_bin *bin, *t;
Florian Westphal24969fa2018-11-07 23:00:35 +01001098
1099 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1100
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001101 list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
1102 __xfrm_policy_inexact_prune_bin(bin, false);
Florian Westphal24969fa2018-11-07 23:00:35 +01001103}
1104
Florian Westphal9cf545e2018-11-07 23:00:38 +01001105static struct hlist_head *
1106xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
1107 struct xfrm_policy *policy, u8 dir)
1108{
1109 struct xfrm_pol_inexact_node *n;
1110 struct net *net;
1111
1112 net = xp_net(policy);
1113 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1114
1115 if (xfrm_policy_inexact_insert_use_any_list(policy))
1116 return &bin->hhead;
1117
1118 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
1119 policy->family,
Florian Westphal64a09a72018-11-07 23:00:40 +01001120 policy->selector.prefixlen_d)) {
1121 write_seqcount_begin(&bin->count);
1122 n = xfrm_policy_inexact_insert_node(net,
1123 &bin->root_s,
1124 &policy->selector.saddr,
1125 policy->family,
1126 policy->selector.prefixlen_s,
1127 dir);
1128 write_seqcount_end(&bin->count);
1129 if (!n)
1130 return NULL;
1131
1132 return &n->hhead;
1133 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001134
1135 /* daddr is fixed */
1136 write_seqcount_begin(&bin->count);
1137 n = xfrm_policy_inexact_insert_node(net,
1138 &bin->root_d,
1139 &policy->selector.daddr,
1140 policy->family,
1141 policy->selector.prefixlen_d, dir);
1142 write_seqcount_end(&bin->count);
1143 if (!n)
1144 return NULL;
Florian Westphal6ac098b2018-11-07 23:00:41 +01001145
1146 /* saddr is wildcard */
1147 if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
1148 policy->family,
1149 policy->selector.prefixlen_s))
1150 return &n->hhead;
1151
1152 write_seqcount_begin(&bin->count);
1153 n = xfrm_policy_inexact_insert_node(net,
1154 &n->root,
1155 &policy->selector.saddr,
1156 policy->family,
1157 policy->selector.prefixlen_s, dir);
1158 write_seqcount_end(&bin->count);
1159 if (!n)
1160 return NULL;
1161
Florian Westphal9cf545e2018-11-07 23:00:38 +01001162 return &n->hhead;
1163}
1164
Florian Westphal24969fa2018-11-07 23:00:35 +01001165static struct xfrm_policy *
1166xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
1167{
1168 struct xfrm_pol_inexact_bin *bin;
1169 struct xfrm_policy *delpol;
1170 struct hlist_head *chain;
1171 struct net *net;
1172
1173 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1174 if (!bin)
1175 return ERR_PTR(-ENOMEM);
1176
Florian Westphal24969fa2018-11-07 23:00:35 +01001177 net = xp_net(policy);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001178 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1179
Florian Westphal9cf545e2018-11-07 23:00:38 +01001180 chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
1181 if (!chain) {
1182 __xfrm_policy_inexact_prune_bin(bin, false);
1183 return ERR_PTR(-ENOMEM);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001184 }
1185
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001186 delpol = xfrm_policy_insert_list(chain, policy, excl);
1187 if (delpol && excl) {
1188 __xfrm_policy_inexact_prune_bin(bin, false);
1189 return ERR_PTR(-EEXIST);
1190 }
1191
Florian Westphal24969fa2018-11-07 23:00:35 +01001192 chain = &net->xfrm.policy_inexact[dir];
1193 xfrm_policy_insert_inexact_list(chain, policy);
1194
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001195 if (delpol)
1196 __xfrm_policy_inexact_prune_bin(bin, false);
1197
Florian Westphal24969fa2018-11-07 23:00:35 +01001198 return delpol;
1199}
1200
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001201static void xfrm_hash_rebuild(struct work_struct *work)
1202{
1203 struct net *net = container_of(work, struct net,
1204 xfrm.policy_hthresh.work);
1205 unsigned int hmask;
1206 struct xfrm_policy *pol;
1207 struct xfrm_policy *policy;
1208 struct hlist_head *chain;
1209 struct hlist_head *odst;
1210 struct hlist_node *newpos;
1211 int i;
1212 int dir;
1213 unsigned seq;
1214 u8 lbits4, rbits4, lbits6, rbits6;
1215
1216 mutex_lock(&hash_resize_mutex);
1217
1218 /* read selector prefixlen thresholds */
1219 do {
1220 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1221
1222 lbits4 = net->xfrm.policy_hthresh.lbits4;
1223 rbits4 = net->xfrm.policy_hthresh.rbits4;
1224 lbits6 = net->xfrm.policy_hthresh.lbits6;
1225 rbits6 = net->xfrm.policy_hthresh.rbits6;
1226 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
1227
Florian Westphal9d0380d2016-08-11 15:17:59 +02001228 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal7a474c32019-01-04 14:17:01 +01001229 write_seqcount_begin(&xfrm_policy_hash_generation);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001230
Florian Westphal24969fa2018-11-07 23:00:35 +01001231 /* make sure that we can insert the indirect policies again before
1232 * we start with destructive action.
1233 */
1234 list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001235 struct xfrm_pol_inexact_bin *bin;
Florian Westphal24969fa2018-11-07 23:00:35 +01001236 u8 dbits, sbits;
1237
1238 dir = xfrm_policy_id2dir(policy->index);
1239 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
1240 continue;
1241
1242 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1243 if (policy->family == AF_INET) {
1244 dbits = rbits4;
1245 sbits = lbits4;
1246 } else {
1247 dbits = rbits6;
1248 sbits = lbits6;
1249 }
1250 } else {
1251 if (policy->family == AF_INET) {
1252 dbits = lbits4;
1253 sbits = rbits4;
1254 } else {
1255 dbits = lbits6;
1256 sbits = rbits6;
1257 }
1258 }
1259
1260 if (policy->selector.prefixlen_d < dbits ||
1261 policy->selector.prefixlen_s < sbits)
1262 continue;
1263
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001264 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
1265 if (!bin)
Florian Westphal24969fa2018-11-07 23:00:35 +01001266 goto out_unlock;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001267
1268 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
1269 goto out_unlock;
Florian Westphal24969fa2018-11-07 23:00:35 +01001270 }
1271
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001272 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +08001273 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Florian Westphal1548bc42019-01-04 14:17:02 +01001274 struct hlist_node *n;
1275
1276 hlist_for_each_entry_safe(policy, n,
1277 &net->xfrm.policy_inexact[dir],
1278 bydst_inexact_list)
1279 hlist_del_init(&policy->bydst_inexact_list);
1280
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001281 hmask = net->xfrm.policy_bydst[dir].hmask;
1282 odst = net->xfrm.policy_bydst[dir].table;
1283 for (i = hmask; i >= 0; i--)
1284 INIT_HLIST_HEAD(odst + i);
1285 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
1286 /* dir out => dst = remote, src = local */
1287 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
1288 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
1289 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
1290 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
1291 } else {
1292 /* dir in/fwd => dst = local, src = remote */
1293 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
1294 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
1295 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
1296 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
1297 }
1298 }
1299
1300 /* re-insert all policies by order of creation */
1301 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal88584c32018-11-27 13:28:54 +01001302 if (policy->walk.dead)
1303 continue;
1304 dir = xfrm_policy_id2dir(policy->index);
1305 if (dir >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +02001306 /* skip socket policies */
1307 continue;
1308 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001309 newpos = NULL;
1310 chain = policy_hash_bysel(net, &policy->selector,
Florian Westphal88584c32018-11-27 13:28:54 +01001311 policy->family, dir);
Florian Westphal1548bc42019-01-04 14:17:02 +01001312
1313 hlist_del_rcu(&policy->bydst);
1314
Florian Westphal24969fa2018-11-07 23:00:35 +01001315 if (!chain) {
1316 void *p = xfrm_policy_inexact_insert(policy, dir, 0);
1317
1318 WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
1319 continue;
1320 }
1321
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001322 hlist_for_each_entry(pol, chain, bydst) {
1323 if (policy->priority >= pol->priority)
1324 newpos = &pol->bydst;
1325 else
1326 break;
1327 }
1328 if (newpos)
Florian Westphal9dffff22018-10-10 18:02:21 +02001329 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001330 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001331 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001332 }
1333
Florian Westphal24969fa2018-11-07 23:00:35 +01001334out_unlock:
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001335 __xfrm_policy_inexact_flush(net);
Florian Westphal7a474c32019-01-04 14:17:01 +01001336 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001337 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02001338
1339 mutex_unlock(&hash_resize_mutex);
1340}
1341
1342void xfrm_policy_hash_rebuild(struct net *net)
1343{
1344 schedule_work(&net->xfrm.policy_hthresh.work);
1345}
1346EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/* Generate new index... KAME seems to generate them ordered by cost
1349 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +08001350static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 static u32 idx_generator;
1353
1354 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001355 struct hlist_head *list;
1356 struct xfrm_policy *p;
1357 u32 idx;
1358 int found;
1359
Fan Due682adf02013-11-07 17:47:48 +08001360 if (!index) {
1361 idx = (idx_generator | dir);
1362 idx_generator += 8;
1363 } else {
1364 idx = index;
1365 index = 0;
1366 }
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (idx == 0)
1369 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -08001370 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -07001371 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001372 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001373 if (p->index == idx) {
1374 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
David S. Miller2518c7c2006-08-24 04:45:07 -07001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
David S. Miller2518c7c2006-08-24 04:45:07 -07001378 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return idx;
1380 }
1381}
1382
David S. Miller2518c7c2006-08-24 04:45:07 -07001383static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
1384{
1385 u32 *p1 = (u32 *) s1;
1386 u32 *p2 = (u32 *) s2;
1387 int len = sizeof(struct xfrm_selector) / sizeof(u32);
1388 int i;
1389
1390 for (i = 0; i < len; i++) {
1391 if (p1[i] != p2[i])
1392 return 1;
1393 }
1394
1395 return 0;
1396}
1397
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001398static void xfrm_policy_requeue(struct xfrm_policy *old,
1399 struct xfrm_policy *new)
1400{
1401 struct xfrm_policy_queue *pq = &old->polq;
1402 struct sk_buff_head list;
1403
Li RongQingde2ad482015-04-30 17:25:19 +08001404 if (skb_queue_empty(&pq->hold_queue))
1405 return;
1406
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001407 __skb_queue_head_init(&list);
1408
1409 spin_lock_bh(&pq->hold_queue.lock);
1410 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001411 if (del_timer(&pq->hold_timer))
1412 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001413 spin_unlock_bh(&pq->hold_queue.lock);
1414
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001415 pq = &new->polq;
1416
1417 spin_lock_bh(&pq->hold_queue.lock);
1418 skb_queue_splice(&list, &pq->hold_queue);
1419 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001420 if (!mod_timer(&pq->hold_timer, jiffies))
1421 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001422 spin_unlock_bh(&pq->hold_queue.lock);
1423}
1424
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001425static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
1426 struct xfrm_policy *pol)
1427{
1428 u32 mark = policy->mark.v & policy->mark.m;
1429
1430 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
1431 return true;
1432
1433 if ((mark & pol->mark.m) == pol->mark.v &&
1434 policy->priority == pol->priority)
1435 return true;
1436
1437 return false;
1438}
1439
Florian Westphal24969fa2018-11-07 23:00:35 +01001440static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
1441{
1442 const struct xfrm_pol_inexact_key *k = data;
1443 u32 a = k->type << 24 | k->dir << 16 | k->family;
1444
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001445 return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
1446 seed);
Florian Westphal24969fa2018-11-07 23:00:35 +01001447}
1448
1449static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
1450{
1451 const struct xfrm_pol_inexact_bin *b = data;
1452
1453 return xfrm_pol_bin_key(&b->k, 0, seed);
1454}
1455
1456static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
1457 const void *ptr)
1458{
1459 const struct xfrm_pol_inexact_key *key = arg->key;
1460 const struct xfrm_pol_inexact_bin *b = ptr;
1461 int ret;
1462
1463 if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
1464 return -1;
1465
1466 ret = b->k.dir ^ key->dir;
1467 if (ret)
1468 return ret;
1469
1470 ret = b->k.type ^ key->type;
1471 if (ret)
1472 return ret;
1473
1474 ret = b->k.family ^ key->family;
1475 if (ret)
1476 return ret;
1477
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001478 return b->k.if_id ^ key->if_id;
Florian Westphal24969fa2018-11-07 23:00:35 +01001479}
1480
1481static const struct rhashtable_params xfrm_pol_inexact_params = {
1482 .head_offset = offsetof(struct xfrm_pol_inexact_bin, head),
1483 .hashfn = xfrm_pol_bin_key,
1484 .obj_hashfn = xfrm_pol_bin_obj,
1485 .obj_cmpfn = xfrm_pol_bin_cmp,
1486 .automatic_shrinking = true,
1487};
1488
1489static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
1490 struct xfrm_policy *policy)
1491{
1492 struct xfrm_policy *pol, *delpol = NULL;
1493 struct hlist_node *newpos = NULL;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001494 int i = 0;
Florian Westphal24969fa2018-11-07 23:00:35 +01001495
1496 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1497 if (pol->type == policy->type &&
1498 pol->if_id == policy->if_id &&
1499 !selector_cmp(&pol->selector, &policy->selector) &&
1500 xfrm_policy_mark_match(policy, pol) &&
1501 xfrm_sec_ctx_match(pol->security, policy->security) &&
1502 !WARN_ON(delpol)) {
1503 delpol = pol;
1504 if (policy->priority > pol->priority)
1505 continue;
1506 } else if (policy->priority >= pol->priority) {
1507 newpos = &pol->bydst_inexact_list;
1508 continue;
1509 }
1510 if (delpol)
1511 break;
1512 }
1513
1514 if (newpos)
1515 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
1516 else
1517 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001518
1519 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
1520 pol->pos = i;
1521 i++;
1522 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001523}
1524
Florian Westphala927d6af2018-11-07 23:00:33 +01001525static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
1526 struct xfrm_policy *policy,
1527 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Florian Westphala927d6af2018-11-07 23:00:33 +01001529 struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Sasha Levinb67bfe02013-02-27 17:06:00 -08001531 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -08001532 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001533 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -07001534 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +01001535 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -08001536 xfrm_sec_ctx_match(pol->security, policy->security) &&
1537 !WARN_ON(delpol)) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001538 if (excl)
1539 return ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 delpol = pol;
1541 if (policy->priority > pol->priority)
1542 continue;
1543 } else if (policy->priority >= pol->priority) {
Florian Westphala927d6af2018-11-07 23:00:33 +01001544 newpos = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 continue;
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (delpol)
1548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (newpos)
Florian Westphala927d6af2018-11-07 23:00:33 +01001552 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001553 else
Florian Westphal9dffff22018-10-10 18:02:21 +02001554 hlist_add_head_rcu(&policy->bydst, chain);
Florian Westphala927d6af2018-11-07 23:00:33 +01001555
1556 return delpol;
1557}
1558
1559int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
1560{
1561 struct net *net = xp_net(policy);
1562 struct xfrm_policy *delpol;
1563 struct hlist_head *chain;
1564
1565 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1566 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001567 if (chain)
Florian Westphalcc1bb842018-11-07 23:00:34 +01001568 delpol = xfrm_policy_insert_list(chain, policy, excl);
Florian Westphal24969fa2018-11-07 23:00:35 +01001569 else
1570 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
Florian Westphala927d6af2018-11-07 23:00:33 +01001571
1572 if (IS_ERR(delpol)) {
1573 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1574 return PTR_ERR(delpol);
1575 }
1576
Herbert Xu12bfa8b2014-11-13 17:09:50 +08001577 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +08001578
1579 /* After previous checking, family can either be AF_INET or AF_INET6 */
1580 if (policy->family == AF_INET)
1581 rt_genid_bump_ipv4(net);
1582 else
1583 rt_genid_bump_ipv6(net);
1584
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001585 if (delpol) {
1586 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001587 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001588 }
Fan Due682adf02013-11-07 17:47:48 +08001589 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001590 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +02001591 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 policy->curlft.use_time = 0;
1593 if (!mod_timer(&policy->timer, jiffies + HZ))
1594 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001595 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
David S. Miller9b78a822005-12-22 07:39:48 -08001597 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001599 else if (xfrm_bydst_should_resize(net, dir, NULL))
1600 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -08001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 0;
1603}
1604EXPORT_SYMBOL(xfrm_policy_insert);
1605
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001606static struct xfrm_policy *
1607__xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id,
1608 u8 type, int dir,
1609 struct xfrm_selector *sel,
1610 struct xfrm_sec_ctx *ctx)
1611{
1612 struct xfrm_policy *pol;
1613
1614 if (!chain)
1615 return NULL;
1616
1617 hlist_for_each_entry(pol, chain, bydst) {
1618 if (pol->type == type &&
1619 pol->if_id == if_id &&
1620 (mark & pol->mark.m) == pol->mark.v &&
1621 !selector_cmp(sel, &pol->selector) &&
1622 xfrm_sec_ctx_match(ctx, pol->security))
1623 return pol;
1624 }
1625
1626 return NULL;
1627}
1628
Steffen Klassert7e652642018-06-12 14:07:07 +02001629struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
1630 u8 type, int dir,
1631 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -08001632 struct xfrm_sec_ctx *ctx, int delete,
1633 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Florian Westphal24969fa2018-11-07 23:00:35 +01001635 struct xfrm_pol_inexact_bin *bin = NULL;
1636 struct xfrm_policy *pol, *ret = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -07001637 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Eric Parisef41aaa2007-03-07 15:37:58 -08001639 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001640 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001641 chain = policy_hash_bysel(net, sel, sel->family, dir);
Florian Westphal24969fa2018-11-07 23:00:35 +01001642 if (!chain) {
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001643 struct xfrm_pol_inexact_candidates cand;
1644 int i;
1645
Florian Westphal24969fa2018-11-07 23:00:35 +01001646 bin = xfrm_policy_inexact_lookup(net, type,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001647 sel->family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001648 if (!bin) {
1649 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1650 return NULL;
1651 }
1652
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001653 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
1654 &sel->saddr,
1655 &sel->daddr)) {
1656 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1657 return NULL;
1658 }
1659
1660 pol = NULL;
1661 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
1662 struct xfrm_policy *tmp;
1663
1664 tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
1665 if_id, type, dir,
1666 sel, ctx);
Florian Westphal39aa6922018-11-15 02:51:57 +01001667 if (!tmp)
1668 continue;
1669
1670 if (!pol || tmp->pos < pol->pos)
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001671 pol = tmp;
1672 }
1673 } else {
1674 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
1675 sel, ctx);
Florian Westphal24969fa2018-11-07 23:00:35 +01001676 }
1677
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001678 if (pol) {
1679 xfrm_pol_hold(pol);
1680 if (delete) {
1681 *err = security_xfrm_policy_delete(pol->security);
1682 if (*err) {
1683 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1684 return pol;
David S. Miller2518c7c2006-08-24 04:45:07 -07001685 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001686 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001688 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001690 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001692 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001693 xfrm_policy_kill(ret);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001694 if (bin && delete)
1695 xfrm_policy_inexact_prune_bin(bin);
David S. Miller2518c7c2006-08-24 04:45:07 -07001696 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
Trent Jaegerdf718372005-12-13 23:12:27 -08001698EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Steffen Klassert7e652642018-06-12 14:07:07 +02001700struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
1701 u8 type, int dir, u32 id, int delete,
1702 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
David S. Miller2518c7c2006-08-24 04:45:07 -07001704 struct xfrm_policy *pol, *ret;
1705 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Herbert Xub5505c62007-05-14 02:15:47 -07001707 *err = -ENOENT;
1708 if (xfrm_policy_id2dir(id) != dir)
1709 return NULL;
1710
Eric Parisef41aaa2007-03-07 15:37:58 -08001711 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +02001712 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -08001713 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -07001714 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001715 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001716 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001717 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001718 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001720 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001721 *err = security_xfrm_policy_delete(
1722 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -08001723 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +02001724 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -08001725 return pol;
1726 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -08001727 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001728 }
1729 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 break;
1731 }
1732 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001733 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001735 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -07001736 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -07001737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739EXPORT_SYMBOL(xfrm_policy_byid);
1740
Joy Latten4aa2e622007-06-04 19:05:57 -04001741#ifdef CONFIG_SECURITY_NETWORK_XFRM
1742static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001743xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Florian Westphalceb159e2018-11-07 23:00:32 +01001745 struct xfrm_policy *pol;
1746 int err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -04001747
Florian Westphalceb159e2018-11-07 23:00:32 +01001748 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1749 if (pol->walk.dead ||
1750 xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
1751 pol->type != type)
1752 continue;
Joy Latten4aa2e622007-06-04 19:05:57 -04001753
Florian Westphalceb159e2018-11-07 23:00:32 +01001754 err = security_xfrm_policy_delete(pol->security);
1755 if (err) {
1756 xfrm_audit_policy_delete(pol, 0, task_valid);
1757 return err;
Joy Latten4aa2e622007-06-04 19:05:57 -04001758 }
1759 }
1760 return err;
1761}
1762#else
1763static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +09001764xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001765{
1766 return 0;
1767}
1768#endif
1769
Tetsuo Handa2e710292014-04-22 21:48:30 +09001770int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -04001771{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001772 int dir, err = 0, cnt = 0;
Florian Westphalceb159e2018-11-07 23:00:32 +01001773 struct xfrm_policy *pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Florian Westphal9d0380d2016-08-11 15:17:59 +02001775 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001776
Tetsuo Handa2e710292014-04-22 21:48:30 +09001777 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -04001778 if (err)
1779 goto out;
1780
Florian Westphalceb159e2018-11-07 23:00:32 +01001781again:
1782 list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
1783 dir = xfrm_policy_id2dir(pol->index);
1784 if (pol->walk.dead ||
1785 dir >= XFRM_POLICY_MAX ||
1786 pol->type != type)
1787 continue;
David S. Miller2518c7c2006-08-24 04:45:07 -07001788
Florian Westphalceb159e2018-11-07 23:00:32 +01001789 __xfrm_policy_unlink(pol, dir);
1790 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
1791 cnt++;
1792 xfrm_audit_policy_delete(pol, 1, task_valid);
1793 xfrm_policy_kill(pol);
1794 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
1795 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Florian Westphal24969fa2018-11-07 23:00:35 +01001797 if (cnt)
1798 __xfrm_policy_inexact_flush(net);
1799 else
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001800 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001801out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001802 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001803 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805EXPORT_SYMBOL(xfrm_policy_flush);
1806
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001807int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001808 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 void *data)
1810{
Herbert Xu12a169e2008-10-01 07:03:24 -07001811 struct xfrm_policy *pol;
1812 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001813 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Timo Teras4c563f72008-02-28 21:31:08 -08001815 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1816 walk->type != XFRM_POLICY_TYPE_ANY)
1817 return -EINVAL;
1818
Herbert Xu12a169e2008-10-01 07:03:24 -07001819 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001820 return 0;
1821
Florian Westphal9d0380d2016-08-11 15:17:59 +02001822 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001823 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001824 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001825 else
Li RongQing80077702015-04-22 17:09:54 +08001826 x = list_first_entry(&walk->walk.all,
1827 struct xfrm_policy_walk_entry, all);
1828
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001829 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001830 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001831 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001832 pol = container_of(x, struct xfrm_policy, walk);
1833 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1834 walk->type != pol->type)
1835 continue;
1836 error = func(pol, xfrm_policy_id2dir(pol->index),
1837 walk->seq, data);
1838 if (error) {
1839 list_move_tail(&walk->walk.all, &x->all);
1840 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001841 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001842 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001844 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001845 error = -ENOENT;
1846 goto out;
1847 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001848 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001850 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return error;
1852}
1853EXPORT_SYMBOL(xfrm_policy_walk);
1854
Herbert Xu12a169e2008-10-01 07:03:24 -07001855void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1856{
1857 INIT_LIST_HEAD(&walk->walk.all);
1858 walk->walk.dead = 1;
1859 walk->type = type;
1860 walk->seq = 0;
1861}
1862EXPORT_SYMBOL(xfrm_policy_walk_init);
1863
Fan Du283bc9f2013-11-07 17:47:50 +08001864void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001865{
1866 if (list_empty(&walk->walk.all))
1867 return;
1868
Florian Westphal9d0380d2016-08-11 15:17:59 +02001869 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001870 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001871 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001872}
1873EXPORT_SYMBOL(xfrm_policy_walk_done);
1874
James Morris134b0fc2006-10-05 15:42:27 -05001875/*
1876 * Find policy to apply to this flow.
1877 *
1878 * Returns 0 if policy found, else an -errno.
1879 */
David S. Millerf299d552011-02-24 01:23:30 -05001880static int xfrm_policy_match(const struct xfrm_policy *pol,
1881 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001882 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001883{
David S. Millerf299d552011-02-24 01:23:30 -05001884 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001885 int ret = -ESRCH;
1886 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001887
1888 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001889 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001890 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001891 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001892 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001893
1894 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001895 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001896 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001897 dir);
James Morris134b0fc2006-10-05 15:42:27 -05001898 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001899}
1900
Florian Westphal9cf545e2018-11-07 23:00:38 +01001901static struct xfrm_pol_inexact_node *
1902xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
1903 seqcount_t *count,
1904 const xfrm_address_t *addr, u16 family)
1905{
1906 const struct rb_node *parent;
1907 int seq;
1908
1909again:
1910 seq = read_seqcount_begin(count);
1911
1912 parent = rcu_dereference_raw(r->rb_node);
1913 while (parent) {
1914 struct xfrm_pol_inexact_node *node;
1915 int delta;
1916
1917 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
1918
1919 delta = xfrm_policy_addr_delta(addr, &node->addr,
1920 node->prefixlen, family);
1921 if (delta < 0) {
1922 parent = rcu_dereference_raw(parent->rb_left);
1923 continue;
1924 } else if (delta > 0) {
1925 parent = rcu_dereference_raw(parent->rb_right);
1926 continue;
1927 }
1928
1929 return node;
1930 }
1931
1932 if (read_seqcount_retry(count, seq))
1933 goto again;
1934
1935 return NULL;
1936}
1937
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001938static bool
1939xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
1940 struct xfrm_pol_inexact_bin *b,
1941 const xfrm_address_t *saddr,
1942 const xfrm_address_t *daddr)
1943{
Florian Westphal9cf545e2018-11-07 23:00:38 +01001944 struct xfrm_pol_inexact_node *n;
1945 u16 family;
1946
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001947 if (!b)
1948 return false;
1949
Florian Westphal9cf545e2018-11-07 23:00:38 +01001950 family = b->k.family;
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001951 memset(cand, 0, sizeof(*cand));
1952 cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
Florian Westphal9cf545e2018-11-07 23:00:38 +01001953
1954 n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
1955 family);
Florian Westphal6ac098b2018-11-07 23:00:41 +01001956 if (n) {
Florian Westphal9cf545e2018-11-07 23:00:38 +01001957 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
Florian Westphal6ac098b2018-11-07 23:00:41 +01001958 n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr,
1959 family);
1960 if (n)
1961 cand->res[XFRM_POL_CAND_BOTH] = &n->hhead;
1962 }
Florian Westphal9cf545e2018-11-07 23:00:38 +01001963
Florian Westphal64a09a72018-11-07 23:00:40 +01001964 n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
1965 family);
1966 if (n)
1967 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
1968
Florian Westphal6be3b0d2018-11-07 23:00:37 +01001969 return true;
1970}
1971
Florian Westphal24969fa2018-11-07 23:00:35 +01001972static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001973xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
1974 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001975{
1976 struct xfrm_pol_inexact_key k = {
1977 .family = family,
1978 .type = type,
1979 .dir = dir,
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001980 .if_id = if_id,
Florian Westphal24969fa2018-11-07 23:00:35 +01001981 };
1982
1983 write_pnet(&k.net, net);
1984
1985 return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
1986 xfrm_pol_inexact_params);
1987}
1988
1989static struct xfrm_pol_inexact_bin *
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001990xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
1991 u8 dir, u32 if_id)
Florian Westphal24969fa2018-11-07 23:00:35 +01001992{
1993 struct xfrm_pol_inexact_bin *bin;
1994
1995 lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
1996
1997 rcu_read_lock();
Florian Westphalb5fe22e2018-11-07 23:00:36 +01001998 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal24969fa2018-11-07 23:00:35 +01001999 rcu_read_unlock();
2000
2001 return bin;
2002}
2003
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002004static struct xfrm_policy *
2005__xfrm_policy_eval_candidates(struct hlist_head *chain,
2006 struct xfrm_policy *prefer,
2007 const struct flowi *fl,
2008 u8 type, u16 family, int dir, u32 if_id)
2009{
2010 u32 priority = prefer ? prefer->priority : ~0u;
2011 struct xfrm_policy *pol;
2012
2013 if (!chain)
2014 return NULL;
2015
2016 hlist_for_each_entry_rcu(pol, chain, bydst) {
2017 int err;
2018
2019 if (pol->priority > priority)
2020 break;
2021
2022 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
2023 if (err) {
2024 if (err != -ESRCH)
2025 return ERR_PTR(err);
2026
2027 continue;
2028 }
2029
2030 if (prefer) {
2031 /* matches. Is it older than *prefer? */
2032 if (pol->priority == priority &&
2033 prefer->pos < pol->pos)
2034 return prefer;
2035 }
2036
2037 return pol;
2038 }
2039
2040 return NULL;
2041}
2042
2043static struct xfrm_policy *
2044xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
2045 struct xfrm_policy *prefer,
2046 const struct flowi *fl,
2047 u8 type, u16 family, int dir, u32 if_id)
2048{
2049 struct xfrm_policy *tmp;
2050 int i;
2051
2052 for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
2053 tmp = __xfrm_policy_eval_candidates(cand->res[i],
2054 prefer,
2055 fl, type, family, dir,
2056 if_id);
2057 if (!tmp)
2058 continue;
2059
2060 if (IS_ERR(tmp))
2061 return tmp;
2062 prefer = tmp;
2063 }
2064
2065 return prefer;
2066}
2067
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002068static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08002069 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07002070 u16 family, u8 dir,
2071 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002073 struct xfrm_pol_inexact_candidates cand;
David S. Miller0b597e72011-02-24 01:22:48 -05002074 const xfrm_address_t *daddr, *saddr;
Florian Westphal24969fa2018-11-07 23:00:35 +01002075 struct xfrm_pol_inexact_bin *bin;
2076 struct xfrm_policy *pol, *ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07002077 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02002078 unsigned int sequence;
Florian Westphal24969fa2018-11-07 23:00:35 +01002079 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07002080
2081 daddr = xfrm_flowi_daddr(fl, family);
2082 saddr = xfrm_flowi_saddr(fl, family);
2083 if (unlikely(!daddr || !saddr))
2084 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Florian Westphala7c442472016-08-11 15:17:56 +02002086 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02002087 retry:
2088 do {
2089 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
2090 chain = policy_hash_direct(net, daddr, saddr, family, dir);
2091 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
2092
David S. Miller2518c7c2006-08-24 04:45:07 -07002093 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02002094 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07002095 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05002096 if (err) {
2097 if (err == -ESRCH)
2098 continue;
2099 else {
2100 ret = ERR_PTR(err);
2101 goto fail;
2102 }
2103 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07002104 ret = pol;
David S. Milleracba48e2006-08-25 15:46:46 -07002105 break;
2106 }
2107 }
Florian Westphalb5fe22e2018-11-07 23:00:36 +01002108 bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002109 if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
2110 daddr))
Florian Westphal24969fa2018-11-07 23:00:35 +01002111 goto skip_inexact;
Li RongQing8faf4912015-05-14 11:16:59 +08002112
Florian Westphal6be3b0d2018-11-07 23:00:37 +01002113 pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
2114 family, dir, if_id);
2115 if (pol) {
2116 ret = pol;
2117 if (IS_ERR(pol))
2118 goto fail;
David S. Miller2518c7c2006-08-24 04:45:07 -07002119 }
Li RongQing586f2eb2015-04-30 17:13:41 +08002120
Florian Westphal24969fa2018-11-07 23:00:35 +01002121skip_inexact:
Florian Westphal30846092016-08-11 15:17:54 +02002122 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
2123 goto retry;
2124
Florian Westphale37cc8a2016-08-11 15:17:55 +02002125 if (ret && !xfrm_pol_hold_rcu(ret))
2126 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05002127fail:
Florian Westphala7c442472016-08-11 15:17:56 +02002128 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002129
David S. Miller2518c7c2006-08-24 04:45:07 -07002130 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002131}
2132
Benedict Wongbc56b332018-07-19 10:50:44 -07002133static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
2134 const struct flowi *fl,
2135 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002136{
2137#ifdef CONFIG_XFRM_SUB_POLICY
2138 struct xfrm_policy *pol;
2139
Benedict Wongbc56b332018-07-19 10:50:44 -07002140 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
2141 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002142 if (pol != NULL)
2143 return pol;
2144#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07002145 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
2146 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002147}
2148
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002149static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07002150 const struct flowi *fl,
2151 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 struct xfrm_policy *pol;
2154
Eric Dumazetd188ba82015-12-08 07:22:02 -08002155 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02002156 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002157 pol = rcu_dereference(sk->sk_policy[dir]);
2158 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01002159 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002160 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08002161
Steffen Klassertddc47e42017-11-29 06:53:55 +01002162 if (pol->family != family) {
2163 pol = NULL;
2164 goto out;
2165 }
2166
2167 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002168 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02002169 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07002170 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002171 pol = NULL;
2172 goto out;
2173 }
Paul Moore03e1ad72008-04-12 19:07:52 -07002174 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05002175 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02002176 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01002177 if (!err) {
2178 if (!xfrm_pol_hold_rcu(pol))
2179 goto again;
2180 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002181 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01002182 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002183 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01002184 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002185 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 pol = NULL;
2187 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00002188out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08002189 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return pol;
2191}
2192
2193static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
2194{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002195 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002196
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002197 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002198 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 xfrm_pol_hold(pol);
2200}
2201
2202static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
2203 int dir)
2204{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002205 struct net *net = xp_net(pol);
2206
Herbert Xu53c2e282014-11-13 17:09:49 +08002207 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07002208 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Herbert Xu53c2e282014-11-13 17:09:49 +08002210 /* Socket policies are not hashed. */
2211 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02002212 hlist_del_rcu(&pol->bydst);
Florian Westphal24969fa2018-11-07 23:00:35 +01002213 hlist_del_init(&pol->bydst_inexact_list);
Herbert Xu53c2e282014-11-13 17:09:49 +08002214 hlist_del(&pol->byidx);
2215 }
2216
2217 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08002218 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07002219
2220 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Herbert Xu53c2e282014-11-13 17:09:49 +08002223static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
2224{
2225 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
2226}
2227
2228static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
2229{
2230 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
2231}
2232
Herbert Xu4666faa2005-06-18 22:43:22 -07002233int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Fan Du283bc9f2013-11-07 17:47:50 +08002235 struct net *net = xp_net(pol);
2236
Florian Westphal9d0380d2016-08-11 15:17:59 +02002237 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002239 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07002242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Herbert Xu4666faa2005-06-18 22:43:22 -07002244 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
David S. Millera70fcb02006-03-20 19:18:52 -08002246EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
2249{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002250 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct xfrm_policy *old_pol;
2252
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002253#ifdef CONFIG_XFRM_SUB_POLICY
2254 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
2255 return -EINVAL;
2256#endif
2257
Florian Westphal9d0380d2016-08-11 15:17:59 +02002258 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08002259 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
2260 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02002262 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08002263 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08002264 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08002266 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002267 if (old_pol) {
2268 if (pol)
2269 xfrm_policy_requeue(old_pol, pol);
2270
Timo Teräsea2dea92010-03-31 00:17:05 +00002271 /* Unlinking succeeds always. This is the only function
2272 * allowed to delete or replace socket policy.
2273 */
Herbert Xu53c2e282014-11-13 17:09:49 +08002274 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002275 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02002276 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 if (old_pol) {
2279 xfrm_policy_kill(old_pol);
2280 }
2281 return 0;
2282}
2283
David S. Millerd3e40a92011-02-24 01:25:41 -05002284static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08002286 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08002287 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 if (newp) {
2290 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07002291 if (security_xfrm_policy_clone(old->security,
2292 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08002293 kfree(newp);
2294 return NULL; /* ENOMEM */
2295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 newp->lft = old->lft;
2297 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08002298 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02002299 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 newp->action = old->action;
2301 newp->flags = old->flags;
2302 newp->xfrm_nr = old->xfrm_nr;
2303 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002304 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11002305 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 memcpy(newp->xfrm_vec, old->xfrm_vec,
2307 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02002308 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08002309 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02002310 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 xfrm_pol_put(newp);
2312 }
2313 return newp;
2314}
2315
Eric Dumazetd188ba82015-12-08 07:22:02 -08002316int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Eric Dumazetd188ba82015-12-08 07:22:02 -08002318 const struct xfrm_policy *p;
2319 struct xfrm_policy *np;
2320 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Eric Dumazetd188ba82015-12-08 07:22:02 -08002322 rcu_read_lock();
2323 for (i = 0; i < 2; i++) {
2324 p = rcu_dereference(osk->sk_policy[i]);
2325 if (p) {
2326 np = clone_policy(p, i);
2327 if (unlikely(!np)) {
2328 ret = -ENOMEM;
2329 break;
2330 }
2331 rcu_assign_pointer(sk->sk_policy[i], np);
2332 }
2333 }
2334 rcu_read_unlock();
2335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002338static int
David Ahern42a7b322015-08-10 16:58:11 -06002339xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002340 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002341{
2342 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01002343 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002344
2345 if (unlikely(afinfo == NULL))
2346 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09002347 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002348 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002349 return err;
2350}
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352/* Resolve list of templates for the flow, given policy. */
2353
2354static int
David S. Millera6c2e612011-02-22 18:35:39 -08002355xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
2356 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08002358 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int nx;
2360 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01002361 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
2362 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07002363 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Weilong Chen9b7a7872013-12-24 09:43:46 +08002365 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01002367 xfrm_address_t *remote = daddr;
2368 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
2370
Steffen Klassert94802152017-11-15 06:40:57 +01002371 if (tmpl->mode == XFRM_MODE_TUNNEL ||
2372 tmpl->mode == XFRM_MODE_BEET) {
2373 remote = &tmpl->id.daddr;
2374 local = &tmpl->saddr;
2375 if (xfrm_addr_any(local, tmpl->encap_family)) {
2376 error = xfrm_get_saddr(net, fl->flowi_oif,
2377 &tmp, remote,
2378 tmpl->encap_family, 0);
2379 if (error)
2380 goto fail;
2381 local = &tmp;
2382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384
Benedict Wongbc56b332018-07-19 10:50:44 -07002385 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
2386 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (x && x->km.state == XFRM_STATE_VALID) {
2389 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01002390 daddr = remote;
2391 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 continue;
2393 }
2394 if (x) {
2395 error = (x->km.state == XFRM_STATE_ERROR ?
2396 -EINVAL : -EAGAIN);
2397 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08002398 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00002399 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08002400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 if (!tmpl->optional)
2403 goto fail;
2404 }
2405 return nx;
2406
2407fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002408 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 xfrm_state_put(xfrm[nx]);
2410 return error;
2411}
2412
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002413static int
David S. Millera6c2e612011-02-22 18:35:39 -08002414xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
2415 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002416{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002417 struct xfrm_state *tp[XFRM_MAX_DEPTH];
2418 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002419 int cnx = 0;
2420 int error;
2421 int ret;
2422 int i;
2423
2424 for (i = 0; i < npols; i++) {
2425 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
2426 error = -ENOBUFS;
2427 goto fail;
2428 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002429
2430 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002431 if (ret < 0) {
2432 error = ret;
2433 goto fail;
2434 } else
2435 cnx += ret;
2436 }
2437
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002438 /* found states are sorted for outbound processing */
2439 if (npols > 1)
2440 xfrm_state_sort(xfrm, tpp, cnx, family);
2441
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002442 return cnx;
2443
2444 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08002445 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002446 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002447 return error;
2448
2449}
2450
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002451static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002452{
Florian Westphal37b10382017-02-07 15:00:19 +01002453 const struct xfrm_policy_afinfo *afinfo;
Xin Long143a4452018-02-17 15:16:22 +08002454 int tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08002455
Florian Westphalf5e2bb42017-02-07 15:00:14 +01002456 afinfo = xfrm_policy_get_afinfo(family);
Xin Long143a4452018-02-17 15:16:22 +08002457 if (!afinfo)
2458 return 0;
2459
2460 tos = afinfo->get_tos(fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002461
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002462 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002463
2464 return tos;
2465}
2466
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002467static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08002468{
Florian Westphal37b10382017-02-07 15:00:19 +01002469 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002470 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08002471 struct xfrm_dst *xdst;
2472
2473 if (!afinfo)
2474 return ERR_PTR(-EINVAL);
2475
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002476 switch (family) {
2477 case AF_INET:
2478 dst_ops = &net->xfrm.xfrm4_dst_ops;
2479 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002480#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002481 case AF_INET6:
2482 dst_ops = &net->xfrm.xfrm6_dst_ops;
2483 break;
2484#endif
2485 default:
2486 BUG();
2487 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07002488 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08002489
Madalin Bucurd4cae562011-09-26 07:04:36 +00002490 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00002491 struct dst_entry *dst = &xdst->u.dst;
2492
2493 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00002494 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08002495 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00002496
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002497 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00002498
Herbert Xu25ee3282007-12-11 09:32:34 -08002499 return xdst;
2500}
2501
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002502static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
2503 int nfheader_len)
2504{
Florian Westphal37b10382017-02-07 15:00:19 +01002505 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002506 xfrm_policy_get_afinfo(dst->ops->family);
2507 int err;
2508
2509 if (!afinfo)
2510 return -EINVAL;
2511
2512 err = afinfo->init_path(path, dst, nfheader_len);
2513
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002514 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002515
2516 return err;
2517}
2518
Herbert Xu87c1e122010-03-02 02:51:56 +00002519static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08002520 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08002521{
Florian Westphal37b10382017-02-07 15:00:19 +01002522 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08002523 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
2524 int err;
2525
2526 if (!afinfo)
2527 return -EINVAL;
2528
Herbert Xu87c1e122010-03-02 02:51:56 +00002529 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002530
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002531 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002532
2533 return err;
2534}
2535
Timo Teräs80c802f2010-04-07 00:30:05 +00002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537/* Allocate chain of dst_entry's, attach known xfrm's, calculate
2538 * all the metrics... Shortly, bundle a bundle.
2539 */
2540
Herbert Xu25ee3282007-12-11 09:32:34 -08002541static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05002542 struct xfrm_state **xfrm,
2543 struct xfrm_dst **bundle,
2544 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08002545 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08002546 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Florian Westphal733a5fa2019-03-29 21:16:30 +01002548 const struct xfrm_state_afinfo *afinfo;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002549 const struct xfrm_mode *inner_mode;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002550 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08002551 unsigned long now = jiffies;
2552 struct net_device *dev;
David Miller45b018be2017-11-28 15:40:28 -05002553 struct xfrm_dst *xdst_prev = NULL;
2554 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08002555 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08002557 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002558 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08002559 int trailer_len = 0;
2560 int tos;
2561 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09002562 xfrm_address_t saddr, daddr;
2563
2564 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002565
2566 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002567
2568 dst_hold(dst);
2569
2570 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002571 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08002572 struct dst_entry *dst1 = &xdst->u.dst;
2573
2574 err = PTR_ERR(xdst);
2575 if (IS_ERR(xdst)) {
2576 dst_release(dst);
2577 goto put_states;
2578 }
2579
David Miller54920932017-11-28 15:41:01 -05002580 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05002581 if (!xdst_prev)
2582 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07002583 else
2584 /* Ref count is taken during xfrm_alloc_dst()
2585 * No need to do dst_clone() on dst1
2586 */
David Miller45b018be2017-11-28 15:40:28 -05002587 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07002588
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002589 if (xfrm[i]->sel.family == AF_UNSPEC) {
2590 inner_mode = xfrm_ip2inner_mode(xfrm[i],
2591 xfrm_af2proto(family));
2592 if (!inner_mode) {
2593 err = -EAFNOSUPPORT;
2594 dst_release(dst);
2595 goto put_states;
2596 }
2597 } else
Florian Westphalc9500d72019-03-29 21:16:32 +01002598 inner_mode = &xfrm[i]->inner_mode;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00002599
Herbert Xu25ee3282007-12-11 09:32:34 -08002600 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08002601 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08002602
2603 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Benedict Wonge2612cd2019-01-14 11:24:38 -08002604 __u32 mark = 0;
2605
2606 if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
2607 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002608
Herbert Xu25ee3282007-12-11 09:32:34 -08002609 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06002610 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02002611 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08002612 err = PTR_ERR(dst);
2613 if (IS_ERR(dst))
2614 goto put_states;
2615 } else
2616 dst_hold(dst);
2617
2618 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00002619 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08002620
David S. Millerf5b0a872012-07-19 12:31:33 -07002621 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08002622 dst1->flags |= DST_HOST;
2623 dst1->lastuse = now;
2624
2625 dst1->input = dst_discard;
Florian Westphal733a5fa2019-03-29 21:16:30 +01002626
2627 rcu_read_lock();
2628 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
2629 if (likely(afinfo))
2630 dst1->output = afinfo->output;
2631 else
2632 dst1->output = dst_discard_out;
2633 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08002634
David Miller45b018be2017-11-28 15:40:28 -05002635 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002636
2637 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08002638 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
2639 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002640 trailer_len += xfrm[i]->props.trailer_len;
2641 }
2642
David Miller45b018be2017-11-28 15:40:28 -05002643 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002644 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002645
2646 err = -ENODEV;
2647 dev = dst->dev;
2648 if (!dev)
2649 goto free_dst;
2650
David Miller45b018be2017-11-28 15:40:28 -05002651 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05002652 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08002653
David Miller45b018be2017-11-28 15:40:28 -05002654 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
2655 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
2656 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08002657 if (err)
2658 goto free_dst;
2659
David Miller45b018be2017-11-28 15:40:28 -05002660 xdst_prev->u.dst.header_len = header_len;
2661 xdst_prev->u.dst.trailer_len = trailer_len;
2662 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
2663 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08002664 }
2665
David Miller45b018be2017-11-28 15:40:28 -05002666 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08002667
2668put_states:
2669 for (; i < nx; i++)
2670 xfrm_state_put(xfrm[i]);
2671free_dst:
David Miller45b018be2017-11-28 15:40:28 -05002672 if (xdst0)
2673 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02002674
2675 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
David S. Miller73ff93c2011-02-22 18:33:42 -08002678static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002679 struct xfrm_policy **pols,
2680 int *num_pols, int *num_xfrms)
2681{
2682 int i;
2683
2684 if (*num_pols == 0 || !pols[0]) {
2685 *num_pols = 0;
2686 *num_xfrms = 0;
2687 return 0;
2688 }
2689 if (IS_ERR(pols[0]))
2690 return PTR_ERR(pols[0]);
2691
2692 *num_xfrms = pols[0]->xfrm_nr;
2693
2694#ifdef CONFIG_XFRM_SUB_POLICY
2695 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
2696 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2697 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
2698 XFRM_POLICY_TYPE_MAIN,
2699 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002700 XFRM_POLICY_OUT,
2701 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002702 if (pols[1]) {
2703 if (IS_ERR(pols[1])) {
2704 xfrm_pols_put(pols, *num_pols);
2705 return PTR_ERR(pols[1]);
2706 }
Weilong Chen02d08922013-12-24 09:43:48 +08002707 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00002708 (*num_xfrms) += pols[1]->xfrm_nr;
2709 }
2710 }
2711#endif
2712 for (i = 0; i < *num_pols; i++) {
2713 if (pols[i]->action != XFRM_POLICY_ALLOW) {
2714 *num_xfrms = -1;
2715 break;
2716 }
2717 }
2718
2719 return 0;
2720
2721}
2722
2723static struct xfrm_dst *
2724xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08002725 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00002726 struct dst_entry *dst_orig)
2727{
2728 struct net *net = xp_net(pols[0]);
2729 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05002730 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02002731 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002732 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002733 int err;
2734
2735 /* Try to instantiate a bundle */
2736 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00002737 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08002738 if (err == 0)
2739 return NULL;
2740
2741 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00002742 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2743 return ERR_PTR(err);
2744 }
2745
David Miller54920932017-11-28 15:41:01 -05002746 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002747 if (IS_ERR(dst)) {
2748 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
2749 return ERR_CAST(dst);
2750 }
2751
2752 xdst = (struct xfrm_dst *)dst;
2753 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00002754 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002755 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002756 xdst->policy_genid = atomic_read(&pols[0]->genid);
2757
2758 return xdst;
2759}
2760
Kees Cookc3aed702017-10-16 17:28:56 -07002761static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002762{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002763 struct sk_buff *skb;
2764 struct sock *sk;
2765 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07002766 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05002767 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002768 struct xfrm_policy_queue *pq = &pol->polq;
2769 struct flowi fl;
2770 struct sk_buff_head list;
2771
2772 spin_lock(&pq->hold_queue.lock);
2773 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02002774 if (!skb) {
2775 spin_unlock(&pq->hold_queue.lock);
2776 goto out;
2777 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002778 dst = skb_dst(skb);
2779 sk = skb->sk;
2780 xfrm_decode_session(skb, &fl, dst->ops->family);
2781 spin_unlock(&pq->hold_queue.lock);
2782
David Miller0f6c4802017-11-28 15:40:46 -05002783 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c982018-02-01 11:26:12 +01002784 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002785 if (IS_ERR(dst))
2786 goto purge_queue;
2787
2788 if (dst->flags & DST_XFRM_QUEUE) {
2789 dst_release(dst);
2790
2791 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
2792 goto purge_queue;
2793
2794 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002795 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
2796 xfrm_pol_hold(pol);
Colin Ian King7759d6a2018-11-13 14:28:42 +00002797 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002798 }
2799
2800 dst_release(dst);
2801
2802 __skb_queue_head_init(&list);
2803
2804 spin_lock(&pq->hold_queue.lock);
2805 pq->timeout = 0;
2806 skb_queue_splice_init(&pq->hold_queue, &list);
2807 spin_unlock(&pq->hold_queue.lock);
2808
2809 while (!skb_queue_empty(&list)) {
2810 skb = __skb_dequeue(&list);
2811
2812 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05002813 dst_hold(xfrm_dst_path(skb_dst(skb)));
2814 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002815 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002816 kfree_skb(skb);
2817 continue;
2818 }
2819
2820 nf_reset(skb);
2821 skb_dst_drop(skb);
2822 skb_dst_set(skb, dst);
2823
Eric W. Biederman13206b62015-10-07 16:48:35 -05002824 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002825 }
2826
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002827out:
2828 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002829 return;
2830
2831purge_queue:
2832 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002833 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002834 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002835}
2836
Eric W. Biedermanede20592015-10-07 16:48:47 -05002837static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002838{
2839 unsigned long sched_next;
2840 struct dst_entry *dst = skb_dst(skb);
2841 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002842 struct xfrm_policy *pol = xdst->pols[0];
2843 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002844
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002845 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002846 kfree_skb(skb);
2847 return 0;
2848 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002849
2850 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2851 kfree_skb(skb);
2852 return -EAGAIN;
2853 }
2854
2855 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002856
2857 spin_lock_bh(&pq->hold_queue.lock);
2858
2859 if (!pq->timeout)
2860 pq->timeout = XFRM_QUEUE_TMO_MIN;
2861
2862 sched_next = jiffies + pq->timeout;
2863
2864 if (del_timer(&pq->hold_timer)) {
2865 if (time_before(pq->hold_timer.expires, sched_next))
2866 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002867 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002868 }
2869
2870 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002871 if (!mod_timer(&pq->hold_timer, sched_next))
2872 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002873
2874 spin_unlock_bh(&pq->hold_queue.lock);
2875
2876 return 0;
2877}
2878
2879static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002880 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002881 const struct flowi *fl,
2882 int num_xfrms,
2883 u16 family)
2884{
2885 int err;
2886 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002887 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002888 struct dst_entry *dst1;
2889 struct xfrm_dst *xdst;
2890
2891 xdst = xfrm_alloc_dst(net, family);
2892 if (IS_ERR(xdst))
2893 return xdst;
2894
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002895 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2896 net->xfrm.sysctl_larval_drop ||
2897 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002898 return xdst;
2899
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002900 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002901 dst1 = &xdst->u.dst;
2902 dst_hold(dst);
2903 xdst->route = dst;
2904
2905 dst_copy_metrics(dst1, dst);
2906
2907 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2908 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2909 dst1->lastuse = jiffies;
2910
2911 dst1->input = dst_discard;
2912 dst1->output = xdst_queue_output;
2913
2914 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05002915 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05002916 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002917
2918 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2919
2920 err = -ENODEV;
2921 dev = dst->dev;
2922 if (!dev)
2923 goto free_dst;
2924
2925 err = xfrm_fill_dst(xdst, dev, fl);
2926 if (err)
2927 goto free_dst;
2928
2929out:
2930 return xdst;
2931
2932free_dst:
2933 dst_release(dst1);
2934 xdst = ERR_PTR(err);
2935 goto out;
2936}
2937
Benedict Wongbc56b332018-07-19 10:50:44 -07002938static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
2939 const struct flowi *fl,
2940 u16 family, u8 dir,
2941 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00002942{
Timo Teräs80c802f2010-04-07 00:30:05 +00002943 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02002944 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02002945 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002946
Timo Teräs80c802f2010-04-07 00:30:05 +00002947 /* Resolve policies to use if we couldn't get them from
2948 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02002949 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002950 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02002951 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00002952 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02002953 if (err < 0)
2954 goto inc_error;
2955 if (num_pols == 0)
2956 return NULL;
2957 if (num_xfrms <= 0)
2958 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002959
Florian Westphalbd45c532017-07-17 13:57:25 +02002960 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01002961 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02002962 if (IS_ERR(xdst)) {
2963 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002964 if (err == -EREMOTE) {
2965 xfrm_pols_put(pols, num_pols);
2966 return NULL;
2967 }
2968
Timo Teräs80c802f2010-04-07 00:30:05 +00002969 if (err != -EAGAIN)
2970 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02002971 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02002972 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00002973 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02002974 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00002975 }
2976
Florian Westphalbd45c532017-07-17 13:57:25 +02002977 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002978
2979make_dummy_bundle:
2980 /* We found policies, but there's no bundles to instantiate:
2981 * either because the policy blocks, has no transformations or
2982 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002983 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002984 if (IS_ERR(xdst)) {
2985 xfrm_pols_put(pols, num_pols);
2986 return ERR_CAST(xdst);
2987 }
2988 xdst->num_pols = num_pols;
2989 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002990 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002991
Florian Westphalbd45c532017-07-17 13:57:25 +02002992 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002993
2994inc_error:
2995 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2996error:
Florian Westphal855dad92017-07-17 13:57:22 +02002997 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002998 return ERR_PTR(err);
2999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
David S. Miller2774c132011-03-01 14:59:04 -08003001static struct dst_entry *make_blackhole(struct net *net, u16 family,
3002 struct dst_entry *dst_orig)
3003{
Florian Westphal37b10382017-02-07 15:00:19 +01003004 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08003005 struct dst_entry *ret;
3006
3007 if (!afinfo) {
3008 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00003009 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08003010 } else {
3011 ret = afinfo->blackhole_route(net, dst_orig);
3012 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003013 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08003014
3015 return ret;
3016}
3017
Benedict Wongbc56b332018-07-19 10:50:44 -07003018/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 *
3020 * At the moment we eat a raw IP route. Mostly to speed up lookups
3021 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07003022 *
3023 * xfrm_lookup uses an if_id of 0 by default, and is provided for
3024 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 */
Benedict Wongbc56b332018-07-19 10:50:44 -07003026struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
3027 struct dst_entry *dst_orig,
3028 const struct flowi *fl,
3029 const struct sock *sk,
3030 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003032 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00003033 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08003034 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00003035 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02003036 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00003037 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003038
Timo Teräs80c802f2010-04-07 00:30:05 +00003039 dst = NULL;
3040 xdst = NULL;
3041 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003042
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003043 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07003044 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003045 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003046 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
3047 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00003048 err = xfrm_expand_policies(fl, family, pols,
3049 &num_pols, &num_xfrms);
3050 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08003051 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00003052
3053 if (num_pols) {
3054 if (num_xfrms <= 0) {
3055 drop_pols = num_pols;
3056 goto no_transform;
3057 }
3058
3059 xdst = xfrm_resolve_and_create_bundle(
3060 pols, num_pols, fl,
3061 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01003062
Timo Teräs80c802f2010-04-07 00:30:05 +00003063 if (IS_ERR(xdst)) {
3064 xfrm_pols_put(pols, num_pols);
3065 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02003066 if (err == -EREMOTE)
3067 goto nopol;
3068
Timo Teräs80c802f2010-04-07 00:30:05 +00003069 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00003070 } else if (xdst == NULL) {
3071 num_xfrms = 0;
3072 drop_pols = num_pols;
3073 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00003074 }
3075
Timo Teräs80c802f2010-04-07 00:30:05 +00003076 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003077 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Timo Teräs80c802f2010-04-07 00:30:05 +00003080 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003081 struct xfrm_flo xflo;
3082
3083 xflo.dst_orig = dst_orig;
3084 xflo.flags = flags;
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07003087 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08003088 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08003089 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Benedict Wongbc56b332018-07-19 10:50:44 -07003091 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02003092 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00003093 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02003094 if (IS_ERR(xdst)) {
3095 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003096 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08003097 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003098
3099 num_pols = xdst->num_pols;
3100 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003101 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00003102 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
Timo Teräs80c802f2010-04-07 00:30:05 +00003105 dst = &xdst->u.dst;
3106 if (route == NULL && num_xfrms > 0) {
3107 /* The only case when xfrm_bundle_lookup() returns a
3108 * bundle with null route, is when the template could
3109 * not be resolved. It means policies are there, but
3110 * bundle could not be created, since we don't yet
3111 * have the xfrm_state's. We need to wait for KM to
3112 * negotiate new SA's or bail out with error.*/
3113 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00003114 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01003115 err = -EREMOTE;
3116 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003117 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003118
Steffen Klassert5b8ef342013-08-27 13:43:30 +02003119 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00003120
3121 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
3122 goto error;
3123 }
3124
3125no_transform:
3126 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08003127 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Timo Teräs80c802f2010-04-07 00:30:05 +00003129 if ((flags & XFRM_LOOKUP_ICMP) &&
3130 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
3131 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08003132 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003133 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08003134
Timo Teräs80c802f2010-04-07 00:30:05 +00003135 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02003136 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08003137
Timo Teräs80c802f2010-04-07 00:30:05 +00003138 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003140 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07003141 err = -EPERM;
3142 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00003143 } else if (num_xfrms > 0) {
3144 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00003145 dst_release(dst_orig);
3146 } else {
3147 /* Flow passes untransformed */
3148 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08003149 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003151ok:
3152 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00003153 if (dst && dst->xfrm &&
3154 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
3155 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08003156 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Timo Teräs80c802f2010-04-07 00:30:05 +00003158nopol:
David S. Miller452edd52011-03-02 13:27:41 -08003159 if (!(flags & XFRM_LOOKUP_ICMP)) {
3160 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00003161 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08003162 }
Timo Teräs80c802f2010-04-07 00:30:05 +00003163 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164error:
Timo Teräs80c802f2010-04-07 00:30:05 +00003165 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08003166dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01003167 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
3168 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00003169 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08003170 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
Benedict Wongbc56b332018-07-19 10:50:44 -07003172EXPORT_SYMBOL(xfrm_lookup_with_ifid);
3173
3174/* Main function: finds/creates a bundle for given flow.
3175 *
3176 * At the moment we eat a raw IP route. Mostly to speed up lookups
3177 * on interfaces with disabled IPsec.
3178 */
3179struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
3180 const struct flowi *fl, const struct sock *sk,
3181 int flags)
3182{
3183 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
3184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185EXPORT_SYMBOL(xfrm_lookup);
3186
Steffen Klassertf92ee612014-09-16 10:08:40 +02003187/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
3188 * Otherwise we may send out blackholed packets.
3189 */
3190struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
3191 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07003192 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02003193{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003194 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01003195 flags | XFRM_LOOKUP_QUEUE |
3196 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02003197
3198 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
3199 return make_blackhole(net, dst_orig->ops->family, dst_orig);
3200
Tommi Rantala8cc88772018-06-21 09:30:47 +03003201 if (IS_ERR(dst))
3202 dst_release(dst_orig);
3203
Steffen Klassertf92ee612014-09-16 10:08:40 +02003204 return dst;
3205}
3206EXPORT_SYMBOL(xfrm_lookup_route);
3207
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003208static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08003209xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003210{
Florian Westphal2294be0f2018-12-18 17:15:20 +01003211 struct sec_path *sp = skb_sec_path(skb);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003212 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003213
Florian Westphal2294be0f2018-12-18 17:15:20 +01003214 if (!sp || idx < 0 || idx >= sp->len)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003215 return 0;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003216 x = sp->xvec[idx];
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003217 if (!x->type->reject)
3218 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07003219 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003220}
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222/* When skb is transformed back to its "native" form, we have to
3223 * check policy restrictions. At the moment we make this in maximally
3224 * stupid way. Shame on me. :-) Of course, connected sockets must
3225 * have policy cached at them.
3226 */
3227
3228static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05003229xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 unsigned short family)
3231{
3232 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08003233 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return x->id.proto == tmpl->id.proto &&
3235 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
3236 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
3237 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07003238 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07003239 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003240 !(x->props.mode != XFRM_MODE_TRANSPORT &&
3241 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003244/*
3245 * 0 or more than 0 is returned when validation is succeeded (either bypass
3246 * because of optional transport mode, or next index of the mathced secpath
3247 * state with the template.
3248 * -1 is returned when no matching template is found.
3249 * Otherwise "-2 - errored_index" is returned.
3250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05003252xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 unsigned short family)
3254{
3255 int idx = start;
3256
3257 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07003258 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return start;
3260 } else
3261 start = -1;
3262 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08003263 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003265 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
3266 if (start == -1)
3267 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
3271 return start;
3272}
3273
Herbert Xud5422ef2007-12-12 10:44:16 -08003274int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
3275 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
Florian Westphal37b10382017-02-07 15:00:19 +01003277 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 if (unlikely(afinfo == NULL))
3281 return -EAFNOSUPPORT;
3282
Herbert Xud5422ef2007-12-12 10:44:16 -08003283 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf203b762018-06-12 14:07:12 +02003284
David S. Miller1d28f422011-03-12 00:29:39 -05003285 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01003286 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07003287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
Herbert Xud5422ef2007-12-12 10:44:16 -08003289EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
David S. Miller9a7386e2011-02-24 01:44:12 -05003291static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
3293 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003294 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003295 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
3299
3300 return 0;
3301}
3302
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003303int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 unsigned short family)
3305{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003306 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003308 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
3309 int npols = 0;
3310 int xfrm_nr;
3311 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08003312 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003314 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07003315 const struct xfrm_if_cb *ifcb;
Florian Westphal2294be0f2018-12-18 17:15:20 +01003316 struct sec_path *sp;
Benedict Wongbc56b332018-07-19 10:50:44 -07003317 struct xfrm_if *xi;
3318 u32 if_id = 0;
3319
3320 rcu_read_lock();
3321 ifcb = xfrm_if_get_cb();
3322
3323 if (ifcb) {
3324 xi = ifcb->decode_session(skb);
Tobias Brunner660899d2019-02-18 10:49:39 +01003325 if (xi) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003326 if_id = xi->p.if_id;
Tobias Brunner660899d2019-02-18 10:49:39 +01003327 net = xi->net;
3328 }
Benedict Wongbc56b332018-07-19 10:50:44 -07003329 }
3330 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Herbert Xud5422ef2007-12-12 10:44:16 -08003332 reverse = dir & ~XFRM_POLICY_MASK;
3333 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08003334
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003335 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003336 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003338 }
3339
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08003340 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 /* First, check used SA against their selectors. */
Florian Westphal2294be0f2018-12-18 17:15:20 +01003343 sp = skb_sec_path(skb);
3344 if (sp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 int i;
3346
Florian Westphal2294be0f2018-12-18 17:15:20 +01003347 for (i = sp->len - 1; i >= 0; i--) {
3348 struct xfrm_state *x = sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003349 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003350 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
3354 }
3355
3356 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08003357 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003358 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07003359 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003360 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003361 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003362 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003363 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05003364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Florian Westphal86dc8ee2017-07-17 13:57:24 +02003366 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07003367 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003369 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003370 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003371 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003372 }
James Morris134b0fc2006-10-05 15:42:27 -05003373
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003374 if (!pol) {
Florian Westphal2294be0f2018-12-18 17:15:20 +01003375 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003376 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003377 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003378 return 0;
3379 }
3380 return 1;
3381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Arnd Bergmann386c5682018-07-11 12:19:13 +02003383 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003385 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08003386 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003387#ifdef CONFIG_XFRM_SUB_POLICY
3388 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08003389 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003390 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07003391 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003392 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003393 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003394 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05003395 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003396 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02003397 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08003398 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003399 }
3400 }
3401#endif
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (pol->action == XFRM_POLICY_ALLOW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003405 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003406 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003407 struct xfrm_tmpl **tpp = tp;
3408 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 int i, k;
3410
Florian Westphal2294be0f2018-12-18 17:15:20 +01003411 sp = skb_sec_path(skb);
3412 if (!sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 sp = &dummy;
3414
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003415 for (pi = 0; pi < npols; pi++) {
3416 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003417 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003418 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003419 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003420 }
3421 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003422 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003423 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003424 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003425 for (i = 0; i < pols[pi]->xfrm_nr; i++)
3426 tpp[ti++] = &pols[pi]->xfrm_vec[i];
3427 }
3428 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003429 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08003430 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07003431 tpp = stp;
3432 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 /* For each tunnel xfrm, find the first matching tmpl.
3435 * For each tmpl before that, find corresponding xfrm.
3436 * Order is _important_. Later we will implement
3437 * some barriers, but at the moment barriers
3438 * are implied between each two transformations.
3439 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003440 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
3441 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003442 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07003443 if (k < -1)
3444 /* "-2 - errored_index" returned */
3445 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003446 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
3450
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003451 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003452 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003456 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 return 1;
3458 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003459 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07003462 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07003463reject_error:
3464 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return 0;
3466}
3467EXPORT_SYMBOL(__xfrm_policy_check);
3468
3469int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
3470{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08003471 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00003473 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07003474 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003476 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00003477 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08003479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Eric Dumazetfafeeb62010-06-01 10:04:49 +00003481 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +02003482 if (!skb_dst(skb)) {
3483 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
3484 return 0;
3485 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003486
Steffen Klassertb8c203b2014-09-16 10:08:49 +02003487 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08003488 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07003489 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08003490 dst = NULL;
3491 }
Eric Dumazetadf30902009-06-02 05:19:30 +00003492 skb_dst_set(skb, dst);
3493 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495EXPORT_SYMBOL(__xfrm_route_forward);
3496
David S. Millerd49c73c2006-08-13 18:55:53 -07003497/* Optimize later using cookies and generation ids. */
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
3500{
David S. Millerd49c73c2006-08-13 18:55:53 -07003501 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07003502 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
3503 * get validated by dst_ops->check on every use. We do this
3504 * because when a normal route referenced by an XFRM dst is
3505 * obsoleted we do not go looking around for all parent
3506 * referencing XFRM dsts so that we can invalidate them. It
3507 * is just too much work. Instead we make the checks here on
3508 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07003509 *
3510 * XFRM dst A --> IPv4 dst X
3511 *
3512 * X is the "xdst->route" of A (X is also the "dst->path" of A
3513 * in this example). If X is marked obsolete, "A" will not
3514 * notice. That's what we are validating here via the
3515 * stale_bundle() check.
3516 *
Wei Wang52df1572017-06-17 10:42:38 -07003517 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
3518 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02003519 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07003520 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08003521 */
David S. Millerd49c73c2006-08-13 18:55:53 -07003522 if (dst->obsolete < 0 && !stale_bundle(dst))
3523 return dst;
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 return NULL;
3526}
3527
3528static int stale_bundle(struct dst_entry *dst)
3529{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003530 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Herbert Xuaabc9762005-05-03 16:27:10 -07003533void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
David Millerb92cf4a2017-11-28 15:40:22 -05003535 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003536 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07003537 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 dev_put(dev);
3539 }
3540}
Herbert Xuaabc9762005-05-03 16:27:10 -07003541EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
3543static void xfrm_link_failure(struct sk_buff *skb)
3544{
3545 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
3548static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
3549{
3550 if (dst) {
3551 if (dst->obsolete) {
3552 dst_release(dst);
3553 dst = NULL;
3554 }
3555 }
3556 return dst;
3557}
3558
David Miller54920932017-11-28 15:41:01 -05003559static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
David Miller54920932017-11-28 15:41:01 -05003561 while (nr--) {
3562 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05003564 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
David Miller54920932017-11-28 15:41:01 -05003566 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05003567 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 xdst->child_mtu_cached = pmtu;
3569
3570 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
3571
3572 route_mtu_cached = dst_mtu(xdst->route);
3573 xdst->route_mtu_cached = route_mtu_cached;
3574
3575 if (pmtu > route_mtu_cached)
3576 pmtu = route_mtu_cached;
3577
David S. Millerdefb3512010-12-08 21:16:57 -08003578 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05003579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582/* Check that the bundle accepts the flow and its components are
3583 * still valid.
3584 */
3585
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00003586static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
David Miller54920932017-11-28 15:41:01 -05003588 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05003590 struct xfrm_dst *xdst;
3591 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 u32 mtu;
3593
David Miller0f6c4802017-11-28 15:40:46 -05003594 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 (dst->dev && !netif_running(dst->dev)))
3596 return 0;
3597
Steffen Klasserta0073fe2013-02-05 12:52:55 +01003598 if (dst->flags & DST_XFRM_QUEUE)
3599 return 1;
3600
David Miller54920932017-11-28 15:41:01 -05003601 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 do {
3603 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 if (dst->xfrm->km.state != XFRM_STATE_VALID)
3606 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00003607 if (xdst->xfrm_genid != dst->xfrm->genid)
3608 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07003609 if (xdst->num_pols > 0 &&
3610 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07003611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
David Miller54920932017-11-28 15:41:01 -05003613 bundle[nr++] = xdst;
3614
David Millerb92cf4a2017-11-28 15:40:22 -05003615 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003617 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 xdst->child_mtu_cached = mtu;
3619 }
3620
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07003621 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 return 0;
3623 mtu = dst_mtu(xdst->route);
3624 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05003625 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 xdst->route_mtu_cached = mtu;
3627 }
3628
David Millerb92cf4a2017-11-28 15:40:22 -05003629 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 } while (dst->xfrm);
3631
David Miller54920932017-11-28 15:41:01 -05003632 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 return 1;
3634
David Miller54920932017-11-28 15:41:01 -05003635 xdst = bundle[start_from - 1];
3636 mtu = xdst->child_mtu_cached;
3637 while (start_from--) {
3638 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05003641 if (mtu > xdst->route_mtu_cached)
3642 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08003643 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05003644 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 break;
3646
David Miller54920932017-11-28 15:41:01 -05003647 xdst = bundle[start_from - 1];
3648 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650
3651 return 1;
3652}
3653
David S. Miller0dbaee32010-12-13 12:52:14 -08003654static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
3655{
David Miller0f6c4802017-11-28 15:40:46 -05003656 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08003657}
3658
Steffen Klassertebb762f2011-11-23 02:12:51 +00003659static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08003660{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00003661 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
3662
David Miller0f6c4802017-11-28 15:40:46 -05003663 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08003664}
3665
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003666static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
3667 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02003668{
David Miller0f6c4802017-11-28 15:40:46 -05003669 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02003670 const struct xfrm_state *xfrm = dst->xfrm;
3671
Steffen Klassert013cb812018-02-19 07:44:07 +01003672 dst = xfrm_dst_child(dst);
3673
Julian Anastasov63fca652017-02-06 23:14:15 +02003674 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
3675 continue;
3676 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
3677 daddr = xfrm->coaddr;
3678 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
3679 daddr = &xfrm->id.daddr;
3680 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003681 return daddr;
3682}
3683
3684static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
3685 struct sk_buff *skb,
3686 const void *daddr)
3687{
David Miller0f6c4802017-11-28 15:40:46 -05003688 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003689
3690 if (!skb)
3691 daddr = xfrm_get_dst_nexthop(dst, daddr);
3692 return path->ops->neigh_lookup(path, skb, daddr);
3693}
3694
3695static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
3696{
David Miller0f6c4802017-11-28 15:40:46 -05003697 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02003698
3699 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02003700 path->ops->confirm_neigh(path, daddr);
3701}
3702
Florian Westphala2817d82017-02-07 15:00:17 +01003703int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
3705 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01003706
3707 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01003709
Eric Dumazetef8531b2012-08-19 12:31:48 +02003710 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01003711 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08003712 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 else {
3714 struct dst_ops *dst_ops = afinfo->dst_ops;
3715 if (likely(dst_ops->kmem_cachep == NULL))
3716 dst_ops->kmem_cachep = xfrm_dst_cache;
3717 if (likely(dst_ops->check == NULL))
3718 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08003719 if (likely(dst_ops->default_advmss == NULL))
3720 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00003721 if (likely(dst_ops->mtu == NULL))
3722 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (likely(dst_ops->negative_advice == NULL))
3724 dst_ops->negative_advice = xfrm_negative_advice;
3725 if (likely(dst_ops->link_failure == NULL))
3726 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07003727 if (likely(dst_ops->neigh_lookup == NULL))
3728 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02003729 if (likely(!dst_ops->confirm_neigh))
3730 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01003731 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02003733 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 return err;
3736}
3737EXPORT_SYMBOL(xfrm_policy_register_afinfo);
3738
Florian Westphala2817d82017-02-07 15:00:17 +01003739void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
Florian Westphal2b619972017-02-07 15:00:15 +01003741 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01003742 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003743
Florian Westphala2817d82017-02-07 15:00:17 +01003744 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
3745 if (xfrm_policy_afinfo[i] != afinfo)
3746 continue;
3747 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
3748 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02003749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Florian Westphal2b619972017-02-07 15:00:15 +01003751 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Florian Westphal2b619972017-02-07 15:00:15 +01003753 dst_ops->kmem_cachep = NULL;
3754 dst_ops->check = NULL;
3755 dst_ops->negative_advice = NULL;
3756 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
3759
Steffen Klassertf203b762018-06-12 14:07:12 +02003760void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
3761{
3762 spin_lock(&xfrm_if_cb_lock);
3763 rcu_assign_pointer(xfrm_if_cb, ifcb);
3764 spin_unlock(&xfrm_if_cb_lock);
3765}
3766EXPORT_SYMBOL(xfrm_if_register_cb);
3767
3768void xfrm_if_unregister_cb(void)
3769{
3770 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3771 synchronize_rcu();
3772}
3773EXPORT_SYMBOL(xfrm_if_unregister_cb);
3774
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003775#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003776static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003777{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003778 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07003779 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
3780 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003781 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003782 rv = xfrm_proc_init(net);
3783 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07003784 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003785 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003786}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003787
3788static void xfrm_statistics_fini(struct net *net)
3789{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08003790 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07003791 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003792}
3793#else
3794static int __net_init xfrm_statistics_init(struct net *net)
3795{
3796 return 0;
3797}
3798
3799static void xfrm_statistics_fini(struct net *net)
3800{
3801}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08003802#endif
3803
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003804static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
David S. Miller2518c7c2006-08-24 04:45:07 -07003806 unsigned int hmask, sz;
Florian Westphal24969fa2018-11-07 23:00:35 +01003807 int dir, err;
David S. Miller2518c7c2006-08-24 04:45:07 -07003808
Florian Westphal24969fa2018-11-07 23:00:35 +01003809 if (net_eq(net, &init_net)) {
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003810 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07003812 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003813 NULL);
Florian Westphal24969fa2018-11-07 23:00:35 +01003814 err = rhashtable_init(&xfrm_policy_inexact_table,
3815 &xfrm_pol_inexact_params);
3816 BUG_ON(err);
3817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
David S. Miller2518c7c2006-08-24 04:45:07 -07003819 hmask = 8 - 1;
3820 sz = (hmask+1) * sizeof(struct hlist_head);
3821
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003822 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
3823 if (!net->xfrm.policy_byidx)
3824 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003825 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07003826
Herbert Xu53c2e282014-11-13 17:09:49 +08003827 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07003828 struct xfrm_policy_hash *htab;
3829
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08003830 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08003831 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003832 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07003833
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003834 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07003835 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07003836 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003837 goto out_bydst;
3838 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02003839 htab->dbits4 = 32;
3840 htab->sbits4 = 32;
3841 htab->dbits6 = 128;
3842 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003843 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003844 net->xfrm.policy_hthresh.lbits4 = 32;
3845 net->xfrm.policy_hthresh.rbits4 = 32;
3846 net->xfrm.policy_hthresh.lbits6 = 128;
3847 net->xfrm.policy_hthresh.rbits6 = 128;
3848
3849 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003850
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003851 INIT_LIST_HEAD(&net->xfrm.policy_all);
Florian Westphal24969fa2018-11-07 23:00:35 +01003852 INIT_LIST_HEAD(&net->xfrm.inexact_bins);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003853 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003854 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003855 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003856
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003857out_bydst:
3858 for (dir--; dir >= 0; dir--) {
3859 struct xfrm_policy_hash *htab;
3860
3861 htab = &net->xfrm.policy_bydst[dir];
3862 xfrm_hash_free(htab->table, sz);
3863 }
3864 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003865out_byidx:
3866 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003869static void xfrm_policy_fini(struct net *net)
3870{
Florian Westphal6be3b0d2018-11-07 23:00:37 +01003871 struct xfrm_pol_inexact_bin *b, *t;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003872 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003873 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003874
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003875 flush_work(&net->xfrm.policy_hash_work);
3876#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003877 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003878#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003879 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003880
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003881 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003882
Herbert Xu53c2e282014-11-13 17:09:49 +08003883 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003884 struct xfrm_policy_hash *htab;
3885
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003886 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003887
3888 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003889 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003890 WARN_ON(!hlist_empty(htab->table));
3891 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003892 }
3893
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003894 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003895 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3896 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Florian Westphal24969fa2018-11-07 23:00:35 +01003897
Florian Westphal6be3b0d2018-11-07 23:00:37 +01003898 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
3899 list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
3900 __xfrm_policy_inexact_prune_bin(b, true);
3901 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003902}
3903
3904static int __net_init xfrm_net_init(struct net *net)
3905{
3906 int rv;
3907
Florian Westphalc2822222017-02-08 11:52:29 +01003908 /* Initialize the per-net locks here */
3909 spin_lock_init(&net->xfrm.xfrm_state_lock);
3910 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3911 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3912
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003913 rv = xfrm_statistics_init(net);
3914 if (rv < 0)
3915 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003916 rv = xfrm_state_init(net);
3917 if (rv < 0)
3918 goto out_state;
3919 rv = xfrm_policy_init(net);
3920 if (rv < 0)
3921 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003922 rv = xfrm_sysctl_init(net);
3923 if (rv < 0)
3924 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08003925
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003926 return 0;
3927
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003928out_sysctl:
3929 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003930out_policy:
3931 xfrm_state_fini(net);
3932out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003933 xfrm_statistics_fini(net);
3934out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003935 return rv;
3936}
3937
3938static void __net_exit xfrm_net_exit(struct net *net)
3939{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003940 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003941 xfrm_policy_fini(net);
3942 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003943 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003944}
3945
3946static struct pernet_operations __net_initdata xfrm_net_ops = {
3947 .init = xfrm_net_init,
3948 .exit = xfrm_net_exit,
3949};
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951void __init xfrm_init(void)
3952{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003953 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03003954 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02003955 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02003957
3958 RCU_INIT_POINTER(xfrm_if_cb, NULL);
3959 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
Joy Lattenab5f5e82007-09-17 11:51:22 -07003962#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003963static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3964 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003965{
Paul Moore875179f2007-12-01 23:27:18 +11003966 struct xfrm_sec_ctx *ctx = xp->security;
3967 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003968
Paul Moore875179f2007-12-01 23:27:18 +11003969 if (ctx)
3970 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3971 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3972
Weilong Chen9b7a7872013-12-24 09:43:46 +08003973 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003974 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003975 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003976 if (sel->prefixlen_s != 32)
3977 audit_log_format(audit_buf, " src_prefixlen=%d",
3978 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003979 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003980 if (sel->prefixlen_d != 32)
3981 audit_log_format(audit_buf, " dst_prefixlen=%d",
3982 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003983 break;
3984 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003985 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003986 if (sel->prefixlen_s != 128)
3987 audit_log_format(audit_buf, " src_prefixlen=%d",
3988 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003989 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003990 if (sel->prefixlen_d != 128)
3991 audit_log_format(audit_buf, " dst_prefixlen=%d",
3992 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003993 break;
3994 }
3995}
3996
Tetsuo Handa2e710292014-04-22 21:48:30 +09003997void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003998{
3999 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004000
Paul Mooreafeb14b2007-12-21 14:58:11 -08004001 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004002 if (audit_buf == NULL)
4003 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004004 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004005 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004006 xfrm_audit_common_policyinfo(xp, audit_buf);
4007 audit_log_end(audit_buf);
4008}
4009EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
4010
Paul Moore68277ac2007-12-20 20:49:33 -08004011void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09004012 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07004013{
4014 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07004015
Paul Mooreafeb14b2007-12-21 14:58:11 -08004016 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07004017 if (audit_buf == NULL)
4018 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09004019 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08004020 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07004021 xfrm_audit_common_policyinfo(xp, audit_buf);
4022 audit_log_end(audit_buf);
4023}
4024EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
4025#endif
4026
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004027#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04004028static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
4029 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004030{
4031 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
4032 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004033 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
4034 sel_cmp->family) &&
4035 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
4036 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004037 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
4038 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004039 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004040 }
4041 } else {
4042 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04004043 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004044 }
4045 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04004046 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004047}
4048
Weilong Chen3e94c2d2013-12-24 09:43:47 +08004049static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
4050 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004051{
4052 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004053 struct hlist_head *chain;
4054 u32 priority = ~0U;
4055
Florian Westphal9d0380d2016-08-11 15:17:59 +02004056 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08004057 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004058 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004059 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
4060 pol->type == type) {
4061 ret = pol;
4062 priority = ret->priority;
4063 break;
4064 }
4065 }
Fan Du8d549c42013-11-07 17:47:49 +08004066 chain = &net->xfrm.policy_inexact[dir];
Florian Westphal24969fa2018-11-07 23:00:35 +01004067 hlist_for_each_entry(pol, chain, bydst_inexact_list) {
Li RongQing8faf4912015-05-14 11:16:59 +08004068 if ((pol->priority >= priority) && ret)
4069 break;
4070
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004071 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08004072 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004073 ret = pol;
4074 break;
4075 }
4076 }
4077
Li RongQing586f2eb2015-04-30 17:13:41 +08004078 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004079
Florian Westphal9d0380d2016-08-11 15:17:59 +02004080 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004081
4082 return ret;
4083}
4084
David S. Millerdd701752011-02-24 00:21:08 -05004085static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004086{
4087 int match = 0;
4088
4089 if (t->mode == m->mode && t->id.proto == m->proto &&
4090 (m->reqid == 0 || t->reqid == m->reqid)) {
4091 switch (t->mode) {
4092 case XFRM_MODE_TUNNEL:
4093 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00004094 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
4095 m->old_family) &&
4096 xfrm_addr_equal(&t->saddr, &m->old_saddr,
4097 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004098 match = 1;
4099 }
4100 break;
4101 case XFRM_MODE_TRANSPORT:
4102 /* in case of transport mode, template does not store
4103 any IP addresses, hence we just compare mode and
4104 protocol */
4105 match = 1;
4106 break;
4107 default:
4108 break;
4109 }
4110 }
4111 return match;
4112}
4113
4114/* update endpoint address(es) of template(s) */
4115static int xfrm_policy_migrate(struct xfrm_policy *pol,
4116 struct xfrm_migrate *m, int num_migrate)
4117{
4118 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004119 int i, j, n = 0;
4120
4121 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07004122 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004123 /* target policy has been deleted */
4124 write_unlock_bh(&pol->lock);
4125 return -ENOENT;
4126 }
4127
4128 for (i = 0; i < pol->xfrm_nr; i++) {
4129 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
4130 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
4131 continue;
4132 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07004133 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
4134 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004135 continue;
4136 /* update endpoints */
4137 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
4138 sizeof(pol->xfrm_vec[i].id.daddr));
4139 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
4140 sizeof(pol->xfrm_vec[i].saddr));
4141 pol->xfrm_vec[i].encap_family = mp->new_family;
4142 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00004143 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004144 }
4145 }
4146
4147 write_unlock_bh(&pol->lock);
4148
4149 if (!n)
4150 return -ENODATA;
4151
4152 return 0;
4153}
4154
David S. Millerdd701752011-02-24 00:21:08 -05004155static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004156{
4157 int i, j;
4158
4159 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
4160 return -EINVAL;
4161
4162 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004163 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
4164 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
4165 return -EINVAL;
4166
4167 /* check if there is any duplicated entry */
4168 for (j = i + 1; j < num_migrate; j++) {
4169 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
4170 sizeof(m[i].old_daddr)) &&
4171 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
4172 sizeof(m[i].old_saddr)) &&
4173 m[i].proto == m[j].proto &&
4174 m[i].mode == m[j].mode &&
4175 m[i].reqid == m[j].reqid &&
4176 m[i].old_family == m[j].old_family)
4177 return -EINVAL;
4178 }
4179 }
4180
4181 return 0;
4182}
4183
David S. Millerb4b7c0b2011-02-24 00:35:06 -05004184int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07004185 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02004186 struct xfrm_kmaddress *k, struct net *net,
4187 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004188{
4189 int i, err, nx_cur = 0, nx_new = 0;
4190 struct xfrm_policy *pol = NULL;
4191 struct xfrm_state *x, *xc;
4192 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
4193 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
4194 struct xfrm_migrate *mp;
4195
Vladis Dronov7bab0962017-08-02 19:50:14 +02004196 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004197 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
4198 goto out;
4199
Vladis Dronov7bab0962017-08-02 19:50:14 +02004200 if (dir >= XFRM_POLICY_MAX) {
4201 err = -EINVAL;
4202 goto out;
4203 }
4204
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004205 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08004206 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004207 err = -ENOENT;
4208 goto out;
4209 }
4210
4211 /* Stage 2 - find and update state(s) */
4212 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08004213 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004214 x_cur[nx_cur] = x;
4215 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02004216 xc = xfrm_state_migrate(x, mp, encap);
4217 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004218 x_new[nx_new] = xc;
4219 nx_new++;
4220 } else {
4221 err = -ENODATA;
4222 goto restore_state;
4223 }
4224 }
4225 }
4226
4227 /* Stage 3 - update policy */
4228 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
4229 goto restore_state;
4230
4231 /* Stage 4 - delete old state(s) */
4232 if (nx_cur) {
4233 xfrm_states_put(x_cur, nx_cur);
4234 xfrm_states_delete(x_cur, nx_cur);
4235 }
4236
4237 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02004238 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004239
4240 xfrm_pol_put(pol);
4241
4242 return 0;
4243out:
4244 return err;
4245
4246restore_state:
4247 if (pol)
4248 xfrm_pol_put(pol);
4249 if (nx_cur)
4250 xfrm_states_put(x_cur, nx_cur);
4251 if (nx_new)
4252 xfrm_states_delete(x_new, nx_new);
4253
4254 return err;
4255}
David S. Millere610e672007-02-08 13:29:15 -08004256EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08004257#endif