blob: 3110c3fbee2099e7a4563a99c988e5ad66d0658c [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>
Herbert Xu25ee3282007-12-11 09:32:34 -080029#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050030#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/xfrm.h>
32#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080033#ifdef CONFIG_XFRM_STATISTICS
34#include <net/snmp.h>
35#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
David S. Miller44e36b42006-08-24 04:50:50 -070037#include "xfrm_hash.h"
38
Steffen Klasserta0073fe2013-02-05 12:52:55 +010039#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
40#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
41#define XFRM_MAX_QUEUE_LEN 100
42
Steffen Klassertb8c203b2014-09-16 10:08:49 +020043struct xfrm_flo {
44 struct dst_entry *dst_orig;
45 u8 flags;
46};
47
Steffen Klassertf203b762018-06-12 14:07:12 +020048static DEFINE_SPINLOCK(xfrm_if_cb_lock);
49static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
50
Priyanka Jain418a99a2012-08-12 21:22:29 +000051static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +010052static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +000053 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Alexey Dobriyanf8c3d0d2018-02-24 21:21:38 +030055static struct kmem_cache *xfrm_dst_cache __ro_after_init;
Florian Westphal30846092016-08-11 15:17:54 +020056static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David Miller54920932017-11-28 15:41:01 -050058static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
Timo Teräs80c802f2010-04-07 00:30:05 +000059static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000060static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Kees Cookc3aed702017-10-16 17:28:56 -070061static void xfrm_policy_queue_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Herbert Xu12bfa8b2014-11-13 17:09:50 +080063static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080064static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
65 int dir);
66
Florian Westphale37cc8a2016-08-11 15:17:55 +020067static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
68{
Reshetova, Elena850a6212017-07-04 15:53:22 +030069 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8a2016-08-11 15:17:55 +020070}
71
David S. Millerbc9b35a2012-05-15 15:04:57 -040072static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050073__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080074{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050075 const struct flowi4 *fl4 = &fl->u.ip4;
76
Alexey Dobriyan26bff942011-11-22 06:46:02 +000077 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
78 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050079 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
80 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
81 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
82 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080083}
84
David S. Millerbc9b35a2012-05-15 15:04:57 -040085static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050086__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080087{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050088 const struct flowi6 *fl6 = &fl->u.ip6;
89
90 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
91 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
92 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
93 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
94 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
95 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080096}
97
David S. Millerbc9b35a2012-05-15 15:04:57 -040098bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
99 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -0800100{
101 switch (family) {
102 case AF_INET:
103 return __xfrm4_selector_match(sel, fl);
104 case AF_INET6:
105 return __xfrm6_selector_match(sel, fl);
106 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400107 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800108}
109
Florian Westphala2817d82017-02-07 15:00:17 +0100110static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200111{
Florian Westphala2817d82017-02-07 15:00:17 +0100112 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200113
Florian Westphala2817d82017-02-07 15:00:17 +0100114 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200115 return NULL;
116 rcu_read_lock();
117 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
118 if (unlikely(!afinfo))
119 rcu_read_unlock();
120 return afinfo;
121}
122
Steffen Klassertf203b762018-06-12 14:07:12 +0200123/* Called with rcu_read_lock(). */
124static const struct xfrm_if_cb *xfrm_if_get_cb(void)
125{
126 return rcu_dereference(xfrm_if_cb);
127}
128
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200129struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
130 const xfrm_address_t *saddr,
131 const xfrm_address_t *daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900132 int family, u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Florian Westphal37b10382017-02-07 15:00:19 +0100134 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800135 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Herbert Xu25ee3282007-12-11 09:32:34 -0800137 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800139 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900141 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900142
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100143 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900144
145 return dst;
146}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200147EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900148
David Ahern42a7b322015-08-10 16:58:11 -0600149static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
150 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900151 xfrm_address_t *prev_saddr,
152 xfrm_address_t *prev_daddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900153 int family, u32 mark)
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900154{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800155 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900156 xfrm_address_t *saddr = &x->props.saddr;
157 xfrm_address_t *daddr = &x->id.daddr;
158 struct dst_entry *dst;
159
160 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
161 saddr = x->coaddr;
162 daddr = prev_daddr;
163 }
164 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
165 saddr = prev_saddr;
166 daddr = x->coaddr;
167 }
168
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900169 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900170
171 if (!IS_ERR(dst)) {
172 if (prev_saddr != saddr)
173 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
174 if (prev_daddr != daddr)
175 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
176 }
177
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800178 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static inline unsigned long make_jiffies(long secs)
182{
183 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
184 return MAX_SCHEDULE_TIMEOUT-1;
185 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900186 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Kees Cookc3aed702017-10-16 17:28:56 -0700189static void xfrm_policy_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Kees Cookc3aed702017-10-16 17:28:56 -0700191 struct xfrm_policy *xp = from_timer(xp, t, timer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200192 time64_t now = ktime_get_real_seconds();
193 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int warn = 0;
195 int dir;
196
197 read_lock(&xp->lock);
198
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000199 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out;
201
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700202 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (xp->lft.hard_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200205 time64_t tmo = xp->lft.hard_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 xp->curlft.add_time - now;
207 if (tmo <= 0)
208 goto expired;
209 if (tmo < next)
210 next = tmo;
211 }
212 if (xp->lft.hard_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200213 time64_t tmo = xp->lft.hard_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
215 if (tmo <= 0)
216 goto expired;
217 if (tmo < next)
218 next = tmo;
219 }
220 if (xp->lft.soft_add_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200221 time64_t tmo = xp->lft.soft_add_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 xp->curlft.add_time - now;
223 if (tmo <= 0) {
224 warn = 1;
225 tmo = XFRM_KM_TIMEOUT;
226 }
227 if (tmo < next)
228 next = tmo;
229 }
230 if (xp->lft.soft_use_expires_seconds) {
Arnd Bergmann386c5682018-07-11 12:19:13 +0200231 time64_t tmo = xp->lft.soft_use_expires_seconds +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
233 if (tmo <= 0) {
234 warn = 1;
235 tmo = XFRM_KM_TIMEOUT;
236 }
237 if (tmo < next)
238 next = tmo;
239 }
240
241 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800242 km_policy_expired(xp, dir, 0, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200243 if (next != TIME64_MAX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
245 xfrm_pol_hold(xp);
246
247out:
248 read_unlock(&xp->lock);
249 xfrm_pol_put(xp);
250 return;
251
252expired:
253 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700254 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800255 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 xfrm_pol_put(xp);
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
260 * SPD calls.
261 */
262
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800263struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct xfrm_policy *policy;
266
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700267 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800270 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700271 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700272 INIT_HLIST_NODE(&policy->bydst);
273 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300275 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100276 skb_queue_head_init(&policy->polq.hold_queue);
Kees Cookc3aed702017-10-16 17:28:56 -0700277 timer_setup(&policy->timer, xfrm_policy_timer, 0);
278 timer_setup(&policy->polq.hold_timer,
279 xfrm_policy_queue_process, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 return policy;
282}
283EXPORT_SYMBOL(xfrm_policy_alloc);
284
Eric Dumazet56f04732015-12-08 07:22:01 -0800285static void xfrm_policy_destroy_rcu(struct rcu_head *head)
286{
287 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
288
289 security_xfrm_policy_free(policy->security);
290 kfree(policy);
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* Destroy xfrm_policy: descendant resources must be released to this moment. */
294
WANG Cong64c31b32008-01-07 22:34:29 -0800295void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Herbert Xu12a169e2008-10-01 07:03:24 -0700297 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Fan Du0659eea2013-08-01 18:08:36 +0800299 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 BUG();
301
Eric Dumazet56f04732015-12-08 07:22:01 -0800302 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
WANG Cong64c31b32008-01-07 22:34:29 -0800304EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100306/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * entry dead. The rule must be unlinked from lists to the moment.
308 */
309
310static void xfrm_policy_kill(struct xfrm_policy *policy)
311{
Herbert Xu12a169e2008-10-01 07:03:24 -0700312 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Timo Teräs285ead12010-04-07 00:30:06 +0000314 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200316 if (del_timer(&policy->polq.hold_timer))
317 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800318 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100319
Timo Teräs285ead12010-04-07 00:30:06 +0000320 if (del_timer(&policy->timer))
321 xfrm_pol_put(policy);
322
323 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
David S. Miller2518c7c2006-08-24 04:45:07 -0700326static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
327
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800328static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700329{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800330 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700331}
332
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200333/* calculate policy hash thresholds */
334static void __get_hash_thresh(struct net *net,
335 unsigned short family, int dir,
336 u8 *dbits, u8 *sbits)
337{
338 switch (family) {
339 case AF_INET:
340 *dbits = net->xfrm.policy_bydst[dir].dbits4;
341 *sbits = net->xfrm.policy_bydst[dir].sbits4;
342 break;
343
344 case AF_INET6:
345 *dbits = net->xfrm.policy_bydst[dir].dbits6;
346 *sbits = net->xfrm.policy_bydst[dir].sbits6;
347 break;
348
349 default:
350 *dbits = 0;
351 *sbits = 0;
352 }
353}
354
David S. Miller5f803b52011-02-24 00:33:19 -0500355static struct hlist_head *policy_hash_bysel(struct net *net,
356 const struct xfrm_selector *sel,
357 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700358{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800359 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200360 unsigned int hash;
361 u8 dbits;
362 u8 sbits;
363
364 __get_hash_thresh(net, family, dir, &dbits, &sbits);
365 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700366
Florian Westphale1e551b2016-08-11 15:17:53 +0200367 if (hash == hmask + 1)
368 return &net->xfrm.policy_inexact[dir];
369
370 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
371 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700372}
373
David S. Miller5f803b52011-02-24 00:33:19 -0500374static struct hlist_head *policy_hash_direct(struct net *net,
375 const xfrm_address_t *daddr,
376 const xfrm_address_t *saddr,
377 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700378{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800379 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200380 unsigned int hash;
381 u8 dbits;
382 u8 sbits;
383
384 __get_hash_thresh(net, family, dir, &dbits, &sbits);
385 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700386
Florian Westphale1e551b2016-08-11 15:17:53 +0200387 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
388 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700389}
390
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200391static void xfrm_dst_hash_transfer(struct net *net,
392 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700393 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200394 unsigned int nhashmask,
395 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700396{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800397 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700398 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800399 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200400 u8 dbits;
401 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700402
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800403redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800404 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700405 unsigned int h;
406
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200407 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700408 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200409 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800410 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200411 hlist_del_rcu(&pol->bydst);
412 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800413 h0 = h;
414 } else {
415 if (h != h0)
416 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200417 hlist_del_rcu(&pol->bydst);
418 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800419 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800420 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421 }
422 if (!hlist_empty(list)) {
423 entry0 = NULL;
424 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700425 }
426}
427
428static void xfrm_idx_hash_transfer(struct hlist_head *list,
429 struct hlist_head *nidxtable,
430 unsigned int nhashmask)
431{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800432 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700433 struct xfrm_policy *pol;
434
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700436 unsigned int h;
437
438 h = __idx_hash(pol->index, nhashmask);
439 hlist_add_head(&pol->byidx, nidxtable+h);
440 }
441}
442
443static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
444{
445 return ((old_hmask + 1) << 1) - 1;
446}
447
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800448static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700449{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800450 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700451 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
452 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700453 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200454 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700455 int i;
456
457 if (!ndst)
458 return;
459
Florian Westphal9d0380d2016-08-11 15:17:59 +0200460 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200461 write_seqcount_begin(&xfrm_policy_hash_generation);
462
463 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
464 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700465
Florian Westphale1e551b2016-08-11 15:17:53 +0200466 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
467 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
468
David S. Miller2518c7c2006-08-24 04:45:07 -0700469 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200470 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700471
Florian Westphale1e551b2016-08-11 15:17:53 +0200472 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800473 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700474
Florian Westphal30846092016-08-11 15:17:54 +0200475 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200476 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700477
Florian Westphale1e551b2016-08-11 15:17:53 +0200478 synchronize_rcu();
479
David S. Miller44e36b42006-08-24 04:50:50 -0700480 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700481}
482
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800483static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700484{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800485 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700486 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
487 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800488 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700489 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700490 int i;
491
492 if (!nidx)
493 return;
494
Florian Westphal9d0380d2016-08-11 15:17:59 +0200495 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700496
497 for (i = hmask; i >= 0; i--)
498 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
499
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800500 net->xfrm.policy_byidx = nidx;
501 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700502
Florian Westphal9d0380d2016-08-11 15:17:59 +0200503 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504
David S. Miller44e36b42006-08-24 04:50:50 -0700505 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700506}
507
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800508static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700509{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510 unsigned int cnt = net->xfrm.policy_count[dir];
511 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
513 if (total)
514 *total += cnt;
515
516 if ((hmask + 1) < xfrm_policy_hashmax &&
517 cnt > hmask)
518 return 1;
519
520 return 0;
521}
522
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800523static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700524{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800525 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700526
527 if ((hmask + 1) < xfrm_policy_hashmax &&
528 total > hmask)
529 return 1;
530
531 return 0;
532}
533
Alexey Dobriyane0710412010-01-23 13:37:10 +0000534void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700535{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000536 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
537 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
538 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
539 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
540 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
541 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
542 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700543 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700544}
545EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700546
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700547static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800548static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700549{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800550 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700551 int dir, total;
552
553 mutex_lock(&hash_resize_mutex);
554
555 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800556 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800557 if (xfrm_bydst_should_resize(net, dir, &total))
558 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700559 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800560 if (xfrm_byidx_should_resize(net, total))
561 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700562
563 mutex_unlock(&hash_resize_mutex);
564}
565
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200566static void xfrm_hash_rebuild(struct work_struct *work)
567{
568 struct net *net = container_of(work, struct net,
569 xfrm.policy_hthresh.work);
570 unsigned int hmask;
571 struct xfrm_policy *pol;
572 struct xfrm_policy *policy;
573 struct hlist_head *chain;
574 struct hlist_head *odst;
575 struct hlist_node *newpos;
576 int i;
577 int dir;
578 unsigned seq;
579 u8 lbits4, rbits4, lbits6, rbits6;
580
581 mutex_lock(&hash_resize_mutex);
582
583 /* read selector prefixlen thresholds */
584 do {
585 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
586
587 lbits4 = net->xfrm.policy_hthresh.lbits4;
588 rbits4 = net->xfrm.policy_hthresh.rbits4;
589 lbits6 = net->xfrm.policy_hthresh.lbits6;
590 rbits6 = net->xfrm.policy_hthresh.rbits6;
591 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
592
Florian Westphal9d0380d2016-08-11 15:17:59 +0200593 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200594
595 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800596 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200597 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
598 hmask = net->xfrm.policy_bydst[dir].hmask;
599 odst = net->xfrm.policy_bydst[dir].table;
600 for (i = hmask; i >= 0; i--)
601 INIT_HLIST_HEAD(odst + i);
602 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
603 /* dir out => dst = remote, src = local */
604 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
605 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
606 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
607 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
608 } else {
609 /* dir in/fwd => dst = local, src = remote */
610 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
611 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
612 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
613 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
614 }
615 }
616
617 /* re-insert all policies by order of creation */
618 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal862591b2017-12-27 23:25:45 +0100619 if (policy->walk.dead ||
620 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200621 /* skip socket policies */
622 continue;
623 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200624 newpos = NULL;
625 chain = policy_hash_bysel(net, &policy->selector,
626 policy->family,
627 xfrm_policy_id2dir(policy->index));
628 hlist_for_each_entry(pol, chain, bydst) {
629 if (policy->priority >= pol->priority)
630 newpos = &pol->bydst;
631 else
632 break;
633 }
634 if (newpos)
635 hlist_add_behind(&policy->bydst, newpos);
636 else
637 hlist_add_head(&policy->bydst, chain);
638 }
639
Florian Westphal9d0380d2016-08-11 15:17:59 +0200640 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200641
642 mutex_unlock(&hash_resize_mutex);
643}
644
645void xfrm_policy_hash_rebuild(struct net *net)
646{
647 schedule_work(&net->xfrm.policy_hthresh.work);
648}
649EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/* Generate new index... KAME seems to generate them ordered by cost
652 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800653static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 static u32 idx_generator;
656
657 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700658 struct hlist_head *list;
659 struct xfrm_policy *p;
660 u32 idx;
661 int found;
662
Fan Due682adf02013-11-07 17:47:48 +0800663 if (!index) {
664 idx = (idx_generator | dir);
665 idx_generator += 8;
666 } else {
667 idx = index;
668 index = 0;
669 }
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (idx == 0)
672 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800673 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700674 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800675 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700676 if (p->index == idx) {
677 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700681 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return idx;
683 }
684}
685
David S. Miller2518c7c2006-08-24 04:45:07 -0700686static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
687{
688 u32 *p1 = (u32 *) s1;
689 u32 *p2 = (u32 *) s2;
690 int len = sizeof(struct xfrm_selector) / sizeof(u32);
691 int i;
692
693 for (i = 0; i < len; i++) {
694 if (p1[i] != p2[i])
695 return 1;
696 }
697
698 return 0;
699}
700
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100701static void xfrm_policy_requeue(struct xfrm_policy *old,
702 struct xfrm_policy *new)
703{
704 struct xfrm_policy_queue *pq = &old->polq;
705 struct sk_buff_head list;
706
Li RongQingde2ad482015-04-30 17:25:19 +0800707 if (skb_queue_empty(&pq->hold_queue))
708 return;
709
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100710 __skb_queue_head_init(&list);
711
712 spin_lock_bh(&pq->hold_queue.lock);
713 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200714 if (del_timer(&pq->hold_timer))
715 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100716 spin_unlock_bh(&pq->hold_queue.lock);
717
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100718 pq = &new->polq;
719
720 spin_lock_bh(&pq->hold_queue.lock);
721 skb_queue_splice(&list, &pq->hold_queue);
722 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200723 if (!mod_timer(&pq->hold_timer, jiffies))
724 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100725 spin_unlock_bh(&pq->hold_queue.lock);
726}
727
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100728static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
729 struct xfrm_policy *pol)
730{
731 u32 mark = policy->mark.v & policy->mark.m;
732
733 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
734 return true;
735
736 if ((mark & pol->mark.m) == pol->mark.v &&
737 policy->priority == pol->priority)
738 return true;
739
740 return false;
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
744{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800745 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700746 struct xfrm_policy *pol;
747 struct xfrm_policy *delpol;
748 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800749 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Florian Westphal9d0380d2016-08-11 15:17:59 +0200751 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800752 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700753 delpol = NULL;
754 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800755 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800756 if (pol->type == policy->type &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200757 pol->if_id == policy->if_id &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700758 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100759 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800760 xfrm_sec_ctx_match(pol->security, policy->security) &&
761 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200763 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -EEXIST;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 delpol = pol;
767 if (policy->priority > pol->priority)
768 continue;
769 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800770 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 continue;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (delpol)
774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700777 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700778 else
779 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800780 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +0800781
782 /* After previous checking, family can either be AF_INET or AF_INET6 */
783 if (policy->family == AF_INET)
784 rt_genid_bump_ipv4(net);
785 else
786 rt_genid_bump_ipv6(net);
787
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100788 if (delpol) {
789 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800790 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100791 }
Fan Due682adf02013-11-07 17:47:48 +0800792 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800793 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
Arnd Bergmann386c5682018-07-11 12:19:13 +0200794 policy->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 policy->curlft.use_time = 0;
796 if (!mod_timer(&policy->timer, jiffies + HZ))
797 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200798 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
David S. Miller9b78a822005-12-22 07:39:48 -0800800 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800802 else if (xfrm_bydst_should_resize(net, dir, NULL))
803 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806}
807EXPORT_SYMBOL(xfrm_policy_insert);
808
Steffen Klassert7e652642018-06-12 14:07:07 +0200809struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
810 u8 type, int dir,
811 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800812 struct xfrm_sec_ctx *ctx, int delete,
813 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
David S. Miller2518c7c2006-08-24 04:45:07 -0700815 struct xfrm_policy *pol, *ret;
816 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Eric Parisef41aaa2007-03-07 15:37:58 -0800818 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200819 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800820 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700821 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800822 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700823 if (pol->type == type &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200824 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000825 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700826 !selector_cmp(sel, &pol->selector) &&
827 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700829 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700830 *err = security_xfrm_policy_delete(
831 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800832 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200833 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800834 return pol;
835 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800836 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700837 }
838 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
840 }
841 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200842 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000844 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700845 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Trent Jaegerdf718372005-12-13 23:12:27 -0800848EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Steffen Klassert7e652642018-06-12 14:07:07 +0200850struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id,
851 u8 type, int dir, u32 id, int delete,
852 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
David S. Miller2518c7c2006-08-24 04:45:07 -0700854 struct xfrm_policy *pol, *ret;
855 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Herbert Xub5505c62007-05-14 02:15:47 -0700857 *err = -ENOENT;
858 if (xfrm_policy_id2dir(id) != dir)
859 return NULL;
860
Eric Parisef41aaa2007-03-07 15:37:58 -0800861 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200862 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800863 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800865 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000866 if (pol->type == type && pol->index == id &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200867 pol->if_id == if_id &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000868 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700871 *err = security_xfrm_policy_delete(
872 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800873 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200874 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800875 return pol;
876 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800877 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700878 }
879 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
881 }
882 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200883 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000885 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700886 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889EXPORT_SYMBOL(xfrm_policy_byid);
890
Joy Latten4aa2e622007-06-04 19:05:57 -0400891#ifdef CONFIG_SECURITY_NETWORK_XFRM
892static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900893xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Joy Latten4aa2e622007-06-04 19:05:57 -0400895 int dir, err = 0;
896
897 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
898 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400899 int i;
900
Sasha Levinb67bfe02013-02-27 17:06:00 -0800901 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800902 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400903 if (pol->type != type)
904 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700905 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400906 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900907 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400908 return err;
909 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900910 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800911 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800912 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800913 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400914 bydst) {
915 if (pol->type != type)
916 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700917 err = security_xfrm_policy_delete(
918 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400919 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700920 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900921 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400922 return err;
923 }
924 }
925 }
926 }
927 return err;
928}
929#else
930static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900931xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400932{
933 return 0;
934}
935#endif
936
Tetsuo Handa2e710292014-04-22 21:48:30 +0900937int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400938{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000939 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Florian Westphal9d0380d2016-08-11 15:17:59 +0200941 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400942
Tetsuo Handa2e710292014-04-22 21:48:30 +0900943 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400944 if (err)
945 goto out;
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700948 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800949 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700950
951 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800952 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800953 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700954 if (pol->type != type)
955 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000956 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200957 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000958 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Tetsuo Handa2e710292014-04-22 21:48:30 +0900960 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600961
David S. Miller2518c7c2006-08-24 04:45:07 -0700962 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Steffen Klassert4141b362016-08-24 13:08:40 +0200964 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700965 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700967
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800968 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700969 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800970 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800971 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700972 bydst) {
973 if (pol->type != type)
974 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000975 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200976 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000977 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700978
Tetsuo Handa2e710292014-04-22 21:48:30 +0900979 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700980 xfrm_policy_kill(pol);
981
Florian Westphal9d0380d2016-08-11 15:17:59 +0200982 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700983 goto again2;
984 }
985 }
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000988 if (!cnt)
989 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400990out:
Florian Westphal9d0380d2016-08-11 15:17:59 +0200991 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994EXPORT_SYMBOL(xfrm_policy_flush);
995
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800996int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800997 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 void *data)
999{
Herbert Xu12a169e2008-10-01 07:03:24 -07001000 struct xfrm_policy *pol;
1001 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001002 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Timo Teras4c563f72008-02-28 21:31:08 -08001004 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1005 walk->type != XFRM_POLICY_TYPE_ANY)
1006 return -EINVAL;
1007
Herbert Xu12a169e2008-10-01 07:03:24 -07001008 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001009 return 0;
1010
Florian Westphal9d0380d2016-08-11 15:17:59 +02001011 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001012 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001013 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001014 else
Li RongQing80077702015-04-22 17:09:54 +08001015 x = list_first_entry(&walk->walk.all,
1016 struct xfrm_policy_walk_entry, all);
1017
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001018 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001019 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001020 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001021 pol = container_of(x, struct xfrm_policy, walk);
1022 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1023 walk->type != pol->type)
1024 continue;
1025 error = func(pol, xfrm_policy_id2dir(pol->index),
1026 walk->seq, data);
1027 if (error) {
1028 list_move_tail(&walk->walk.all, &x->all);
1029 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001030 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001031 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001033 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001034 error = -ENOENT;
1035 goto out;
1036 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001037 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001039 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return error;
1041}
1042EXPORT_SYMBOL(xfrm_policy_walk);
1043
Herbert Xu12a169e2008-10-01 07:03:24 -07001044void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1045{
1046 INIT_LIST_HEAD(&walk->walk.all);
1047 walk->walk.dead = 1;
1048 walk->type = type;
1049 walk->seq = 0;
1050}
1051EXPORT_SYMBOL(xfrm_policy_walk_init);
1052
Fan Du283bc9f2013-11-07 17:47:50 +08001053void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001054{
1055 if (list_empty(&walk->walk.all))
1056 return;
1057
Florian Westphal9d0380d2016-08-11 15:17:59 +02001058 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001059 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001060 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001061}
1062EXPORT_SYMBOL(xfrm_policy_walk_done);
1063
James Morris134b0fc2006-10-05 15:42:27 -05001064/*
1065 * Find policy to apply to this flow.
1066 *
1067 * Returns 0 if policy found, else an -errno.
1068 */
David S. Millerf299d552011-02-24 01:23:30 -05001069static int xfrm_policy_match(const struct xfrm_policy *pol,
1070 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001071 u8 type, u16 family, int dir, u32 if_id)
David S. Miller2518c7c2006-08-24 04:45:07 -07001072{
David S. Millerf299d552011-02-24 01:23:30 -05001073 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001074 int ret = -ESRCH;
1075 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001076
1077 if (pol->family != family ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001078 pol->if_id != if_id ||
David S. Miller1d28f422011-03-12 00:29:39 -05001079 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001080 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001081 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001082
1083 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001084 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001085 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001086 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001087
James Morris134b0fc2006-10-05 15:42:27 -05001088 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001089}
1090
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001091static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001092 const struct flowi *fl,
Benedict Wongbc56b332018-07-19 10:50:44 -07001093 u16 family, u8 dir,
1094 u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
James Morris134b0fc2006-10-05 15:42:27 -05001096 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001097 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001098 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001099 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001100 unsigned int sequence;
1101 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001102
1103 daddr = xfrm_flowi_daddr(fl, family);
1104 saddr = xfrm_flowi_saddr(fl, family);
1105 if (unlikely(!daddr || !saddr))
1106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Florian Westphala7c442472016-08-11 15:17:56 +02001108 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001109 retry:
1110 do {
1111 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1112 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1113 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1114
1115 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001116 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001117 hlist_for_each_entry_rcu(pol, chain, bydst) {
Benedict Wongbc56b332018-07-19 10:50:44 -07001118 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05001119 if (err) {
1120 if (err == -ESRCH)
1121 continue;
1122 else {
1123 ret = ERR_PTR(err);
1124 goto fail;
1125 }
1126 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001127 ret = pol;
1128 priority = ret->priority;
1129 break;
1130 }
1131 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001132 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001133 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001134 if ((pol->priority >= priority) && ret)
1135 break;
1136
Benedict Wongbc56b332018-07-19 10:50:44 -07001137 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
James Morris134b0fc2006-10-05 15:42:27 -05001138 if (err) {
1139 if (err == -ESRCH)
1140 continue;
1141 else {
1142 ret = ERR_PTR(err);
1143 goto fail;
1144 }
Li RongQing8faf4912015-05-14 11:16:59 +08001145 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001146 ret = pol;
1147 break;
1148 }
1149 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001150
Florian Westphal30846092016-08-11 15:17:54 +02001151 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1152 goto retry;
1153
Florian Westphale37cc8a2016-08-11 15:17:55 +02001154 if (ret && !xfrm_pol_hold_rcu(ret))
1155 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001156fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001157 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001158
David S. Miller2518c7c2006-08-24 04:45:07 -07001159 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001160}
1161
Benedict Wongbc56b332018-07-19 10:50:44 -07001162static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
1163 const struct flowi *fl,
1164 u16 family, u8 dir, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00001165{
1166#ifdef CONFIG_XFRM_SUB_POLICY
1167 struct xfrm_policy *pol;
1168
Benedict Wongbc56b332018-07-19 10:50:44 -07001169 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
1170 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00001171 if (pol != NULL)
1172 return pol;
1173#endif
Benedict Wongbc56b332018-07-19 10:50:44 -07001174 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
1175 dir, if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00001176}
1177
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001178static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Benedict Wongbc56b332018-07-19 10:50:44 -07001179 const struct flowi *fl,
1180 u16 family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct xfrm_policy *pol;
1183
Eric Dumazetd188ba82015-12-08 07:22:02 -08001184 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001185 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001186 pol = rcu_dereference(sk->sk_policy[dir]);
1187 if (pol != NULL) {
Steffen Klassertddc47e42017-11-29 06:53:55 +01001188 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001189 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001190
Steffen Klassertddc47e42017-11-29 06:53:55 +01001191 if (pol->family != family) {
1192 pol = NULL;
1193 goto out;
1194 }
1195
1196 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001197 if (match) {
Steffen Klassert7e652642018-06-12 14:07:07 +02001198 if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
Benedict Wongbc56b332018-07-19 10:50:44 -07001199 pol->if_id != if_id) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001200 pol = NULL;
1201 goto out;
1202 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001203 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001204 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02001205 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01001206 if (!err) {
1207 if (!xfrm_pol_hold_rcu(pol))
1208 goto again;
1209 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001210 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001211 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001212 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001213 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001214 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 pol = NULL;
1216 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001217out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001218 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return pol;
1220}
1221
1222static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1223{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001224 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001225
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001226 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001227 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xfrm_pol_hold(pol);
1229}
1230
1231static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1232 int dir)
1233{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001234 struct net *net = xp_net(pol);
1235
Herbert Xu53c2e282014-11-13 17:09:49 +08001236 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001237 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Herbert Xu53c2e282014-11-13 17:09:49 +08001239 /* Socket policies are not hashed. */
1240 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001241 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001242 hlist_del(&pol->byidx);
1243 }
1244
1245 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001246 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001247
1248 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Herbert Xu53c2e282014-11-13 17:09:49 +08001251static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1252{
1253 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1254}
1255
1256static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1257{
1258 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1259}
1260
Herbert Xu4666faa2005-06-18 22:43:22 -07001261int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Fan Du283bc9f2013-11-07 17:47:50 +08001263 struct net *net = xp_net(pol);
1264
Florian Westphal9d0380d2016-08-11 15:17:59 +02001265 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001267 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001272 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
David S. Millera70fcb02006-03-20 19:18:52 -08001274EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1277{
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09001278 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct xfrm_policy *old_pol;
1280
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001281#ifdef CONFIG_XFRM_SUB_POLICY
1282 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1283 return -EINVAL;
1284#endif
1285
Florian Westphal9d0380d2016-08-11 15:17:59 +02001286 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001287 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1288 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (pol) {
Arnd Bergmann386c5682018-07-11 12:19:13 +02001290 pol->curlft.add_time = ktime_get_real_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001291 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001292 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001294 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001295 if (old_pol) {
1296 if (pol)
1297 xfrm_policy_requeue(old_pol, pol);
1298
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001299 /* Unlinking succeeds always. This is the only function
1300 * allowed to delete or replace socket policy.
1301 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001302 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001303 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001304 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (old_pol) {
1307 xfrm_policy_kill(old_pol);
1308 }
1309 return 0;
1310}
1311
David S. Millerd3e40a92011-02-24 01:25:41 -05001312static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001314 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001315 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (newp) {
1318 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001319 if (security_xfrm_policy_clone(old->security,
1320 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001321 kfree(newp);
1322 return NULL; /* ENOMEM */
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 newp->lft = old->lft;
1325 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001326 newp->mark = old->mark;
Steffen Klassert7e652642018-06-12 14:07:07 +02001327 newp->if_id = old->if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 newp->action = old->action;
1329 newp->flags = old->flags;
1330 newp->xfrm_nr = old->xfrm_nr;
1331 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001332 newp->type = old->type;
Herbert Xu0e74aa12017-11-10 14:14:06 +11001333 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 memcpy(newp->xfrm_vec, old->xfrm_vec,
1335 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001336 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001337 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001338 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 xfrm_pol_put(newp);
1340 }
1341 return newp;
1342}
1343
Eric Dumazetd188ba82015-12-08 07:22:02 -08001344int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001346 const struct xfrm_policy *p;
1347 struct xfrm_policy *np;
1348 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Eric Dumazetd188ba82015-12-08 07:22:02 -08001350 rcu_read_lock();
1351 for (i = 0; i < 2; i++) {
1352 p = rcu_dereference(osk->sk_policy[i]);
1353 if (p) {
1354 np = clone_policy(p, i);
1355 if (unlikely(!np)) {
1356 ret = -ENOMEM;
1357 break;
1358 }
1359 rcu_assign_pointer(sk->sk_policy[i], np);
1360 }
1361 }
1362 rcu_read_unlock();
1363 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001366static int
David Ahern42a7b322015-08-10 16:58:11 -06001367xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09001368 xfrm_address_t *remote, unsigned short family, u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001369{
1370 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01001371 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001372
1373 if (unlikely(afinfo == NULL))
1374 return -EINVAL;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +09001375 err = afinfo->get_saddr(net, oif, local, remote, mark);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001376 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001377 return err;
1378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/* Resolve list of templates for the flow, given policy. */
1381
1382static int
David S. Millera6c2e612011-02-22 18:35:39 -08001383xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1384 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001386 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int nx;
1388 int i, error;
Steffen Klassert94802152017-11-15 06:40:57 +01001389 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1390 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001391 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Weilong Chen9b7a7872013-12-24 09:43:46 +08001393 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct xfrm_state *x;
Steffen Klassert94802152017-11-15 06:40:57 +01001395 xfrm_address_t *remote = daddr;
1396 xfrm_address_t *local = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1398
Steffen Klassert94802152017-11-15 06:40:57 +01001399 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1400 tmpl->mode == XFRM_MODE_BEET) {
1401 remote = &tmpl->id.daddr;
1402 local = &tmpl->saddr;
1403 if (xfrm_addr_any(local, tmpl->encap_family)) {
1404 error = xfrm_get_saddr(net, fl->flowi_oif,
1405 &tmp, remote,
1406 tmpl->encap_family, 0);
1407 if (error)
1408 goto fail;
1409 local = &tmp;
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Benedict Wongbc56b332018-07-19 10:50:44 -07001413 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
1414 family, policy->if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 if (x && x->km.state == XFRM_STATE_VALID) {
1417 xfrm[nx++] = x;
Steffen Klassert94802152017-11-15 06:40:57 +01001418 daddr = remote;
1419 saddr = local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 continue;
1421 }
1422 if (x) {
1423 error = (x->km.state == XFRM_STATE_ERROR ?
1424 -EINVAL : -EAGAIN);
1425 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001426 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001427 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 if (!tmpl->optional)
1431 goto fail;
1432 }
1433 return nx;
1434
1435fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001436 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 xfrm_state_put(xfrm[nx]);
1438 return error;
1439}
1440
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001441static int
David S. Millera6c2e612011-02-22 18:35:39 -08001442xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1443 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001444{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001445 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1446 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001447 int cnx = 0;
1448 int error;
1449 int ret;
1450 int i;
1451
1452 for (i = 0; i < npols; i++) {
1453 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1454 error = -ENOBUFS;
1455 goto fail;
1456 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001457
1458 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001459 if (ret < 0) {
1460 error = ret;
1461 goto fail;
1462 } else
1463 cnx += ret;
1464 }
1465
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001466 /* found states are sorted for outbound processing */
1467 if (npols > 1)
1468 xfrm_state_sort(xfrm, tpp, cnx, family);
1469
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001470 return cnx;
1471
1472 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001473 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001474 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001475 return error;
1476
1477}
1478
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001479static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001480{
Florian Westphal37b10382017-02-07 15:00:19 +01001481 const struct xfrm_policy_afinfo *afinfo;
Xin Long143a4452018-02-17 15:16:22 +08001482 int tos;
Herbert Xu25ee3282007-12-11 09:32:34 -08001483
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001484 afinfo = xfrm_policy_get_afinfo(family);
Xin Long143a4452018-02-17 15:16:22 +08001485 if (!afinfo)
1486 return 0;
1487
1488 tos = afinfo->get_tos(fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001489
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001490 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001491
1492 return tos;
1493}
1494
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001495static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001496{
Florian Westphal37b10382017-02-07 15:00:19 +01001497 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001498 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001499 struct xfrm_dst *xdst;
1500
1501 if (!afinfo)
1502 return ERR_PTR(-EINVAL);
1503
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001504 switch (family) {
1505 case AF_INET:
1506 dst_ops = &net->xfrm.xfrm4_dst_ops;
1507 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001508#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001509 case AF_INET6:
1510 dst_ops = &net->xfrm.xfrm6_dst_ops;
1511 break;
1512#endif
1513 default:
1514 BUG();
1515 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07001516 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001517
Madalin Bucurd4cae562011-09-26 07:04:36 +00001518 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001519 struct dst_entry *dst = &xdst->u.dst;
1520
1521 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00001522 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001523 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001524
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001525 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00001526
Herbert Xu25ee3282007-12-11 09:32:34 -08001527 return xdst;
1528}
1529
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001530static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1531 int nfheader_len)
1532{
Florian Westphal37b10382017-02-07 15:00:19 +01001533 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001534 xfrm_policy_get_afinfo(dst->ops->family);
1535 int err;
1536
1537 if (!afinfo)
1538 return -EINVAL;
1539
1540 err = afinfo->init_path(path, dst, nfheader_len);
1541
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001542 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001543
1544 return err;
1545}
1546
Herbert Xu87c1e122010-03-02 02:51:56 +00001547static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001548 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001549{
Florian Westphal37b10382017-02-07 15:00:19 +01001550 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08001551 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1552 int err;
1553
1554 if (!afinfo)
1555 return -EINVAL;
1556
Herbert Xu87c1e122010-03-02 02:51:56 +00001557 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001558
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001559 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001560
1561 return err;
1562}
1563
Timo Teräs80c802f2010-04-07 00:30:05 +00001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1566 * all the metrics... Shortly, bundle a bundle.
1567 */
1568
Herbert Xu25ee3282007-12-11 09:32:34 -08001569static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
David Miller54920932017-11-28 15:41:01 -05001570 struct xfrm_state **xfrm,
1571 struct xfrm_dst **bundle,
1572 int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001573 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001574 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001576 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001577 unsigned long now = jiffies;
1578 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001579 struct xfrm_mode *inner_mode;
David Miller45b018be2017-11-28 15:40:28 -05001580 struct xfrm_dst *xdst_prev = NULL;
1581 struct xfrm_dst *xdst0 = NULL;
Herbert Xu25ee3282007-12-11 09:32:34 -08001582 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001584 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001585 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001586 int trailer_len = 0;
1587 int tos;
1588 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001589 xfrm_address_t saddr, daddr;
1590
1591 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001592
1593 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001594
1595 dst_hold(dst);
1596
1597 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001598 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001599 struct dst_entry *dst1 = &xdst->u.dst;
1600
1601 err = PTR_ERR(xdst);
1602 if (IS_ERR(xdst)) {
1603 dst_release(dst);
1604 goto put_states;
1605 }
1606
David Miller54920932017-11-28 15:41:01 -05001607 bundle[i] = xdst;
David Miller45b018be2017-11-28 15:40:28 -05001608 if (!xdst_prev)
1609 xdst0 = xdst;
David Miller10a7ef32017-10-10 20:59:38 -07001610 else
1611 /* Ref count is taken during xfrm_alloc_dst()
1612 * No need to do dst_clone() on dst1
1613 */
David Miller45b018be2017-11-28 15:40:28 -05001614 xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
David Miller10a7ef32017-10-10 20:59:38 -07001615
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001616 if (xfrm[i]->sel.family == AF_UNSPEC) {
1617 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1618 xfrm_af2proto(family));
1619 if (!inner_mode) {
1620 err = -EAFNOSUPPORT;
1621 dst_release(dst);
1622 goto put_states;
1623 }
1624 } else
1625 inner_mode = xfrm[i]->inner_mode;
1626
Herbert Xu25ee3282007-12-11 09:32:34 -08001627 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001628 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001629
1630 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02001631 __u32 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
1632
Herbert Xu25ee3282007-12-11 09:32:34 -08001633 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001634 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
Steffen Klassert9b42c1f2018-06-12 12:44:26 +02001635 &saddr, &daddr, family, mark);
Herbert Xu25ee3282007-12-11 09:32:34 -08001636 err = PTR_ERR(dst);
1637 if (IS_ERR(dst))
1638 goto put_states;
1639 } else
1640 dst_hold(dst);
1641
1642 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001643 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001644
David S. Millerf5b0a872012-07-19 12:31:33 -07001645 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001646 dst1->flags |= DST_HOST;
1647 dst1->lastuse = now;
1648
1649 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001650 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001651
David Miller45b018be2017-11-28 15:40:28 -05001652 xdst_prev = xdst;
Herbert Xu25ee3282007-12-11 09:32:34 -08001653
1654 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001655 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1656 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001657 trailer_len += xfrm[i]->props.trailer_len;
1658 }
1659
David Miller45b018be2017-11-28 15:40:28 -05001660 xfrm_dst_set_child(xdst_prev, dst);
David Miller0f6c4802017-11-28 15:40:46 -05001661 xdst0->path = dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08001662
1663 err = -ENODEV;
1664 dev = dst->dev;
1665 if (!dev)
1666 goto free_dst;
1667
David Miller45b018be2017-11-28 15:40:28 -05001668 xfrm_init_path(xdst0, dst, nfheader_len);
David Miller54920932017-11-28 15:41:01 -05001669 xfrm_init_pmtu(bundle, nx);
Herbert Xu25ee3282007-12-11 09:32:34 -08001670
David Miller45b018be2017-11-28 15:40:28 -05001671 for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
1672 xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
1673 err = xfrm_fill_dst(xdst_prev, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001674 if (err)
1675 goto free_dst;
1676
David Miller45b018be2017-11-28 15:40:28 -05001677 xdst_prev->u.dst.header_len = header_len;
1678 xdst_prev->u.dst.trailer_len = trailer_len;
1679 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
1680 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001681 }
1682
David Miller45b018be2017-11-28 15:40:28 -05001683 return &xdst0->u.dst;
Herbert Xu25ee3282007-12-11 09:32:34 -08001684
1685put_states:
1686 for (; i < nx; i++)
1687 xfrm_state_put(xfrm[i]);
1688free_dst:
David Miller45b018be2017-11-28 15:40:28 -05001689 if (xdst0)
1690 dst_release_immediate(&xdst0->u.dst);
Steffen Klassert38369f52018-05-31 09:45:18 +02001691
1692 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
David S. Miller73ff93c2011-02-22 18:33:42 -08001695static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001696 struct xfrm_policy **pols,
1697 int *num_pols, int *num_xfrms)
1698{
1699 int i;
1700
1701 if (*num_pols == 0 || !pols[0]) {
1702 *num_pols = 0;
1703 *num_xfrms = 0;
1704 return 0;
1705 }
1706 if (IS_ERR(pols[0]))
1707 return PTR_ERR(pols[0]);
1708
1709 *num_xfrms = pols[0]->xfrm_nr;
1710
1711#ifdef CONFIG_XFRM_SUB_POLICY
1712 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1713 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1714 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1715 XFRM_POLICY_TYPE_MAIN,
1716 fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07001717 XFRM_POLICY_OUT,
1718 pols[0]->if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00001719 if (pols[1]) {
1720 if (IS_ERR(pols[1])) {
1721 xfrm_pols_put(pols, *num_pols);
1722 return PTR_ERR(pols[1]);
1723 }
Weilong Chen02d08922013-12-24 09:43:48 +08001724 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001725 (*num_xfrms) += pols[1]->xfrm_nr;
1726 }
1727 }
1728#endif
1729 for (i = 0; i < *num_pols; i++) {
1730 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1731 *num_xfrms = -1;
1732 break;
1733 }
1734 }
1735
1736 return 0;
1737
1738}
1739
1740static struct xfrm_dst *
1741xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001742 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001743 struct dst_entry *dst_orig)
1744{
1745 struct net *net = xp_net(pols[0]);
1746 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
David Miller54920932017-11-28 15:41:01 -05001747 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Florian Westphale4db5b62018-06-25 17:26:02 +02001748 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001749 struct dst_entry *dst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001750 int err;
1751
1752 /* Try to instantiate a bundle */
1753 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001754 if (err <= 0) {
YueHaibing934ffce2018-07-25 16:54:33 +08001755 if (err == 0)
1756 return NULL;
1757
1758 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001759 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1760 return ERR_PTR(err);
1761 }
1762
David Miller54920932017-11-28 15:41:01 -05001763 dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00001764 if (IS_ERR(dst)) {
1765 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1766 return ERR_CAST(dst);
1767 }
1768
1769 xdst = (struct xfrm_dst *)dst;
1770 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00001771 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001772 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001773 xdst->policy_genid = atomic_read(&pols[0]->genid);
1774
1775 return xdst;
1776}
1777
Kees Cookc3aed702017-10-16 17:28:56 -07001778static void xfrm_policy_queue_process(struct timer_list *t)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001779{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001780 struct sk_buff *skb;
1781 struct sock *sk;
1782 struct dst_entry *dst;
Kees Cookc3aed702017-10-16 17:28:56 -07001783 struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001784 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001785 struct xfrm_policy_queue *pq = &pol->polq;
1786 struct flowi fl;
1787 struct sk_buff_head list;
1788
1789 spin_lock(&pq->hold_queue.lock);
1790 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001791 if (!skb) {
1792 spin_unlock(&pq->hold_queue.lock);
1793 goto out;
1794 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001795 dst = skb_dst(skb);
1796 sk = skb->sk;
1797 xfrm_decode_session(skb, &fl, dst->ops->family);
1798 spin_unlock(&pq->hold_queue.lock);
1799
David Miller0f6c4802017-11-28 15:40:46 -05001800 dst_hold(xfrm_dst_path(dst));
Steffen Klassert2471c9812018-02-01 11:26:12 +01001801 dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001802 if (IS_ERR(dst))
1803 goto purge_queue;
1804
1805 if (dst->flags & DST_XFRM_QUEUE) {
1806 dst_release(dst);
1807
1808 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1809 goto purge_queue;
1810
1811 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001812 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1813 xfrm_pol_hold(pol);
1814 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001815 }
1816
1817 dst_release(dst);
1818
1819 __skb_queue_head_init(&list);
1820
1821 spin_lock(&pq->hold_queue.lock);
1822 pq->timeout = 0;
1823 skb_queue_splice_init(&pq->hold_queue, &list);
1824 spin_unlock(&pq->hold_queue.lock);
1825
1826 while (!skb_queue_empty(&list)) {
1827 skb = __skb_dequeue(&list);
1828
1829 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
David Miller0f6c4802017-11-28 15:40:46 -05001830 dst_hold(xfrm_dst_path(skb_dst(skb)));
1831 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001832 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001833 kfree_skb(skb);
1834 continue;
1835 }
1836
1837 nf_reset(skb);
1838 skb_dst_drop(skb);
1839 skb_dst_set(skb, dst);
1840
Eric W. Biederman13206b62015-10-07 16:48:35 -05001841 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001842 }
1843
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001844out:
1845 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001846 return;
1847
1848purge_queue:
1849 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001850 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001851 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001852}
1853
Eric W. Biedermanede20592015-10-07 16:48:47 -05001854static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001855{
1856 unsigned long sched_next;
1857 struct dst_entry *dst = skb_dst(skb);
1858 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001859 struct xfrm_policy *pol = xdst->pols[0];
1860 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001861
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001862 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001863 kfree_skb(skb);
1864 return 0;
1865 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001866
1867 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1868 kfree_skb(skb);
1869 return -EAGAIN;
1870 }
1871
1872 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001873
1874 spin_lock_bh(&pq->hold_queue.lock);
1875
1876 if (!pq->timeout)
1877 pq->timeout = XFRM_QUEUE_TMO_MIN;
1878
1879 sched_next = jiffies + pq->timeout;
1880
1881 if (del_timer(&pq->hold_timer)) {
1882 if (time_before(pq->hold_timer.expires, sched_next))
1883 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001884 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001885 }
1886
1887 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001888 if (!mod_timer(&pq->hold_timer, sched_next))
1889 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001890
1891 spin_unlock_bh(&pq->hold_queue.lock);
1892
1893 return 0;
1894}
1895
1896static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001897 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001898 const struct flowi *fl,
1899 int num_xfrms,
1900 u16 family)
1901{
1902 int err;
1903 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001904 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001905 struct dst_entry *dst1;
1906 struct xfrm_dst *xdst;
1907
1908 xdst = xfrm_alloc_dst(net, family);
1909 if (IS_ERR(xdst))
1910 return xdst;
1911
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001912 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
1913 net->xfrm.sysctl_larval_drop ||
1914 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001915 return xdst;
1916
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001917 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001918 dst1 = &xdst->u.dst;
1919 dst_hold(dst);
1920 xdst->route = dst;
1921
1922 dst_copy_metrics(dst1, dst);
1923
1924 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1925 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1926 dst1->lastuse = jiffies;
1927
1928 dst1->input = dst_discard;
1929 dst1->output = xdst_queue_output;
1930
1931 dst_hold(dst);
David Miller45b018be2017-11-28 15:40:28 -05001932 xfrm_dst_set_child(xdst, dst);
David Miller0f6c4802017-11-28 15:40:46 -05001933 xdst->path = dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001934
1935 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1936
1937 err = -ENODEV;
1938 dev = dst->dev;
1939 if (!dev)
1940 goto free_dst;
1941
1942 err = xfrm_fill_dst(xdst, dev, fl);
1943 if (err)
1944 goto free_dst;
1945
1946out:
1947 return xdst;
1948
1949free_dst:
1950 dst_release(dst1);
1951 xdst = ERR_PTR(err);
1952 goto out;
1953}
1954
Benedict Wongbc56b332018-07-19 10:50:44 -07001955static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
1956 const struct flowi *fl,
1957 u16 family, u8 dir,
1958 struct xfrm_flo *xflo, u32 if_id)
Timo Teräs80c802f2010-04-07 00:30:05 +00001959{
Timo Teräs80c802f2010-04-07 00:30:05 +00001960 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02001961 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02001962 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001963
Timo Teräs80c802f2010-04-07 00:30:05 +00001964 /* Resolve policies to use if we couldn't get them from
1965 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02001966 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07001967 pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
Florian Westphal855dad92017-07-17 13:57:22 +02001968 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00001969 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02001970 if (err < 0)
1971 goto inc_error;
1972 if (num_pols == 0)
1973 return NULL;
1974 if (num_xfrms <= 0)
1975 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00001976
Florian Westphalbd45c532017-07-17 13:57:25 +02001977 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassert76a42012018-01-10 12:14:28 +01001978 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02001979 if (IS_ERR(xdst)) {
1980 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02001981 if (err == -EREMOTE) {
1982 xfrm_pols_put(pols, num_pols);
1983 return NULL;
1984 }
1985
Timo Teräs80c802f2010-04-07 00:30:05 +00001986 if (err != -EAGAIN)
1987 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02001988 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02001989 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00001990 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02001991 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00001992 }
1993
Florian Westphalbd45c532017-07-17 13:57:25 +02001994 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001995
1996make_dummy_bundle:
1997 /* We found policies, but there's no bundles to instantiate:
1998 * either because the policy blocks, has no transformations or
1999 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002000 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002001 if (IS_ERR(xdst)) {
2002 xfrm_pols_put(pols, num_pols);
2003 return ERR_CAST(xdst);
2004 }
2005 xdst->num_pols = num_pols;
2006 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002007 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002008
Florian Westphalbd45c532017-07-17 13:57:25 +02002009 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002010
2011inc_error:
2012 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2013error:
Florian Westphal855dad92017-07-17 13:57:22 +02002014 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002015 return ERR_PTR(err);
2016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
David S. Miller2774c132011-03-01 14:59:04 -08002018static struct dst_entry *make_blackhole(struct net *net, u16 family,
2019 struct dst_entry *dst_orig)
2020{
Florian Westphal37b10382017-02-07 15:00:19 +01002021 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08002022 struct dst_entry *ret;
2023
2024 if (!afinfo) {
2025 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002026 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002027 } else {
2028 ret = afinfo->blackhole_route(net, dst_orig);
2029 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002030 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08002031
2032 return ret;
2033}
2034
Benedict Wongbc56b332018-07-19 10:50:44 -07002035/* Finds/creates a bundle for given flow and if_id
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *
2037 * At the moment we eat a raw IP route. Mostly to speed up lookups
2038 * on interfaces with disabled IPsec.
Benedict Wongbc56b332018-07-19 10:50:44 -07002039 *
2040 * xfrm_lookup uses an if_id of 0 by default, and is provided for
2041 * compatibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
Benedict Wongbc56b332018-07-19 10:50:44 -07002043struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
2044 struct dst_entry *dst_orig,
2045 const struct flowi *fl,
2046 const struct sock *sk,
2047 int flags, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002049 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002050 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002051 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002052 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02002053 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00002054 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002055
Timo Teräs80c802f2010-04-07 00:30:05 +00002056 dst = NULL;
2057 xdst = NULL;
2058 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002059
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002060 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002061 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002062 num_pols = 1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002063 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
2064 if_id);
Timo Teräs80c802f2010-04-07 00:30:05 +00002065 err = xfrm_expand_policies(fl, family, pols,
2066 &num_pols, &num_xfrms);
2067 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002068 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002069
2070 if (num_pols) {
2071 if (num_xfrms <= 0) {
2072 drop_pols = num_pols;
2073 goto no_transform;
2074 }
2075
2076 xdst = xfrm_resolve_and_create_bundle(
2077 pols, num_pols, fl,
2078 family, dst_orig);
Steffen Klassert76a42012018-01-10 12:14:28 +01002079
Timo Teräs80c802f2010-04-07 00:30:05 +00002080 if (IS_ERR(xdst)) {
2081 xfrm_pols_put(pols, num_pols);
2082 err = PTR_ERR(xdst);
Steffen Klassertf203b762018-06-12 14:07:12 +02002083 if (err == -EREMOTE)
2084 goto nopol;
2085
Timo Teräs80c802f2010-04-07 00:30:05 +00002086 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002087 } else if (xdst == NULL) {
2088 num_xfrms = 0;
2089 drop_pols = num_pols;
2090 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002091 }
2092
Timo Teräs80c802f2010-04-07 00:30:05 +00002093 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002094 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Timo Teräs80c802f2010-04-07 00:30:05 +00002097 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002098 struct xfrm_flo xflo;
2099
2100 xflo.dst_orig = dst_orig;
2101 xflo.flags = flags;
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002104 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002105 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002106 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Benedict Wongbc56b332018-07-19 10:50:44 -07002108 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
Florian Westphalbd45c532017-07-17 13:57:25 +02002109 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00002110 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02002111 if (IS_ERR(xdst)) {
2112 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002113 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002114 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002115
2116 num_pols = xdst->num_pols;
2117 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002118 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002119 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
2121
Timo Teräs80c802f2010-04-07 00:30:05 +00002122 dst = &xdst->u.dst;
2123 if (route == NULL && num_xfrms > 0) {
2124 /* The only case when xfrm_bundle_lookup() returns a
2125 * bundle with null route, is when the template could
2126 * not be resolved. It means policies are there, but
2127 * bundle could not be created, since we don't yet
2128 * have the xfrm_state's. We need to wait for KM to
2129 * negotiate new SA's or bail out with error.*/
2130 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002131 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002132 err = -EREMOTE;
2133 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002134 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002135
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002136 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002137
2138 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2139 goto error;
2140 }
2141
2142no_transform:
2143 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002144 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Timo Teräs80c802f2010-04-07 00:30:05 +00002146 if ((flags & XFRM_LOOKUP_ICMP) &&
2147 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2148 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002149 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002150 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002151
Timo Teräs80c802f2010-04-07 00:30:05 +00002152 for (i = 0; i < num_pols; i++)
Arnd Bergmann386c5682018-07-11 12:19:13 +02002153 pols[i]->curlft.use_time = ktime_get_real_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002154
Timo Teräs80c802f2010-04-07 00:30:05 +00002155 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002157 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002158 err = -EPERM;
2159 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002160 } else if (num_xfrms > 0) {
2161 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002162 dst_release(dst_orig);
2163 } else {
2164 /* Flow passes untransformed */
2165 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002166 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002168ok:
2169 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002170 if (dst && dst->xfrm &&
2171 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2172 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002173 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Timo Teräs80c802f2010-04-07 00:30:05 +00002175nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002176 if (!(flags & XFRM_LOOKUP_ICMP)) {
2177 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002178 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002179 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002180 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002182 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002183dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002184 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2185 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002186 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002187 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
Benedict Wongbc56b332018-07-19 10:50:44 -07002189EXPORT_SYMBOL(xfrm_lookup_with_ifid);
2190
2191/* Main function: finds/creates a bundle for given flow.
2192 *
2193 * At the moment we eat a raw IP route. Mostly to speed up lookups
2194 * on interfaces with disabled IPsec.
2195 */
2196struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2197 const struct flowi *fl, const struct sock *sk,
2198 int flags)
2199{
2200 return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
2201}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202EXPORT_SYMBOL(xfrm_lookup);
2203
Steffen Klassertf92ee612014-09-16 10:08:40 +02002204/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2205 * Otherwise we may send out blackholed packets.
2206 */
2207struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2208 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002209 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002210{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002211 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002212 flags | XFRM_LOOKUP_QUEUE |
2213 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002214
2215 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2216 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2217
Tommi Rantala8cc88772018-06-21 09:30:47 +03002218 if (IS_ERR(dst))
2219 dst_release(dst_orig);
2220
Steffen Klassertf92ee612014-09-16 10:08:40 +02002221 return dst;
2222}
2223EXPORT_SYMBOL(xfrm_lookup_route);
2224
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002225static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002226xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002227{
2228 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002229
2230 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2231 return 0;
2232 x = skb->sp->xvec[idx];
2233 if (!x->type->reject)
2234 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002235 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/* When skb is transformed back to its "native" form, we have to
2239 * check policy restrictions. At the moment we make this in maximally
2240 * stupid way. Shame on me. :-) Of course, connected sockets must
2241 * have policy cached at them.
2242 */
2243
2244static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002245xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 unsigned short family)
2247{
2248 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002249 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return x->id.proto == tmpl->id.proto &&
2251 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2252 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2253 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002254 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002255 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002256 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2257 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002260/*
2261 * 0 or more than 0 is returned when validation is succeeded (either bypass
2262 * because of optional transport mode, or next index of the mathced secpath
2263 * state with the template.
2264 * -1 is returned when no matching template is found.
2265 * Otherwise "-2 - errored_index" is returned.
2266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002268xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 unsigned short family)
2270{
2271 int idx = start;
2272
2273 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002274 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return start;
2276 } else
2277 start = -1;
2278 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002279 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002281 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2282 if (start == -1)
2283 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287 return start;
2288}
2289
Herbert Xud5422ef2007-12-12 10:44:16 -08002290int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2291 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Florian Westphal37b10382017-02-07 15:00:19 +01002293 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002294 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 if (unlikely(afinfo == NULL))
2297 return -EAFNOSUPPORT;
2298
Herbert Xud5422ef2007-12-12 10:44:16 -08002299 afinfo->decode_session(skb, fl, reverse);
Steffen Klassertf203b762018-06-12 14:07:12 +02002300
David S. Miller1d28f422011-03-12 00:29:39 -05002301 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002302 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
Herbert Xud5422ef2007-12-12 10:44:16 -08002305EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
David S. Miller9a7386e2011-02-24 01:44:12 -05002307static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002310 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002311 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315
2316 return 0;
2317}
2318
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002319int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 unsigned short family)
2321{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002322 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002324 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2325 int npols = 0;
2326 int xfrm_nr;
2327 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002328 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002330 int xerr_idx = -1;
Benedict Wongbc56b332018-07-19 10:50:44 -07002331 const struct xfrm_if_cb *ifcb;
2332 struct xfrm_if *xi;
2333 u32 if_id = 0;
2334
2335 rcu_read_lock();
2336 ifcb = xfrm_if_get_cb();
2337
2338 if (ifcb) {
2339 xi = ifcb->decode_session(skb);
2340 if (xi)
2341 if_id = xi->p.if_id;
2342 }
2343 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Herbert Xud5422ef2007-12-12 10:44:16 -08002345 reverse = dir & ~XFRM_POLICY_MASK;
2346 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08002347
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002348 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002349 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002351 }
2352
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002353 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 /* First, check used SA against their selectors. */
2356 if (skb->sp) {
2357 int i;
2358
Weilong Chen9b7a7872013-12-24 09:43:46 +08002359 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002360 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002361 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002362 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366 }
2367
2368 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002369 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002370 if (sk && sk->sk_policy[dir]) {
Benedict Wongbc56b332018-07-19 10:50:44 -07002371 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002372 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002373 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002374 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002375 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Florian Westphal86dc8ee2017-07-17 13:57:24 +02002378 if (!pol)
Benedict Wongbc56b332018-07-19 10:50:44 -07002379 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002381 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002382 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002383 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002384 }
James Morris134b0fc2006-10-05 15:42:27 -05002385
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002386 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002387 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002388 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002389 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002390 return 0;
2391 }
2392 return 1;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Arnd Bergmann386c5682018-07-11 12:19:13 +02002395 pol->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002397 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002398 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002399#ifdef CONFIG_XFRM_SUB_POLICY
2400 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002401 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002402 &fl, family,
Benedict Wongbc56b332018-07-19 10:50:44 -07002403 XFRM_POLICY_IN, if_id);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002404 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002405 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002406 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002407 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002408 }
Arnd Bergmann386c5682018-07-11 12:19:13 +02002409 pols[1]->curlft.use_time = ktime_get_real_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002410 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002411 }
2412 }
2413#endif
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (pol->action == XFRM_POLICY_ALLOW) {
2416 struct sec_path *sp;
2417 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002419 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002420 struct xfrm_tmpl **tpp = tp;
2421 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 int i, k;
2423
2424 if ((sp = skb->sp) == NULL)
2425 sp = &dummy;
2426
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002427 for (pi = 0; pi < npols; pi++) {
2428 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002429 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002430 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002431 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002432 }
2433 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002434 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002435 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002436 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002437 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2438 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2439 }
2440 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002441 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002442 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002443 tpp = stp;
2444 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* For each tunnel xfrm, find the first matching tmpl.
2447 * For each tmpl before that, find corresponding xfrm.
2448 * Order is _important_. Later we will implement
2449 * some barriers, but at the moment barriers
2450 * are implied between each two transformations.
2451 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002452 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2453 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002454 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002455 if (k < -1)
2456 /* "-2 - errored_index" returned */
2457 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002458 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002463 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002464 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002468 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return 1;
2470 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002471 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002474 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002475reject_error:
2476 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return 0;
2478}
2479EXPORT_SYMBOL(__xfrm_policy_check);
2480
2481int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2482{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002483 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002485 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002486 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002488 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002489 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002493 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002494
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002495 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002496 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002497 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002498 dst = NULL;
2499 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002500 skb_dst_set(skb, dst);
2501 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503EXPORT_SYMBOL(__xfrm_route_forward);
2504
David S. Millerd49c73c2006-08-13 18:55:53 -07002505/* Optimize later using cookies and generation ids. */
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2508{
David S. Millerd49c73c2006-08-13 18:55:53 -07002509 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002510 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2511 * get validated by dst_ops->check on every use. We do this
2512 * because when a normal route referenced by an XFRM dst is
2513 * obsoleted we do not go looking around for all parent
2514 * referencing XFRM dsts so that we can invalidate them. It
2515 * is just too much work. Instead we make the checks here on
2516 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002517 *
2518 * XFRM dst A --> IPv4 dst X
2519 *
2520 * X is the "xdst->route" of A (X is also the "dst->path" of A
2521 * in this example). If X is marked obsolete, "A" will not
2522 * notice. That's what we are validating here via the
2523 * stale_bundle() check.
2524 *
Wei Wang52df1572017-06-17 10:42:38 -07002525 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2526 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02002527 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07002528 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08002529 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002530 if (dst->obsolete < 0 && !stale_bundle(dst))
2531 return dst;
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return NULL;
2534}
2535
2536static int stale_bundle(struct dst_entry *dst)
2537{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002538 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Herbert Xuaabc9762005-05-03 16:27:10 -07002541void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
David Millerb92cf4a2017-11-28 15:40:22 -05002543 while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002544 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002545 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 dev_put(dev);
2547 }
2548}
Herbert Xuaabc9762005-05-03 16:27:10 -07002549EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551static void xfrm_link_failure(struct sk_buff *skb)
2552{
2553 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
2556static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2557{
2558 if (dst) {
2559 if (dst->obsolete) {
2560 dst_release(dst);
2561 dst = NULL;
2562 }
2563 }
2564 return dst;
2565}
2566
David Miller54920932017-11-28 15:41:01 -05002567static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
David Miller54920932017-11-28 15:41:01 -05002569 while (nr--) {
2570 struct xfrm_dst *xdst = bundle[nr];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 u32 pmtu, route_mtu_cached;
David Miller54920932017-11-28 15:41:01 -05002572 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
David Miller54920932017-11-28 15:41:01 -05002574 dst = &xdst->u.dst;
David Millerb92cf4a2017-11-28 15:40:22 -05002575 pmtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 xdst->child_mtu_cached = pmtu;
2577
2578 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2579
2580 route_mtu_cached = dst_mtu(xdst->route);
2581 xdst->route_mtu_cached = route_mtu_cached;
2582
2583 if (pmtu > route_mtu_cached)
2584 pmtu = route_mtu_cached;
2585
David S. Millerdefb3512010-12-08 21:16:57 -08002586 dst_metric_set(dst, RTAX_MTU, pmtu);
David Miller54920932017-11-28 15:41:01 -05002587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590/* Check that the bundle accepts the flow and its components are
2591 * still valid.
2592 */
2593
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002594static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
David Miller54920932017-11-28 15:41:01 -05002596 struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct dst_entry *dst = &first->u.dst;
David Miller54920932017-11-28 15:41:01 -05002598 struct xfrm_dst *xdst;
2599 int start_from, nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 u32 mtu;
2601
David Miller0f6c4802017-11-28 15:40:46 -05002602 if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 (dst->dev && !netif_running(dst->dev)))
2604 return 0;
2605
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002606 if (dst->flags & DST_XFRM_QUEUE)
2607 return 1;
2608
David Miller54920932017-11-28 15:41:01 -05002609 start_from = nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 do {
2611 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2614 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002615 if (xdst->xfrm_genid != dst->xfrm->genid)
2616 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002617 if (xdst->num_pols > 0 &&
2618 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
David Miller54920932017-11-28 15:41:01 -05002621 bundle[nr++] = xdst;
2622
David Millerb92cf4a2017-11-28 15:40:22 -05002623 mtu = dst_mtu(xfrm_dst_child(dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (xdst->child_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05002625 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 xdst->child_mtu_cached = mtu;
2627 }
2628
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002629 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return 0;
2631 mtu = dst_mtu(xdst->route);
2632 if (xdst->route_mtu_cached != mtu) {
David Miller54920932017-11-28 15:41:01 -05002633 start_from = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 xdst->route_mtu_cached = mtu;
2635 }
2636
David Millerb92cf4a2017-11-28 15:40:22 -05002637 dst = xfrm_dst_child(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 } while (dst->xfrm);
2639
David Miller54920932017-11-28 15:41:01 -05002640 if (likely(!start_from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return 1;
2642
David Miller54920932017-11-28 15:41:01 -05002643 xdst = bundle[start_from - 1];
2644 mtu = xdst->child_mtu_cached;
2645 while (start_from--) {
2646 dst = &xdst->u.dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 mtu = xfrm_state_mtu(dst->xfrm, mtu);
David Miller54920932017-11-28 15:41:01 -05002649 if (mtu > xdst->route_mtu_cached)
2650 mtu = xdst->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002651 dst_metric_set(dst, RTAX_MTU, mtu);
David Miller54920932017-11-28 15:41:01 -05002652 if (!start_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 break;
2654
David Miller54920932017-11-28 15:41:01 -05002655 xdst = bundle[start_from - 1];
2656 xdst->child_mtu_cached = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
2659 return 1;
2660}
2661
David S. Miller0dbaee32010-12-13 12:52:14 -08002662static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2663{
David Miller0f6c4802017-11-28 15:40:46 -05002664 return dst_metric_advmss(xfrm_dst_path(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08002665}
2666
Steffen Klassertebb762f2011-11-23 02:12:51 +00002667static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002668{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002669 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2670
David Miller0f6c4802017-11-28 15:40:46 -05002671 return mtu ? : dst_mtu(xfrm_dst_path(dst));
David S. Millerd33e4552010-12-14 13:01:14 -08002672}
2673
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002674static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
2675 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02002676{
David Miller0f6c4802017-11-28 15:40:46 -05002677 while (dst->xfrm) {
Julian Anastasov63fca652017-02-06 23:14:15 +02002678 const struct xfrm_state *xfrm = dst->xfrm;
2679
Steffen Klassert013cb812018-02-19 07:44:07 +01002680 dst = xfrm_dst_child(dst);
2681
Julian Anastasov63fca652017-02-06 23:14:15 +02002682 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
2683 continue;
2684 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
2685 daddr = xfrm->coaddr;
2686 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
2687 daddr = &xfrm->id.daddr;
2688 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002689 return daddr;
2690}
2691
2692static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2693 struct sk_buff *skb,
2694 const void *daddr)
2695{
David Miller0f6c4802017-11-28 15:40:46 -05002696 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002697
2698 if (!skb)
2699 daddr = xfrm_get_dst_nexthop(dst, daddr);
2700 return path->ops->neigh_lookup(path, skb, daddr);
2701}
2702
2703static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
2704{
David Miller0f6c4802017-11-28 15:40:46 -05002705 const struct dst_entry *path = xfrm_dst_path(dst);
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002706
2707 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02002708 path->ops->confirm_neigh(path, daddr);
2709}
2710
Florian Westphala2817d82017-02-07 15:00:17 +01002711int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
2713 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01002714
2715 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01002717
Eric Dumazetef8531b2012-08-19 12:31:48 +02002718 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01002719 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002720 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 else {
2722 struct dst_ops *dst_ops = afinfo->dst_ops;
2723 if (likely(dst_ops->kmem_cachep == NULL))
2724 dst_ops->kmem_cachep = xfrm_dst_cache;
2725 if (likely(dst_ops->check == NULL))
2726 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002727 if (likely(dst_ops->default_advmss == NULL))
2728 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002729 if (likely(dst_ops->mtu == NULL))
2730 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (likely(dst_ops->negative_advice == NULL))
2732 dst_ops->negative_advice = xfrm_negative_advice;
2733 if (likely(dst_ops->link_failure == NULL))
2734 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002735 if (likely(dst_ops->neigh_lookup == NULL))
2736 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02002737 if (likely(!dst_ops->confirm_neigh))
2738 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01002739 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002741 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return err;
2744}
2745EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2746
Florian Westphala2817d82017-02-07 15:00:17 +01002747void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Florian Westphal2b619972017-02-07 15:00:15 +01002749 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01002750 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002751
Florian Westphala2817d82017-02-07 15:00:17 +01002752 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
2753 if (xfrm_policy_afinfo[i] != afinfo)
2754 continue;
2755 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
2756 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Florian Westphal2b619972017-02-07 15:00:15 +01002759 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Florian Westphal2b619972017-02-07 15:00:15 +01002761 dst_ops->kmem_cachep = NULL;
2762 dst_ops->check = NULL;
2763 dst_ops->negative_advice = NULL;
2764 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2767
Steffen Klassertf203b762018-06-12 14:07:12 +02002768void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
2769{
2770 spin_lock(&xfrm_if_cb_lock);
2771 rcu_assign_pointer(xfrm_if_cb, ifcb);
2772 spin_unlock(&xfrm_if_cb_lock);
2773}
2774EXPORT_SYMBOL(xfrm_if_register_cb);
2775
2776void xfrm_if_unregister_cb(void)
2777{
2778 RCU_INIT_POINTER(xfrm_if_cb, NULL);
2779 synchronize_rcu();
2780}
2781EXPORT_SYMBOL(xfrm_if_unregister_cb);
2782
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002783#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002784static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002785{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002786 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002787 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2788 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002789 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002790 rv = xfrm_proc_init(net);
2791 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002792 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002793 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002794}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002795
2796static void xfrm_statistics_fini(struct net *net)
2797{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002798 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002799 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002800}
2801#else
2802static int __net_init xfrm_statistics_init(struct net *net)
2803{
2804 return 0;
2805}
2806
2807static void xfrm_statistics_fini(struct net *net)
2808{
2809}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002810#endif
2811
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002812static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
David S. Miller2518c7c2006-08-24 04:45:07 -07002814 unsigned int hmask, sz;
2815 int dir;
2816
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002817 if (net_eq(net, &init_net))
2818 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002820 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002821 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
David S. Miller2518c7c2006-08-24 04:45:07 -07002823 hmask = 8 - 1;
2824 sz = (hmask+1) * sizeof(struct hlist_head);
2825
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002826 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2827 if (!net->xfrm.policy_byidx)
2828 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002829 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002830
Herbert Xu53c2e282014-11-13 17:09:49 +08002831 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002832 struct xfrm_policy_hash *htab;
2833
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002834 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002835 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002836 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002837
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002838 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002839 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002840 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002841 goto out_bydst;
2842 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002843 htab->dbits4 = 32;
2844 htab->sbits4 = 32;
2845 htab->dbits6 = 128;
2846 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002847 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002848 net->xfrm.policy_hthresh.lbits4 = 32;
2849 net->xfrm.policy_hthresh.rbits4 = 32;
2850 net->xfrm.policy_hthresh.lbits6 = 128;
2851 net->xfrm.policy_hthresh.rbits6 = 128;
2852
2853 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002854
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002855 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002856 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002857 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002858 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002859
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002860out_bydst:
2861 for (dir--; dir >= 0; dir--) {
2862 struct xfrm_policy_hash *htab;
2863
2864 htab = &net->xfrm.policy_bydst[dir];
2865 xfrm_hash_free(htab->table, sz);
2866 }
2867 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002868out_byidx:
2869 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002872static void xfrm_policy_fini(struct net *net)
2873{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002874 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002875 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002876
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002877 flush_work(&net->xfrm.policy_hash_work);
2878#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09002879 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002880#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09002881 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002882
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002883 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002884
Herbert Xu53c2e282014-11-13 17:09:49 +08002885 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002886 struct xfrm_policy_hash *htab;
2887
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002888 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002889
2890 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002891 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002892 WARN_ON(!hlist_empty(htab->table));
2893 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002894 }
2895
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002896 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002897 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2898 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002899}
2900
2901static int __net_init xfrm_net_init(struct net *net)
2902{
2903 int rv;
2904
Florian Westphalc2822222017-02-08 11:52:29 +01002905 /* Initialize the per-net locks here */
2906 spin_lock_init(&net->xfrm.xfrm_state_lock);
2907 spin_lock_init(&net->xfrm.xfrm_policy_lock);
2908 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2909
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002910 rv = xfrm_statistics_init(net);
2911 if (rv < 0)
2912 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002913 rv = xfrm_state_init(net);
2914 if (rv < 0)
2915 goto out_state;
2916 rv = xfrm_policy_init(net);
2917 if (rv < 0)
2918 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002919 rv = xfrm_sysctl_init(net);
2920 if (rv < 0)
2921 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08002922
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002923 return 0;
2924
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002925out_sysctl:
2926 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002927out_policy:
2928 xfrm_state_fini(net);
2929out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002930 xfrm_statistics_fini(net);
2931out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002932 return rv;
2933}
2934
2935static void __net_exit xfrm_net_exit(struct net *net)
2936{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002937 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002938 xfrm_policy_fini(net);
2939 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002940 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002941}
2942
2943static struct pernet_operations __net_initdata xfrm_net_ops = {
2944 .init = xfrm_net_init,
2945 .exit = xfrm_net_exit,
2946};
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948void __init xfrm_init(void)
2949{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002950 register_pernet_subsys(&xfrm_net_ops);
Kirill Tkhaie9a441b2018-03-29 17:03:25 +03002951 xfrm_dev_init();
Florian Westphal30846092016-08-11 15:17:54 +02002952 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 xfrm_input_init();
Steffen Klassertf203b762018-06-12 14:07:12 +02002954
2955 RCU_INIT_POINTER(xfrm_if_cb, NULL);
2956 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
Joy Lattenab5f5e82007-09-17 11:51:22 -07002959#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002960static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2961 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002962{
Paul Moore875179f2007-12-01 23:27:18 +11002963 struct xfrm_sec_ctx *ctx = xp->security;
2964 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002965
Paul Moore875179f2007-12-01 23:27:18 +11002966 if (ctx)
2967 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2968 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2969
Weilong Chen9b7a7872013-12-24 09:43:46 +08002970 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002971 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002972 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002973 if (sel->prefixlen_s != 32)
2974 audit_log_format(audit_buf, " src_prefixlen=%d",
2975 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002976 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002977 if (sel->prefixlen_d != 32)
2978 audit_log_format(audit_buf, " dst_prefixlen=%d",
2979 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002980 break;
2981 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002982 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002983 if (sel->prefixlen_s != 128)
2984 audit_log_format(audit_buf, " src_prefixlen=%d",
2985 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002986 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002987 if (sel->prefixlen_d != 128)
2988 audit_log_format(audit_buf, " dst_prefixlen=%d",
2989 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002990 break;
2991 }
2992}
2993
Tetsuo Handa2e710292014-04-22 21:48:30 +09002994void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002995{
2996 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002997
Paul Mooreafeb14b2007-12-21 14:58:11 -08002998 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002999 if (audit_buf == NULL)
3000 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003001 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003002 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003003 xfrm_audit_common_policyinfo(xp, audit_buf);
3004 audit_log_end(audit_buf);
3005}
3006EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3007
Paul Moore68277ac2007-12-20 20:49:33 -08003008void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003009 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003010{
3011 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003012
Paul Mooreafeb14b2007-12-21 14:58:11 -08003013 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003014 if (audit_buf == NULL)
3015 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003016 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003017 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003018 xfrm_audit_common_policyinfo(xp, audit_buf);
3019 audit_log_end(audit_buf);
3020}
3021EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3022#endif
3023
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003024#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003025static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3026 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003027{
3028 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3029 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003030 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3031 sel_cmp->family) &&
3032 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3033 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003034 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3035 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003036 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003037 }
3038 } else {
3039 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003040 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003041 }
3042 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003043 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003044}
3045
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003046static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3047 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003048{
3049 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003050 struct hlist_head *chain;
3051 u32 priority = ~0U;
3052
Florian Westphal9d0380d2016-08-11 15:17:59 +02003053 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003054 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003055 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003056 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3057 pol->type == type) {
3058 ret = pol;
3059 priority = ret->priority;
3060 break;
3061 }
3062 }
Fan Du8d549c42013-11-07 17:47:49 +08003063 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003064 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003065 if ((pol->priority >= priority) && ret)
3066 break;
3067
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003068 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003069 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003070 ret = pol;
3071 break;
3072 }
3073 }
3074
Li RongQing586f2eb2015-04-30 17:13:41 +08003075 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003076
Florian Westphal9d0380d2016-08-11 15:17:59 +02003077 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003078
3079 return ret;
3080}
3081
David S. Millerdd701752011-02-24 00:21:08 -05003082static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003083{
3084 int match = 0;
3085
3086 if (t->mode == m->mode && t->id.proto == m->proto &&
3087 (m->reqid == 0 || t->reqid == m->reqid)) {
3088 switch (t->mode) {
3089 case XFRM_MODE_TUNNEL:
3090 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003091 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3092 m->old_family) &&
3093 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3094 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003095 match = 1;
3096 }
3097 break;
3098 case XFRM_MODE_TRANSPORT:
3099 /* in case of transport mode, template does not store
3100 any IP addresses, hence we just compare mode and
3101 protocol */
3102 match = 1;
3103 break;
3104 default:
3105 break;
3106 }
3107 }
3108 return match;
3109}
3110
3111/* update endpoint address(es) of template(s) */
3112static int xfrm_policy_migrate(struct xfrm_policy *pol,
3113 struct xfrm_migrate *m, int num_migrate)
3114{
3115 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003116 int i, j, n = 0;
3117
3118 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003119 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003120 /* target policy has been deleted */
3121 write_unlock_bh(&pol->lock);
3122 return -ENOENT;
3123 }
3124
3125 for (i = 0; i < pol->xfrm_nr; i++) {
3126 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3127 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3128 continue;
3129 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003130 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3131 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003132 continue;
3133 /* update endpoints */
3134 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3135 sizeof(pol->xfrm_vec[i].id.daddr));
3136 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3137 sizeof(pol->xfrm_vec[i].saddr));
3138 pol->xfrm_vec[i].encap_family = mp->new_family;
3139 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003140 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003141 }
3142 }
3143
3144 write_unlock_bh(&pol->lock);
3145
3146 if (!n)
3147 return -ENODATA;
3148
3149 return 0;
3150}
3151
David S. Millerdd701752011-02-24 00:21:08 -05003152static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003153{
3154 int i, j;
3155
3156 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3157 return -EINVAL;
3158
3159 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003160 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3161 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3162 return -EINVAL;
3163
3164 /* check if there is any duplicated entry */
3165 for (j = i + 1; j < num_migrate; j++) {
3166 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3167 sizeof(m[i].old_daddr)) &&
3168 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3169 sizeof(m[i].old_saddr)) &&
3170 m[i].proto == m[j].proto &&
3171 m[i].mode == m[j].mode &&
3172 m[i].reqid == m[j].reqid &&
3173 m[i].old_family == m[j].old_family)
3174 return -EINVAL;
3175 }
3176 }
3177
3178 return 0;
3179}
3180
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003181int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003182 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02003183 struct xfrm_kmaddress *k, struct net *net,
3184 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003185{
3186 int i, err, nx_cur = 0, nx_new = 0;
3187 struct xfrm_policy *pol = NULL;
3188 struct xfrm_state *x, *xc;
3189 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3190 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3191 struct xfrm_migrate *mp;
3192
Vladis Dronov7bab0962017-08-02 19:50:14 +02003193 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003194 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3195 goto out;
3196
Vladis Dronov7bab0962017-08-02 19:50:14 +02003197 if (dir >= XFRM_POLICY_MAX) {
3198 err = -EINVAL;
3199 goto out;
3200 }
3201
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003202 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003203 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204 err = -ENOENT;
3205 goto out;
3206 }
3207
3208 /* Stage 2 - find and update state(s) */
3209 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003210 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003211 x_cur[nx_cur] = x;
3212 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02003213 xc = xfrm_state_migrate(x, mp, encap);
3214 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003215 x_new[nx_new] = xc;
3216 nx_new++;
3217 } else {
3218 err = -ENODATA;
3219 goto restore_state;
3220 }
3221 }
3222 }
3223
3224 /* Stage 3 - update policy */
3225 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3226 goto restore_state;
3227
3228 /* Stage 4 - delete old state(s) */
3229 if (nx_cur) {
3230 xfrm_states_put(x_cur, nx_cur);
3231 xfrm_states_delete(x_cur, nx_cur);
3232 }
3233
3234 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02003235 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003236
3237 xfrm_pol_put(pol);
3238
3239 return 0;
3240out:
3241 return err;
3242
3243restore_state:
3244 if (pol)
3245 xfrm_pol_put(pol);
3246 if (nx_cur)
3247 xfrm_states_put(x_cur, nx_cur);
3248 if (nx_new)
3249 xfrm_states_delete(x_new, nx_new);
3250
3251 return err;
3252}
David S. Millere610e672007-02-08 13:29:15 -08003253EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003254#endif