blob: 0f1db4c18b228341b68b2ac6c95f14463ba21a25 [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>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Steffen Klassertb8c203b2014-09-16 10:08:49 +020042struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
Priyanka Jain418a99a2012-08-12 21:22:29 +000047static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
Florian Westphal37b10382017-02-07 15:00:19 +010048static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
Priyanka Jain418a99a2012-08-12 21:22:29 +000049 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *xfrm_dst_cache __read_mostly;
Florian Westphal30846092016-08-11 15:17:54 +020052static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Herbert Xu25ee3282007-12-11 09:32:34 -080054static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000055static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000056static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010057static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Herbert Xu12bfa8b2014-11-13 17:09:50 +080059static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080060static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
Florian Westphale37cc8a2016-08-11 15:17:55 +020063static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
64{
Reshetova, Elena850a6212017-07-04 15:53:22 +030065 return refcount_inc_not_zero(&policy->refcnt);
Florian Westphale37cc8a2016-08-11 15:17:55 +020066}
67
David S. Millerbc9b35a2012-05-15 15:04:57 -040068static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050069__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080070{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050071 const struct flowi4 *fl4 = &fl->u.ip4;
72
Alexey Dobriyan26bff942011-11-22 06:46:02 +000073 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
74 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050075 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
76 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
77 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
78 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080079}
80
David S. Millerbc9b35a2012-05-15 15:04:57 -040081static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050082__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080083{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050084 const struct flowi6 *fl6 = &fl->u.ip6;
85
86 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
87 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
88 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
89 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
90 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
91 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080092}
93
David S. Millerbc9b35a2012-05-15 15:04:57 -040094bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
95 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080096{
97 switch (family) {
98 case AF_INET:
99 return __xfrm4_selector_match(sel, fl);
100 case AF_INET6:
101 return __xfrm6_selector_match(sel, fl);
102 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400103 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800104}
105
Florian Westphala2817d82017-02-07 15:00:17 +0100106static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
Eric Dumazetef8531b2012-08-19 12:31:48 +0200107{
Florian Westphala2817d82017-02-07 15:00:17 +0100108 const struct xfrm_policy_afinfo *afinfo;
Eric Dumazetef8531b2012-08-19 12:31:48 +0200109
Florian Westphala2817d82017-02-07 15:00:17 +0100110 if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Eric Dumazetef8531b2012-08-19 12:31:48 +0200111 return NULL;
112 rcu_read_lock();
113 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
114 if (unlikely(!afinfo))
115 rcu_read_unlock();
116 return afinfo;
117}
118
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200119struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Florian Westphal37b10382017-02-07 15:00:19 +0100124 const struct xfrm_policy_afinfo *afinfo;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David Ahern42a7b322015-08-10 16:58:11 -0600131 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Florian Westphalbdba9fe2017-02-07 15:00:18 +0100133 rcu_read_unlock();
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200137EXPORT_SYMBOL(__xfrm_dst_lookup);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900138
David Ahern42a7b322015-08-10 16:58:11 -0600139static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
140 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900141 xfrm_address_t *prev_saddr,
142 xfrm_address_t *prev_daddr,
143 int family)
144{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800145 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900146 xfrm_address_t *saddr = &x->props.saddr;
147 xfrm_address_t *daddr = &x->id.daddr;
148 struct dst_entry *dst;
149
150 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
151 saddr = x->coaddr;
152 daddr = prev_daddr;
153 }
154 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
155 saddr = prev_saddr;
156 daddr = x->coaddr;
157 }
158
David Ahern42a7b322015-08-10 16:58:11 -0600159 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900160
161 if (!IS_ERR(dst)) {
162 if (prev_saddr != saddr)
163 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
164 if (prev_daddr != daddr)
165 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
166 }
167
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800168 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static inline unsigned long make_jiffies(long secs)
172{
173 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
174 return MAX_SCHEDULE_TIMEOUT-1;
175 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900176 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static void xfrm_policy_timer(unsigned long data)
180{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800181 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800182 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 long next = LONG_MAX;
184 int warn = 0;
185 int dir;
186
187 read_lock(&xp->lock);
188
Timo Teräsea2dea92010-03-31 00:17:05 +0000189 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto out;
191
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700192 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (xp->lft.hard_add_expires_seconds) {
195 long tmo = xp->lft.hard_add_expires_seconds +
196 xp->curlft.add_time - now;
197 if (tmo <= 0)
198 goto expired;
199 if (tmo < next)
200 next = tmo;
201 }
202 if (xp->lft.hard_use_expires_seconds) {
203 long tmo = xp->lft.hard_use_expires_seconds +
204 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
205 if (tmo <= 0)
206 goto expired;
207 if (tmo < next)
208 next = tmo;
209 }
210 if (xp->lft.soft_add_expires_seconds) {
211 long tmo = xp->lft.soft_add_expires_seconds +
212 xp->curlft.add_time - now;
213 if (tmo <= 0) {
214 warn = 1;
215 tmo = XFRM_KM_TIMEOUT;
216 }
217 if (tmo < next)
218 next = tmo;
219 }
220 if (xp->lft.soft_use_expires_seconds) {
221 long tmo = xp->lft.soft_use_expires_seconds +
222 (xp->curlft.use_time ? : 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
231 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800232 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (next != LONG_MAX &&
234 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
235 xfrm_pol_hold(xp);
236
237out:
238 read_unlock(&xp->lock);
239 xfrm_pol_put(xp);
240 return;
241
242expired:
243 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700244 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800245 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfrm_pol_put(xp);
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
250 * SPD calls.
251 */
252
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800253struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct xfrm_policy *policy;
256
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700257 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800260 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700261 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700262 INIT_HLIST_NODE(&policy->bydst);
263 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 rwlock_init(&policy->lock);
Reshetova, Elena850a6212017-07-04 15:53:22 +0300265 refcount_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100266 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800267 setup_timer(&policy->timer, xfrm_policy_timer,
268 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100269 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
270 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 return policy;
273}
274EXPORT_SYMBOL(xfrm_policy_alloc);
275
Eric Dumazet56f04732015-12-08 07:22:01 -0800276static void xfrm_policy_destroy_rcu(struct rcu_head *head)
277{
278 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
279
280 security_xfrm_policy_free(policy->security);
281 kfree(policy);
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* Destroy xfrm_policy: descendant resources must be released to this moment. */
285
WANG Cong64c31b32008-01-07 22:34:29 -0800286void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Herbert Xu12a169e2008-10-01 07:03:24 -0700288 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Fan Du0659eea2013-08-01 18:08:36 +0800290 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 BUG();
292
Eric Dumazet56f04732015-12-08 07:22:01 -0800293 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
WANG Cong64c31b32008-01-07 22:34:29 -0800295EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100297/* Rule must be locked. Release descendant resources, announce
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * entry dead. The rule must be unlinked from lists to the moment.
299 */
300
301static void xfrm_policy_kill(struct xfrm_policy *policy)
302{
Herbert Xu12a169e2008-10-01 07:03:24 -0700303 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Timo Teräs285ead12010-04-07 00:30:06 +0000305 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200307 if (del_timer(&policy->polq.hold_timer))
308 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800309 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100310
Timo Teräs285ead12010-04-07 00:30:06 +0000311 if (del_timer(&policy->timer))
312 xfrm_pol_put(policy);
313
314 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
David S. Miller2518c7c2006-08-24 04:45:07 -0700317static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
318
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800319static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700320{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800321 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700322}
323
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200324/* calculate policy hash thresholds */
325static void __get_hash_thresh(struct net *net,
326 unsigned short family, int dir,
327 u8 *dbits, u8 *sbits)
328{
329 switch (family) {
330 case AF_INET:
331 *dbits = net->xfrm.policy_bydst[dir].dbits4;
332 *sbits = net->xfrm.policy_bydst[dir].sbits4;
333 break;
334
335 case AF_INET6:
336 *dbits = net->xfrm.policy_bydst[dir].dbits6;
337 *sbits = net->xfrm.policy_bydst[dir].sbits6;
338 break;
339
340 default:
341 *dbits = 0;
342 *sbits = 0;
343 }
344}
345
David S. Miller5f803b52011-02-24 00:33:19 -0500346static struct hlist_head *policy_hash_bysel(struct net *net,
347 const struct xfrm_selector *sel,
348 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700349{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800350 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200351 unsigned int hash;
352 u8 dbits;
353 u8 sbits;
354
355 __get_hash_thresh(net, family, dir, &dbits, &sbits);
356 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700357
Florian Westphale1e551b2016-08-11 15:17:53 +0200358 if (hash == hmask + 1)
359 return &net->xfrm.policy_inexact[dir];
360
361 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
362 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700363}
364
David S. Miller5f803b52011-02-24 00:33:19 -0500365static struct hlist_head *policy_hash_direct(struct net *net,
366 const xfrm_address_t *daddr,
367 const xfrm_address_t *saddr,
368 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700369{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800370 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200371 unsigned int hash;
372 u8 dbits;
373 u8 sbits;
374
375 __get_hash_thresh(net, family, dir, &dbits, &sbits);
376 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700377
Florian Westphale1e551b2016-08-11 15:17:53 +0200378 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
379 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700380}
381
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200382static void xfrm_dst_hash_transfer(struct net *net,
383 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700384 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200385 unsigned int nhashmask,
386 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700387{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800388 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700389 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800390 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200391 u8 dbits;
392 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700393
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800394redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800395 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700396 unsigned int h;
397
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200398 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700399 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200400 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800401 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200402 hlist_del_rcu(&pol->bydst);
403 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800404 h0 = h;
405 } else {
406 if (h != h0)
407 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200408 hlist_del_rcu(&pol->bydst);
409 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800410 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800411 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800412 }
413 if (!hlist_empty(list)) {
414 entry0 = NULL;
415 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 }
417}
418
419static void xfrm_idx_hash_transfer(struct hlist_head *list,
420 struct hlist_head *nidxtable,
421 unsigned int nhashmask)
422{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800423 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700424 struct xfrm_policy *pol;
425
Sasha Levinb67bfe02013-02-27 17:06:00 -0800426 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700427 unsigned int h;
428
429 h = __idx_hash(pol->index, nhashmask);
430 hlist_add_head(&pol->byidx, nidxtable+h);
431 }
432}
433
434static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
435{
436 return ((old_hmask + 1) << 1) - 1;
437}
438
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800439static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700440{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800441 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700442 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
443 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700444 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200445 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700446 int i;
447
448 if (!ndst)
449 return;
450
Florian Westphal9d0380d2016-08-11 15:17:59 +0200451 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200452 write_seqcount_begin(&xfrm_policy_hash_generation);
453
454 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
455 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700456
Florian Westphale1e551b2016-08-11 15:17:53 +0200457 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
458 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
459
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200461 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700462
Florian Westphale1e551b2016-08-11 15:17:53 +0200463 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800464 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700465
Florian Westphal30846092016-08-11 15:17:54 +0200466 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200467 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700468
Florian Westphale1e551b2016-08-11 15:17:53 +0200469 synchronize_rcu();
470
David S. Miller44e36b42006-08-24 04:50:50 -0700471 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700472}
473
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800474static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700475{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800476 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700477 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
478 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800479 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700480 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700481 int i;
482
483 if (!nidx)
484 return;
485
Florian Westphal9d0380d2016-08-11 15:17:59 +0200486 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700487
488 for (i = hmask; i >= 0; i--)
489 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
490
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800491 net->xfrm.policy_byidx = nidx;
492 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700493
Florian Westphal9d0380d2016-08-11 15:17:59 +0200494 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700495
David S. Miller44e36b42006-08-24 04:50:50 -0700496 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700497}
498
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800499static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700500{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800501 unsigned int cnt = net->xfrm.policy_count[dir];
502 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
504 if (total)
505 *total += cnt;
506
507 if ((hmask + 1) < xfrm_policy_hashmax &&
508 cnt > hmask)
509 return 1;
510
511 return 0;
512}
513
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800514static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700515{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700517
518 if ((hmask + 1) < xfrm_policy_hashmax &&
519 total > hmask)
520 return 1;
521
522 return 0;
523}
524
Alexey Dobriyane0710412010-01-23 13:37:10 +0000525void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700526{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000527 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
528 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
529 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
530 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
531 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
532 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
533 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700534 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700535}
536EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700537
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700538static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800539static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700540{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800541 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700542 int dir, total;
543
544 mutex_lock(&hash_resize_mutex);
545
546 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800547 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800548 if (xfrm_bydst_should_resize(net, dir, &total))
549 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700550 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800551 if (xfrm_byidx_should_resize(net, total))
552 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700553
554 mutex_unlock(&hash_resize_mutex);
555}
556
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200557static void xfrm_hash_rebuild(struct work_struct *work)
558{
559 struct net *net = container_of(work, struct net,
560 xfrm.policy_hthresh.work);
561 unsigned int hmask;
562 struct xfrm_policy *pol;
563 struct xfrm_policy *policy;
564 struct hlist_head *chain;
565 struct hlist_head *odst;
566 struct hlist_node *newpos;
567 int i;
568 int dir;
569 unsigned seq;
570 u8 lbits4, rbits4, lbits6, rbits6;
571
572 mutex_lock(&hash_resize_mutex);
573
574 /* read selector prefixlen thresholds */
575 do {
576 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
577
578 lbits4 = net->xfrm.policy_hthresh.lbits4;
579 rbits4 = net->xfrm.policy_hthresh.rbits4;
580 lbits6 = net->xfrm.policy_hthresh.lbits6;
581 rbits6 = net->xfrm.policy_hthresh.rbits6;
582 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
583
Florian Westphal9d0380d2016-08-11 15:17:59 +0200584 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200585
586 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800587 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200588 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
589 hmask = net->xfrm.policy_bydst[dir].hmask;
590 odst = net->xfrm.policy_bydst[dir].table;
591 for (i = hmask; i >= 0; i--)
592 INIT_HLIST_HEAD(odst + i);
593 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
594 /* dir out => dst = remote, src = local */
595 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
596 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
597 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
598 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
599 } else {
600 /* dir in/fwd => dst = local, src = remote */
601 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
602 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
603 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
604 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
605 }
606 }
607
608 /* re-insert all policies by order of creation */
609 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200610 if (xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
611 /* skip socket policies */
612 continue;
613 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200614 newpos = NULL;
615 chain = policy_hash_bysel(net, &policy->selector,
616 policy->family,
617 xfrm_policy_id2dir(policy->index));
618 hlist_for_each_entry(pol, chain, bydst) {
619 if (policy->priority >= pol->priority)
620 newpos = &pol->bydst;
621 else
622 break;
623 }
624 if (newpos)
625 hlist_add_behind(&policy->bydst, newpos);
626 else
627 hlist_add_head(&policy->bydst, chain);
628 }
629
Florian Westphal9d0380d2016-08-11 15:17:59 +0200630 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200631
632 mutex_unlock(&hash_resize_mutex);
633}
634
635void xfrm_policy_hash_rebuild(struct net *net)
636{
637 schedule_work(&net->xfrm.policy_hthresh.work);
638}
639EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* Generate new index... KAME seems to generate them ordered by cost
642 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800643static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 static u32 idx_generator;
646
647 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700648 struct hlist_head *list;
649 struct xfrm_policy *p;
650 u32 idx;
651 int found;
652
Fan Due682adf02013-11-07 17:47:48 +0800653 if (!index) {
654 idx = (idx_generator | dir);
655 idx_generator += 8;
656 } else {
657 idx = index;
658 index = 0;
659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (idx == 0)
662 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800663 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700664 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800665 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700666 if (p->index == idx) {
667 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700671 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return idx;
673 }
674}
675
David S. Miller2518c7c2006-08-24 04:45:07 -0700676static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
677{
678 u32 *p1 = (u32 *) s1;
679 u32 *p2 = (u32 *) s2;
680 int len = sizeof(struct xfrm_selector) / sizeof(u32);
681 int i;
682
683 for (i = 0; i < len; i++) {
684 if (p1[i] != p2[i])
685 return 1;
686 }
687
688 return 0;
689}
690
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100691static void xfrm_policy_requeue(struct xfrm_policy *old,
692 struct xfrm_policy *new)
693{
694 struct xfrm_policy_queue *pq = &old->polq;
695 struct sk_buff_head list;
696
Li RongQingde2ad482015-04-30 17:25:19 +0800697 if (skb_queue_empty(&pq->hold_queue))
698 return;
699
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100700 __skb_queue_head_init(&list);
701
702 spin_lock_bh(&pq->hold_queue.lock);
703 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200704 if (del_timer(&pq->hold_timer))
705 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100706 spin_unlock_bh(&pq->hold_queue.lock);
707
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100708 pq = &new->polq;
709
710 spin_lock_bh(&pq->hold_queue.lock);
711 skb_queue_splice(&list, &pq->hold_queue);
712 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200713 if (!mod_timer(&pq->hold_timer, jiffies))
714 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100715 spin_unlock_bh(&pq->hold_queue.lock);
716}
717
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100718static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
719 struct xfrm_policy *pol)
720{
721 u32 mark = policy->mark.v & policy->mark.m;
722
723 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
724 return true;
725
726 if ((mark & pol->mark.m) == pol->mark.v &&
727 policy->priority == pol->priority)
728 return true;
729
730 return false;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
734{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800735 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700736 struct xfrm_policy *pol;
737 struct xfrm_policy *delpol;
738 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800739 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Florian Westphal9d0380d2016-08-11 15:17:59 +0200741 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800742 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700743 delpol = NULL;
744 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800745 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800746 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700747 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100748 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800749 xfrm_sec_ctx_match(pol->security, policy->security) &&
750 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200752 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return -EEXIST;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 delpol = pol;
756 if (policy->priority > pol->priority)
757 continue;
758 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800759 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 continue;
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (delpol)
763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700766 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700767 else
768 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800769 __xfrm_policy_link(policy, dir);
fan.duca4c3fc2013-07-30 08:33:53 +0800770
771 /* After previous checking, family can either be AF_INET or AF_INET6 */
772 if (policy->family == AF_INET)
773 rt_genid_bump_ipv4(net);
774 else
775 rt_genid_bump_ipv6(net);
776
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100777 if (delpol) {
778 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800779 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100780 }
Fan Due682adf02013-11-07 17:47:48 +0800781 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800782 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800783 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 policy->curlft.use_time = 0;
785 if (!mod_timer(&policy->timer, jiffies + HZ))
786 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200787 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
David S. Miller9b78a822005-12-22 07:39:48 -0800789 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800791 else if (xfrm_bydst_should_resize(net, dir, NULL))
792 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
795}
796EXPORT_SYMBOL(xfrm_policy_insert);
797
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000798struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
799 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800800 struct xfrm_sec_ctx *ctx, int delete,
801 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
David S. Miller2518c7c2006-08-24 04:45:07 -0700803 struct xfrm_policy *pol, *ret;
804 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Eric Parisef41aaa2007-03-07 15:37:58 -0800806 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200807 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800808 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700809 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800810 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700811 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000812 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700813 !selector_cmp(sel, &pol->selector) &&
814 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700816 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700817 *err = security_xfrm_policy_delete(
818 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800819 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200820 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800821 return pol;
822 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800823 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700824 }
825 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
827 }
828 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200829 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000831 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700832 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
Trent Jaegerdf718372005-12-13 23:12:27 -0800835EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000837struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
838 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 struct xfrm_policy *pol, *ret;
841 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Herbert Xub5505c62007-05-14 02:15:47 -0700843 *err = -ENOENT;
844 if (xfrm_policy_id2dir(id) != dir)
845 return NULL;
846
Eric Parisef41aaa2007-03-07 15:37:58 -0800847 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200848 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800849 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700850 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800851 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000852 if (pol->type == type && pol->index == id &&
853 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700855 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700856 *err = security_xfrm_policy_delete(
857 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800858 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200859 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800860 return pol;
861 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800862 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700863 }
864 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 break;
866 }
867 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200868 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000870 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700872 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874EXPORT_SYMBOL(xfrm_policy_byid);
875
Joy Latten4aa2e622007-06-04 19:05:57 -0400876#ifdef CONFIG_SECURITY_NETWORK_XFRM
877static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900878xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Joy Latten4aa2e622007-06-04 19:05:57 -0400880 int dir, err = 0;
881
882 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
883 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400884 int i;
885
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800887 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400888 if (pol->type != type)
889 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700890 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400891 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900892 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400893 return err;
894 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900895 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800896 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800897 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800898 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400899 bydst) {
900 if (pol->type != type)
901 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700902 err = security_xfrm_policy_delete(
903 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400904 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700905 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900906 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400907 return err;
908 }
909 }
910 }
911 }
912 return err;
913}
914#else
915static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900916xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400917{
918 return 0;
919}
920#endif
921
Tetsuo Handa2e710292014-04-22 21:48:30 +0900922int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400923{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000924 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Florian Westphal9d0380d2016-08-11 15:17:59 +0200926 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400927
Tetsuo Handa2e710292014-04-22 21:48:30 +0900928 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400929 if (err)
930 goto out;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700933 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800934 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700935
936 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800937 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800938 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700939 if (pol->type != type)
940 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000941 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200942 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000943 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Tetsuo Handa2e710292014-04-22 21:48:30 +0900945 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600946
David S. Miller2518c7c2006-08-24 04:45:07 -0700947 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Steffen Klassert4141b362016-08-24 13:08:40 +0200949 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700950 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700952
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800953 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700954 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800955 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800956 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700957 bydst) {
958 if (pol->type != type)
959 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000960 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200961 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000962 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700963
Tetsuo Handa2e710292014-04-22 21:48:30 +0900964 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700965 xfrm_policy_kill(pol);
966
Florian Westphal9d0380d2016-08-11 15:17:59 +0200967 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700968 goto again2;
969 }
970 }
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000973 if (!cnt)
974 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400975out:
Florian Westphal9d0380d2016-08-11 15:17:59 +0200976 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400977 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979EXPORT_SYMBOL(xfrm_policy_flush);
980
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800981int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800982 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 void *data)
984{
Herbert Xu12a169e2008-10-01 07:03:24 -0700985 struct xfrm_policy *pol;
986 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800987 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Timo Teras4c563f72008-02-28 21:31:08 -0800989 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
990 walk->type != XFRM_POLICY_TYPE_ANY)
991 return -EINVAL;
992
Herbert Xu12a169e2008-10-01 07:03:24 -0700993 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800994 return 0;
995
Florian Westphal9d0380d2016-08-11 15:17:59 +0200996 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700997 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800998 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700999 else
Li RongQing80077702015-04-22 17:09:54 +08001000 x = list_first_entry(&walk->walk.all,
1001 struct xfrm_policy_walk_entry, all);
1002
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001003 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001004 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001005 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001006 pol = container_of(x, struct xfrm_policy, walk);
1007 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1008 walk->type != pol->type)
1009 continue;
1010 error = func(pol, xfrm_policy_id2dir(pol->index),
1011 walk->seq, data);
1012 if (error) {
1013 list_move_tail(&walk->walk.all, &x->all);
1014 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001015 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001016 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001018 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001019 error = -ENOENT;
1020 goto out;
1021 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001022 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001024 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return error;
1026}
1027EXPORT_SYMBOL(xfrm_policy_walk);
1028
Herbert Xu12a169e2008-10-01 07:03:24 -07001029void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1030{
1031 INIT_LIST_HEAD(&walk->walk.all);
1032 walk->walk.dead = 1;
1033 walk->type = type;
1034 walk->seq = 0;
1035}
1036EXPORT_SYMBOL(xfrm_policy_walk_init);
1037
Fan Du283bc9f2013-11-07 17:47:50 +08001038void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001039{
1040 if (list_empty(&walk->walk.all))
1041 return;
1042
Florian Westphal9d0380d2016-08-11 15:17:59 +02001043 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001044 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001045 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001046}
1047EXPORT_SYMBOL(xfrm_policy_walk_done);
1048
James Morris134b0fc2006-10-05 15:42:27 -05001049/*
1050 * Find policy to apply to this flow.
1051 *
1052 * Returns 0 if policy found, else an -errno.
1053 */
David S. Millerf299d552011-02-24 01:23:30 -05001054static int xfrm_policy_match(const struct xfrm_policy *pol,
1055 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001056 u8 type, u16 family, int dir)
1057{
David S. Millerf299d552011-02-24 01:23:30 -05001058 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001059 int ret = -ESRCH;
1060 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001061
1062 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001063 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001064 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001065 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001066
1067 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001068 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001069 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001070 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001071
James Morris134b0fc2006-10-05 15:42:27 -05001072 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001073}
1074
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001075static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001076 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001077 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
James Morris134b0fc2006-10-05 15:42:27 -05001079 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001080 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001081 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001082 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001083 unsigned int sequence;
1084 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001085
1086 daddr = xfrm_flowi_daddr(fl, family);
1087 saddr = xfrm_flowi_saddr(fl, family);
1088 if (unlikely(!daddr || !saddr))
1089 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Florian Westphala7c442472016-08-11 15:17:56 +02001091 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001092 retry:
1093 do {
1094 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1095 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1096 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1097
1098 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001099 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001100 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001101 err = xfrm_policy_match(pol, fl, type, family, dir);
1102 if (err) {
1103 if (err == -ESRCH)
1104 continue;
1105 else {
1106 ret = ERR_PTR(err);
1107 goto fail;
1108 }
1109 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001110 ret = pol;
1111 priority = ret->priority;
1112 break;
1113 }
1114 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001115 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001116 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001117 if ((pol->priority >= priority) && ret)
1118 break;
1119
James Morris134b0fc2006-10-05 15:42:27 -05001120 err = xfrm_policy_match(pol, fl, type, family, dir);
1121 if (err) {
1122 if (err == -ESRCH)
1123 continue;
1124 else {
1125 ret = ERR_PTR(err);
1126 goto fail;
1127 }
Li RongQing8faf4912015-05-14 11:16:59 +08001128 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001129 ret = pol;
1130 break;
1131 }
1132 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001133
Florian Westphal30846092016-08-11 15:17:54 +02001134 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1135 goto retry;
1136
Florian Westphale37cc8a2016-08-11 15:17:55 +02001137 if (ret && !xfrm_pol_hold_rcu(ret))
1138 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001139fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001140 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001141
David S. Miller2518c7c2006-08-24 04:45:07 -07001142 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001143}
1144
Timo Teräs80c802f2010-04-07 00:30:05 +00001145static struct xfrm_policy *
Florian Westphal86dc8ee2017-07-17 13:57:24 +02001146xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001147{
1148#ifdef CONFIG_XFRM_SUB_POLICY
1149 struct xfrm_policy *pol;
1150
1151 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1152 if (pol != NULL)
1153 return pol;
1154#endif
1155 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1156}
1157
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001158static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Steffen Klassert4c86d772017-02-14 07:43:56 +01001159 const struct flowi *fl, u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct xfrm_policy *pol;
1162
Eric Dumazetd188ba82015-12-08 07:22:02 -08001163 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001164 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001165 pol = rcu_dereference(sk->sk_policy[dir]);
1166 if (pol != NULL) {
Steffen Klassert4c86d772017-02-14 07:43:56 +01001167 bool match = xfrm_selector_match(&pol->selector, fl, family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001168 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001169
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001170 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001171 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1172 pol = NULL;
1173 goto out;
1174 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001175 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001176 fl->flowi_secid,
Florian Westphalaff669b2017-07-17 13:57:23 +02001177 dir);
Florian Westphal330e8322016-11-17 13:21:46 +01001178 if (!err) {
1179 if (!xfrm_pol_hold_rcu(pol))
1180 goto again;
1181 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001182 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001183 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001184 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001185 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001186 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 pol = NULL;
1188 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001189out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001190 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return pol;
1192}
1193
1194static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1195{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001196 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001197
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001198 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001199 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 xfrm_pol_hold(pol);
1201}
1202
1203static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1204 int dir)
1205{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001206 struct net *net = xp_net(pol);
1207
Herbert Xu53c2e282014-11-13 17:09:49 +08001208 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001209 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Herbert Xu53c2e282014-11-13 17:09:49 +08001211 /* Socket policies are not hashed. */
1212 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001213 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001214 hlist_del(&pol->byidx);
1215 }
1216
1217 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001218 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001219
1220 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Herbert Xu53c2e282014-11-13 17:09:49 +08001223static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1224{
1225 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1226}
1227
1228static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1229{
1230 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1231}
1232
Herbert Xu4666faa2005-06-18 22:43:22 -07001233int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Fan Du283bc9f2013-11-07 17:47:50 +08001235 struct net *net = xp_net(pol);
1236
Florian Westphal9d0380d2016-08-11 15:17:59 +02001237 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001239 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001244 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
David S. Millera70fcb02006-03-20 19:18:52 -08001246EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1249{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001250 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct xfrm_policy *old_pol;
1252
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001253#ifdef CONFIG_XFRM_SUB_POLICY
1254 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1255 return -EINVAL;
1256#endif
1257
Florian Westphal9d0380d2016-08-11 15:17:59 +02001258 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001259 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1260 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001262 pol->curlft.add_time = get_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001263 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001264 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001266 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001267 if (old_pol) {
1268 if (pol)
1269 xfrm_policy_requeue(old_pol, pol);
1270
Timo Teräsea2dea92010-03-31 00:17:05 +00001271 /* Unlinking succeeds always. This is the only function
1272 * allowed to delete or replace socket policy.
1273 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001274 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001275 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001276 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (old_pol) {
1279 xfrm_policy_kill(old_pol);
1280 }
1281 return 0;
1282}
1283
David S. Millerd3e40a92011-02-24 01:25:41 -05001284static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001286 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001287 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (newp) {
1290 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001291 if (security_xfrm_policy_clone(old->security,
1292 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001293 kfree(newp);
1294 return NULL; /* ENOMEM */
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 newp->lft = old->lft;
1297 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001298 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 newp->action = old->action;
1300 newp->flags = old->flags;
1301 newp->xfrm_nr = old->xfrm_nr;
1302 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001303 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 memcpy(newp->xfrm_vec, old->xfrm_vec,
1305 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001306 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001307 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001308 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 xfrm_pol_put(newp);
1310 }
1311 return newp;
1312}
1313
Eric Dumazetd188ba82015-12-08 07:22:02 -08001314int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001316 const struct xfrm_policy *p;
1317 struct xfrm_policy *np;
1318 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Eric Dumazetd188ba82015-12-08 07:22:02 -08001320 rcu_read_lock();
1321 for (i = 0; i < 2; i++) {
1322 p = rcu_dereference(osk->sk_policy[i]);
1323 if (p) {
1324 np = clone_policy(p, i);
1325 if (unlikely(!np)) {
1326 ret = -ENOMEM;
1327 break;
1328 }
1329 rcu_assign_pointer(sk->sk_policy[i], np);
1330 }
1331 }
1332 rcu_read_unlock();
1333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001336static int
David Ahern42a7b322015-08-10 16:58:11 -06001337xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1338 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001339{
1340 int err;
Florian Westphal37b10382017-02-07 15:00:19 +01001341 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001342
1343 if (unlikely(afinfo == NULL))
1344 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001345 err = afinfo->get_saddr(net, oif, local, remote);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001346 rcu_read_unlock();
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001347 return err;
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350/* Resolve list of templates for the flow, given policy. */
1351
1352static int
David S. Millera6c2e612011-02-22 18:35:39 -08001353xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1354 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001356 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 int nx;
1358 int i, error;
1359 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1360 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001361 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Weilong Chen9b7a7872013-12-24 09:43:46 +08001363 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 struct xfrm_state *x;
1365 xfrm_address_t *remote = daddr;
1366 xfrm_address_t *local = saddr;
1367 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1368
Joakim Koskela48b8d782007-07-26 00:08:42 -07001369 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1370 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 remote = &tmpl->id.daddr;
1372 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001373 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001374 error = xfrm_get_saddr(net, fl->flowi_oif,
1375 &tmp, remote,
1376 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001377 if (error)
1378 goto fail;
1379 local = &tmp;
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1384
1385 if (x && x->km.state == XFRM_STATE_VALID) {
1386 xfrm[nx++] = x;
1387 daddr = remote;
1388 saddr = local;
1389 continue;
1390 }
1391 if (x) {
1392 error = (x->km.state == XFRM_STATE_ERROR ?
1393 -EINVAL : -EAGAIN);
1394 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001395 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001396 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (!tmpl->optional)
1400 goto fail;
1401 }
1402 return nx;
1403
1404fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001405 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 xfrm_state_put(xfrm[nx]);
1407 return error;
1408}
1409
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001410static int
David S. Millera6c2e612011-02-22 18:35:39 -08001411xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1412 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001413{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001414 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1415 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001416 int cnx = 0;
1417 int error;
1418 int ret;
1419 int i;
1420
1421 for (i = 0; i < npols; i++) {
1422 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1423 error = -ENOBUFS;
1424 goto fail;
1425 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001426
1427 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001428 if (ret < 0) {
1429 error = ret;
1430 goto fail;
1431 } else
1432 cnx += ret;
1433 }
1434
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001435 /* found states are sorted for outbound processing */
1436 if (npols > 1)
1437 xfrm_state_sort(xfrm, tpp, cnx, family);
1438
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001439 return cnx;
1440
1441 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001442 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001443 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001444 return error;
1445
1446}
1447
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001448static int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001449{
Florian Westphal37b10382017-02-07 15:00:19 +01001450 const struct xfrm_policy_afinfo *afinfo;
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001451 int tos = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001452
Florian Westphalf5e2bb42017-02-07 15:00:14 +01001453 afinfo = xfrm_policy_get_afinfo(family);
1454 tos = afinfo ? afinfo->get_tos(fl) : 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001455
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001456 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001457
1458 return tos;
1459}
1460
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001461static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001462{
Florian Westphal37b10382017-02-07 15:00:19 +01001463 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001464 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001465 struct xfrm_dst *xdst;
1466
1467 if (!afinfo)
1468 return ERR_PTR(-EINVAL);
1469
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001470 switch (family) {
1471 case AF_INET:
1472 dst_ops = &net->xfrm.xfrm4_dst_ops;
1473 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001474#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001475 case AF_INET6:
1476 dst_ops = &net->xfrm.xfrm6_dst_ops;
1477 break;
1478#endif
1479 default:
1480 BUG();
1481 }
Wei Wangb2a9c0e2017-06-17 10:42:41 -07001482 xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001483
Madalin Bucurd4cae562011-09-26 07:04:36 +00001484 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001485 struct dst_entry *dst = &xdst->u.dst;
1486
1487 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Madalin Bucurd4cae562011-09-26 07:04:36 +00001488 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001489 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001490
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001491 rcu_read_unlock();
Madalin Bucurd4cae562011-09-26 07:04:36 +00001492
Herbert Xu25ee3282007-12-11 09:32:34 -08001493 return xdst;
1494}
1495
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001496static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1497 int nfheader_len)
1498{
Florian Westphal37b10382017-02-07 15:00:19 +01001499 const struct xfrm_policy_afinfo *afinfo =
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001500 xfrm_policy_get_afinfo(dst->ops->family);
1501 int err;
1502
1503 if (!afinfo)
1504 return -EINVAL;
1505
1506 err = afinfo->init_path(path, dst, nfheader_len);
1507
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001508 rcu_read_unlock();
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001509
1510 return err;
1511}
1512
Herbert Xu87c1e122010-03-02 02:51:56 +00001513static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001514 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001515{
Florian Westphal37b10382017-02-07 15:00:19 +01001516 const struct xfrm_policy_afinfo *afinfo =
Herbert Xu25ee3282007-12-11 09:32:34 -08001517 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1518 int err;
1519
1520 if (!afinfo)
1521 return -EINVAL;
1522
Herbert Xu87c1e122010-03-02 02:51:56 +00001523 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001524
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001525 rcu_read_unlock();
Herbert Xu25ee3282007-12-11 09:32:34 -08001526
1527 return err;
1528}
1529
Timo Teräs80c802f2010-04-07 00:30:05 +00001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1532 * all the metrics... Shortly, bundle a bundle.
1533 */
1534
Herbert Xu25ee3282007-12-11 09:32:34 -08001535static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1536 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001537 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001538 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001540 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001541 unsigned long now = jiffies;
1542 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001543 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001544 struct dst_entry *dst_prev = NULL;
1545 struct dst_entry *dst0 = NULL;
1546 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001548 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001549 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001550 int trailer_len = 0;
1551 int tos;
1552 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001553 xfrm_address_t saddr, daddr;
1554
1555 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001556
1557 tos = xfrm_get_tos(fl, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001558
1559 dst_hold(dst);
1560
1561 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001562 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001563 struct dst_entry *dst1 = &xdst->u.dst;
1564
1565 err = PTR_ERR(xdst);
1566 if (IS_ERR(xdst)) {
1567 dst_release(dst);
1568 goto put_states;
1569 }
1570
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001571 if (xfrm[i]->sel.family == AF_UNSPEC) {
1572 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1573 xfrm_af2proto(family));
1574 if (!inner_mode) {
1575 err = -EAFNOSUPPORT;
1576 dst_release(dst);
1577 goto put_states;
1578 }
1579 } else
1580 inner_mode = xfrm[i]->inner_mode;
1581
Herbert Xu25ee3282007-12-11 09:32:34 -08001582 if (!dst_prev)
1583 dst0 = dst1;
Wei Wang52df1572017-06-17 10:42:38 -07001584 else
1585 /* Ref count is taken during xfrm_alloc_dst()
1586 * No need to do dst_clone() on dst1
1587 */
1588 dst_prev->child = dst1;
Herbert Xu25ee3282007-12-11 09:32:34 -08001589
1590 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001591 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001592
1593 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1594 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001595 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1596 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001597 err = PTR_ERR(dst);
1598 if (IS_ERR(dst))
1599 goto put_states;
1600 } else
1601 dst_hold(dst);
1602
1603 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001604 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001605
David S. Millerf5b0a872012-07-19 12:31:33 -07001606 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001607 dst1->flags |= DST_HOST;
1608 dst1->lastuse = now;
1609
1610 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001611 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001612
1613 dst1->next = dst_prev;
1614 dst_prev = dst1;
1615
1616 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001617 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1618 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001619 trailer_len += xfrm[i]->props.trailer_len;
1620 }
1621
1622 dst_prev->child = dst;
1623 dst0->path = dst;
1624
1625 err = -ENODEV;
1626 dev = dst->dev;
1627 if (!dev)
1628 goto free_dst;
1629
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001630 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001631 xfrm_init_pmtu(dst_prev);
1632
1633 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1634 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1635
Herbert Xu87c1e122010-03-02 02:51:56 +00001636 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001637 if (err)
1638 goto free_dst;
1639
1640 dst_prev->header_len = header_len;
1641 dst_prev->trailer_len = trailer_len;
1642 header_len -= xdst->u.dst.xfrm->props.header_len;
1643 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1644 }
1645
1646out:
1647 return dst0;
1648
1649put_states:
1650 for (; i < nx; i++)
1651 xfrm_state_put(xfrm[i]);
1652free_dst:
1653 if (dst0)
Wei Wang52df1572017-06-17 10:42:38 -07001654 dst_release_immediate(dst0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001655 dst0 = ERR_PTR(err);
1656 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
David S. Miller73ff93c2011-02-22 18:33:42 -08001659static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001660 struct xfrm_policy **pols,
1661 int *num_pols, int *num_xfrms)
1662{
1663 int i;
1664
1665 if (*num_pols == 0 || !pols[0]) {
1666 *num_pols = 0;
1667 *num_xfrms = 0;
1668 return 0;
1669 }
1670 if (IS_ERR(pols[0]))
1671 return PTR_ERR(pols[0]);
1672
1673 *num_xfrms = pols[0]->xfrm_nr;
1674
1675#ifdef CONFIG_XFRM_SUB_POLICY
1676 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1677 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1678 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1679 XFRM_POLICY_TYPE_MAIN,
1680 fl, family,
1681 XFRM_POLICY_OUT);
1682 if (pols[1]) {
1683 if (IS_ERR(pols[1])) {
1684 xfrm_pols_put(pols, *num_pols);
1685 return PTR_ERR(pols[1]);
1686 }
Weilong Chen02d08922013-12-24 09:43:48 +08001687 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001688 (*num_xfrms) += pols[1]->xfrm_nr;
1689 }
1690 }
1691#endif
1692 for (i = 0; i < *num_pols; i++) {
1693 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1694 *num_xfrms = -1;
1695 break;
1696 }
1697 }
1698
1699 return 0;
1700
1701}
1702
1703static struct xfrm_dst *
1704xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001705 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001706 struct dst_entry *dst_orig)
1707{
1708 struct net *net = xp_net(pols[0]);
1709 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1710 struct dst_entry *dst;
1711 struct xfrm_dst *xdst;
1712 int err;
1713
1714 /* Try to instantiate a bundle */
1715 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001716 if (err <= 0) {
1717 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001718 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1719 return ERR_PTR(err);
1720 }
1721
1722 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1723 if (IS_ERR(dst)) {
1724 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1725 return ERR_CAST(dst);
1726 }
1727
1728 xdst = (struct xfrm_dst *)dst;
1729 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00001730 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001731 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001732 xdst->policy_genid = atomic_read(&pols[0]->genid);
1733
1734 return xdst;
1735}
1736
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001737static void xfrm_policy_queue_process(unsigned long arg)
1738{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001739 struct sk_buff *skb;
1740 struct sock *sk;
1741 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001742 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001743 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001744 struct xfrm_policy_queue *pq = &pol->polq;
1745 struct flowi fl;
1746 struct sk_buff_head list;
1747
1748 spin_lock(&pq->hold_queue.lock);
1749 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001750 if (!skb) {
1751 spin_unlock(&pq->hold_queue.lock);
1752 goto out;
1753 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001754 dst = skb_dst(skb);
1755 sk = skb->sk;
1756 xfrm_decode_session(skb, &fl, dst->ops->family);
1757 spin_unlock(&pq->hold_queue.lock);
1758
1759 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001760 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001761 if (IS_ERR(dst))
1762 goto purge_queue;
1763
1764 if (dst->flags & DST_XFRM_QUEUE) {
1765 dst_release(dst);
1766
1767 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1768 goto purge_queue;
1769
1770 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001771 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1772 xfrm_pol_hold(pol);
1773 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001774 }
1775
1776 dst_release(dst);
1777
1778 __skb_queue_head_init(&list);
1779
1780 spin_lock(&pq->hold_queue.lock);
1781 pq->timeout = 0;
1782 skb_queue_splice_init(&pq->hold_queue, &list);
1783 spin_unlock(&pq->hold_queue.lock);
1784
1785 while (!skb_queue_empty(&list)) {
1786 skb = __skb_dequeue(&list);
1787
1788 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1789 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001790 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001791 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001792 kfree_skb(skb);
1793 continue;
1794 }
1795
1796 nf_reset(skb);
1797 skb_dst_drop(skb);
1798 skb_dst_set(skb, dst);
1799
Eric W. Biederman13206b62015-10-07 16:48:35 -05001800 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001801 }
1802
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001803out:
1804 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001805 return;
1806
1807purge_queue:
1808 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001809 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001810 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001811}
1812
Eric W. Biedermanede20592015-10-07 16:48:47 -05001813static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001814{
1815 unsigned long sched_next;
1816 struct dst_entry *dst = skb_dst(skb);
1817 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001818 struct xfrm_policy *pol = xdst->pols[0];
1819 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001820
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001821 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001822 kfree_skb(skb);
1823 return 0;
1824 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001825
1826 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1827 kfree_skb(skb);
1828 return -EAGAIN;
1829 }
1830
1831 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001832
1833 spin_lock_bh(&pq->hold_queue.lock);
1834
1835 if (!pq->timeout)
1836 pq->timeout = XFRM_QUEUE_TMO_MIN;
1837
1838 sched_next = jiffies + pq->timeout;
1839
1840 if (del_timer(&pq->hold_timer)) {
1841 if (time_before(pq->hold_timer.expires, sched_next))
1842 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001843 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001844 }
1845
1846 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001847 if (!mod_timer(&pq->hold_timer, sched_next))
1848 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001849
1850 spin_unlock_bh(&pq->hold_queue.lock);
1851
1852 return 0;
1853}
1854
1855static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001856 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001857 const struct flowi *fl,
1858 int num_xfrms,
1859 u16 family)
1860{
1861 int err;
1862 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001863 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001864 struct dst_entry *dst1;
1865 struct xfrm_dst *xdst;
1866
1867 xdst = xfrm_alloc_dst(net, family);
1868 if (IS_ERR(xdst))
1869 return xdst;
1870
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001871 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
1872 net->xfrm.sysctl_larval_drop ||
1873 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001874 return xdst;
1875
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001876 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001877 dst1 = &xdst->u.dst;
1878 dst_hold(dst);
1879 xdst->route = dst;
1880
1881 dst_copy_metrics(dst1, dst);
1882
1883 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1884 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1885 dst1->lastuse = jiffies;
1886
1887 dst1->input = dst_discard;
1888 dst1->output = xdst_queue_output;
1889
1890 dst_hold(dst);
1891 dst1->child = dst;
1892 dst1->path = dst;
1893
1894 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1895
1896 err = -ENODEV;
1897 dev = dst->dev;
1898 if (!dev)
1899 goto free_dst;
1900
1901 err = xfrm_fill_dst(xdst, dev, fl);
1902 if (err)
1903 goto free_dst;
1904
1905out:
1906 return xdst;
1907
1908free_dst:
1909 dst_release(dst1);
1910 xdst = ERR_PTR(err);
1911 goto out;
1912}
1913
Florian Westphalbd45c532017-07-17 13:57:25 +02001914static struct xfrm_dst *
Florian Westphal3ca28282017-07-17 13:57:21 +02001915xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir, struct xfrm_flo *xflo)
Timo Teräs80c802f2010-04-07 00:30:05 +00001916{
Timo Teräs80c802f2010-04-07 00:30:05 +00001917 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Florian Westphal855dad92017-07-17 13:57:22 +02001918 int num_pols = 0, num_xfrms = 0, err;
Florian Westphalbd45c532017-07-17 13:57:25 +02001919 struct xfrm_dst *xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001920
Timo Teräs80c802f2010-04-07 00:30:05 +00001921 /* Resolve policies to use if we couldn't get them from
1922 * previous cache entry */
Florian Westphal855dad92017-07-17 13:57:22 +02001923 num_pols = 1;
Florian Westphal86dc8ee2017-07-17 13:57:24 +02001924 pols[0] = xfrm_policy_lookup(net, fl, family, dir);
Florian Westphal855dad92017-07-17 13:57:22 +02001925 err = xfrm_expand_policies(fl, family, pols,
Timo Teräs80c802f2010-04-07 00:30:05 +00001926 &num_pols, &num_xfrms);
Florian Westphal855dad92017-07-17 13:57:22 +02001927 if (err < 0)
1928 goto inc_error;
1929 if (num_pols == 0)
1930 return NULL;
1931 if (num_xfrms <= 0)
1932 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00001933
Florian Westphalbd45c532017-07-17 13:57:25 +02001934 xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001935 xflo->dst_orig);
Florian Westphalbd45c532017-07-17 13:57:25 +02001936 if (IS_ERR(xdst)) {
1937 err = PTR_ERR(xdst);
Timo Teräs80c802f2010-04-07 00:30:05 +00001938 if (err != -EAGAIN)
1939 goto error;
Florian Westphal855dad92017-07-17 13:57:22 +02001940 goto make_dummy_bundle;
Florian Westphalbd45c532017-07-17 13:57:25 +02001941 } else if (xdst == NULL) {
Timo Teräsd809ec82010-07-12 21:29:42 +00001942 num_xfrms = 0;
Florian Westphal855dad92017-07-17 13:57:22 +02001943 goto make_dummy_bundle;
Timo Teräs80c802f2010-04-07 00:30:05 +00001944 }
1945
Florian Westphalbd45c532017-07-17 13:57:25 +02001946 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001947
1948make_dummy_bundle:
1949 /* We found policies, but there's no bundles to instantiate:
1950 * either because the policy blocks, has no transformations or
1951 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001952 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00001953 if (IS_ERR(xdst)) {
1954 xfrm_pols_put(pols, num_pols);
1955 return ERR_CAST(xdst);
1956 }
1957 xdst->num_pols = num_pols;
1958 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001959 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001960
1961 dst_hold(&xdst->u.dst);
Florian Westphalbd45c532017-07-17 13:57:25 +02001962 return xdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001963
1964inc_error:
1965 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1966error:
Florian Westphal855dad92017-07-17 13:57:22 +02001967 xfrm_pols_put(pols, num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001968 return ERR_PTR(err);
1969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
David S. Miller2774c132011-03-01 14:59:04 -08001971static struct dst_entry *make_blackhole(struct net *net, u16 family,
1972 struct dst_entry *dst_orig)
1973{
Florian Westphal37b10382017-02-07 15:00:19 +01001974 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
David S. Miller2774c132011-03-01 14:59:04 -08001975 struct dst_entry *ret;
1976
1977 if (!afinfo) {
1978 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00001979 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08001980 } else {
1981 ret = afinfo->blackhole_route(net, dst_orig);
1982 }
Florian Westphalbdba9fe2017-02-07 15:00:18 +01001983 rcu_read_unlock();
David S. Miller2774c132011-03-01 14:59:04 -08001984
1985 return ret;
1986}
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988/* Main function: finds/creates a bundle for given flow.
1989 *
1990 * At the moment we eat a raw IP route. Mostly to speed up lookups
1991 * on interfaces with disabled IPsec.
1992 */
David S. Miller452edd52011-03-02 13:27:41 -08001993struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
1994 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001995 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001997 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00001998 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08001999 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002000 u16 family = dst_orig->ops->family;
Florian Westphalaff669b2017-07-17 13:57:23 +02002001 u8 dir = XFRM_POLICY_OUT;
Changli Gao4b021622010-04-27 21:20:22 +00002002 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002003
Timo Teräs80c802f2010-04-07 00:30:05 +00002004 dst = NULL;
2005 xdst = NULL;
2006 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002007
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002008 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002009 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002010 num_pols = 1;
Steffen Klassert4c86d772017-02-14 07:43:56 +01002011 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002012 err = xfrm_expand_policies(fl, family, pols,
2013 &num_pols, &num_xfrms);
2014 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002015 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002016
2017 if (num_pols) {
2018 if (num_xfrms <= 0) {
2019 drop_pols = num_pols;
2020 goto no_transform;
2021 }
2022
2023 xdst = xfrm_resolve_and_create_bundle(
2024 pols, num_pols, fl,
2025 family, dst_orig);
2026 if (IS_ERR(xdst)) {
2027 xfrm_pols_put(pols, num_pols);
2028 err = PTR_ERR(xdst);
2029 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002030 } else if (xdst == NULL) {
2031 num_xfrms = 0;
2032 drop_pols = num_pols;
2033 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002034 }
2035
Steffen Klassertb7eea452014-06-18 12:34:21 +02002036 dst_hold(&xdst->u.dst);
Timo Teräs80c802f2010-04-07 00:30:05 +00002037 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002038 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Timo Teräs80c802f2010-04-07 00:30:05 +00002041 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002042 struct xfrm_flo xflo;
2043
2044 xflo.dst_orig = dst_orig;
2045 xflo.flags = flags;
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002048 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002049 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002050 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Florian Westphalbd45c532017-07-17 13:57:25 +02002052 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo);
2053 if (xdst == NULL)
Timo Teräs80c802f2010-04-07 00:30:05 +00002054 goto nopol;
Florian Westphalbd45c532017-07-17 13:57:25 +02002055 if (IS_ERR(xdst)) {
2056 err = PTR_ERR(xdst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002057 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002058 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002059
2060 num_pols = xdst->num_pols;
2061 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002062 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002063 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
Timo Teräs80c802f2010-04-07 00:30:05 +00002066 dst = &xdst->u.dst;
2067 if (route == NULL && num_xfrms > 0) {
2068 /* The only case when xfrm_bundle_lookup() returns a
2069 * bundle with null route, is when the template could
2070 * not be resolved. It means policies are there, but
2071 * bundle could not be created, since we don't yet
2072 * have the xfrm_state's. We need to wait for KM to
2073 * negotiate new SA's or bail out with error.*/
2074 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002075 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002076 err = -EREMOTE;
2077 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002078 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002079
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002080 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002081
2082 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2083 goto error;
2084 }
2085
2086no_transform:
2087 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002088 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Timo Teräs80c802f2010-04-07 00:30:05 +00002090 if ((flags & XFRM_LOOKUP_ICMP) &&
2091 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2092 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002093 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002094 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002095
Timo Teräs80c802f2010-04-07 00:30:05 +00002096 for (i = 0; i < num_pols; i++)
2097 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002098
Timo Teräs80c802f2010-04-07 00:30:05 +00002099 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002101 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002102 err = -EPERM;
2103 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002104 } else if (num_xfrms > 0) {
2105 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002106 dst_release(dst_orig);
2107 } else {
2108 /* Flow passes untransformed */
2109 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002110 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002112ok:
2113 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002114 if (dst && dst->xfrm &&
2115 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2116 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002117 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Timo Teräs80c802f2010-04-07 00:30:05 +00002119nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002120 if (!(flags & XFRM_LOOKUP_ICMP)) {
2121 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002122 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002123 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002124 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002126 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002127dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002128 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2129 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002130 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002131 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133EXPORT_SYMBOL(xfrm_lookup);
2134
Steffen Klassertf92ee612014-09-16 10:08:40 +02002135/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2136 * Otherwise we may send out blackholed packets.
2137 */
2138struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2139 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002140 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002141{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002142 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002143 flags | XFRM_LOOKUP_QUEUE |
2144 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002145
2146 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2147 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2148
2149 return dst;
2150}
2151EXPORT_SYMBOL(xfrm_lookup_route);
2152
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002153static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002154xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002155{
2156 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002157
2158 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2159 return 0;
2160 x = skb->sp->xvec[idx];
2161 if (!x->type->reject)
2162 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002163 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/* When skb is transformed back to its "native" form, we have to
2167 * check policy restrictions. At the moment we make this in maximally
2168 * stupid way. Shame on me. :-) Of course, connected sockets must
2169 * have policy cached at them.
2170 */
2171
2172static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002173xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 unsigned short family)
2175{
2176 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002177 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return x->id.proto == tmpl->id.proto &&
2179 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2180 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2181 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002182 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002183 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002184 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2185 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002188/*
2189 * 0 or more than 0 is returned when validation is succeeded (either bypass
2190 * because of optional transport mode, or next index of the mathced secpath
2191 * state with the template.
2192 * -1 is returned when no matching template is found.
2193 * Otherwise "-2 - errored_index" is returned.
2194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002196xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 unsigned short family)
2198{
2199 int idx = start;
2200
2201 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002202 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return start;
2204 } else
2205 start = -1;
2206 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002207 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002209 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2210 if (start == -1)
2211 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 return start;
2216}
2217
Herbert Xud5422ef2007-12-12 10:44:16 -08002218int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2219 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Florian Westphal37b10382017-02-07 15:00:19 +01002221 const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002222 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (unlikely(afinfo == NULL))
2225 return -EAFNOSUPPORT;
2226
Herbert Xud5422ef2007-12-12 10:44:16 -08002227 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002228 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Florian Westphalbdba9fe2017-02-07 15:00:18 +01002229 rcu_read_unlock();
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002230 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
Herbert Xud5422ef2007-12-12 10:44:16 -08002232EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
David S. Miller9a7386e2011-02-24 01:44:12 -05002234static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002237 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002238 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242
2243 return 0;
2244}
2245
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002246int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 unsigned short family)
2248{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002249 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002251 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2252 int npols = 0;
2253 int xfrm_nr;
2254 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002255 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct flowi fl;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002257 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Herbert Xud5422ef2007-12-12 10:44:16 -08002259 reverse = dir & ~XFRM_POLICY_MASK;
2260 dir &= XFRM_POLICY_MASK;
Herbert Xud5422ef2007-12-12 10:44:16 -08002261
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002262 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002263 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002265 }
2266
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002267 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 /* First, check used SA against their selectors. */
2270 if (skb->sp) {
2271 int i;
2272
Weilong Chen9b7a7872013-12-24 09:43:46 +08002273 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002274 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002275 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002276 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 }
2281
2282 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002283 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002284 if (sk && sk->sk_policy[dir]) {
Steffen Klassert4c86d772017-02-14 07:43:56 +01002285 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002286 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002287 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002288 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002289 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Florian Westphal86dc8ee2017-07-17 13:57:24 +02002292 if (!pol)
2293 pol = xfrm_policy_lookup(net, &fl, family, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002295 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002296 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002297 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002298 }
James Morris134b0fc2006-10-05 15:42:27 -05002299
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002300 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002301 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002302 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002303 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002304 return 0;
2305 }
2306 return 1;
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
James Morris9d729f72007-03-04 16:12:44 -08002309 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002311 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002312 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002313#ifdef CONFIG_XFRM_SUB_POLICY
2314 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002315 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002316 &fl, family,
2317 XFRM_POLICY_IN);
2318 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002319 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002320 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002321 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002322 }
James Morris9d729f72007-03-04 16:12:44 -08002323 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002324 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002325 }
2326 }
2327#endif
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (pol->action == XFRM_POLICY_ALLOW) {
2330 struct sec_path *sp;
2331 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002332 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002333 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002334 struct xfrm_tmpl **tpp = tp;
2335 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int i, k;
2337
2338 if ((sp = skb->sp) == NULL)
2339 sp = &dummy;
2340
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002341 for (pi = 0; pi < npols; pi++) {
2342 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002343 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002344 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002345 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002346 }
2347 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002348 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002349 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002350 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002351 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2352 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2353 }
2354 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002355 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002356 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002357 tpp = stp;
2358 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* For each tunnel xfrm, find the first matching tmpl.
2361 * For each tmpl before that, find corresponding xfrm.
2362 * Order is _important_. Later we will implement
2363 * some barriers, but at the moment barriers
2364 * are implied between each two transformations.
2365 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002366 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2367 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002368 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002369 if (k < -1)
2370 /* "-2 - errored_index" returned */
2371 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002372 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002377 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002378 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002382 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return 1;
2384 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002385 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002388 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002389reject_error:
2390 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return 0;
2392}
2393EXPORT_SYMBOL(__xfrm_policy_check);
2394
2395int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2396{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002397 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002399 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002400 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002402 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002403 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002407 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002408
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002409 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002410 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002411 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002412 dst = NULL;
2413 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002414 skb_dst_set(skb, dst);
2415 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417EXPORT_SYMBOL(__xfrm_route_forward);
2418
David S. Millerd49c73c2006-08-13 18:55:53 -07002419/* Optimize later using cookies and generation ids. */
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2422{
David S. Millerd49c73c2006-08-13 18:55:53 -07002423 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002424 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2425 * get validated by dst_ops->check on every use. We do this
2426 * because when a normal route referenced by an XFRM dst is
2427 * obsoleted we do not go looking around for all parent
2428 * referencing XFRM dsts so that we can invalidate them. It
2429 * is just too much work. Instead we make the checks here on
2430 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002431 *
2432 * XFRM dst A --> IPv4 dst X
2433 *
2434 * X is the "xdst->route" of A (X is also the "dst->path" of A
2435 * in this example). If X is marked obsolete, "A" will not
2436 * notice. That's what we are validating here via the
2437 * stale_bundle() check.
2438 *
Wei Wang52df1572017-06-17 10:42:38 -07002439 * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
2440 * be marked on it.
Florian Westphal09c75702017-07-17 13:57:26 +02002441 * This will force stale_bundle() to fail on any xdst bundle with
Wei Wang52df1572017-06-17 10:42:38 -07002442 * this dst linked in it.
David S. Miller399c1802005-12-19 14:23:23 -08002443 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002444 if (dst->obsolete < 0 && !stale_bundle(dst))
2445 return dst;
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return NULL;
2448}
2449
2450static int stale_bundle(struct dst_entry *dst)
2451{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002452 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Herbert Xuaabc9762005-05-03 16:27:10 -07002455void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002458 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002459 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 dev_put(dev);
2461 }
2462}
Herbert Xuaabc9762005-05-03 16:27:10 -07002463EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465static void xfrm_link_failure(struct sk_buff *skb)
2466{
2467 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
2470static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2471{
2472 if (dst) {
2473 if (dst->obsolete) {
2474 dst_release(dst);
2475 dst = NULL;
2476 }
2477 }
2478 return dst;
2479}
2480
Herbert Xu25ee3282007-12-11 09:32:34 -08002481static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 do {
2484 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2485 u32 pmtu, route_mtu_cached;
2486
2487 pmtu = dst_mtu(dst->child);
2488 xdst->child_mtu_cached = pmtu;
2489
2490 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2491
2492 route_mtu_cached = dst_mtu(xdst->route);
2493 xdst->route_mtu_cached = route_mtu_cached;
2494
2495 if (pmtu > route_mtu_cached)
2496 pmtu = route_mtu_cached;
2497
David S. Millerdefb3512010-12-08 21:16:57 -08002498 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 } while ((dst = dst->next));
2500}
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502/* Check that the bundle accepts the flow and its components are
2503 * still valid.
2504 */
2505
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002506static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct dst_entry *dst = &first->u.dst;
2509 struct xfrm_dst *last;
2510 u32 mtu;
2511
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002512 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 (dst->dev && !netif_running(dst->dev)))
2514 return 0;
2515
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002516 if (dst->flags & DST_XFRM_QUEUE)
2517 return 1;
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 last = NULL;
2520
2521 do {
2522 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2525 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002526 if (xdst->xfrm_genid != dst->xfrm->genid)
2527 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002528 if (xdst->num_pols > 0 &&
2529 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 mtu = dst_mtu(dst->child);
2533 if (xdst->child_mtu_cached != mtu) {
2534 last = xdst;
2535 xdst->child_mtu_cached = mtu;
2536 }
2537
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002538 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 0;
2540 mtu = dst_mtu(xdst->route);
2541 if (xdst->route_mtu_cached != mtu) {
2542 last = xdst;
2543 xdst->route_mtu_cached = mtu;
2544 }
2545
2546 dst = dst->child;
2547 } while (dst->xfrm);
2548
2549 if (likely(!last))
2550 return 1;
2551
2552 mtu = last->child_mtu_cached;
2553 for (;;) {
2554 dst = &last->u.dst;
2555
2556 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2557 if (mtu > last->route_mtu_cached)
2558 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002559 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 if (last == first)
2562 break;
2563
Patrick McHardybd0bf072007-07-18 01:55:52 -07002564 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 last->child_mtu_cached = mtu;
2566 }
2567
2568 return 1;
2569}
2570
David S. Miller0dbaee32010-12-13 12:52:14 -08002571static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2572{
2573 return dst_metric_advmss(dst->path);
2574}
2575
Steffen Klassertebb762f2011-11-23 02:12:51 +00002576static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002577{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002578 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2579
2580 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002581}
2582
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002583static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
2584 const void *daddr)
Julian Anastasov63fca652017-02-06 23:14:15 +02002585{
2586 const struct dst_entry *path = dst->path;
2587
2588 for (; dst != path; dst = dst->child) {
2589 const struct xfrm_state *xfrm = dst->xfrm;
2590
2591 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
2592 continue;
2593 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
2594 daddr = xfrm->coaddr;
2595 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
2596 daddr = &xfrm->id.daddr;
2597 }
Julian Anastasov1ecc9ad2017-02-25 17:57:43 +02002598 return daddr;
2599}
2600
2601static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2602 struct sk_buff *skb,
2603 const void *daddr)
2604{
2605 const struct dst_entry *path = dst->path;
2606
2607 if (!skb)
2608 daddr = xfrm_get_dst_nexthop(dst, daddr);
2609 return path->ops->neigh_lookup(path, skb, daddr);
2610}
2611
2612static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
2613{
2614 const struct dst_entry *path = dst->path;
2615
2616 daddr = xfrm_get_dst_nexthop(dst, daddr);
Julian Anastasov63fca652017-02-06 23:14:15 +02002617 path->ops->confirm_neigh(path, daddr);
2618}
2619
Florian Westphala2817d82017-02-07 15:00:17 +01002620int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 int err = 0;
Florian Westphala2817d82017-02-07 15:00:17 +01002623
2624 if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return -EAFNOSUPPORT;
Florian Westphala2817d82017-02-07 15:00:17 +01002626
Eric Dumazetef8531b2012-08-19 12:31:48 +02002627 spin_lock(&xfrm_policy_afinfo_lock);
Florian Westphala2817d82017-02-07 15:00:17 +01002628 if (unlikely(xfrm_policy_afinfo[family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002629 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 else {
2631 struct dst_ops *dst_ops = afinfo->dst_ops;
2632 if (likely(dst_ops->kmem_cachep == NULL))
2633 dst_ops->kmem_cachep = xfrm_dst_cache;
2634 if (likely(dst_ops->check == NULL))
2635 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002636 if (likely(dst_ops->default_advmss == NULL))
2637 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002638 if (likely(dst_ops->mtu == NULL))
2639 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (likely(dst_ops->negative_advice == NULL))
2641 dst_ops->negative_advice = xfrm_negative_advice;
2642 if (likely(dst_ops->link_failure == NULL))
2643 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002644 if (likely(dst_ops->neigh_lookup == NULL))
2645 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Julian Anastasov63fca652017-02-06 23:14:15 +02002646 if (likely(!dst_ops->confirm_neigh))
2647 dst_ops->confirm_neigh = xfrm_confirm_neigh;
Florian Westphala2817d82017-02-07 15:00:17 +01002648 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002650 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return err;
2653}
2654EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2655
Florian Westphala2817d82017-02-07 15:00:17 +01002656void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Florian Westphal2b619972017-02-07 15:00:15 +01002658 struct dst_ops *dst_ops = afinfo->dst_ops;
Florian Westphala2817d82017-02-07 15:00:17 +01002659 int i;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002660
Florian Westphala2817d82017-02-07 15:00:17 +01002661 for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
2662 if (xfrm_policy_afinfo[i] != afinfo)
2663 continue;
2664 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
2665 break;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Florian Westphal2b619972017-02-07 15:00:15 +01002668 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Florian Westphal2b619972017-02-07 15:00:15 +01002670 dst_ops->kmem_cachep = NULL;
2671 dst_ops->check = NULL;
2672 dst_ops->negative_advice = NULL;
2673 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2676
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002677#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002678static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002679{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002680 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002681 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2682 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002683 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002684 rv = xfrm_proc_init(net);
2685 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002686 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002687 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002688}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002689
2690static void xfrm_statistics_fini(struct net *net)
2691{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002692 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002693 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002694}
2695#else
2696static int __net_init xfrm_statistics_init(struct net *net)
2697{
2698 return 0;
2699}
2700
2701static void xfrm_statistics_fini(struct net *net)
2702{
2703}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002704#endif
2705
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002706static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
David S. Miller2518c7c2006-08-24 04:45:07 -07002708 unsigned int hmask, sz;
2709 int dir;
2710
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002711 if (net_eq(net, &init_net))
2712 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002714 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002715 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
David S. Miller2518c7c2006-08-24 04:45:07 -07002717 hmask = 8 - 1;
2718 sz = (hmask+1) * sizeof(struct hlist_head);
2719
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002720 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2721 if (!net->xfrm.policy_byidx)
2722 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002723 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002724
Herbert Xu53c2e282014-11-13 17:09:49 +08002725 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002726 struct xfrm_policy_hash *htab;
2727
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002728 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002729 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002730 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002731
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002732 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002733 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002734 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002735 goto out_bydst;
2736 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002737 htab->dbits4 = 32;
2738 htab->sbits4 = 32;
2739 htab->dbits6 = 128;
2740 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002741 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002742 net->xfrm.policy_hthresh.lbits4 = 32;
2743 net->xfrm.policy_hthresh.rbits4 = 32;
2744 net->xfrm.policy_hthresh.lbits6 = 128;
2745 net->xfrm.policy_hthresh.rbits6 = 128;
2746
2747 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002748
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002749 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002750 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002751 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002752 if (net_eq(net, &init_net))
Steffen Klassert21f42cc2017-04-14 10:05:53 +02002753 xfrm_dev_init();
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002754 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002755
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002756out_bydst:
2757 for (dir--; dir >= 0; dir--) {
2758 struct xfrm_policy_hash *htab;
2759
2760 htab = &net->xfrm.policy_bydst[dir];
2761 xfrm_hash_free(htab->table, sz);
2762 }
2763 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002764out_byidx:
2765 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002768static void xfrm_policy_fini(struct net *net)
2769{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002770 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002771 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002772
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002773 flush_work(&net->xfrm.policy_hash_work);
2774#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09002775 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002776#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09002777 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002778
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002779 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002780
Herbert Xu53c2e282014-11-13 17:09:49 +08002781 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002782 struct xfrm_policy_hash *htab;
2783
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002784 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002785
2786 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002787 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002788 WARN_ON(!hlist_empty(htab->table));
2789 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002790 }
2791
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002792 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002793 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2794 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002795}
2796
2797static int __net_init xfrm_net_init(struct net *net)
2798{
2799 int rv;
2800
Florian Westphalc2822222017-02-08 11:52:29 +01002801 /* Initialize the per-net locks here */
2802 spin_lock_init(&net->xfrm.xfrm_state_lock);
2803 spin_lock_init(&net->xfrm.xfrm_policy_lock);
2804 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2805
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002806 rv = xfrm_statistics_init(net);
2807 if (rv < 0)
2808 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002809 rv = xfrm_state_init(net);
2810 if (rv < 0)
2811 goto out_state;
2812 rv = xfrm_policy_init(net);
2813 if (rv < 0)
2814 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002815 rv = xfrm_sysctl_init(net);
2816 if (rv < 0)
2817 goto out_sysctl;
Fan Du283bc9f2013-11-07 17:47:50 +08002818
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002819 return 0;
2820
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002821out_sysctl:
2822 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002823out_policy:
2824 xfrm_state_fini(net);
2825out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002826 xfrm_statistics_fini(net);
2827out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002828 return rv;
2829}
2830
2831static void __net_exit xfrm_net_exit(struct net *net)
2832{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002833 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002834 xfrm_policy_fini(net);
2835 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002836 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002837}
2838
2839static struct pernet_operations __net_initdata xfrm_net_ops = {
2840 .init = xfrm_net_init,
2841 .exit = xfrm_net_exit,
2842};
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844void __init xfrm_init(void)
2845{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002846 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02002847 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 xfrm_input_init();
2849}
2850
Joy Lattenab5f5e82007-09-17 11:51:22 -07002851#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002852static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2853 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002854{
Paul Moore875179f2007-12-01 23:27:18 +11002855 struct xfrm_sec_ctx *ctx = xp->security;
2856 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002857
Paul Moore875179f2007-12-01 23:27:18 +11002858 if (ctx)
2859 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2860 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2861
Weilong Chen9b7a7872013-12-24 09:43:46 +08002862 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002863 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002864 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002865 if (sel->prefixlen_s != 32)
2866 audit_log_format(audit_buf, " src_prefixlen=%d",
2867 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002868 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002869 if (sel->prefixlen_d != 32)
2870 audit_log_format(audit_buf, " dst_prefixlen=%d",
2871 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002872 break;
2873 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002874 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002875 if (sel->prefixlen_s != 128)
2876 audit_log_format(audit_buf, " src_prefixlen=%d",
2877 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002878 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002879 if (sel->prefixlen_d != 128)
2880 audit_log_format(audit_buf, " dst_prefixlen=%d",
2881 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002882 break;
2883 }
2884}
2885
Tetsuo Handa2e710292014-04-22 21:48:30 +09002886void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002887{
2888 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002889
Paul Mooreafeb14b2007-12-21 14:58:11 -08002890 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002891 if (audit_buf == NULL)
2892 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002893 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002894 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002895 xfrm_audit_common_policyinfo(xp, audit_buf);
2896 audit_log_end(audit_buf);
2897}
2898EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2899
Paul Moore68277ac2007-12-20 20:49:33 -08002900void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09002901 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002902{
2903 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002904
Paul Mooreafeb14b2007-12-21 14:58:11 -08002905 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002906 if (audit_buf == NULL)
2907 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002908 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002909 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002910 xfrm_audit_common_policyinfo(xp, audit_buf);
2911 audit_log_end(audit_buf);
2912}
2913EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2914#endif
2915
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002916#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04002917static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
2918 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002919{
2920 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2921 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00002922 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
2923 sel_cmp->family) &&
2924 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
2925 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002926 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2927 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04002928 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002929 }
2930 } else {
2931 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04002932 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002933 }
2934 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04002935 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002936}
2937
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002938static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
2939 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002940{
2941 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002942 struct hlist_head *chain;
2943 u32 priority = ~0U;
2944
Florian Westphal9d0380d2016-08-11 15:17:59 +02002945 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08002946 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002947 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002948 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2949 pol->type == type) {
2950 ret = pol;
2951 priority = ret->priority;
2952 break;
2953 }
2954 }
Fan Du8d549c42013-11-07 17:47:49 +08002955 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08002956 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08002957 if ((pol->priority >= priority) && ret)
2958 break;
2959
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002960 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08002961 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002962 ret = pol;
2963 break;
2964 }
2965 }
2966
Li RongQing586f2eb2015-04-30 17:13:41 +08002967 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002968
Florian Westphal9d0380d2016-08-11 15:17:59 +02002969 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002970
2971 return ret;
2972}
2973
David S. Millerdd701752011-02-24 00:21:08 -05002974static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002975{
2976 int match = 0;
2977
2978 if (t->mode == m->mode && t->id.proto == m->proto &&
2979 (m->reqid == 0 || t->reqid == m->reqid)) {
2980 switch (t->mode) {
2981 case XFRM_MODE_TUNNEL:
2982 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00002983 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
2984 m->old_family) &&
2985 xfrm_addr_equal(&t->saddr, &m->old_saddr,
2986 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002987 match = 1;
2988 }
2989 break;
2990 case XFRM_MODE_TRANSPORT:
2991 /* in case of transport mode, template does not store
2992 any IP addresses, hence we just compare mode and
2993 protocol */
2994 match = 1;
2995 break;
2996 default:
2997 break;
2998 }
2999 }
3000 return match;
3001}
3002
3003/* update endpoint address(es) of template(s) */
3004static int xfrm_policy_migrate(struct xfrm_policy *pol,
3005 struct xfrm_migrate *m, int num_migrate)
3006{
3007 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003008 int i, j, n = 0;
3009
3010 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003011 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003012 /* target policy has been deleted */
3013 write_unlock_bh(&pol->lock);
3014 return -ENOENT;
3015 }
3016
3017 for (i = 0; i < pol->xfrm_nr; i++) {
3018 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3019 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3020 continue;
3021 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003022 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3023 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003024 continue;
3025 /* update endpoints */
3026 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3027 sizeof(pol->xfrm_vec[i].id.daddr));
3028 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3029 sizeof(pol->xfrm_vec[i].saddr));
3030 pol->xfrm_vec[i].encap_family = mp->new_family;
3031 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003032 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003033 }
3034 }
3035
3036 write_unlock_bh(&pol->lock);
3037
3038 if (!n)
3039 return -ENODATA;
3040
3041 return 0;
3042}
3043
David S. Millerdd701752011-02-24 00:21:08 -05003044static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003045{
3046 int i, j;
3047
3048 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3049 return -EINVAL;
3050
3051 for (i = 0; i < num_migrate; i++) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003052 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3053 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3054 return -EINVAL;
3055
3056 /* check if there is any duplicated entry */
3057 for (j = i + 1; j < num_migrate; j++) {
3058 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3059 sizeof(m[i].old_daddr)) &&
3060 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3061 sizeof(m[i].old_saddr)) &&
3062 m[i].proto == m[j].proto &&
3063 m[i].mode == m[j].mode &&
3064 m[i].reqid == m[j].reqid &&
3065 m[i].old_family == m[j].old_family)
3066 return -EINVAL;
3067 }
3068 }
3069
3070 return 0;
3071}
3072
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003073int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003074 struct xfrm_migrate *m, int num_migrate,
Antony Antony4ab47d42017-06-06 12:12:13 +02003075 struct xfrm_kmaddress *k, struct net *net,
3076 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003077{
3078 int i, err, nx_cur = 0, nx_new = 0;
3079 struct xfrm_policy *pol = NULL;
3080 struct xfrm_state *x, *xc;
3081 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3082 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3083 struct xfrm_migrate *mp;
3084
3085 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3086 goto out;
3087
3088 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003089 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003090 err = -ENOENT;
3091 goto out;
3092 }
3093
3094 /* Stage 2 - find and update state(s) */
3095 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003096 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003097 x_cur[nx_cur] = x;
3098 nx_cur++;
Antony Antony4ab47d42017-06-06 12:12:13 +02003099 xc = xfrm_state_migrate(x, mp, encap);
3100 if (xc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003101 x_new[nx_new] = xc;
3102 nx_new++;
3103 } else {
3104 err = -ENODATA;
3105 goto restore_state;
3106 }
3107 }
3108 }
3109
3110 /* Stage 3 - update policy */
3111 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3112 goto restore_state;
3113
3114 /* Stage 4 - delete old state(s) */
3115 if (nx_cur) {
3116 xfrm_states_put(x_cur, nx_cur);
3117 xfrm_states_delete(x_cur, nx_cur);
3118 }
3119
3120 /* Stage 5 - announce */
Antony Antony8bafd732017-06-06 12:12:14 +02003121 km_migrate(sel, dir, type, m, num_migrate, k, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003122
3123 xfrm_pol_put(pol);
3124
3125 return 0;
3126out:
3127 return err;
3128
3129restore_state:
3130 if (pol)
3131 xfrm_pol_put(pol);
3132 if (nx_cur)
3133 xfrm_states_put(x_cur, nx_cur);
3134 if (nx_new)
3135 xfrm_states_delete(x_new, nx_new);
3136
3137 return err;
3138}
David S. Millere610e672007-02-08 13:29:15 -08003139EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003140#endif