blob: 155b1591b17a205eb1d9c6acd7eef6e14a2b799e [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);
48static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __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{
65 return atomic_inc_not_zero(&policy->refcnt);
66}
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
Eric Dumazetef8531b2012-08-19 12:31:48 +0200106static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
107{
108 struct xfrm_policy_afinfo *afinfo;
109
110 if (unlikely(family >= NPROTO))
111 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
119static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
120{
121 rcu_read_unlock();
122}
123
David Ahern42a7b322015-08-10 16:58:11 -0600124static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
125 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500126 const xfrm_address_t *saddr,
127 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900128 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800130 struct xfrm_policy_afinfo *afinfo;
131 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Herbert Xu25ee3282007-12-11 09:32:34 -0800133 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800135 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David Ahern42a7b322015-08-10 16:58:11 -0600137 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140
141 return dst;
142}
143
David Ahern42a7b322015-08-10 16:58:11 -0600144static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
145 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900146 xfrm_address_t *prev_saddr,
147 xfrm_address_t *prev_daddr,
148 int family)
149{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800150 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900151 xfrm_address_t *saddr = &x->props.saddr;
152 xfrm_address_t *daddr = &x->id.daddr;
153 struct dst_entry *dst;
154
155 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
156 saddr = x->coaddr;
157 daddr = prev_daddr;
158 }
159 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
160 saddr = prev_saddr;
161 daddr = x->coaddr;
162 }
163
David Ahern42a7b322015-08-10 16:58:11 -0600164 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900165
166 if (!IS_ERR(dst)) {
167 if (prev_saddr != saddr)
168 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
169 if (prev_daddr != daddr)
170 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
171 }
172
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800173 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static inline unsigned long make_jiffies(long secs)
177{
178 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
179 return MAX_SCHEDULE_TIMEOUT-1;
180 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900181 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static void xfrm_policy_timer(unsigned long data)
185{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800186 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800187 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 long next = LONG_MAX;
189 int warn = 0;
190 int dir;
191
192 read_lock(&xp->lock);
193
Timo Teräsea2dea92010-03-31 00:17:05 +0000194 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700197 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (xp->lft.hard_add_expires_seconds) {
200 long tmo = xp->lft.hard_add_expires_seconds +
201 xp->curlft.add_time - now;
202 if (tmo <= 0)
203 goto expired;
204 if (tmo < next)
205 next = tmo;
206 }
207 if (xp->lft.hard_use_expires_seconds) {
208 long tmo = xp->lft.hard_use_expires_seconds +
209 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
210 if (tmo <= 0)
211 goto expired;
212 if (tmo < next)
213 next = tmo;
214 }
215 if (xp->lft.soft_add_expires_seconds) {
216 long tmo = xp->lft.soft_add_expires_seconds +
217 xp->curlft.add_time - now;
218 if (tmo <= 0) {
219 warn = 1;
220 tmo = XFRM_KM_TIMEOUT;
221 }
222 if (tmo < next)
223 next = tmo;
224 }
225 if (xp->lft.soft_use_expires_seconds) {
226 long tmo = xp->lft.soft_use_expires_seconds +
227 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
228 if (tmo <= 0) {
229 warn = 1;
230 tmo = XFRM_KM_TIMEOUT;
231 }
232 if (tmo < next)
233 next = tmo;
234 }
235
236 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800237 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (next != LONG_MAX &&
239 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
240 xfrm_pol_hold(xp);
241
242out:
243 read_unlock(&xp->lock);
244 xfrm_pol_put(xp);
245 return;
246
247expired:
248 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700249 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800250 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfrm_pol_put(xp);
252}
253
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000254static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
255{
256 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
257
258 if (unlikely(pol->walk.dead))
259 flo = NULL;
260 else
261 xfrm_pol_hold(pol);
262
263 return flo;
264}
265
266static int xfrm_policy_flo_check(struct flow_cache_object *flo)
267{
268 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
269
270 return !pol->walk.dead;
271}
272
273static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
274{
275 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
276}
277
278static const struct flow_cache_ops xfrm_policy_fc_ops = {
279 .get = xfrm_policy_flo_get,
280 .check = xfrm_policy_flo_check,
281 .delete = xfrm_policy_flo_delete,
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
285 * SPD calls.
286 */
287
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800288struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct xfrm_policy *policy;
291
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700292 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800295 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700296 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700297 INIT_HLIST_NODE(&policy->bydst);
298 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700300 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100301 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800302 setup_timer(&policy->timer, xfrm_policy_timer,
303 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100304 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
305 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000306 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 return policy;
309}
310EXPORT_SYMBOL(xfrm_policy_alloc);
311
Eric Dumazet56f04732015-12-08 07:22:01 -0800312static void xfrm_policy_destroy_rcu(struct rcu_head *head)
313{
314 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
315
316 security_xfrm_policy_free(policy->security);
317 kfree(policy);
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Destroy xfrm_policy: descendant resources must be released to this moment. */
321
WANG Cong64c31b32008-01-07 22:34:29 -0800322void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Herbert Xu12a169e2008-10-01 07:03:24 -0700324 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Fan Du0659eea2013-08-01 18:08:36 +0800326 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 BUG();
328
Eric Dumazet56f04732015-12-08 07:22:01 -0800329 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
WANG Cong64c31b32008-01-07 22:34:29 -0800331EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/* Rule must be locked. Release descentant resources, announce
334 * entry dead. The rule must be unlinked from lists to the moment.
335 */
336
337static void xfrm_policy_kill(struct xfrm_policy *policy)
338{
Herbert Xu12a169e2008-10-01 07:03:24 -0700339 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Timo Teräs285ead12010-04-07 00:30:06 +0000341 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200343 if (del_timer(&policy->polq.hold_timer))
344 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800345 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100346
Timo Teräs285ead12010-04-07 00:30:06 +0000347 if (del_timer(&policy->timer))
348 xfrm_pol_put(policy);
349
350 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
David S. Miller2518c7c2006-08-24 04:45:07 -0700353static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
354
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800355static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700356{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800357 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700358}
359
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200360/* calculate policy hash thresholds */
361static void __get_hash_thresh(struct net *net,
362 unsigned short family, int dir,
363 u8 *dbits, u8 *sbits)
364{
365 switch (family) {
366 case AF_INET:
367 *dbits = net->xfrm.policy_bydst[dir].dbits4;
368 *sbits = net->xfrm.policy_bydst[dir].sbits4;
369 break;
370
371 case AF_INET6:
372 *dbits = net->xfrm.policy_bydst[dir].dbits6;
373 *sbits = net->xfrm.policy_bydst[dir].sbits6;
374 break;
375
376 default:
377 *dbits = 0;
378 *sbits = 0;
379 }
380}
381
David S. Miller5f803b52011-02-24 00:33:19 -0500382static struct hlist_head *policy_hash_bysel(struct net *net,
383 const struct xfrm_selector *sel,
384 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700385{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800386 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200387 unsigned int hash;
388 u8 dbits;
389 u8 sbits;
390
391 __get_hash_thresh(net, family, dir, &dbits, &sbits);
392 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700393
Florian Westphale1e551b2016-08-11 15:17:53 +0200394 if (hash == hmask + 1)
395 return &net->xfrm.policy_inexact[dir];
396
397 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
398 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700399}
400
David S. Miller5f803b52011-02-24 00:33:19 -0500401static struct hlist_head *policy_hash_direct(struct net *net,
402 const xfrm_address_t *daddr,
403 const xfrm_address_t *saddr,
404 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700405{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800406 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200407 unsigned int hash;
408 u8 dbits;
409 u8 sbits;
410
411 __get_hash_thresh(net, family, dir, &dbits, &sbits);
412 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700413
Florian Westphale1e551b2016-08-11 15:17:53 +0200414 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
415 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416}
417
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418static void xfrm_dst_hash_transfer(struct net *net,
419 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200421 unsigned int nhashmask,
422 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700423{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800424 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700425 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800426 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200427 u8 dbits;
428 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700429
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800431 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700432 unsigned int h;
433
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200434 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700435 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200436 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200438 hlist_del_rcu(&pol->bydst);
439 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800440 h0 = h;
441 } else {
442 if (h != h0)
443 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200444 hlist_del_rcu(&pol->bydst);
445 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800446 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800447 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800448 }
449 if (!hlist_empty(list)) {
450 entry0 = NULL;
451 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700452 }
453}
454
455static void xfrm_idx_hash_transfer(struct hlist_head *list,
456 struct hlist_head *nidxtable,
457 unsigned int nhashmask)
458{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 struct xfrm_policy *pol;
461
Sasha Levinb67bfe02013-02-27 17:06:00 -0800462 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700463 unsigned int h;
464
465 h = __idx_hash(pol->index, nhashmask);
466 hlist_add_head(&pol->byidx, nidxtable+h);
467 }
468}
469
470static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
471{
472 return ((old_hmask + 1) << 1) - 1;
473}
474
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800475static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700476{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800477 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
479 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700480 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200481 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700482 int i;
483
484 if (!ndst)
485 return;
486
Florian Westphal9d0380d2016-08-11 15:17:59 +0200487 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200488 write_seqcount_begin(&xfrm_policy_hash_generation);
489
490 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
491 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700492
Florian Westphale1e551b2016-08-11 15:17:53 +0200493 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
494 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
495
David S. Miller2518c7c2006-08-24 04:45:07 -0700496 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200497 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498
Florian Westphale1e551b2016-08-11 15:17:53 +0200499 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800500 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700501
Florian Westphal30846092016-08-11 15:17:54 +0200502 write_seqcount_end(&xfrm_policy_hash_generation);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200503 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504
Florian Westphale1e551b2016-08-11 15:17:53 +0200505 synchronize_rcu();
506
David S. Miller44e36b42006-08-24 04:50:50 -0700507 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700508}
509
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700511{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800512 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700513 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
514 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800515 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700516 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700517 int i;
518
519 if (!nidx)
520 return;
521
Florian Westphal9d0380d2016-08-11 15:17:59 +0200522 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700523
524 for (i = hmask; i >= 0; i--)
525 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
526
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800527 net->xfrm.policy_byidx = nidx;
528 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700529
Florian Westphal9d0380d2016-08-11 15:17:59 +0200530 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700531
David S. Miller44e36b42006-08-24 04:50:50 -0700532 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700533}
534
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800535static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700536{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800537 unsigned int cnt = net->xfrm.policy_count[dir];
538 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700539
540 if (total)
541 *total += cnt;
542
543 if ((hmask + 1) < xfrm_policy_hashmax &&
544 cnt > hmask)
545 return 1;
546
547 return 0;
548}
549
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800550static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700551{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800552 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700553
554 if ((hmask + 1) < xfrm_policy_hashmax &&
555 total > hmask)
556 return 1;
557
558 return 0;
559}
560
Alexey Dobriyane0710412010-01-23 13:37:10 +0000561void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700562{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000563 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
564 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
565 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
566 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
567 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
568 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
569 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700570 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700571}
572EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700573
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700574static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800575static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700576{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800577 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700578 int dir, total;
579
580 mutex_lock(&hash_resize_mutex);
581
582 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800583 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800584 if (xfrm_bydst_should_resize(net, dir, &total))
585 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700586 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800587 if (xfrm_byidx_should_resize(net, total))
588 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700589
590 mutex_unlock(&hash_resize_mutex);
591}
592
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200593static void xfrm_hash_rebuild(struct work_struct *work)
594{
595 struct net *net = container_of(work, struct net,
596 xfrm.policy_hthresh.work);
597 unsigned int hmask;
598 struct xfrm_policy *pol;
599 struct xfrm_policy *policy;
600 struct hlist_head *chain;
601 struct hlist_head *odst;
602 struct hlist_node *newpos;
603 int i;
604 int dir;
605 unsigned seq;
606 u8 lbits4, rbits4, lbits6, rbits6;
607
608 mutex_lock(&hash_resize_mutex);
609
610 /* read selector prefixlen thresholds */
611 do {
612 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
613
614 lbits4 = net->xfrm.policy_hthresh.lbits4;
615 rbits4 = net->xfrm.policy_hthresh.rbits4;
616 lbits6 = net->xfrm.policy_hthresh.lbits6;
617 rbits6 = net->xfrm.policy_hthresh.rbits6;
618 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
619
Florian Westphal9d0380d2016-08-11 15:17:59 +0200620 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200621
622 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800623 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200624 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
625 hmask = net->xfrm.policy_bydst[dir].hmask;
626 odst = net->xfrm.policy_bydst[dir].table;
627 for (i = hmask; i >= 0; i--)
628 INIT_HLIST_HEAD(odst + i);
629 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
630 /* dir out => dst = remote, src = local */
631 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
632 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
633 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
634 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
635 } else {
636 /* dir in/fwd => dst = local, src = remote */
637 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
638 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
639 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
640 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
641 }
642 }
643
644 /* re-insert all policies by order of creation */
645 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Florian Westphal5d899172017-12-27 23:25:45 +0100646 if (policy->walk.dead ||
647 xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200648 /* skip socket policies */
649 continue;
650 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200651 newpos = NULL;
652 chain = policy_hash_bysel(net, &policy->selector,
653 policy->family,
654 xfrm_policy_id2dir(policy->index));
655 hlist_for_each_entry(pol, chain, bydst) {
656 if (policy->priority >= pol->priority)
657 newpos = &pol->bydst;
658 else
659 break;
660 }
661 if (newpos)
Florian Westphal9da255b2018-10-10 18:02:21 +0200662 hlist_add_behind_rcu(&policy->bydst, newpos);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200663 else
Florian Westphal9da255b2018-10-10 18:02:21 +0200664 hlist_add_head_rcu(&policy->bydst, chain);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200665 }
666
Florian Westphal9d0380d2016-08-11 15:17:59 +0200667 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200668
669 mutex_unlock(&hash_resize_mutex);
670}
671
672void xfrm_policy_hash_rebuild(struct net *net)
673{
674 schedule_work(&net->xfrm.policy_hthresh.work);
675}
676EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/* Generate new index... KAME seems to generate them ordered by cost
679 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800680static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 static u32 idx_generator;
683
684 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700685 struct hlist_head *list;
686 struct xfrm_policy *p;
687 u32 idx;
688 int found;
689
Fan Due682adf2013-11-07 17:47:48 +0800690 if (!index) {
691 idx = (idx_generator | dir);
692 idx_generator += 8;
693 } else {
694 idx = index;
695 index = 0;
696 }
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (idx == 0)
699 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800700 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800702 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 if (p->index == idx) {
704 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700708 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return idx;
710 }
711}
712
David S. Miller2518c7c2006-08-24 04:45:07 -0700713static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
714{
715 u32 *p1 = (u32 *) s1;
716 u32 *p2 = (u32 *) s2;
717 int len = sizeof(struct xfrm_selector) / sizeof(u32);
718 int i;
719
720 for (i = 0; i < len; i++) {
721 if (p1[i] != p2[i])
722 return 1;
723 }
724
725 return 0;
726}
727
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100728static void xfrm_policy_requeue(struct xfrm_policy *old,
729 struct xfrm_policy *new)
730{
731 struct xfrm_policy_queue *pq = &old->polq;
732 struct sk_buff_head list;
733
Li RongQingde2ad482015-04-30 17:25:19 +0800734 if (skb_queue_empty(&pq->hold_queue))
735 return;
736
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100737 __skb_queue_head_init(&list);
738
739 spin_lock_bh(&pq->hold_queue.lock);
740 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200741 if (del_timer(&pq->hold_timer))
742 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100743 spin_unlock_bh(&pq->hold_queue.lock);
744
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100745 pq = &new->polq;
746
747 spin_lock_bh(&pq->hold_queue.lock);
748 skb_queue_splice(&list, &pq->hold_queue);
749 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200750 if (!mod_timer(&pq->hold_timer, jiffies))
751 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100752 spin_unlock_bh(&pq->hold_queue.lock);
753}
754
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100755static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
756 struct xfrm_policy *pol)
757{
758 u32 mark = policy->mark.v & policy->mark.m;
759
760 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
761 return true;
762
763 if ((mark & pol->mark.m) == pol->mark.v &&
764 policy->priority == pol->priority)
765 return true;
766
767 return false;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
771{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800772 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700773 struct xfrm_policy *pol;
774 struct xfrm_policy *delpol;
775 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800776 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Florian Westphal9d0380d2016-08-11 15:17:59 +0200778 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800779 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700780 delpol = NULL;
781 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800782 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800783 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700784 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100785 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800786 xfrm_sec_ctx_match(pol->security, policy->security) &&
787 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (excl) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200789 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EEXIST;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 delpol = pol;
793 if (policy->priority > pol->priority)
794 continue;
795 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800796 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 continue;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (delpol)
800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 if (newpos)
Florian Westphal9da255b2018-10-10 18:02:21 +0200803 hlist_add_behind_rcu(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700804 else
Florian Westphal9da255b2018-10-10 18:02:21 +0200805 hlist_add_head_rcu(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800806 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800807 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800808
809 /* After previous checking, family can either be AF_INET or AF_INET6 */
810 if (policy->family == AF_INET)
811 rt_genid_bump_ipv4(net);
812 else
813 rt_genid_bump_ipv6(net);
814
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100815 if (delpol) {
816 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800817 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100818 }
Fan Due682adf2013-11-07 17:47:48 +0800819 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800820 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800821 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 policy->curlft.use_time = 0;
823 if (!mod_timer(&policy->timer, jiffies + HZ))
824 xfrm_pol_hold(policy);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200825 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
David S. Miller9b78a822005-12-22 07:39:48 -0800827 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800829 else if (xfrm_bydst_should_resize(net, dir, NULL))
830 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834EXPORT_SYMBOL(xfrm_policy_insert);
835
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000836struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
837 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800838 struct xfrm_sec_ctx *ctx, int delete,
839 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 struct xfrm_policy *pol, *ret;
842 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Eric Parisef41aaa2007-03-07 15:37:58 -0800844 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200845 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800846 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700847 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800848 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000850 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700851 !selector_cmp(sel, &pol->selector) &&
852 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700854 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700855 *err = security_xfrm_policy_delete(
856 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800857 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200858 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800859 return pol;
860 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800861 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 }
863 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865 }
866 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200867 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000869 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
Trent Jaegerdf718372005-12-13 23:12:27 -0800873EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000875struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
876 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
David S. Miller2518c7c2006-08-24 04:45:07 -0700878 struct xfrm_policy *pol, *ret;
879 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Herbert Xub5505c62007-05-14 02:15:47 -0700881 *err = -ENOENT;
882 if (xfrm_policy_id2dir(id) != dir)
883 return NULL;
884
Eric Parisef41aaa2007-03-07 15:37:58 -0800885 *err = 0;
Florian Westphal9d0380d2016-08-11 15:17:59 +0200886 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800887 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700888 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800889 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000890 if (pol->type == type && pol->index == id &&
891 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700893 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700894 *err = security_xfrm_policy_delete(
895 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800896 if (*err) {
Florian Westphal9d0380d2016-08-11 15:17:59 +0200897 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800898 return pol;
899 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800900 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700901 }
902 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
904 }
905 }
Florian Westphal9d0380d2016-08-11 15:17:59 +0200906 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000908 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700909 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700910 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912EXPORT_SYMBOL(xfrm_policy_byid);
913
Joy Latten4aa2e622007-06-04 19:05:57 -0400914#ifdef CONFIG_SECURITY_NETWORK_XFRM
915static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900916xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Joy Latten4aa2e622007-06-04 19:05:57 -0400918 int dir, err = 0;
919
920 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
921 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400922 int i;
923
Sasha Levinb67bfe02013-02-27 17:06:00 -0800924 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800925 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400926 if (pol->type != type)
927 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700928 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400929 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900930 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400931 return err;
932 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900933 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800934 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800935 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800936 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400937 bydst) {
938 if (pol->type != type)
939 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700940 err = security_xfrm_policy_delete(
941 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400942 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700943 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900944 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400945 return err;
946 }
947 }
948 }
949 }
950 return err;
951}
952#else
953static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900954xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400955{
956 return 0;
957}
958#endif
959
Tetsuo Handa2e710292014-04-22 21:48:30 +0900960int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400961{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000962 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Florian Westphal9d0380d2016-08-11 15:17:59 +0200964 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400965
Tetsuo Handa2e710292014-04-22 21:48:30 +0900966 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400967 if (err)
968 goto out;
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700971 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800972 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700973
974 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800975 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800976 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700977 if (pol->type != type)
978 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000979 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200980 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000981 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Tetsuo Handa2e710292014-04-22 21:48:30 +0900983 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600984
David S. Miller2518c7c2006-08-24 04:45:07 -0700985 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Steffen Klassert4141b362016-08-24 13:08:40 +0200987 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700988 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700990
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800991 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700992 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800993 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800994 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700995 bydst) {
996 if (pol->type != type)
997 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000998 __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +0200999 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +00001000 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -07001001
Tetsuo Handa2e710292014-04-22 21:48:30 +09001002 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -07001003 xfrm_policy_kill(pol);
1004
Florian Westphal9d0380d2016-08-11 15:17:59 +02001005 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001006 goto again2;
1007 }
1008 }
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001011 if (!cnt)
1012 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001013out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001014 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001015 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017EXPORT_SYMBOL(xfrm_policy_flush);
1018
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001019int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001020 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 void *data)
1022{
Herbert Xu12a169e2008-10-01 07:03:24 -07001023 struct xfrm_policy *pol;
1024 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001025 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Timo Teras4c563f72008-02-28 21:31:08 -08001027 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1028 walk->type != XFRM_POLICY_TYPE_ANY)
1029 return -EINVAL;
1030
Herbert Xu12a169e2008-10-01 07:03:24 -07001031 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001032 return 0;
1033
Florian Westphal9d0380d2016-08-11 15:17:59 +02001034 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001035 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001036 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001037 else
Li RongQing80077702015-04-22 17:09:54 +08001038 x = list_first_entry(&walk->walk.all,
1039 struct xfrm_policy_walk_entry, all);
1040
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001041 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001042 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001043 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001044 pol = container_of(x, struct xfrm_policy, walk);
1045 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1046 walk->type != pol->type)
1047 continue;
1048 error = func(pol, xfrm_policy_id2dir(pol->index),
1049 walk->seq, data);
1050 if (error) {
1051 list_move_tail(&walk->walk.all, &x->all);
1052 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001053 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001054 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001056 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001057 error = -ENOENT;
1058 goto out;
1059 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001060 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061out:
Florian Westphal9d0380d2016-08-11 15:17:59 +02001062 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return error;
1064}
1065EXPORT_SYMBOL(xfrm_policy_walk);
1066
Herbert Xu12a169e2008-10-01 07:03:24 -07001067void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1068{
1069 INIT_LIST_HEAD(&walk->walk.all);
1070 walk->walk.dead = 1;
1071 walk->type = type;
1072 walk->seq = 0;
1073}
1074EXPORT_SYMBOL(xfrm_policy_walk_init);
1075
Fan Du283bc9f2013-11-07 17:47:50 +08001076void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001077{
1078 if (list_empty(&walk->walk.all))
1079 return;
1080
Florian Westphal9d0380d2016-08-11 15:17:59 +02001081 spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001082 list_del(&walk->walk.all);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001083 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001084}
1085EXPORT_SYMBOL(xfrm_policy_walk_done);
1086
James Morris134b0fc2006-10-05 15:42:27 -05001087/*
1088 * Find policy to apply to this flow.
1089 *
1090 * Returns 0 if policy found, else an -errno.
1091 */
David S. Millerf299d552011-02-24 01:23:30 -05001092static int xfrm_policy_match(const struct xfrm_policy *pol,
1093 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001094 u8 type, u16 family, int dir)
1095{
David S. Millerf299d552011-02-24 01:23:30 -05001096 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001097 int ret = -ESRCH;
1098 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001099
1100 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001101 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001102 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001103 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001104
1105 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001106 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001107 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001108 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001109
James Morris134b0fc2006-10-05 15:42:27 -05001110 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001111}
1112
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001113static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001114 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001115 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
James Morris134b0fc2006-10-05 15:42:27 -05001117 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001118 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001119 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001120 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001121 unsigned int sequence;
1122 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001123
1124 daddr = xfrm_flowi_daddr(fl, family);
1125 saddr = xfrm_flowi_saddr(fl, family);
1126 if (unlikely(!daddr || !saddr))
1127 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Florian Westphala7c442472016-08-11 15:17:56 +02001129 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001130 retry:
1131 do {
1132 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1133 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1134 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1135
1136 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001137 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001138 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001139 err = xfrm_policy_match(pol, fl, type, family, dir);
1140 if (err) {
1141 if (err == -ESRCH)
1142 continue;
1143 else {
1144 ret = ERR_PTR(err);
1145 goto fail;
1146 }
1147 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001148 ret = pol;
1149 priority = ret->priority;
1150 break;
1151 }
1152 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001153 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001154 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001155 if ((pol->priority >= priority) && ret)
1156 break;
1157
James Morris134b0fc2006-10-05 15:42:27 -05001158 err = xfrm_policy_match(pol, fl, type, family, dir);
1159 if (err) {
1160 if (err == -ESRCH)
1161 continue;
1162 else {
1163 ret = ERR_PTR(err);
1164 goto fail;
1165 }
Li RongQing8faf4912015-05-14 11:16:59 +08001166 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001167 ret = pol;
1168 break;
1169 }
1170 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001171
Florian Westphal30846092016-08-11 15:17:54 +02001172 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1173 goto retry;
1174
Florian Westphale37cc8a2016-08-11 15:17:55 +02001175 if (ret && !xfrm_pol_hold_rcu(ret))
1176 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001177fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001178 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001179
David S. Miller2518c7c2006-08-24 04:45:07 -07001180 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001181}
1182
Timo Teräs80c802f2010-04-07 00:30:05 +00001183static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001184__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001185{
1186#ifdef CONFIG_XFRM_SUB_POLICY
1187 struct xfrm_policy *pol;
1188
1189 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1190 if (pol != NULL)
1191 return pol;
1192#endif
1193 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1194}
1195
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001196static int flow_to_policy_dir(int dir)
1197{
1198 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1199 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1200 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1201 return dir;
1202
1203 switch (dir) {
1204 default:
1205 case FLOW_DIR_IN:
1206 return XFRM_POLICY_IN;
1207 case FLOW_DIR_OUT:
1208 return XFRM_POLICY_OUT;
1209 case FLOW_DIR_FWD:
1210 return XFRM_POLICY_FWD;
1211 }
1212}
1213
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001214static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001215xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001216 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001217{
1218 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001219
1220 if (old_obj)
1221 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001222
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001223 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001224 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001225 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001226
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001227 /* Resolver returns two references:
1228 * one for cache and one for caller of flow_cache_lookup() */
1229 xfrm_pol_hold(pol);
1230
1231 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Trent Jaegerdf718372005-12-13 23:12:27 -08001234static inline int policy_to_flow_dir(int dir)
1235{
1236 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001237 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1238 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1239 return dir;
1240 switch (dir) {
1241 default:
1242 case XFRM_POLICY_IN:
1243 return FLOW_DIR_IN;
1244 case XFRM_POLICY_OUT:
1245 return FLOW_DIR_OUT;
1246 case XFRM_POLICY_FWD:
1247 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001248 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001249}
1250
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001251static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
Steffen Klassert0b865642017-02-14 07:43:56 +01001252 const struct flowi *fl, u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct xfrm_policy *pol;
1255
Eric Dumazetd188ba82015-12-08 07:22:02 -08001256 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001257 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001258 pol = rcu_dereference(sk->sk_policy[dir]);
1259 if (pol != NULL) {
Steffen Klassert46b31712017-11-29 06:53:55 +01001260 bool match;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001261 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001262
Steffen Klassert46b31712017-11-29 06:53:55 +01001263 if (pol->family != family) {
1264 pol = NULL;
1265 goto out;
1266 }
1267
1268 match = xfrm_selector_match(&pol->selector, fl, family);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001269 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001270 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1271 pol = NULL;
1272 goto out;
1273 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001274 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001275 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001276 policy_to_flow_dir(dir));
Florian Westphal330e8322016-11-17 13:21:46 +01001277 if (!err) {
1278 if (!xfrm_pol_hold_rcu(pol))
1279 goto again;
1280 } else if (err == -ESRCH) {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001281 pol = NULL;
Florian Westphal330e8322016-11-17 13:21:46 +01001282 } else {
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001283 pol = ERR_PTR(err);
Florian Westphal330e8322016-11-17 13:21:46 +01001284 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001285 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 pol = NULL;
1287 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001288out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001289 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return pol;
1291}
1292
1293static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1294{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001295 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001296
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001297 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001298 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 xfrm_pol_hold(pol);
1300}
1301
1302static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1303 int dir)
1304{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001305 struct net *net = xp_net(pol);
1306
Herbert Xu53c2e282014-11-13 17:09:49 +08001307 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001308 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Herbert Xu53c2e282014-11-13 17:09:49 +08001310 /* Socket policies are not hashed. */
1311 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001312 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001313 hlist_del(&pol->byidx);
1314 }
1315
1316 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001317 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001318
1319 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Herbert Xu53c2e282014-11-13 17:09:49 +08001322static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1323{
1324 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1325}
1326
1327static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1328{
1329 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1330}
1331
Herbert Xu4666faa2005-06-18 22:43:22 -07001332int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Fan Du283bc9f2013-11-07 17:47:50 +08001334 struct net *net = xp_net(pol);
1335
Florian Westphal9d0380d2016-08-11 15:17:59 +02001336 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 pol = __xfrm_policy_unlink(pol, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001338 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001343 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
David S. Millera70fcb02006-03-20 19:18:52 -08001345EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1348{
Lorenzo Colitti5eef9b52017-11-20 19:26:02 +09001349 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct xfrm_policy *old_pol;
1351
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001352#ifdef CONFIG_XFRM_SUB_POLICY
1353 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1354 return -EINVAL;
1355#endif
1356
Florian Westphal9d0380d2016-08-11 15:17:59 +02001357 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001358 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1359 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001361 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001362 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001363 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001365 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001366 if (old_pol) {
1367 if (pol)
1368 xfrm_policy_requeue(old_pol, pol);
1369
Timo Teräsea2dea92010-03-31 00:17:05 +00001370 /* Unlinking succeeds always. This is the only function
1371 * allowed to delete or replace socket policy.
1372 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001373 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001374 }
Florian Westphal9d0380d2016-08-11 15:17:59 +02001375 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 if (old_pol) {
1378 xfrm_policy_kill(old_pol);
1379 }
1380 return 0;
1381}
1382
David S. Millerd3e40a92011-02-24 01:25:41 -05001383static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001385 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001386 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 if (newp) {
1389 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001390 if (security_xfrm_policy_clone(old->security,
1391 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001392 kfree(newp);
1393 return NULL; /* ENOMEM */
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 newp->lft = old->lft;
1396 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001397 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 newp->action = old->action;
1399 newp->flags = old->flags;
1400 newp->xfrm_nr = old->xfrm_nr;
1401 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001402 newp->type = old->type;
Herbert Xu6ceabde2017-11-10 14:14:06 +11001403 newp->family = old->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 memcpy(newp->xfrm_vec, old->xfrm_vec,
1405 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Florian Westphal9d0380d2016-08-11 15:17:59 +02001406 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001407 xfrm_sk_policy_link(newp, dir);
Florian Westphal9d0380d2016-08-11 15:17:59 +02001408 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 xfrm_pol_put(newp);
1410 }
1411 return newp;
1412}
1413
Eric Dumazetd188ba82015-12-08 07:22:02 -08001414int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001416 const struct xfrm_policy *p;
1417 struct xfrm_policy *np;
1418 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Eric Dumazetd188ba82015-12-08 07:22:02 -08001420 rcu_read_lock();
1421 for (i = 0; i < 2; i++) {
1422 p = rcu_dereference(osk->sk_policy[i]);
1423 if (p) {
1424 np = clone_policy(p, i);
1425 if (unlikely(!np)) {
1426 ret = -ENOMEM;
1427 break;
1428 }
1429 rcu_assign_pointer(sk->sk_policy[i], np);
1430 }
1431 }
1432 rcu_read_unlock();
1433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001436static int
David Ahern42a7b322015-08-10 16:58:11 -06001437xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1438 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001439{
1440 int err;
1441 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1442
1443 if (unlikely(afinfo == NULL))
1444 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001445 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001446 xfrm_policy_put_afinfo(afinfo);
1447 return err;
1448}
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450/* Resolve list of templates for the flow, given policy. */
1451
1452static int
David S. Millera6c2e612011-02-22 18:35:39 -08001453xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1454 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001456 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int nx;
1458 int i, error;
1459 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1460 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001461 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Weilong Chen9b7a7872013-12-24 09:43:46 +08001463 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct xfrm_state *x;
1465 xfrm_address_t *remote = daddr;
1466 xfrm_address_t *local = saddr;
1467 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1468
Joakim Koskela48b8d782007-07-26 00:08:42 -07001469 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1470 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 remote = &tmpl->id.daddr;
1472 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001473 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001474 error = xfrm_get_saddr(net, fl->flowi_oif,
1475 &tmp, remote,
1476 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001477 if (error)
1478 goto fail;
1479 local = &tmp;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
1483 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1484
1485 if (x && x->km.state == XFRM_STATE_VALID) {
1486 xfrm[nx++] = x;
1487 daddr = remote;
1488 saddr = local;
1489 continue;
1490 }
1491 if (x) {
1492 error = (x->km.state == XFRM_STATE_ERROR ?
1493 -EINVAL : -EAGAIN);
1494 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001495 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001496 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 if (!tmpl->optional)
1500 goto fail;
1501 }
1502 return nx;
1503
1504fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001505 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 xfrm_state_put(xfrm[nx]);
1507 return error;
1508}
1509
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001510static int
David S. Millera6c2e612011-02-22 18:35:39 -08001511xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1512 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001513{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001514 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1515 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001516 int cnx = 0;
1517 int error;
1518 int ret;
1519 int i;
1520
1521 for (i = 0; i < npols; i++) {
1522 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1523 error = -ENOBUFS;
1524 goto fail;
1525 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001526
1527 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001528 if (ret < 0) {
1529 error = ret;
1530 goto fail;
1531 } else
1532 cnx += ret;
1533 }
1534
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001535 /* found states are sorted for outbound processing */
1536 if (npols > 1)
1537 xfrm_state_sort(xfrm, tpp, cnx, family);
1538
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001539 return cnx;
1540
1541 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001542 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001543 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001544 return error;
1545
1546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/* Check that the bundle accepts the flow and its components are
1549 * still valid.
1550 */
1551
David S. Miller05d84022011-02-22 17:47:10 -08001552static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001553{
1554 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1555 int tos;
1556
1557 if (!afinfo)
1558 return -EINVAL;
1559
1560 tos = afinfo->get_tos(fl);
1561
1562 xfrm_policy_put_afinfo(afinfo);
1563
1564 return tos;
1565}
1566
Timo Teräs80c802f2010-04-07 00:30:05 +00001567static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1568{
1569 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1570 struct dst_entry *dst = &xdst->u.dst;
1571
1572 if (xdst->route == NULL) {
1573 /* Dummy bundle - if it has xfrms we were not
1574 * able to build bundle as template resolution failed.
1575 * It means we need to try again resolving. */
1576 if (xdst->num_xfrms > 0)
1577 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001578 } else if (dst->flags & DST_XFRM_QUEUE) {
1579 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001580 } else {
1581 /* Real bundle */
1582 if (stale_bundle(dst))
1583 return NULL;
1584 }
1585
1586 dst_hold(dst);
1587 return flo;
1588}
1589
1590static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1591{
1592 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1593 struct dst_entry *dst = &xdst->u.dst;
1594
1595 if (!xdst->route)
1596 return 0;
1597 if (stale_bundle(dst))
1598 return 0;
1599
1600 return 1;
1601}
1602
1603static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1604{
1605 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1606 struct dst_entry *dst = &xdst->u.dst;
1607
1608 dst_free(dst);
1609}
1610
1611static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1612 .get = xfrm_bundle_flo_get,
1613 .check = xfrm_bundle_flo_check,
1614 .delete = xfrm_bundle_flo_delete,
1615};
1616
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001617static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001618{
1619 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001620 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001621 struct xfrm_dst *xdst;
1622
1623 if (!afinfo)
1624 return ERR_PTR(-EINVAL);
1625
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001626 switch (family) {
1627 case AF_INET:
1628 dst_ops = &net->xfrm.xfrm4_dst_ops;
1629 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001630#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001631 case AF_INET6:
1632 dst_ops = &net->xfrm.xfrm6_dst_ops;
1633 break;
1634#endif
1635 default:
1636 BUG();
1637 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001638 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001639
Madalin Bucurd4cae562011-09-26 07:04:36 +00001640 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001641 struct dst_entry *dst = &xdst->u.dst;
1642
1643 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001644 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001645 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001646 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001647
Madalin Bucurd4cae562011-09-26 07:04:36 +00001648 xfrm_policy_put_afinfo(afinfo);
1649
Herbert Xu25ee3282007-12-11 09:32:34 -08001650 return xdst;
1651}
1652
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001653static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1654 int nfheader_len)
1655{
1656 struct xfrm_policy_afinfo *afinfo =
1657 xfrm_policy_get_afinfo(dst->ops->family);
1658 int err;
1659
1660 if (!afinfo)
1661 return -EINVAL;
1662
1663 err = afinfo->init_path(path, dst, nfheader_len);
1664
1665 xfrm_policy_put_afinfo(afinfo);
1666
1667 return err;
1668}
1669
Herbert Xu87c1e122010-03-02 02:51:56 +00001670static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001671 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001672{
1673 struct xfrm_policy_afinfo *afinfo =
1674 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1675 int err;
1676
1677 if (!afinfo)
1678 return -EINVAL;
1679
Herbert Xu87c1e122010-03-02 02:51:56 +00001680 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001681
1682 xfrm_policy_put_afinfo(afinfo);
1683
1684 return err;
1685}
1686
Timo Teräs80c802f2010-04-07 00:30:05 +00001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1689 * all the metrics... Shortly, bundle a bundle.
1690 */
1691
Herbert Xu25ee3282007-12-11 09:32:34 -08001692static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1693 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001694 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001695 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001697 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001698 unsigned long now = jiffies;
1699 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001700 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001701 struct dst_entry *dst_prev = NULL;
1702 struct dst_entry *dst0 = NULL;
1703 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001705 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001706 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001707 int trailer_len = 0;
1708 int tos;
1709 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001710 xfrm_address_t saddr, daddr;
1711
1712 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001713
1714 tos = xfrm_get_tos(fl, family);
1715 err = tos;
1716 if (tos < 0)
1717 goto put_states;
1718
1719 dst_hold(dst);
1720
1721 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001722 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001723 struct dst_entry *dst1 = &xdst->u.dst;
1724
1725 err = PTR_ERR(xdst);
1726 if (IS_ERR(xdst)) {
1727 dst_release(dst);
1728 goto put_states;
1729 }
1730
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001731 if (xfrm[i]->sel.family == AF_UNSPEC) {
1732 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1733 xfrm_af2proto(family));
1734 if (!inner_mode) {
1735 err = -EAFNOSUPPORT;
1736 dst_release(dst);
1737 goto put_states;
1738 }
1739 } else
1740 inner_mode = xfrm[i]->inner_mode;
1741
Herbert Xu25ee3282007-12-11 09:32:34 -08001742 if (!dst_prev)
1743 dst0 = dst1;
1744 else {
1745 dst_prev->child = dst_clone(dst1);
1746 dst1->flags |= DST_NOHASH;
1747 }
1748
1749 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001750 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001751
1752 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1753 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001754 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1755 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001756 err = PTR_ERR(dst);
1757 if (IS_ERR(dst))
1758 goto put_states;
1759 } else
1760 dst_hold(dst);
1761
1762 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001763 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001764
David S. Millerf5b0a872012-07-19 12:31:33 -07001765 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001766 dst1->flags |= DST_HOST;
1767 dst1->lastuse = now;
1768
1769 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001770 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001771
1772 dst1->next = dst_prev;
1773 dst_prev = dst1;
1774
1775 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001776 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1777 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001778 trailer_len += xfrm[i]->props.trailer_len;
1779 }
1780
1781 dst_prev->child = dst;
1782 dst0->path = dst;
1783
1784 err = -ENODEV;
1785 dev = dst->dev;
1786 if (!dev)
1787 goto free_dst;
1788
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001789 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001790 xfrm_init_pmtu(dst_prev);
1791
1792 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1793 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1794
Herbert Xu87c1e122010-03-02 02:51:56 +00001795 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001796 if (err)
1797 goto free_dst;
1798
1799 dst_prev->header_len = header_len;
1800 dst_prev->trailer_len = trailer_len;
1801 header_len -= xdst->u.dst.xfrm->props.header_len;
1802 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1803 }
1804
1805out:
1806 return dst0;
1807
1808put_states:
1809 for (; i < nx; i++)
1810 xfrm_state_put(xfrm[i]);
1811free_dst:
1812 if (dst0)
1813 dst_free(dst0);
1814 dst0 = ERR_PTR(err);
1815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
David S. Miller73ff93c2011-02-22 18:33:42 -08001818static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001819 struct xfrm_policy **pols,
1820 int *num_pols, int *num_xfrms)
1821{
1822 int i;
1823
1824 if (*num_pols == 0 || !pols[0]) {
1825 *num_pols = 0;
1826 *num_xfrms = 0;
1827 return 0;
1828 }
1829 if (IS_ERR(pols[0]))
1830 return PTR_ERR(pols[0]);
1831
1832 *num_xfrms = pols[0]->xfrm_nr;
1833
1834#ifdef CONFIG_XFRM_SUB_POLICY
1835 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1836 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1837 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1838 XFRM_POLICY_TYPE_MAIN,
1839 fl, family,
1840 XFRM_POLICY_OUT);
1841 if (pols[1]) {
1842 if (IS_ERR(pols[1])) {
1843 xfrm_pols_put(pols, *num_pols);
1844 return PTR_ERR(pols[1]);
1845 }
Weilong Chen02d08922013-12-24 09:43:48 +08001846 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001847 (*num_xfrms) += pols[1]->xfrm_nr;
1848 }
1849 }
1850#endif
1851 for (i = 0; i < *num_pols; i++) {
1852 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1853 *num_xfrms = -1;
1854 break;
1855 }
1856 }
1857
1858 return 0;
1859
1860}
1861
1862static struct xfrm_dst *
1863xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001864 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001865 struct dst_entry *dst_orig)
1866{
1867 struct net *net = xp_net(pols[0]);
1868 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1869 struct dst_entry *dst;
1870 struct xfrm_dst *xdst;
1871 int err;
1872
1873 /* Try to instantiate a bundle */
1874 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001875 if (err <= 0) {
YueHaibingaf322ba2018-07-25 16:54:33 +08001876 if (err == 0)
1877 return NULL;
1878
1879 if (err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001880 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1881 return ERR_PTR(err);
1882 }
1883
1884 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1885 if (IS_ERR(dst)) {
1886 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1887 return ERR_CAST(dst);
1888 }
1889
1890 xdst = (struct xfrm_dst *)dst;
1891 xdst->num_xfrms = err;
Timo Teräs80c802f2010-04-07 00:30:05 +00001892 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001893 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001894 xdst->policy_genid = atomic_read(&pols[0]->genid);
1895
1896 return xdst;
1897}
1898
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001899static void xfrm_policy_queue_process(unsigned long arg)
1900{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001901 struct sk_buff *skb;
1902 struct sock *sk;
1903 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001904 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001905 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001906 struct xfrm_policy_queue *pq = &pol->polq;
1907 struct flowi fl;
1908 struct sk_buff_head list;
1909
1910 spin_lock(&pq->hold_queue.lock);
1911 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001912 if (!skb) {
1913 spin_unlock(&pq->hold_queue.lock);
1914 goto out;
1915 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001916 dst = skb_dst(skb);
1917 sk = skb->sk;
1918 xfrm_decode_session(skb, &fl, dst->ops->family);
1919 spin_unlock(&pq->hold_queue.lock);
1920
1921 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001922 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001923 if (IS_ERR(dst))
1924 goto purge_queue;
1925
1926 if (dst->flags & DST_XFRM_QUEUE) {
1927 dst_release(dst);
1928
1929 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1930 goto purge_queue;
1931
1932 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001933 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1934 xfrm_pol_hold(pol);
1935 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001936 }
1937
1938 dst_release(dst);
1939
1940 __skb_queue_head_init(&list);
1941
1942 spin_lock(&pq->hold_queue.lock);
1943 pq->timeout = 0;
1944 skb_queue_splice_init(&pq->hold_queue, &list);
1945 spin_unlock(&pq->hold_queue.lock);
1946
1947 while (!skb_queue_empty(&list)) {
1948 skb = __skb_dequeue(&list);
1949
1950 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1951 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001952 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001953 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001954 kfree_skb(skb);
1955 continue;
1956 }
1957
1958 nf_reset(skb);
1959 skb_dst_drop(skb);
1960 skb_dst_set(skb, dst);
1961
Eric W. Biederman13206b62015-10-07 16:48:35 -05001962 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001963 }
1964
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001965out:
1966 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001967 return;
1968
1969purge_queue:
1970 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001971 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001972 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001973}
1974
Eric W. Biedermanede20592015-10-07 16:48:47 -05001975static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001976{
1977 unsigned long sched_next;
1978 struct dst_entry *dst = skb_dst(skb);
1979 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001980 struct xfrm_policy *pol = xdst->pols[0];
1981 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001982
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001983 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001984 kfree_skb(skb);
1985 return 0;
1986 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001987
1988 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1989 kfree_skb(skb);
1990 return -EAGAIN;
1991 }
1992
1993 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001994
1995 spin_lock_bh(&pq->hold_queue.lock);
1996
1997 if (!pq->timeout)
1998 pq->timeout = XFRM_QUEUE_TMO_MIN;
1999
2000 sched_next = jiffies + pq->timeout;
2001
2002 if (del_timer(&pq->hold_timer)) {
2003 if (time_before(pq->hold_timer.expires, sched_next))
2004 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002005 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002006 }
2007
2008 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002009 if (!mod_timer(&pq->hold_timer, sched_next))
2010 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002011
2012 spin_unlock_bh(&pq->hold_queue.lock);
2013
2014 return 0;
2015}
2016
2017static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002018 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002019 const struct flowi *fl,
2020 int num_xfrms,
2021 u16 family)
2022{
2023 int err;
2024 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002025 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002026 struct dst_entry *dst1;
2027 struct xfrm_dst *xdst;
2028
2029 xdst = xfrm_alloc_dst(net, family);
2030 if (IS_ERR(xdst))
2031 return xdst;
2032
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002033 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2034 net->xfrm.sysctl_larval_drop ||
2035 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002036 return xdst;
2037
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002038 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002039 dst1 = &xdst->u.dst;
2040 dst_hold(dst);
2041 xdst->route = dst;
2042
2043 dst_copy_metrics(dst1, dst);
2044
2045 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2046 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2047 dst1->lastuse = jiffies;
2048
2049 dst1->input = dst_discard;
2050 dst1->output = xdst_queue_output;
2051
2052 dst_hold(dst);
2053 dst1->child = dst;
2054 dst1->path = dst;
2055
2056 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2057
2058 err = -ENODEV;
2059 dev = dst->dev;
2060 if (!dev)
2061 goto free_dst;
2062
2063 err = xfrm_fill_dst(xdst, dev, fl);
2064 if (err)
2065 goto free_dst;
2066
2067out:
2068 return xdst;
2069
2070free_dst:
2071 dst_release(dst1);
2072 xdst = ERR_PTR(err);
2073 goto out;
2074}
2075
Timo Teräs80c802f2010-04-07 00:30:05 +00002076static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002077xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002078 struct flow_cache_object *oldflo, void *ctx)
2079{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002080 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002081 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2082 struct xfrm_dst *xdst, *new_xdst;
2083 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2084
2085 /* Check if the policies from old bundle are usable */
2086 xdst = NULL;
2087 if (oldflo) {
2088 xdst = container_of(oldflo, struct xfrm_dst, flo);
2089 num_pols = xdst->num_pols;
2090 num_xfrms = xdst->num_xfrms;
2091 pol_dead = 0;
2092 for (i = 0; i < num_pols; i++) {
2093 pols[i] = xdst->pols[i];
2094 pol_dead |= pols[i]->walk.dead;
2095 }
2096 if (pol_dead) {
2097 dst_free(&xdst->u.dst);
2098 xdst = NULL;
2099 num_pols = 0;
2100 num_xfrms = 0;
2101 oldflo = NULL;
2102 }
2103 }
2104
2105 /* Resolve policies to use if we couldn't get them from
2106 * previous cache entry */
2107 if (xdst == NULL) {
2108 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002109 pols[0] = __xfrm_policy_lookup(net, fl, family,
2110 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002111 err = xfrm_expand_policies(fl, family, pols,
2112 &num_pols, &num_xfrms);
2113 if (err < 0)
2114 goto inc_error;
2115 if (num_pols == 0)
2116 return NULL;
2117 if (num_xfrms <= 0)
2118 goto make_dummy_bundle;
2119 }
2120
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002121 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2122 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002123 if (IS_ERR(new_xdst)) {
2124 err = PTR_ERR(new_xdst);
2125 if (err != -EAGAIN)
2126 goto error;
2127 if (oldflo == NULL)
2128 goto make_dummy_bundle;
2129 dst_hold(&xdst->u.dst);
2130 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002131 } else if (new_xdst == NULL) {
2132 num_xfrms = 0;
2133 if (oldflo == NULL)
2134 goto make_dummy_bundle;
2135 xdst->num_xfrms = 0;
2136 dst_hold(&xdst->u.dst);
2137 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002138 }
2139
2140 /* Kill the previous bundle */
2141 if (xdst) {
2142 /* The policies were stolen for newly generated bundle */
2143 xdst->num_pols = 0;
2144 dst_free(&xdst->u.dst);
2145 }
2146
2147 /* Flow cache does not have reference, it dst_free()'s,
2148 * but we do need to return one reference for original caller */
2149 dst_hold(&new_xdst->u.dst);
2150 return &new_xdst->flo;
2151
2152make_dummy_bundle:
2153 /* We found policies, but there's no bundles to instantiate:
2154 * either because the policy blocks, has no transformations or
2155 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002156 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002157 if (IS_ERR(xdst)) {
2158 xfrm_pols_put(pols, num_pols);
2159 return ERR_CAST(xdst);
2160 }
2161 xdst->num_pols = num_pols;
2162 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002163 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002164
2165 dst_hold(&xdst->u.dst);
2166 return &xdst->flo;
2167
2168inc_error:
2169 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2170error:
2171 if (xdst != NULL)
2172 dst_free(&xdst->u.dst);
2173 else
2174 xfrm_pols_put(pols, num_pols);
2175 return ERR_PTR(err);
2176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
David S. Miller2774c132011-03-01 14:59:04 -08002178static struct dst_entry *make_blackhole(struct net *net, u16 family,
2179 struct dst_entry *dst_orig)
2180{
2181 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2182 struct dst_entry *ret;
2183
2184 if (!afinfo) {
2185 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002186 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002187 } else {
2188 ret = afinfo->blackhole_route(net, dst_orig);
2189 }
2190 xfrm_policy_put_afinfo(afinfo);
2191
2192 return ret;
2193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/* Main function: finds/creates a bundle for given flow.
2196 *
2197 * At the moment we eat a raw IP route. Mostly to speed up lookups
2198 * on interfaces with disabled IPsec.
2199 */
David S. Miller452edd52011-03-02 13:27:41 -08002200struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2201 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002202 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002204 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002205 struct flow_cache_object *flo;
2206 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002207 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002208 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002209 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002210 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002211
Timo Teräs80c802f2010-04-07 00:30:05 +00002212 dst = NULL;
2213 xdst = NULL;
2214 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002215
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002216 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002217 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002218 num_pols = 1;
Steffen Klassert0b865642017-02-14 07:43:56 +01002219 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002220 err = xfrm_expand_policies(fl, family, pols,
2221 &num_pols, &num_xfrms);
2222 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002223 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002224
2225 if (num_pols) {
2226 if (num_xfrms <= 0) {
2227 drop_pols = num_pols;
2228 goto no_transform;
2229 }
2230
2231 xdst = xfrm_resolve_and_create_bundle(
2232 pols, num_pols, fl,
2233 family, dst_orig);
2234 if (IS_ERR(xdst)) {
2235 xfrm_pols_put(pols, num_pols);
2236 err = PTR_ERR(xdst);
2237 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002238 } else if (xdst == NULL) {
2239 num_xfrms = 0;
2240 drop_pols = num_pols;
2241 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002242 }
2243
Steffen Klassertb7eea452014-06-18 12:34:21 +02002244 dst_hold(&xdst->u.dst);
2245 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002246 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002247 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Timo Teräs80c802f2010-04-07 00:30:05 +00002250 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002251 struct xfrm_flo xflo;
2252
2253 xflo.dst_orig = dst_orig;
2254 xflo.flags = flags;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002257 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002258 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002259 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Timo Teräs80c802f2010-04-07 00:30:05 +00002261 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002262 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002263 if (flo == NULL)
2264 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002265 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002266 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002267 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002268 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002269 xdst = container_of(flo, struct xfrm_dst, flo);
2270
2271 num_pols = xdst->num_pols;
2272 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002273 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002274 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276
Timo Teräs80c802f2010-04-07 00:30:05 +00002277 dst = &xdst->u.dst;
2278 if (route == NULL && num_xfrms > 0) {
2279 /* The only case when xfrm_bundle_lookup() returns a
2280 * bundle with null route, is when the template could
2281 * not be resolved. It means policies are there, but
2282 * bundle could not be created, since we don't yet
2283 * have the xfrm_state's. We need to wait for KM to
2284 * negotiate new SA's or bail out with error.*/
2285 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002286 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002287 err = -EREMOTE;
2288 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002289 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002290
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002291 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002292
2293 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2294 goto error;
2295 }
2296
2297no_transform:
2298 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002299 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Timo Teräs80c802f2010-04-07 00:30:05 +00002301 if ((flags & XFRM_LOOKUP_ICMP) &&
2302 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2303 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002304 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002305 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002306
Timo Teräs80c802f2010-04-07 00:30:05 +00002307 for (i = 0; i < num_pols; i++)
2308 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002309
Timo Teräs80c802f2010-04-07 00:30:05 +00002310 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002312 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002313 err = -EPERM;
2314 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002315 } else if (num_xfrms > 0) {
2316 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002317 dst_release(dst_orig);
2318 } else {
2319 /* Flow passes untransformed */
2320 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002321 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002323ok:
2324 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002325 if (dst && dst->xfrm &&
2326 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2327 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002328 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Timo Teräs80c802f2010-04-07 00:30:05 +00002330nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002331 if (!(flags & XFRM_LOOKUP_ICMP)) {
2332 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002333 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002334 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002335 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002337 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002338dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002339 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2340 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002341 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002342 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344EXPORT_SYMBOL(xfrm_lookup);
2345
Steffen Klassertf92ee612014-09-16 10:08:40 +02002346/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2347 * Otherwise we may send out blackholed packets.
2348 */
2349struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2350 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002351 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002352{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002353 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002354 flags | XFRM_LOOKUP_QUEUE |
2355 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002356
2357 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2358 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2359
Tommi Rantala590f3122018-06-21 09:30:47 +03002360 if (IS_ERR(dst))
2361 dst_release(dst_orig);
2362
Steffen Klassertf92ee612014-09-16 10:08:40 +02002363 return dst;
2364}
2365EXPORT_SYMBOL(xfrm_lookup_route);
2366
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002367static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002368xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002369{
2370 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371
2372 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2373 return 0;
2374 x = skb->sp->xvec[idx];
2375 if (!x->type->reject)
2376 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002377 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002378}
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380/* When skb is transformed back to its "native" form, we have to
2381 * check policy restrictions. At the moment we make this in maximally
2382 * stupid way. Shame on me. :-) Of course, connected sockets must
2383 * have policy cached at them.
2384 */
2385
2386static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002387xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 unsigned short family)
2389{
2390 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002391 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return x->id.proto == tmpl->id.proto &&
2393 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2394 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2395 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002396 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002397 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002398 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2399 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002402/*
2403 * 0 or more than 0 is returned when validation is succeeded (either bypass
2404 * because of optional transport mode, or next index of the mathced secpath
2405 * state with the template.
2406 * -1 is returned when no matching template is found.
2407 * Otherwise "-2 - errored_index" is returned.
2408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002410xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 unsigned short family)
2412{
2413 int idx = start;
2414
2415 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002416 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return start;
2418 } else
2419 start = -1;
2420 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002421 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002423 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2424 if (start == -1)
2425 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429 return start;
2430}
2431
Herbert Xud5422ef2007-12-12 10:44:16 -08002432int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2433 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002436 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (unlikely(afinfo == NULL))
2439 return -EAFNOSUPPORT;
2440
Herbert Xud5422ef2007-12-12 10:44:16 -08002441 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002442 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
Herbert Xud5422ef2007-12-12 10:44:16 -08002446EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
David S. Miller9a7386e2011-02-24 01:44:12 -05002448static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
2450 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002451 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002452 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456
2457 return 0;
2458}
2459
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002460int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 unsigned short family)
2462{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002463 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002465 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2466 int npols = 0;
2467 int xfrm_nr;
2468 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002469 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002471 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002472 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Herbert Xud5422ef2007-12-12 10:44:16 -08002474 reverse = dir & ~XFRM_POLICY_MASK;
2475 dir &= XFRM_POLICY_MASK;
2476 fl_dir = policy_to_flow_dir(dir);
2477
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002478 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002479 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002481 }
2482
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002483 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 /* First, check used SA against their selectors. */
2486 if (skb->sp) {
2487 int i;
2488
Weilong Chen9b7a7872013-12-24 09:43:46 +08002489 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002490 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002491 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002492 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496 }
2497
2498 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002499 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002500 if (sk && sk->sk_policy[dir]) {
Steffen Klassert0b865642017-02-14 07:43:56 +01002501 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002502 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002503 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002504 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002505 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002508 if (!pol) {
2509 struct flow_cache_object *flo;
2510
2511 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2512 xfrm_policy_lookup, NULL);
2513 if (IS_ERR_OR_NULL(flo))
2514 pol = ERR_CAST(flo);
2515 else
2516 pol = container_of(flo, struct xfrm_policy, flo);
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002519 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002520 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002521 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002522 }
James Morris134b0fc2006-10-05 15:42:27 -05002523
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002524 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002525 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002526 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002527 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002528 return 0;
2529 }
2530 return 1;
2531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
James Morris9d729f72007-03-04 16:12:44 -08002533 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002535 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002536 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002537#ifdef CONFIG_XFRM_SUB_POLICY
2538 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002539 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002540 &fl, family,
2541 XFRM_POLICY_IN);
2542 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002543 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002544 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002545 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002546 }
James Morris9d729f72007-03-04 16:12:44 -08002547 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002548 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002549 }
2550 }
2551#endif
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (pol->action == XFRM_POLICY_ALLOW) {
2554 struct sec_path *sp;
2555 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002556 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002557 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002558 struct xfrm_tmpl **tpp = tp;
2559 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 int i, k;
2561
2562 if ((sp = skb->sp) == NULL)
2563 sp = &dummy;
2564
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002565 for (pi = 0; pi < npols; pi++) {
2566 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002567 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002568 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002569 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002570 }
2571 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002572 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002573 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002574 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002575 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2576 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2577 }
2578 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002579 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002580 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002581 tpp = stp;
2582 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /* For each tunnel xfrm, find the first matching tmpl.
2585 * For each tmpl before that, find corresponding xfrm.
2586 * Order is _important_. Later we will implement
2587 * some barriers, but at the moment barriers
2588 * are implied between each two transformations.
2589 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002590 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2591 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002592 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002593 if (k < -1)
2594 /* "-2 - errored_index" returned */
2595 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002596 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002601 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002602 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002606 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 1;
2608 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002609 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002612 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002613reject_error:
2614 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return 0;
2616}
2617EXPORT_SYMBOL(__xfrm_policy_check);
2618
2619int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2620{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002621 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002623 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002624 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002626 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002627 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002631 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002632
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002633 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002634 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002635 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002636 dst = NULL;
2637 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002638 skb_dst_set(skb, dst);
2639 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641EXPORT_SYMBOL(__xfrm_route_forward);
2642
David S. Millerd49c73c2006-08-13 18:55:53 -07002643/* Optimize later using cookies and generation ids. */
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2646{
David S. Millerd49c73c2006-08-13 18:55:53 -07002647 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002648 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2649 * get validated by dst_ops->check on every use. We do this
2650 * because when a normal route referenced by an XFRM dst is
2651 * obsoleted we do not go looking around for all parent
2652 * referencing XFRM dsts so that we can invalidate them. It
2653 * is just too much work. Instead we make the checks here on
2654 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002655 *
2656 * XFRM dst A --> IPv4 dst X
2657 *
2658 * X is the "xdst->route" of A (X is also the "dst->path" of A
2659 * in this example). If X is marked obsolete, "A" will not
2660 * notice. That's what we are validating here via the
2661 * stale_bundle() check.
2662 *
2663 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002664 * dst which causes it's ->obsolete field to be set to
2665 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2666 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002667 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002668 if (dst->obsolete < 0 && !stale_bundle(dst))
2669 return dst;
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return NULL;
2672}
2673
2674static int stale_bundle(struct dst_entry *dst)
2675{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002676 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Herbert Xuaabc9762005-05-03 16:27:10 -07002679void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002682 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002683 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 dev_put(dev);
2685 }
2686}
Herbert Xuaabc9762005-05-03 16:27:10 -07002687EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689static void xfrm_link_failure(struct sk_buff *skb)
2690{
2691 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
2694static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2695{
2696 if (dst) {
2697 if (dst->obsolete) {
2698 dst_release(dst);
2699 dst = NULL;
2700 }
2701 }
2702 return dst;
2703}
2704
Paul Mooree4c17212013-05-29 07:36:25 +00002705void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002706{
Fan Duca925cf2014-01-18 09:55:27 +08002707 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002708}
Paul Mooree4c17212013-05-29 07:36:25 +00002709EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002710
2711static void xfrm_garbage_collect_deferred(struct net *net)
2712{
Fan Duca925cf2014-01-18 09:55:27 +08002713 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002714}
2715
Herbert Xu25ee3282007-12-11 09:32:34 -08002716static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 do {
2719 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2720 u32 pmtu, route_mtu_cached;
2721
2722 pmtu = dst_mtu(dst->child);
2723 xdst->child_mtu_cached = pmtu;
2724
2725 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2726
2727 route_mtu_cached = dst_mtu(xdst->route);
2728 xdst->route_mtu_cached = route_mtu_cached;
2729
2730 if (pmtu > route_mtu_cached)
2731 pmtu = route_mtu_cached;
2732
David S. Millerdefb3512010-12-08 21:16:57 -08002733 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 } while ((dst = dst->next));
2735}
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737/* Check that the bundle accepts the flow and its components are
2738 * still valid.
2739 */
2740
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002741static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
2743 struct dst_entry *dst = &first->u.dst;
2744 struct xfrm_dst *last;
2745 u32 mtu;
2746
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002747 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 (dst->dev && !netif_running(dst->dev)))
2749 return 0;
2750
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002751 if (dst->flags & DST_XFRM_QUEUE)
2752 return 1;
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 last = NULL;
2755
2756 do {
2757 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2760 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002761 if (xdst->xfrm_genid != dst->xfrm->genid)
2762 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002763 if (xdst->num_pols > 0 &&
2764 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 mtu = dst_mtu(dst->child);
2768 if (xdst->child_mtu_cached != mtu) {
2769 last = xdst;
2770 xdst->child_mtu_cached = mtu;
2771 }
2772
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002773 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return 0;
2775 mtu = dst_mtu(xdst->route);
2776 if (xdst->route_mtu_cached != mtu) {
2777 last = xdst;
2778 xdst->route_mtu_cached = mtu;
2779 }
2780
2781 dst = dst->child;
2782 } while (dst->xfrm);
2783
2784 if (likely(!last))
2785 return 1;
2786
2787 mtu = last->child_mtu_cached;
2788 for (;;) {
2789 dst = &last->u.dst;
2790
2791 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2792 if (mtu > last->route_mtu_cached)
2793 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002794 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 if (last == first)
2797 break;
2798
Patrick McHardybd0bf072007-07-18 01:55:52 -07002799 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 last->child_mtu_cached = mtu;
2801 }
2802
2803 return 1;
2804}
2805
David S. Miller0dbaee32010-12-13 12:52:14 -08002806static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2807{
2808 return dst_metric_advmss(dst->path);
2809}
2810
Steffen Klassertebb762f2011-11-23 02:12:51 +00002811static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002812{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002813 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2814
2815 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002816}
2817
David S. Millerf894cbf2012-07-02 21:52:24 -07002818static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2819 struct sk_buff *skb,
2820 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002821{
David S. Millerf894cbf2012-07-02 21:52:24 -07002822 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002823}
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2826{
2827 int err = 0;
2828 if (unlikely(afinfo == NULL))
2829 return -EINVAL;
2830 if (unlikely(afinfo->family >= NPROTO))
2831 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002832 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002834 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 else {
2836 struct dst_ops *dst_ops = afinfo->dst_ops;
2837 if (likely(dst_ops->kmem_cachep == NULL))
2838 dst_ops->kmem_cachep = xfrm_dst_cache;
2839 if (likely(dst_ops->check == NULL))
2840 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002841 if (likely(dst_ops->default_advmss == NULL))
2842 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002843 if (likely(dst_ops->mtu == NULL))
2844 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (likely(dst_ops->negative_advice == NULL))
2846 dst_ops->negative_advice = xfrm_negative_advice;
2847 if (likely(dst_ops->link_failure == NULL))
2848 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002849 if (likely(dst_ops->neigh_lookup == NULL))
2850 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002852 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002853 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002855 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return err;
2858}
2859EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2860
2861int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2862{
2863 int err = 0;
2864 if (unlikely(afinfo == NULL))
2865 return -EINVAL;
2866 if (unlikely(afinfo->family >= NPROTO))
2867 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002868 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2870 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2871 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002872 else
2873 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2874 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002876 spin_unlock(&xfrm_policy_afinfo_lock);
2877 if (!err) {
2878 struct dst_ops *dst_ops = afinfo->dst_ops;
2879
2880 synchronize_rcu();
2881
2882 dst_ops->kmem_cachep = NULL;
2883 dst_ops->check = NULL;
2884 dst_ops->negative_advice = NULL;
2885 dst_ops->link_failure = NULL;
2886 afinfo->garbage_collect = NULL;
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return err;
2889}
2890EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2893{
Jiri Pirko351638e2013-05-28 01:30:21 +00002894 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 switch (event) {
2897 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002898 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900 return NOTIFY_DONE;
2901}
2902
2903static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002904 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905};
2906
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002907#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002908static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002909{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002910 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002911 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2912 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002913 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002914 rv = xfrm_proc_init(net);
2915 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002916 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002917 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002918}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002919
2920static void xfrm_statistics_fini(struct net *net)
2921{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002922 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002923 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002924}
2925#else
2926static int __net_init xfrm_statistics_init(struct net *net)
2927{
2928 return 0;
2929}
2930
2931static void xfrm_statistics_fini(struct net *net)
2932{
2933}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002934#endif
2935
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002936static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
David S. Miller2518c7c2006-08-24 04:45:07 -07002938 unsigned int hmask, sz;
2939 int dir;
2940
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002941 if (net_eq(net, &init_net))
2942 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002944 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002945 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
David S. Miller2518c7c2006-08-24 04:45:07 -07002947 hmask = 8 - 1;
2948 sz = (hmask+1) * sizeof(struct hlist_head);
2949
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002950 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2951 if (!net->xfrm.policy_byidx)
2952 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002953 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002954
Herbert Xu53c2e282014-11-13 17:09:49 +08002955 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002956 struct xfrm_policy_hash *htab;
2957
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002958 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002959 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002960 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002961
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002962 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002963 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002964 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002965 goto out_bydst;
2966 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002967 htab->dbits4 = 32;
2968 htab->sbits4 = 32;
2969 htab->dbits6 = 128;
2970 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002971 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002972 net->xfrm.policy_hthresh.lbits4 = 32;
2973 net->xfrm.policy_hthresh.rbits4 = 32;
2974 net->xfrm.policy_hthresh.lbits6 = 128;
2975 net->xfrm.policy_hthresh.rbits6 = 128;
2976
2977 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002978
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002979 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002980 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002981 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002982 if (net_eq(net, &init_net))
2983 register_netdevice_notifier(&xfrm_dev_notifier);
2984 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002985
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002986out_bydst:
2987 for (dir--; dir >= 0; dir--) {
2988 struct xfrm_policy_hash *htab;
2989
2990 htab = &net->xfrm.policy_bydst[dir];
2991 xfrm_hash_free(htab->table, sz);
2992 }
2993 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002994out_byidx:
2995 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002998static void xfrm_policy_fini(struct net *net)
2999{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003000 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003001 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003002
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003003 flush_work(&net->xfrm.policy_hash_work);
3004#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003005 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003006#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003007 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003008
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003009 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003010
Herbert Xu53c2e282014-11-13 17:09:49 +08003011 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003012 struct xfrm_policy_hash *htab;
3013
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003014 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003015
3016 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003017 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003018 WARN_ON(!hlist_empty(htab->table));
3019 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003020 }
3021
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003022 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003023 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3024 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003025}
3026
3027static int __net_init xfrm_net_init(struct net *net)
3028{
3029 int rv;
3030
Florian Westphalf68a09c2017-02-08 11:52:29 +01003031 /* Initialize the per-net locks here */
3032 spin_lock_init(&net->xfrm.xfrm_state_lock);
3033 spin_lock_init(&net->xfrm.xfrm_policy_lock);
3034 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3035
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003036 rv = xfrm_statistics_init(net);
3037 if (rv < 0)
3038 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003039 rv = xfrm_state_init(net);
3040 if (rv < 0)
3041 goto out_state;
3042 rv = xfrm_policy_init(net);
3043 if (rv < 0)
3044 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003045 rv = xfrm_sysctl_init(net);
3046 if (rv < 0)
3047 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003048 rv = flow_cache_init(net);
3049 if (rv < 0)
3050 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003051
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003052 return 0;
3053
Steffen Klassert4a93f502014-03-12 09:43:17 +01003054out:
3055 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003056out_sysctl:
3057 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003058out_policy:
3059 xfrm_state_fini(net);
3060out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003061 xfrm_statistics_fini(net);
3062out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003063 return rv;
3064}
3065
3066static void __net_exit xfrm_net_exit(struct net *net)
3067{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003068 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003069 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003070 xfrm_policy_fini(net);
3071 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003072 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003073}
3074
3075static struct pernet_operations __net_initdata xfrm_net_ops = {
3076 .init = xfrm_net_init,
3077 .exit = xfrm_net_exit,
3078};
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080void __init xfrm_init(void)
3081{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003082 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003083 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 xfrm_input_init();
3085}
3086
Joy Lattenab5f5e82007-09-17 11:51:22 -07003087#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003088static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3089 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003090{
Paul Moore875179f2007-12-01 23:27:18 +11003091 struct xfrm_sec_ctx *ctx = xp->security;
3092 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003093
Paul Moore875179f2007-12-01 23:27:18 +11003094 if (ctx)
3095 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3096 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3097
Weilong Chen9b7a7872013-12-24 09:43:46 +08003098 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003099 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003100 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003101 if (sel->prefixlen_s != 32)
3102 audit_log_format(audit_buf, " src_prefixlen=%d",
3103 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003104 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003105 if (sel->prefixlen_d != 32)
3106 audit_log_format(audit_buf, " dst_prefixlen=%d",
3107 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003108 break;
3109 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003110 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003111 if (sel->prefixlen_s != 128)
3112 audit_log_format(audit_buf, " src_prefixlen=%d",
3113 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003114 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003115 if (sel->prefixlen_d != 128)
3116 audit_log_format(audit_buf, " dst_prefixlen=%d",
3117 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003118 break;
3119 }
3120}
3121
Tetsuo Handa2e710292014-04-22 21:48:30 +09003122void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003123{
3124 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003125
Paul Mooreafeb14b2007-12-21 14:58:11 -08003126 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003127 if (audit_buf == NULL)
3128 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003129 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003130 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003131 xfrm_audit_common_policyinfo(xp, audit_buf);
3132 audit_log_end(audit_buf);
3133}
3134EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3135
Paul Moore68277ac2007-12-20 20:49:33 -08003136void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003137 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003138{
3139 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003140
Paul Mooreafeb14b2007-12-21 14:58:11 -08003141 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003142 if (audit_buf == NULL)
3143 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003144 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003145 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003146 xfrm_audit_common_policyinfo(xp, audit_buf);
3147 audit_log_end(audit_buf);
3148}
3149EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3150#endif
3151
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003152#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003153static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3154 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003155{
3156 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3157 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003158 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3159 sel_cmp->family) &&
3160 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3161 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003162 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3163 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003164 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003165 }
3166 } else {
3167 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003168 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003169 }
3170 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003171 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003172}
3173
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003174static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3175 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003176{
3177 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178 struct hlist_head *chain;
3179 u32 priority = ~0U;
3180
Florian Westphal9d0380d2016-08-11 15:17:59 +02003181 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
Fan Du8d549c42013-11-07 17:47:49 +08003182 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003183 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003184 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3185 pol->type == type) {
3186 ret = pol;
3187 priority = ret->priority;
3188 break;
3189 }
3190 }
Fan Du8d549c42013-11-07 17:47:49 +08003191 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003192 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003193 if ((pol->priority >= priority) && ret)
3194 break;
3195
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003196 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003197 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003198 ret = pol;
3199 break;
3200 }
3201 }
3202
Li RongQing586f2eb2015-04-30 17:13:41 +08003203 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204
Florian Westphal9d0380d2016-08-11 15:17:59 +02003205 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003206
3207 return ret;
3208}
3209
David S. Millerdd701752011-02-24 00:21:08 -05003210static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003211{
3212 int match = 0;
3213
3214 if (t->mode == m->mode && t->id.proto == m->proto &&
3215 (m->reqid == 0 || t->reqid == m->reqid)) {
3216 switch (t->mode) {
3217 case XFRM_MODE_TUNNEL:
3218 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003219 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3220 m->old_family) &&
3221 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3222 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003223 match = 1;
3224 }
3225 break;
3226 case XFRM_MODE_TRANSPORT:
3227 /* in case of transport mode, template does not store
3228 any IP addresses, hence we just compare mode and
3229 protocol */
3230 match = 1;
3231 break;
3232 default:
3233 break;
3234 }
3235 }
3236 return match;
3237}
3238
3239/* update endpoint address(es) of template(s) */
3240static int xfrm_policy_migrate(struct xfrm_policy *pol,
3241 struct xfrm_migrate *m, int num_migrate)
3242{
3243 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003244 int i, j, n = 0;
3245
3246 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003247 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003248 /* target policy has been deleted */
3249 write_unlock_bh(&pol->lock);
3250 return -ENOENT;
3251 }
3252
3253 for (i = 0; i < pol->xfrm_nr; i++) {
3254 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3255 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3256 continue;
3257 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003258 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3259 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003260 continue;
3261 /* update endpoints */
3262 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3263 sizeof(pol->xfrm_vec[i].id.daddr));
3264 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3265 sizeof(pol->xfrm_vec[i].saddr));
3266 pol->xfrm_vec[i].encap_family = mp->new_family;
3267 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003268 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003269 }
3270 }
3271
3272 write_unlock_bh(&pol->lock);
3273
3274 if (!n)
3275 return -ENODATA;
3276
3277 return 0;
3278}
3279
David S. Millerdd701752011-02-24 00:21:08 -05003280static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003281{
3282 int i, j;
3283
3284 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3285 return -EINVAL;
3286
3287 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003288 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3289 m[i].old_family) &&
3290 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3291 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003292 return -EINVAL;
3293 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3294 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3295 return -EINVAL;
3296
3297 /* check if there is any duplicated entry */
3298 for (j = i + 1; j < num_migrate; j++) {
3299 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3300 sizeof(m[i].old_daddr)) &&
3301 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3302 sizeof(m[i].old_saddr)) &&
3303 m[i].proto == m[j].proto &&
3304 m[i].mode == m[j].mode &&
3305 m[i].reqid == m[j].reqid &&
3306 m[i].old_family == m[j].old_family)
3307 return -EINVAL;
3308 }
3309 }
3310
3311 return 0;
3312}
3313
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003314int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003315 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003316 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003317{
3318 int i, err, nx_cur = 0, nx_new = 0;
3319 struct xfrm_policy *pol = NULL;
3320 struct xfrm_state *x, *xc;
3321 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3322 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3323 struct xfrm_migrate *mp;
3324
Vladis Dronov12a70cc2017-08-02 19:50:14 +02003325 /* Stage 0 - sanity checks */
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003326 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3327 goto out;
3328
Vladis Dronov12a70cc2017-08-02 19:50:14 +02003329 if (dir >= XFRM_POLICY_MAX) {
3330 err = -EINVAL;
3331 goto out;
3332 }
3333
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003334 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003335 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003336 err = -ENOENT;
3337 goto out;
3338 }
3339
3340 /* Stage 2 - find and update state(s) */
3341 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003342 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003343 x_cur[nx_cur] = x;
3344 nx_cur++;
3345 if ((xc = xfrm_state_migrate(x, mp))) {
3346 x_new[nx_new] = xc;
3347 nx_new++;
3348 } else {
3349 err = -ENODATA;
3350 goto restore_state;
3351 }
3352 }
3353 }
3354
3355 /* Stage 3 - update policy */
3356 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3357 goto restore_state;
3358
3359 /* Stage 4 - delete old state(s) */
3360 if (nx_cur) {
3361 xfrm_states_put(x_cur, nx_cur);
3362 xfrm_states_delete(x_cur, nx_cur);
3363 }
3364
3365 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003366 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003367
3368 xfrm_pol_put(pol);
3369
3370 return 0;
3371out:
3372 return err;
3373
3374restore_state:
3375 if (pol)
3376 xfrm_pol_put(pol);
3377 if (nx_cur)
3378 xfrm_states_put(x_cur, nx_cur);
3379 if (nx_new)
3380 xfrm_states_delete(x_new, nx_new);
3381
3382 return err;
3383}
David S. Millere610e672007-02-08 13:29:15 -08003384EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003385#endif