blob: 3d27b9a2fbacbb5c814a2915b527b5104310cdbc [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
Fan Du283bc9f2013-11-07 17:47:50 +0800487 write_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);
Fan Du283bc9f2013-11-07 17:47:50 +0800503 write_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
Fan Du283bc9f2013-11-07 17:47:50 +0800522 write_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
Fan Du283bc9f2013-11-07 17:47:50 +0800530 write_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{
Fan Du283bc9f2013-11-07 17:47:50 +0800563 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000564 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
565 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
566 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
567 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
568 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
569 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
570 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700571 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800572 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700573}
574EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700575
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700576static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800577static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700578{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800579 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700580 int dir, total;
581
582 mutex_lock(&hash_resize_mutex);
583
584 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800585 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800586 if (xfrm_bydst_should_resize(net, dir, &total))
587 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700588 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800589 if (xfrm_byidx_should_resize(net, total))
590 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700591
592 mutex_unlock(&hash_resize_mutex);
593}
594
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200595static void xfrm_hash_rebuild(struct work_struct *work)
596{
597 struct net *net = container_of(work, struct net,
598 xfrm.policy_hthresh.work);
599 unsigned int hmask;
600 struct xfrm_policy *pol;
601 struct xfrm_policy *policy;
602 struct hlist_head *chain;
603 struct hlist_head *odst;
604 struct hlist_node *newpos;
605 int i;
606 int dir;
607 unsigned seq;
608 u8 lbits4, rbits4, lbits6, rbits6;
609
610 mutex_lock(&hash_resize_mutex);
611
612 /* read selector prefixlen thresholds */
613 do {
614 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
615
616 lbits4 = net->xfrm.policy_hthresh.lbits4;
617 rbits4 = net->xfrm.policy_hthresh.rbits4;
618 lbits6 = net->xfrm.policy_hthresh.lbits6;
619 rbits6 = net->xfrm.policy_hthresh.rbits6;
620 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
621
622 write_lock_bh(&net->xfrm.xfrm_policy_lock);
623
624 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800625 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200626 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
627 hmask = net->xfrm.policy_bydst[dir].hmask;
628 odst = net->xfrm.policy_bydst[dir].table;
629 for (i = hmask; i >= 0; i--)
630 INIT_HLIST_HEAD(odst + i);
631 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
632 /* dir out => dst = remote, src = local */
633 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
634 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
635 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
636 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
637 } else {
638 /* dir in/fwd => dst = local, src = remote */
639 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
640 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
641 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
642 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
643 }
644 }
645
646 /* re-insert all policies by order of creation */
647 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
648 newpos = NULL;
649 chain = policy_hash_bysel(net, &policy->selector,
650 policy->family,
651 xfrm_policy_id2dir(policy->index));
652 hlist_for_each_entry(pol, chain, bydst) {
653 if (policy->priority >= pol->priority)
654 newpos = &pol->bydst;
655 else
656 break;
657 }
658 if (newpos)
659 hlist_add_behind(&policy->bydst, newpos);
660 else
661 hlist_add_head(&policy->bydst, chain);
662 }
663
664 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
665
666 mutex_unlock(&hash_resize_mutex);
667}
668
669void xfrm_policy_hash_rebuild(struct net *net)
670{
671 schedule_work(&net->xfrm.policy_hthresh.work);
672}
673EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/* Generate new index... KAME seems to generate them ordered by cost
676 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800677static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 static u32 idx_generator;
680
681 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700682 struct hlist_head *list;
683 struct xfrm_policy *p;
684 u32 idx;
685 int found;
686
Fan Due682adf02013-11-07 17:47:48 +0800687 if (!index) {
688 idx = (idx_generator | dir);
689 idx_generator += 8;
690 } else {
691 idx = index;
692 index = 0;
693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (idx == 0)
696 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800697 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700698 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800699 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 if (p->index == idx) {
701 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700705 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return idx;
707 }
708}
709
David S. Miller2518c7c2006-08-24 04:45:07 -0700710static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
711{
712 u32 *p1 = (u32 *) s1;
713 u32 *p2 = (u32 *) s2;
714 int len = sizeof(struct xfrm_selector) / sizeof(u32);
715 int i;
716
717 for (i = 0; i < len; i++) {
718 if (p1[i] != p2[i])
719 return 1;
720 }
721
722 return 0;
723}
724
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100725static void xfrm_policy_requeue(struct xfrm_policy *old,
726 struct xfrm_policy *new)
727{
728 struct xfrm_policy_queue *pq = &old->polq;
729 struct sk_buff_head list;
730
Li RongQingde2ad482015-04-30 17:25:19 +0800731 if (skb_queue_empty(&pq->hold_queue))
732 return;
733
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100734 __skb_queue_head_init(&list);
735
736 spin_lock_bh(&pq->hold_queue.lock);
737 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200738 if (del_timer(&pq->hold_timer))
739 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100740 spin_unlock_bh(&pq->hold_queue.lock);
741
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100742 pq = &new->polq;
743
744 spin_lock_bh(&pq->hold_queue.lock);
745 skb_queue_splice(&list, &pq->hold_queue);
746 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200747 if (!mod_timer(&pq->hold_timer, jiffies))
748 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100749 spin_unlock_bh(&pq->hold_queue.lock);
750}
751
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100752static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
753 struct xfrm_policy *pol)
754{
755 u32 mark = policy->mark.v & policy->mark.m;
756
757 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
758 return true;
759
760 if ((mark & pol->mark.m) == pol->mark.v &&
761 policy->priority == pol->priority)
762 return true;
763
764 return false;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
768{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800769 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700770 struct xfrm_policy *pol;
771 struct xfrm_policy *delpol;
772 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800773 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Fan Du283bc9f2013-11-07 17:47:50 +0800775 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800776 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700777 delpol = NULL;
778 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800779 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800780 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700781 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100782 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800783 xfrm_sec_ctx_match(pol->security, policy->security) &&
784 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800786 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EEXIST;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 delpol = pol;
790 if (policy->priority > pol->priority)
791 continue;
792 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800793 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 continue;
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (delpol)
797 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700800 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700801 else
802 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800803 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800804 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800805
806 /* After previous checking, family can either be AF_INET or AF_INET6 */
807 if (policy->family == AF_INET)
808 rt_genid_bump_ipv4(net);
809 else
810 rt_genid_bump_ipv6(net);
811
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100812 if (delpol) {
813 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800814 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100815 }
Fan Due682adf02013-11-07 17:47:48 +0800816 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800817 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800818 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 policy->curlft.use_time = 0;
820 if (!mod_timer(&policy->timer, jiffies + HZ))
821 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800822 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
David S. Miller9b78a822005-12-22 07:39:48 -0800824 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800826 else if (xfrm_bydst_should_resize(net, dir, NULL))
827 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831EXPORT_SYMBOL(xfrm_policy_insert);
832
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000833struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
834 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800835 struct xfrm_sec_ctx *ctx, int delete,
836 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
David S. Miller2518c7c2006-08-24 04:45:07 -0700838 struct xfrm_policy *pol, *ret;
839 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Eric Parisef41aaa2007-03-07 15:37:58 -0800841 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800842 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800843 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700844 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800845 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000847 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 !selector_cmp(sel, &pol->selector) &&
849 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700851 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700852 *err = security_xfrm_policy_delete(
853 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800854 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800855 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800856 return pol;
857 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800858 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700859 }
860 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 }
863 }
Fan Du283bc9f2013-11-07 17:47:50 +0800864 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000866 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700867 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
Trent Jaegerdf718372005-12-13 23:12:27 -0800870EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000872struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
873 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
David S. Miller2518c7c2006-08-24 04:45:07 -0700875 struct xfrm_policy *pol, *ret;
876 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Herbert Xub5505c62007-05-14 02:15:47 -0700878 *err = -ENOENT;
879 if (xfrm_policy_id2dir(id) != dir)
880 return NULL;
881
Eric Parisef41aaa2007-03-07 15:37:58 -0800882 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800883 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800884 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700885 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000887 if (pol->type == type && pol->index == id &&
888 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700890 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700891 *err = security_xfrm_policy_delete(
892 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800893 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800894 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800895 return pol;
896 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800897 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700898 }
899 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
901 }
902 }
Fan Du283bc9f2013-11-07 17:47:50 +0800903 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000905 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700906 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909EXPORT_SYMBOL(xfrm_policy_byid);
910
Joy Latten4aa2e622007-06-04 19:05:57 -0400911#ifdef CONFIG_SECURITY_NETWORK_XFRM
912static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900913xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Joy Latten4aa2e622007-06-04 19:05:57 -0400915 int dir, err = 0;
916
917 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
918 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400919 int i;
920
Sasha Levinb67bfe02013-02-27 17:06:00 -0800921 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800922 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400923 if (pol->type != type)
924 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700925 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400926 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900927 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400928 return err;
929 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900930 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800931 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800932 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800933 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400934 bydst) {
935 if (pol->type != type)
936 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700937 err = security_xfrm_policy_delete(
938 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400939 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700940 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900941 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400942 return err;
943 }
944 }
945 }
946 }
947 return err;
948}
949#else
950static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900951xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400952{
953 return 0;
954}
955#endif
956
Tetsuo Handa2e710292014-04-22 21:48:30 +0900957int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400958{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000959 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Fan Du283bc9f2013-11-07 17:47:50 +0800961 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400962
Tetsuo Handa2e710292014-04-22 21:48:30 +0900963 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400964 if (err)
965 goto out;
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700968 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800969 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700970
971 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800972 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800973 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700974 if (pol->type != type)
975 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000976 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800977 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000978 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Tetsuo Handa2e710292014-04-22 21:48:30 +0900980 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600981
David S. Miller2518c7c2006-08-24 04:45:07 -0700982 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Fan Du283bc9f2013-11-07 17:47:50 +0800984 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700985 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700987
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800988 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700989 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800990 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800991 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700992 bydst) {
993 if (pol->type != type)
994 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000995 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800996 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000997 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700998
Tetsuo Handa2e710292014-04-22 21:48:30 +0900999 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -07001000 xfrm_policy_kill(pol);
1001
Fan Du283bc9f2013-11-07 17:47:50 +08001002 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001003 goto again2;
1004 }
1005 }
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001008 if (!cnt)
1009 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001010out:
Fan Du283bc9f2013-11-07 17:47:50 +08001011 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001012 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014EXPORT_SYMBOL(xfrm_policy_flush);
1015
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001016int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001017 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 void *data)
1019{
Herbert Xu12a169e2008-10-01 07:03:24 -07001020 struct xfrm_policy *pol;
1021 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001022 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Timo Teras4c563f72008-02-28 21:31:08 -08001024 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1025 walk->type != XFRM_POLICY_TYPE_ANY)
1026 return -EINVAL;
1027
Herbert Xu12a169e2008-10-01 07:03:24 -07001028 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001029 return 0;
1030
Fan Du283bc9f2013-11-07 17:47:50 +08001031 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001032 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001033 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001034 else
Li RongQing80077702015-04-22 17:09:54 +08001035 x = list_first_entry(&walk->walk.all,
1036 struct xfrm_policy_walk_entry, all);
1037
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001038 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001039 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001040 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001041 pol = container_of(x, struct xfrm_policy, walk);
1042 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1043 walk->type != pol->type)
1044 continue;
1045 error = func(pol, xfrm_policy_id2dir(pol->index),
1046 walk->seq, data);
1047 if (error) {
1048 list_move_tail(&walk->walk.all, &x->all);
1049 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001050 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001051 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001053 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001054 error = -ENOENT;
1055 goto out;
1056 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001057 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058out:
Fan Du283bc9f2013-11-07 17:47:50 +08001059 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return error;
1061}
1062EXPORT_SYMBOL(xfrm_policy_walk);
1063
Herbert Xu12a169e2008-10-01 07:03:24 -07001064void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1065{
1066 INIT_LIST_HEAD(&walk->walk.all);
1067 walk->walk.dead = 1;
1068 walk->type = type;
1069 walk->seq = 0;
1070}
1071EXPORT_SYMBOL(xfrm_policy_walk_init);
1072
Fan Du283bc9f2013-11-07 17:47:50 +08001073void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001074{
1075 if (list_empty(&walk->walk.all))
1076 return;
1077
Fan Du283bc9f2013-11-07 17:47:50 +08001078 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001079 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001080 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001081}
1082EXPORT_SYMBOL(xfrm_policy_walk_done);
1083
James Morris134b0fc2006-10-05 15:42:27 -05001084/*
1085 * Find policy to apply to this flow.
1086 *
1087 * Returns 0 if policy found, else an -errno.
1088 */
David S. Millerf299d552011-02-24 01:23:30 -05001089static int xfrm_policy_match(const struct xfrm_policy *pol,
1090 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001091 u8 type, u16 family, int dir)
1092{
David S. Millerf299d552011-02-24 01:23:30 -05001093 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001094 int ret = -ESRCH;
1095 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001096
1097 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001098 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001099 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001100 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001101
1102 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001103 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001104 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001105 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001106
James Morris134b0fc2006-10-05 15:42:27 -05001107 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001108}
1109
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001110static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001111 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001112 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
James Morris134b0fc2006-10-05 15:42:27 -05001114 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001115 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001116 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001117 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001118 unsigned int sequence;
1119 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001120
1121 daddr = xfrm_flowi_daddr(fl, family);
1122 saddr = xfrm_flowi_saddr(fl, family);
1123 if (unlikely(!daddr || !saddr))
1124 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Florian Westphala7c442472016-08-11 15:17:56 +02001126 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001127 retry:
1128 do {
1129 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1130 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1131 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1132
1133 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001134 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001135 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001136 err = xfrm_policy_match(pol, fl, type, family, dir);
1137 if (err) {
1138 if (err == -ESRCH)
1139 continue;
1140 else {
1141 ret = ERR_PTR(err);
1142 goto fail;
1143 }
1144 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001145 ret = pol;
1146 priority = ret->priority;
1147 break;
1148 }
1149 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001150 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001151 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001152 if ((pol->priority >= priority) && ret)
1153 break;
1154
James Morris134b0fc2006-10-05 15:42:27 -05001155 err = xfrm_policy_match(pol, fl, type, family, dir);
1156 if (err) {
1157 if (err == -ESRCH)
1158 continue;
1159 else {
1160 ret = ERR_PTR(err);
1161 goto fail;
1162 }
Li RongQing8faf4912015-05-14 11:16:59 +08001163 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001164 ret = pol;
1165 break;
1166 }
1167 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001168
Florian Westphal30846092016-08-11 15:17:54 +02001169 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1170 goto retry;
1171
Florian Westphale37cc8a2016-08-11 15:17:55 +02001172 if (ret && !xfrm_pol_hold_rcu(ret))
1173 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001174fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001175 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001176
David S. Miller2518c7c2006-08-24 04:45:07 -07001177 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001178}
1179
Timo Teräs80c802f2010-04-07 00:30:05 +00001180static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001181__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001182{
1183#ifdef CONFIG_XFRM_SUB_POLICY
1184 struct xfrm_policy *pol;
1185
1186 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1187 if (pol != NULL)
1188 return pol;
1189#endif
1190 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1191}
1192
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001193static int flow_to_policy_dir(int dir)
1194{
1195 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1196 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1197 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1198 return dir;
1199
1200 switch (dir) {
1201 default:
1202 case FLOW_DIR_IN:
1203 return XFRM_POLICY_IN;
1204 case FLOW_DIR_OUT:
1205 return XFRM_POLICY_OUT;
1206 case FLOW_DIR_FWD:
1207 return XFRM_POLICY_FWD;
1208 }
1209}
1210
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001211static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001212xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001213 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001214{
1215 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001216
1217 if (old_obj)
1218 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001219
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001220 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001221 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001222 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001223
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001224 /* Resolver returns two references:
1225 * one for cache and one for caller of flow_cache_lookup() */
1226 xfrm_pol_hold(pol);
1227
1228 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Trent Jaegerdf718372005-12-13 23:12:27 -08001231static inline int policy_to_flow_dir(int dir)
1232{
1233 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001234 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1235 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1236 return dir;
1237 switch (dir) {
1238 default:
1239 case XFRM_POLICY_IN:
1240 return FLOW_DIR_IN;
1241 case XFRM_POLICY_OUT:
1242 return FLOW_DIR_OUT;
1243 case XFRM_POLICY_FWD:
1244 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001245 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001246}
1247
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001248static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001249 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct xfrm_policy *pol;
1252
Eric Dumazetd188ba82015-12-08 07:22:02 -08001253 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001254 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001255 pol = rcu_dereference(sk->sk_policy[dir]);
1256 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001257 bool match = xfrm_selector_match(&pol->selector, fl,
1258 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001259 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001260
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001261 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001262 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1263 pol = NULL;
1264 goto out;
1265 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001266 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001267 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001268 policy_to_flow_dir(dir));
Florian Westphalae337862016-08-11 15:17:57 +02001269 if (!err && !xfrm_pol_hold_rcu(pol))
1270 goto again;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001271 else if (err == -ESRCH)
1272 pol = NULL;
1273 else
1274 pol = ERR_PTR(err);
1275 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 pol = NULL;
1277 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001278out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001279 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return pol;
1281}
1282
1283static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1284{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001285 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001286
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001287 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001288 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 xfrm_pol_hold(pol);
1290}
1291
1292static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1293 int dir)
1294{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001295 struct net *net = xp_net(pol);
1296
Herbert Xu53c2e282014-11-13 17:09:49 +08001297 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001298 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Herbert Xu53c2e282014-11-13 17:09:49 +08001300 /* Socket policies are not hashed. */
1301 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001302 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001303 hlist_del(&pol->byidx);
1304 }
1305
1306 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001307 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001308
1309 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Herbert Xu53c2e282014-11-13 17:09:49 +08001312static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1313{
1314 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1315}
1316
1317static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1318{
1319 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1320}
1321
Herbert Xu4666faa2005-06-18 22:43:22 -07001322int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Fan Du283bc9f2013-11-07 17:47:50 +08001324 struct net *net = xp_net(pol);
1325
1326 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001328 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001333 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
David S. Millera70fcb02006-03-20 19:18:52 -08001335EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1338{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001339 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct xfrm_policy *old_pol;
1341
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001342#ifdef CONFIG_XFRM_SUB_POLICY
1343 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1344 return -EINVAL;
1345#endif
1346
Fan Du283bc9f2013-11-07 17:47:50 +08001347 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001348 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1349 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001351 pol->curlft.add_time = get_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001352 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001353 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001355 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001356 if (old_pol) {
1357 if (pol)
1358 xfrm_policy_requeue(old_pol, pol);
1359
Timo Teräsea2dea92010-03-31 00:17:05 +00001360 /* Unlinking succeeds always. This is the only function
1361 * allowed to delete or replace socket policy.
1362 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001363 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001364 }
Fan Du283bc9f2013-11-07 17:47:50 +08001365 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 if (old_pol) {
1368 xfrm_policy_kill(old_pol);
1369 }
1370 return 0;
1371}
1372
David S. Millerd3e40a92011-02-24 01:25:41 -05001373static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001375 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001376 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 if (newp) {
1379 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001380 if (security_xfrm_policy_clone(old->security,
1381 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001382 kfree(newp);
1383 return NULL; /* ENOMEM */
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 newp->lft = old->lft;
1386 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001387 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 newp->action = old->action;
1389 newp->flags = old->flags;
1390 newp->xfrm_nr = old->xfrm_nr;
1391 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001392 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 memcpy(newp->xfrm_vec, old->xfrm_vec,
1394 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001395 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001396 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001397 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 xfrm_pol_put(newp);
1399 }
1400 return newp;
1401}
1402
Eric Dumazetd188ba82015-12-08 07:22:02 -08001403int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001405 const struct xfrm_policy *p;
1406 struct xfrm_policy *np;
1407 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Eric Dumazetd188ba82015-12-08 07:22:02 -08001409 rcu_read_lock();
1410 for (i = 0; i < 2; i++) {
1411 p = rcu_dereference(osk->sk_policy[i]);
1412 if (p) {
1413 np = clone_policy(p, i);
1414 if (unlikely(!np)) {
1415 ret = -ENOMEM;
1416 break;
1417 }
1418 rcu_assign_pointer(sk->sk_policy[i], np);
1419 }
1420 }
1421 rcu_read_unlock();
1422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001425static int
David Ahern42a7b322015-08-10 16:58:11 -06001426xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1427 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001428{
1429 int err;
1430 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1431
1432 if (unlikely(afinfo == NULL))
1433 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001434 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001435 xfrm_policy_put_afinfo(afinfo);
1436 return err;
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/* Resolve list of templates for the flow, given policy. */
1440
1441static int
David S. Millera6c2e612011-02-22 18:35:39 -08001442xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1443 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001445 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int nx;
1447 int i, error;
1448 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1449 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001450 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Weilong Chen9b7a7872013-12-24 09:43:46 +08001452 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 struct xfrm_state *x;
1454 xfrm_address_t *remote = daddr;
1455 xfrm_address_t *local = saddr;
1456 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1457
Joakim Koskela48b8d782007-07-26 00:08:42 -07001458 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1459 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 remote = &tmpl->id.daddr;
1461 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001462 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001463 error = xfrm_get_saddr(net, fl->flowi_oif,
1464 &tmp, remote,
1465 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001466 if (error)
1467 goto fail;
1468 local = &tmp;
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
1472 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1473
1474 if (x && x->km.state == XFRM_STATE_VALID) {
1475 xfrm[nx++] = x;
1476 daddr = remote;
1477 saddr = local;
1478 continue;
1479 }
1480 if (x) {
1481 error = (x->km.state == XFRM_STATE_ERROR ?
1482 -EINVAL : -EAGAIN);
1483 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001484 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001485 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (!tmpl->optional)
1489 goto fail;
1490 }
1491 return nx;
1492
1493fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001494 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 xfrm_state_put(xfrm[nx]);
1496 return error;
1497}
1498
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001499static int
David S. Millera6c2e612011-02-22 18:35:39 -08001500xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1501 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001502{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001503 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1504 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001505 int cnx = 0;
1506 int error;
1507 int ret;
1508 int i;
1509
1510 for (i = 0; i < npols; i++) {
1511 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1512 error = -ENOBUFS;
1513 goto fail;
1514 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001515
1516 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001517 if (ret < 0) {
1518 error = ret;
1519 goto fail;
1520 } else
1521 cnx += ret;
1522 }
1523
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001524 /* found states are sorted for outbound processing */
1525 if (npols > 1)
1526 xfrm_state_sort(xfrm, tpp, cnx, family);
1527
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001528 return cnx;
1529
1530 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001531 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001532 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001533 return error;
1534
1535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/* Check that the bundle accepts the flow and its components are
1538 * still valid.
1539 */
1540
David S. Miller05d84022011-02-22 17:47:10 -08001541static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001542{
1543 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1544 int tos;
1545
1546 if (!afinfo)
1547 return -EINVAL;
1548
1549 tos = afinfo->get_tos(fl);
1550
1551 xfrm_policy_put_afinfo(afinfo);
1552
1553 return tos;
1554}
1555
Timo Teräs80c802f2010-04-07 00:30:05 +00001556static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1557{
1558 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1559 struct dst_entry *dst = &xdst->u.dst;
1560
1561 if (xdst->route == NULL) {
1562 /* Dummy bundle - if it has xfrms we were not
1563 * able to build bundle as template resolution failed.
1564 * It means we need to try again resolving. */
1565 if (xdst->num_xfrms > 0)
1566 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001567 } else if (dst->flags & DST_XFRM_QUEUE) {
1568 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001569 } else {
1570 /* Real bundle */
1571 if (stale_bundle(dst))
1572 return NULL;
1573 }
1574
1575 dst_hold(dst);
1576 return flo;
1577}
1578
1579static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1580{
1581 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1582 struct dst_entry *dst = &xdst->u.dst;
1583
1584 if (!xdst->route)
1585 return 0;
1586 if (stale_bundle(dst))
1587 return 0;
1588
1589 return 1;
1590}
1591
1592static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1593{
1594 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1595 struct dst_entry *dst = &xdst->u.dst;
1596
1597 dst_free(dst);
1598}
1599
1600static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1601 .get = xfrm_bundle_flo_get,
1602 .check = xfrm_bundle_flo_check,
1603 .delete = xfrm_bundle_flo_delete,
1604};
1605
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001606static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001607{
1608 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001609 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001610 struct xfrm_dst *xdst;
1611
1612 if (!afinfo)
1613 return ERR_PTR(-EINVAL);
1614
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001615 switch (family) {
1616 case AF_INET:
1617 dst_ops = &net->xfrm.xfrm4_dst_ops;
1618 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001619#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001620 case AF_INET6:
1621 dst_ops = &net->xfrm.xfrm6_dst_ops;
1622 break;
1623#endif
1624 default:
1625 BUG();
1626 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001627 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001628
Madalin Bucurd4cae562011-09-26 07:04:36 +00001629 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001630 struct dst_entry *dst = &xdst->u.dst;
1631
1632 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001633 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001634 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001635 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001636
Madalin Bucurd4cae562011-09-26 07:04:36 +00001637 xfrm_policy_put_afinfo(afinfo);
1638
Herbert Xu25ee3282007-12-11 09:32:34 -08001639 return xdst;
1640}
1641
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001642static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1643 int nfheader_len)
1644{
1645 struct xfrm_policy_afinfo *afinfo =
1646 xfrm_policy_get_afinfo(dst->ops->family);
1647 int err;
1648
1649 if (!afinfo)
1650 return -EINVAL;
1651
1652 err = afinfo->init_path(path, dst, nfheader_len);
1653
1654 xfrm_policy_put_afinfo(afinfo);
1655
1656 return err;
1657}
1658
Herbert Xu87c1e122010-03-02 02:51:56 +00001659static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001660 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001661{
1662 struct xfrm_policy_afinfo *afinfo =
1663 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1664 int err;
1665
1666 if (!afinfo)
1667 return -EINVAL;
1668
Herbert Xu87c1e122010-03-02 02:51:56 +00001669 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001670
1671 xfrm_policy_put_afinfo(afinfo);
1672
1673 return err;
1674}
1675
Timo Teräs80c802f2010-04-07 00:30:05 +00001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1678 * all the metrics... Shortly, bundle a bundle.
1679 */
1680
Herbert Xu25ee3282007-12-11 09:32:34 -08001681static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1682 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001683 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001684 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001686 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001687 unsigned long now = jiffies;
1688 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001689 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001690 struct dst_entry *dst_prev = NULL;
1691 struct dst_entry *dst0 = NULL;
1692 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001694 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001695 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001696 int trailer_len = 0;
1697 int tos;
1698 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001699 xfrm_address_t saddr, daddr;
1700
1701 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001702
1703 tos = xfrm_get_tos(fl, family);
1704 err = tos;
1705 if (tos < 0)
1706 goto put_states;
1707
1708 dst_hold(dst);
1709
1710 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001711 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001712 struct dst_entry *dst1 = &xdst->u.dst;
1713
1714 err = PTR_ERR(xdst);
1715 if (IS_ERR(xdst)) {
1716 dst_release(dst);
1717 goto put_states;
1718 }
1719
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001720 if (xfrm[i]->sel.family == AF_UNSPEC) {
1721 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1722 xfrm_af2proto(family));
1723 if (!inner_mode) {
1724 err = -EAFNOSUPPORT;
1725 dst_release(dst);
1726 goto put_states;
1727 }
1728 } else
1729 inner_mode = xfrm[i]->inner_mode;
1730
Herbert Xu25ee3282007-12-11 09:32:34 -08001731 if (!dst_prev)
1732 dst0 = dst1;
1733 else {
1734 dst_prev->child = dst_clone(dst1);
1735 dst1->flags |= DST_NOHASH;
1736 }
1737
1738 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001739 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001740
1741 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1742 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001743 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1744 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001745 err = PTR_ERR(dst);
1746 if (IS_ERR(dst))
1747 goto put_states;
1748 } else
1749 dst_hold(dst);
1750
1751 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001752 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001753
David S. Millerf5b0a872012-07-19 12:31:33 -07001754 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001755 dst1->flags |= DST_HOST;
1756 dst1->lastuse = now;
1757
1758 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001759 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001760
1761 dst1->next = dst_prev;
1762 dst_prev = dst1;
1763
1764 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001765 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1766 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001767 trailer_len += xfrm[i]->props.trailer_len;
1768 }
1769
1770 dst_prev->child = dst;
1771 dst0->path = dst;
1772
1773 err = -ENODEV;
1774 dev = dst->dev;
1775 if (!dev)
1776 goto free_dst;
1777
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001778 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001779 xfrm_init_pmtu(dst_prev);
1780
1781 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1782 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1783
Herbert Xu87c1e122010-03-02 02:51:56 +00001784 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001785 if (err)
1786 goto free_dst;
1787
1788 dst_prev->header_len = header_len;
1789 dst_prev->trailer_len = trailer_len;
1790 header_len -= xdst->u.dst.xfrm->props.header_len;
1791 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1792 }
1793
1794out:
1795 return dst0;
1796
1797put_states:
1798 for (; i < nx; i++)
1799 xfrm_state_put(xfrm[i]);
1800free_dst:
1801 if (dst0)
1802 dst_free(dst0);
1803 dst0 = ERR_PTR(err);
1804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Ying Xueda7c2242014-01-08 10:26:39 +08001807#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001808static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001809{
1810 if (!*target) {
1811 *target = kmalloc(size, GFP_ATOMIC);
1812 if (!*target)
1813 return -ENOMEM;
1814 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001815
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001816 memcpy(*target, src, size);
1817 return 0;
1818}
Ying Xueda7c2242014-01-08 10:26:39 +08001819#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001820
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001821static int xfrm_dst_update_parent(struct dst_entry *dst,
1822 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001823{
1824#ifdef CONFIG_XFRM_SUB_POLICY
1825 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1826 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1827 sel, sizeof(*sel));
1828#else
1829 return 0;
1830#endif
1831}
1832
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001833static int xfrm_dst_update_origin(struct dst_entry *dst,
1834 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001835{
1836#ifdef CONFIG_XFRM_SUB_POLICY
1837 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1838 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1839#else
1840 return 0;
1841#endif
1842}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
David S. Miller73ff93c2011-02-22 18:33:42 -08001844static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001845 struct xfrm_policy **pols,
1846 int *num_pols, int *num_xfrms)
1847{
1848 int i;
1849
1850 if (*num_pols == 0 || !pols[0]) {
1851 *num_pols = 0;
1852 *num_xfrms = 0;
1853 return 0;
1854 }
1855 if (IS_ERR(pols[0]))
1856 return PTR_ERR(pols[0]);
1857
1858 *num_xfrms = pols[0]->xfrm_nr;
1859
1860#ifdef CONFIG_XFRM_SUB_POLICY
1861 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1862 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1863 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1864 XFRM_POLICY_TYPE_MAIN,
1865 fl, family,
1866 XFRM_POLICY_OUT);
1867 if (pols[1]) {
1868 if (IS_ERR(pols[1])) {
1869 xfrm_pols_put(pols, *num_pols);
1870 return PTR_ERR(pols[1]);
1871 }
Weilong Chen02d08922013-12-24 09:43:48 +08001872 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001873 (*num_xfrms) += pols[1]->xfrm_nr;
1874 }
1875 }
1876#endif
1877 for (i = 0; i < *num_pols; i++) {
1878 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1879 *num_xfrms = -1;
1880 break;
1881 }
1882 }
1883
1884 return 0;
1885
1886}
1887
1888static struct xfrm_dst *
1889xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001890 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001891 struct dst_entry *dst_orig)
1892{
1893 struct net *net = xp_net(pols[0]);
1894 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1895 struct dst_entry *dst;
1896 struct xfrm_dst *xdst;
1897 int err;
1898
1899 /* Try to instantiate a bundle */
1900 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001901 if (err <= 0) {
1902 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001903 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1904 return ERR_PTR(err);
1905 }
1906
1907 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1908 if (IS_ERR(dst)) {
1909 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1910 return ERR_CAST(dst);
1911 }
1912
1913 xdst = (struct xfrm_dst *)dst;
1914 xdst->num_xfrms = err;
1915 if (num_pols > 1)
1916 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1917 else
1918 err = xfrm_dst_update_origin(dst, fl);
1919 if (unlikely(err)) {
1920 dst_free(dst);
1921 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1922 return ERR_PTR(err);
1923 }
1924
1925 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001926 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001927 xdst->policy_genid = atomic_read(&pols[0]->genid);
1928
1929 return xdst;
1930}
1931
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001932static void xfrm_policy_queue_process(unsigned long arg)
1933{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001934 struct sk_buff *skb;
1935 struct sock *sk;
1936 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001937 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001938 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001939 struct xfrm_policy_queue *pq = &pol->polq;
1940 struct flowi fl;
1941 struct sk_buff_head list;
1942
1943 spin_lock(&pq->hold_queue.lock);
1944 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001945 if (!skb) {
1946 spin_unlock(&pq->hold_queue.lock);
1947 goto out;
1948 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001949 dst = skb_dst(skb);
1950 sk = skb->sk;
1951 xfrm_decode_session(skb, &fl, dst->ops->family);
1952 spin_unlock(&pq->hold_queue.lock);
1953
1954 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001955 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001956 if (IS_ERR(dst))
1957 goto purge_queue;
1958
1959 if (dst->flags & DST_XFRM_QUEUE) {
1960 dst_release(dst);
1961
1962 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1963 goto purge_queue;
1964
1965 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001966 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1967 xfrm_pol_hold(pol);
1968 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001969 }
1970
1971 dst_release(dst);
1972
1973 __skb_queue_head_init(&list);
1974
1975 spin_lock(&pq->hold_queue.lock);
1976 pq->timeout = 0;
1977 skb_queue_splice_init(&pq->hold_queue, &list);
1978 spin_unlock(&pq->hold_queue.lock);
1979
1980 while (!skb_queue_empty(&list)) {
1981 skb = __skb_dequeue(&list);
1982
1983 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1984 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001985 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001986 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001987 kfree_skb(skb);
1988 continue;
1989 }
1990
1991 nf_reset(skb);
1992 skb_dst_drop(skb);
1993 skb_dst_set(skb, dst);
1994
Eric W. Biederman13206b62015-10-07 16:48:35 -05001995 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001996 }
1997
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001998out:
1999 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002000 return;
2001
2002purge_queue:
2003 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002004 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002005 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002006}
2007
Eric W. Biedermanede20592015-10-07 16:48:47 -05002008static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002009{
2010 unsigned long sched_next;
2011 struct dst_entry *dst = skb_dst(skb);
2012 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002013 struct xfrm_policy *pol = xdst->pols[0];
2014 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002015
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002016 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002017 kfree_skb(skb);
2018 return 0;
2019 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002020
2021 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2022 kfree_skb(skb);
2023 return -EAGAIN;
2024 }
2025
2026 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002027
2028 spin_lock_bh(&pq->hold_queue.lock);
2029
2030 if (!pq->timeout)
2031 pq->timeout = XFRM_QUEUE_TMO_MIN;
2032
2033 sched_next = jiffies + pq->timeout;
2034
2035 if (del_timer(&pq->hold_timer)) {
2036 if (time_before(pq->hold_timer.expires, sched_next))
2037 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002038 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002039 }
2040
2041 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002042 if (!mod_timer(&pq->hold_timer, sched_next))
2043 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002044
2045 spin_unlock_bh(&pq->hold_queue.lock);
2046
2047 return 0;
2048}
2049
2050static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002051 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002052 const struct flowi *fl,
2053 int num_xfrms,
2054 u16 family)
2055{
2056 int err;
2057 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002058 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002059 struct dst_entry *dst1;
2060 struct xfrm_dst *xdst;
2061
2062 xdst = xfrm_alloc_dst(net, family);
2063 if (IS_ERR(xdst))
2064 return xdst;
2065
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002066 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2067 net->xfrm.sysctl_larval_drop ||
2068 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002069 return xdst;
2070
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002071 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002072 dst1 = &xdst->u.dst;
2073 dst_hold(dst);
2074 xdst->route = dst;
2075
2076 dst_copy_metrics(dst1, dst);
2077
2078 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2079 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2080 dst1->lastuse = jiffies;
2081
2082 dst1->input = dst_discard;
2083 dst1->output = xdst_queue_output;
2084
2085 dst_hold(dst);
2086 dst1->child = dst;
2087 dst1->path = dst;
2088
2089 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2090
2091 err = -ENODEV;
2092 dev = dst->dev;
2093 if (!dev)
2094 goto free_dst;
2095
2096 err = xfrm_fill_dst(xdst, dev, fl);
2097 if (err)
2098 goto free_dst;
2099
2100out:
2101 return xdst;
2102
2103free_dst:
2104 dst_release(dst1);
2105 xdst = ERR_PTR(err);
2106 goto out;
2107}
2108
Timo Teräs80c802f2010-04-07 00:30:05 +00002109static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002110xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002111 struct flow_cache_object *oldflo, void *ctx)
2112{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002113 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002114 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2115 struct xfrm_dst *xdst, *new_xdst;
2116 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2117
2118 /* Check if the policies from old bundle are usable */
2119 xdst = NULL;
2120 if (oldflo) {
2121 xdst = container_of(oldflo, struct xfrm_dst, flo);
2122 num_pols = xdst->num_pols;
2123 num_xfrms = xdst->num_xfrms;
2124 pol_dead = 0;
2125 for (i = 0; i < num_pols; i++) {
2126 pols[i] = xdst->pols[i];
2127 pol_dead |= pols[i]->walk.dead;
2128 }
2129 if (pol_dead) {
2130 dst_free(&xdst->u.dst);
2131 xdst = NULL;
2132 num_pols = 0;
2133 num_xfrms = 0;
2134 oldflo = NULL;
2135 }
2136 }
2137
2138 /* Resolve policies to use if we couldn't get them from
2139 * previous cache entry */
2140 if (xdst == NULL) {
2141 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002142 pols[0] = __xfrm_policy_lookup(net, fl, family,
2143 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002144 err = xfrm_expand_policies(fl, family, pols,
2145 &num_pols, &num_xfrms);
2146 if (err < 0)
2147 goto inc_error;
2148 if (num_pols == 0)
2149 return NULL;
2150 if (num_xfrms <= 0)
2151 goto make_dummy_bundle;
2152 }
2153
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002154 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2155 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002156 if (IS_ERR(new_xdst)) {
2157 err = PTR_ERR(new_xdst);
2158 if (err != -EAGAIN)
2159 goto error;
2160 if (oldflo == NULL)
2161 goto make_dummy_bundle;
2162 dst_hold(&xdst->u.dst);
2163 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002164 } else if (new_xdst == NULL) {
2165 num_xfrms = 0;
2166 if (oldflo == NULL)
2167 goto make_dummy_bundle;
2168 xdst->num_xfrms = 0;
2169 dst_hold(&xdst->u.dst);
2170 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002171 }
2172
2173 /* Kill the previous bundle */
2174 if (xdst) {
2175 /* The policies were stolen for newly generated bundle */
2176 xdst->num_pols = 0;
2177 dst_free(&xdst->u.dst);
2178 }
2179
2180 /* Flow cache does not have reference, it dst_free()'s,
2181 * but we do need to return one reference for original caller */
2182 dst_hold(&new_xdst->u.dst);
2183 return &new_xdst->flo;
2184
2185make_dummy_bundle:
2186 /* We found policies, but there's no bundles to instantiate:
2187 * either because the policy blocks, has no transformations or
2188 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002189 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002190 if (IS_ERR(xdst)) {
2191 xfrm_pols_put(pols, num_pols);
2192 return ERR_CAST(xdst);
2193 }
2194 xdst->num_pols = num_pols;
2195 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002196 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002197
2198 dst_hold(&xdst->u.dst);
2199 return &xdst->flo;
2200
2201inc_error:
2202 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2203error:
2204 if (xdst != NULL)
2205 dst_free(&xdst->u.dst);
2206 else
2207 xfrm_pols_put(pols, num_pols);
2208 return ERR_PTR(err);
2209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
David S. Miller2774c132011-03-01 14:59:04 -08002211static struct dst_entry *make_blackhole(struct net *net, u16 family,
2212 struct dst_entry *dst_orig)
2213{
2214 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2215 struct dst_entry *ret;
2216
2217 if (!afinfo) {
2218 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002219 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002220 } else {
2221 ret = afinfo->blackhole_route(net, dst_orig);
2222 }
2223 xfrm_policy_put_afinfo(afinfo);
2224
2225 return ret;
2226}
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228/* Main function: finds/creates a bundle for given flow.
2229 *
2230 * At the moment we eat a raw IP route. Mostly to speed up lookups
2231 * on interfaces with disabled IPsec.
2232 */
David S. Miller452edd52011-03-02 13:27:41 -08002233struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2234 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002235 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002237 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002238 struct flow_cache_object *flo;
2239 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002240 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002241 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002242 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002243 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002244
Timo Teräs80c802f2010-04-07 00:30:05 +00002245 dst = NULL;
2246 xdst = NULL;
2247 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002248
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002249 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002250 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002251 num_pols = 1;
2252 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2253 err = xfrm_expand_policies(fl, family, pols,
2254 &num_pols, &num_xfrms);
2255 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002256 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002257
2258 if (num_pols) {
2259 if (num_xfrms <= 0) {
2260 drop_pols = num_pols;
2261 goto no_transform;
2262 }
2263
2264 xdst = xfrm_resolve_and_create_bundle(
2265 pols, num_pols, fl,
2266 family, dst_orig);
2267 if (IS_ERR(xdst)) {
2268 xfrm_pols_put(pols, num_pols);
2269 err = PTR_ERR(xdst);
2270 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002271 } else if (xdst == NULL) {
2272 num_xfrms = 0;
2273 drop_pols = num_pols;
2274 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002275 }
2276
Steffen Klassertb7eea452014-06-18 12:34:21 +02002277 dst_hold(&xdst->u.dst);
2278 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002279 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002280 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Timo Teräs80c802f2010-04-07 00:30:05 +00002283 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002284 struct xfrm_flo xflo;
2285
2286 xflo.dst_orig = dst_orig;
2287 xflo.flags = flags;
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002290 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002291 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002292 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Timo Teräs80c802f2010-04-07 00:30:05 +00002294 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002295 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002296 if (flo == NULL)
2297 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002298 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002299 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002300 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002301 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002302 xdst = container_of(flo, struct xfrm_dst, flo);
2303
2304 num_pols = xdst->num_pols;
2305 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002306 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002307 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309
Timo Teräs80c802f2010-04-07 00:30:05 +00002310 dst = &xdst->u.dst;
2311 if (route == NULL && num_xfrms > 0) {
2312 /* The only case when xfrm_bundle_lookup() returns a
2313 * bundle with null route, is when the template could
2314 * not be resolved. It means policies are there, but
2315 * bundle could not be created, since we don't yet
2316 * have the xfrm_state's. We need to wait for KM to
2317 * negotiate new SA's or bail out with error.*/
2318 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002319 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002320 err = -EREMOTE;
2321 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002322 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002323
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002324 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002325
2326 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2327 goto error;
2328 }
2329
2330no_transform:
2331 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002332 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Timo Teräs80c802f2010-04-07 00:30:05 +00002334 if ((flags & XFRM_LOOKUP_ICMP) &&
2335 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2336 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002337 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002339
Timo Teräs80c802f2010-04-07 00:30:05 +00002340 for (i = 0; i < num_pols; i++)
2341 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002342
Timo Teräs80c802f2010-04-07 00:30:05 +00002343 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002345 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002346 err = -EPERM;
2347 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002348 } else if (num_xfrms > 0) {
2349 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002350 dst_release(dst_orig);
2351 } else {
2352 /* Flow passes untransformed */
2353 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002354 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002356ok:
2357 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002358 if (dst && dst->xfrm &&
2359 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2360 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002361 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Timo Teräs80c802f2010-04-07 00:30:05 +00002363nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002364 if (!(flags & XFRM_LOOKUP_ICMP)) {
2365 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002366 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002367 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002368 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002370 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002371dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002372 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2373 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002374 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002375 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377EXPORT_SYMBOL(xfrm_lookup);
2378
Steffen Klassertf92ee612014-09-16 10:08:40 +02002379/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2380 * Otherwise we may send out blackholed packets.
2381 */
2382struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2383 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002384 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002385{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002386 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002387 flags | XFRM_LOOKUP_QUEUE |
2388 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002389
2390 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2391 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2392
2393 return dst;
2394}
2395EXPORT_SYMBOL(xfrm_lookup_route);
2396
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002397static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002398xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002399{
2400 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002401
2402 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2403 return 0;
2404 x = skb->sp->xvec[idx];
2405 if (!x->type->reject)
2406 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002407 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002408}
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410/* When skb is transformed back to its "native" form, we have to
2411 * check policy restrictions. At the moment we make this in maximally
2412 * stupid way. Shame on me. :-) Of course, connected sockets must
2413 * have policy cached at them.
2414 */
2415
2416static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002417xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 unsigned short family)
2419{
2420 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002421 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return x->id.proto == tmpl->id.proto &&
2423 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2424 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2425 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002426 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002427 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002428 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2429 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002432/*
2433 * 0 or more than 0 is returned when validation is succeeded (either bypass
2434 * because of optional transport mode, or next index of the mathced secpath
2435 * state with the template.
2436 * -1 is returned when no matching template is found.
2437 * Otherwise "-2 - errored_index" is returned.
2438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002440xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 unsigned short family)
2442{
2443 int idx = start;
2444
2445 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002446 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 return start;
2448 } else
2449 start = -1;
2450 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002451 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002453 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2454 if (start == -1)
2455 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
2459 return start;
2460}
2461
Herbert Xud5422ef2007-12-12 10:44:16 -08002462int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2463 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002466 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 if (unlikely(afinfo == NULL))
2469 return -EAFNOSUPPORT;
2470
Herbert Xud5422ef2007-12-12 10:44:16 -08002471 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002472 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
Herbert Xud5422ef2007-12-12 10:44:16 -08002476EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
David S. Miller9a7386e2011-02-24 01:44:12 -05002478static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002481 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002482 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486
2487 return 0;
2488}
2489
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002490int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 unsigned short family)
2492{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002493 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002495 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2496 int npols = 0;
2497 int xfrm_nr;
2498 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002499 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002501 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002502 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Herbert Xud5422ef2007-12-12 10:44:16 -08002504 reverse = dir & ~XFRM_POLICY_MASK;
2505 dir &= XFRM_POLICY_MASK;
2506 fl_dir = policy_to_flow_dir(dir);
2507
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002508 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002509 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002511 }
2512
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002513 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /* First, check used SA against their selectors. */
2516 if (skb->sp) {
2517 int i;
2518
Weilong Chen9b7a7872013-12-24 09:43:46 +08002519 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002520 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002521 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002522 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526 }
2527
2528 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002529 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002530 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002531 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002532 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002533 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002534 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002535 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002538 if (!pol) {
2539 struct flow_cache_object *flo;
2540
2541 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2542 xfrm_policy_lookup, NULL);
2543 if (IS_ERR_OR_NULL(flo))
2544 pol = ERR_CAST(flo);
2545 else
2546 pol = container_of(flo, struct xfrm_policy, flo);
2547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002549 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002550 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002551 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002552 }
James Morris134b0fc2006-10-05 15:42:27 -05002553
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002554 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002555 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002556 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002557 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002558 return 0;
2559 }
2560 return 1;
2561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
James Morris9d729f72007-03-04 16:12:44 -08002563 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002565 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002566 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002567#ifdef CONFIG_XFRM_SUB_POLICY
2568 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002569 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002570 &fl, family,
2571 XFRM_POLICY_IN);
2572 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002573 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002574 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002575 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002576 }
James Morris9d729f72007-03-04 16:12:44 -08002577 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002578 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002579 }
2580 }
2581#endif
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (pol->action == XFRM_POLICY_ALLOW) {
2584 struct sec_path *sp;
2585 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002586 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002587 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002588 struct xfrm_tmpl **tpp = tp;
2589 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int i, k;
2591
2592 if ((sp = skb->sp) == NULL)
2593 sp = &dummy;
2594
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002595 for (pi = 0; pi < npols; pi++) {
2596 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002597 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002598 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002599 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002600 }
2601 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002602 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002603 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002604 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002605 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2606 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2607 }
2608 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002609 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002610 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002611 tpp = stp;
2612 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* For each tunnel xfrm, find the first matching tmpl.
2615 * For each tmpl before that, find corresponding xfrm.
2616 * Order is _important_. Later we will implement
2617 * some barriers, but at the moment barriers
2618 * are implied between each two transformations.
2619 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002620 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2621 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002622 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002623 if (k < -1)
2624 /* "-2 - errored_index" returned */
2625 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002626 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002631 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002632 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002636 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return 1;
2638 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002639 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002642 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002643reject_error:
2644 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return 0;
2646}
2647EXPORT_SYMBOL(__xfrm_policy_check);
2648
2649int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2650{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002651 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002653 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002654 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002656 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002657 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002661 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002662
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002663 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002664 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002665 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002666 dst = NULL;
2667 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002668 skb_dst_set(skb, dst);
2669 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671EXPORT_SYMBOL(__xfrm_route_forward);
2672
David S. Millerd49c73c2006-08-13 18:55:53 -07002673/* Optimize later using cookies and generation ids. */
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2676{
David S. Millerd49c73c2006-08-13 18:55:53 -07002677 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002678 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2679 * get validated by dst_ops->check on every use. We do this
2680 * because when a normal route referenced by an XFRM dst is
2681 * obsoleted we do not go looking around for all parent
2682 * referencing XFRM dsts so that we can invalidate them. It
2683 * is just too much work. Instead we make the checks here on
2684 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002685 *
2686 * XFRM dst A --> IPv4 dst X
2687 *
2688 * X is the "xdst->route" of A (X is also the "dst->path" of A
2689 * in this example). If X is marked obsolete, "A" will not
2690 * notice. That's what we are validating here via the
2691 * stale_bundle() check.
2692 *
2693 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002694 * dst which causes it's ->obsolete field to be set to
2695 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2696 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002697 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002698 if (dst->obsolete < 0 && !stale_bundle(dst))
2699 return dst;
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return NULL;
2702}
2703
2704static int stale_bundle(struct dst_entry *dst)
2705{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002706 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
Herbert Xuaabc9762005-05-03 16:27:10 -07002709void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002712 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002713 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 dev_put(dev);
2715 }
2716}
Herbert Xuaabc9762005-05-03 16:27:10 -07002717EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719static void xfrm_link_failure(struct sk_buff *skb)
2720{
2721 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
2724static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2725{
2726 if (dst) {
2727 if (dst->obsolete) {
2728 dst_release(dst);
2729 dst = NULL;
2730 }
2731 }
2732 return dst;
2733}
2734
Paul Mooree4c17212013-05-29 07:36:25 +00002735void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002736{
Fan Duca925cf2014-01-18 09:55:27 +08002737 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002738}
Paul Mooree4c17212013-05-29 07:36:25 +00002739EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002740
2741static void xfrm_garbage_collect_deferred(struct net *net)
2742{
Fan Duca925cf2014-01-18 09:55:27 +08002743 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002744}
2745
Herbert Xu25ee3282007-12-11 09:32:34 -08002746static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
2748 do {
2749 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2750 u32 pmtu, route_mtu_cached;
2751
2752 pmtu = dst_mtu(dst->child);
2753 xdst->child_mtu_cached = pmtu;
2754
2755 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2756
2757 route_mtu_cached = dst_mtu(xdst->route);
2758 xdst->route_mtu_cached = route_mtu_cached;
2759
2760 if (pmtu > route_mtu_cached)
2761 pmtu = route_mtu_cached;
2762
David S. Millerdefb3512010-12-08 21:16:57 -08002763 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 } while ((dst = dst->next));
2765}
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767/* Check that the bundle accepts the flow and its components are
2768 * still valid.
2769 */
2770
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002771static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 struct dst_entry *dst = &first->u.dst;
2774 struct xfrm_dst *last;
2775 u32 mtu;
2776
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002777 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 (dst->dev && !netif_running(dst->dev)))
2779 return 0;
2780
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002781 if (dst->flags & DST_XFRM_QUEUE)
2782 return 1;
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 last = NULL;
2785
2786 do {
2787 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2790 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002791 if (xdst->xfrm_genid != dst->xfrm->genid)
2792 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002793 if (xdst->num_pols > 0 &&
2794 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 mtu = dst_mtu(dst->child);
2798 if (xdst->child_mtu_cached != mtu) {
2799 last = xdst;
2800 xdst->child_mtu_cached = mtu;
2801 }
2802
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002803 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
2805 mtu = dst_mtu(xdst->route);
2806 if (xdst->route_mtu_cached != mtu) {
2807 last = xdst;
2808 xdst->route_mtu_cached = mtu;
2809 }
2810
2811 dst = dst->child;
2812 } while (dst->xfrm);
2813
2814 if (likely(!last))
2815 return 1;
2816
2817 mtu = last->child_mtu_cached;
2818 for (;;) {
2819 dst = &last->u.dst;
2820
2821 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2822 if (mtu > last->route_mtu_cached)
2823 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002824 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 if (last == first)
2827 break;
2828
Patrick McHardybd0bf072007-07-18 01:55:52 -07002829 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 last->child_mtu_cached = mtu;
2831 }
2832
2833 return 1;
2834}
2835
David S. Miller0dbaee32010-12-13 12:52:14 -08002836static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2837{
2838 return dst_metric_advmss(dst->path);
2839}
2840
Steffen Klassertebb762f2011-11-23 02:12:51 +00002841static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002842{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002843 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2844
2845 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002846}
2847
David S. Millerf894cbf2012-07-02 21:52:24 -07002848static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2849 struct sk_buff *skb,
2850 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002851{
David S. Millerf894cbf2012-07-02 21:52:24 -07002852 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2856{
2857 int err = 0;
2858 if (unlikely(afinfo == NULL))
2859 return -EINVAL;
2860 if (unlikely(afinfo->family >= NPROTO))
2861 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002862 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002864 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 else {
2866 struct dst_ops *dst_ops = afinfo->dst_ops;
2867 if (likely(dst_ops->kmem_cachep == NULL))
2868 dst_ops->kmem_cachep = xfrm_dst_cache;
2869 if (likely(dst_ops->check == NULL))
2870 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002871 if (likely(dst_ops->default_advmss == NULL))
2872 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002873 if (likely(dst_ops->mtu == NULL))
2874 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (likely(dst_ops->negative_advice == NULL))
2876 dst_ops->negative_advice = xfrm_negative_advice;
2877 if (likely(dst_ops->link_failure == NULL))
2878 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002879 if (likely(dst_ops->neigh_lookup == NULL))
2880 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002882 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002883 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002885 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return err;
2888}
2889EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2890
2891int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2892{
2893 int err = 0;
2894 if (unlikely(afinfo == NULL))
2895 return -EINVAL;
2896 if (unlikely(afinfo->family >= NPROTO))
2897 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002898 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2900 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2901 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002902 else
2903 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2904 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002906 spin_unlock(&xfrm_policy_afinfo_lock);
2907 if (!err) {
2908 struct dst_ops *dst_ops = afinfo->dst_ops;
2909
2910 synchronize_rcu();
2911
2912 dst_ops->kmem_cachep = NULL;
2913 dst_ops->check = NULL;
2914 dst_ops->negative_advice = NULL;
2915 dst_ops->link_failure = NULL;
2916 afinfo->garbage_collect = NULL;
2917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return err;
2919}
2920EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2923{
Jiri Pirko351638e2013-05-28 01:30:21 +00002924 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 switch (event) {
2927 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002928 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
2930 return NOTIFY_DONE;
2931}
2932
2933static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002934 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935};
2936
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002937#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002938static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002939{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002940 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002941 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2942 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002943 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002944 rv = xfrm_proc_init(net);
2945 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002946 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002947 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002948}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002949
2950static void xfrm_statistics_fini(struct net *net)
2951{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002952 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002953 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002954}
2955#else
2956static int __net_init xfrm_statistics_init(struct net *net)
2957{
2958 return 0;
2959}
2960
2961static void xfrm_statistics_fini(struct net *net)
2962{
2963}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002964#endif
2965
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002966static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
David S. Miller2518c7c2006-08-24 04:45:07 -07002968 unsigned int hmask, sz;
2969 int dir;
2970
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002971 if (net_eq(net, &init_net))
2972 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002974 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002975 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
David S. Miller2518c7c2006-08-24 04:45:07 -07002977 hmask = 8 - 1;
2978 sz = (hmask+1) * sizeof(struct hlist_head);
2979
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002980 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2981 if (!net->xfrm.policy_byidx)
2982 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002983 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002984
Herbert Xu53c2e282014-11-13 17:09:49 +08002985 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002986 struct xfrm_policy_hash *htab;
2987
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002988 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002989 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002990 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002991
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002992 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002993 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002994 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002995 goto out_bydst;
2996 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002997 htab->dbits4 = 32;
2998 htab->sbits4 = 32;
2999 htab->dbits6 = 128;
3000 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07003001 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003002 net->xfrm.policy_hthresh.lbits4 = 32;
3003 net->xfrm.policy_hthresh.rbits4 = 32;
3004 net->xfrm.policy_hthresh.lbits6 = 128;
3005 net->xfrm.policy_hthresh.rbits6 = 128;
3006
3007 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003008
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003009 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003010 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003011 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003012 if (net_eq(net, &init_net))
3013 register_netdevice_notifier(&xfrm_dev_notifier);
3014 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003015
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003016out_bydst:
3017 for (dir--; dir >= 0; dir--) {
3018 struct xfrm_policy_hash *htab;
3019
3020 htab = &net->xfrm.policy_bydst[dir];
3021 xfrm_hash_free(htab->table, sz);
3022 }
3023 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003024out_byidx:
3025 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003028static void xfrm_policy_fini(struct net *net)
3029{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003030 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003031 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003032
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003033 flush_work(&net->xfrm.policy_hash_work);
3034#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003035 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003036#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003037 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003038
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003039 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003040
Herbert Xu53c2e282014-11-13 17:09:49 +08003041 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003042 struct xfrm_policy_hash *htab;
3043
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003044 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003045
3046 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003047 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003048 WARN_ON(!hlist_empty(htab->table));
3049 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003050 }
3051
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003052 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003053 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3054 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003055}
3056
3057static int __net_init xfrm_net_init(struct net *net)
3058{
3059 int rv;
3060
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003061 rv = xfrm_statistics_init(net);
3062 if (rv < 0)
3063 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003064 rv = xfrm_state_init(net);
3065 if (rv < 0)
3066 goto out_state;
3067 rv = xfrm_policy_init(net);
3068 if (rv < 0)
3069 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003070 rv = xfrm_sysctl_init(net);
3071 if (rv < 0)
3072 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003073 rv = flow_cache_init(net);
3074 if (rv < 0)
3075 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003076
3077 /* Initialize the per-net locks here */
3078 spin_lock_init(&net->xfrm.xfrm_state_lock);
3079 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003080 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3081
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003082 return 0;
3083
Steffen Klassert4a93f502014-03-12 09:43:17 +01003084out:
3085 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003086out_sysctl:
3087 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003088out_policy:
3089 xfrm_state_fini(net);
3090out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003091 xfrm_statistics_fini(net);
3092out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003093 return rv;
3094}
3095
3096static void __net_exit xfrm_net_exit(struct net *net)
3097{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003098 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003099 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003100 xfrm_policy_fini(net);
3101 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003102 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003103}
3104
3105static struct pernet_operations __net_initdata xfrm_net_ops = {
3106 .init = xfrm_net_init,
3107 .exit = xfrm_net_exit,
3108};
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110void __init xfrm_init(void)
3111{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003112 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003113 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 xfrm_input_init();
3115}
3116
Joy Lattenab5f5e82007-09-17 11:51:22 -07003117#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003118static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3119 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003120{
Paul Moore875179f2007-12-01 23:27:18 +11003121 struct xfrm_sec_ctx *ctx = xp->security;
3122 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003123
Paul Moore875179f2007-12-01 23:27:18 +11003124 if (ctx)
3125 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3126 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3127
Weilong Chen9b7a7872013-12-24 09:43:46 +08003128 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003129 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003130 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003131 if (sel->prefixlen_s != 32)
3132 audit_log_format(audit_buf, " src_prefixlen=%d",
3133 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003134 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003135 if (sel->prefixlen_d != 32)
3136 audit_log_format(audit_buf, " dst_prefixlen=%d",
3137 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003138 break;
3139 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003140 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003141 if (sel->prefixlen_s != 128)
3142 audit_log_format(audit_buf, " src_prefixlen=%d",
3143 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003144 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003145 if (sel->prefixlen_d != 128)
3146 audit_log_format(audit_buf, " dst_prefixlen=%d",
3147 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003148 break;
3149 }
3150}
3151
Tetsuo Handa2e710292014-04-22 21:48:30 +09003152void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153{
3154 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003155
Paul Mooreafeb14b2007-12-21 14:58:11 -08003156 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003157 if (audit_buf == NULL)
3158 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003159 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003160 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003161 xfrm_audit_common_policyinfo(xp, audit_buf);
3162 audit_log_end(audit_buf);
3163}
3164EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3165
Paul Moore68277ac2007-12-20 20:49:33 -08003166void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003167 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003168{
3169 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003170
Paul Mooreafeb14b2007-12-21 14:58:11 -08003171 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003172 if (audit_buf == NULL)
3173 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003174 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003175 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003176 xfrm_audit_common_policyinfo(xp, audit_buf);
3177 audit_log_end(audit_buf);
3178}
3179EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3180#endif
3181
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003182#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003183static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3184 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003185{
3186 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3187 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003188 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3189 sel_cmp->family) &&
3190 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3191 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003192 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3193 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003194 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003195 }
3196 } else {
3197 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003198 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003199 }
3200 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003201 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003202}
3203
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003204static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3205 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003206{
3207 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003208 struct hlist_head *chain;
3209 u32 priority = ~0U;
3210
Fan Du283bc9f2013-11-07 17:47:50 +08003211 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003212 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003213 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003214 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3215 pol->type == type) {
3216 ret = pol;
3217 priority = ret->priority;
3218 break;
3219 }
3220 }
Fan Du8d549c42013-11-07 17:47:49 +08003221 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003222 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003223 if ((pol->priority >= priority) && ret)
3224 break;
3225
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003226 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003227 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003228 ret = pol;
3229 break;
3230 }
3231 }
3232
Li RongQing586f2eb2015-04-30 17:13:41 +08003233 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003234
Fan Du283bc9f2013-11-07 17:47:50 +08003235 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003236
3237 return ret;
3238}
3239
David S. Millerdd701752011-02-24 00:21:08 -05003240static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003241{
3242 int match = 0;
3243
3244 if (t->mode == m->mode && t->id.proto == m->proto &&
3245 (m->reqid == 0 || t->reqid == m->reqid)) {
3246 switch (t->mode) {
3247 case XFRM_MODE_TUNNEL:
3248 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003249 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3250 m->old_family) &&
3251 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3252 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003253 match = 1;
3254 }
3255 break;
3256 case XFRM_MODE_TRANSPORT:
3257 /* in case of transport mode, template does not store
3258 any IP addresses, hence we just compare mode and
3259 protocol */
3260 match = 1;
3261 break;
3262 default:
3263 break;
3264 }
3265 }
3266 return match;
3267}
3268
3269/* update endpoint address(es) of template(s) */
3270static int xfrm_policy_migrate(struct xfrm_policy *pol,
3271 struct xfrm_migrate *m, int num_migrate)
3272{
3273 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003274 int i, j, n = 0;
3275
3276 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003277 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003278 /* target policy has been deleted */
3279 write_unlock_bh(&pol->lock);
3280 return -ENOENT;
3281 }
3282
3283 for (i = 0; i < pol->xfrm_nr; i++) {
3284 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3285 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3286 continue;
3287 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003288 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3289 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003290 continue;
3291 /* update endpoints */
3292 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3293 sizeof(pol->xfrm_vec[i].id.daddr));
3294 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3295 sizeof(pol->xfrm_vec[i].saddr));
3296 pol->xfrm_vec[i].encap_family = mp->new_family;
3297 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003298 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003299 }
3300 }
3301
3302 write_unlock_bh(&pol->lock);
3303
3304 if (!n)
3305 return -ENODATA;
3306
3307 return 0;
3308}
3309
David S. Millerdd701752011-02-24 00:21:08 -05003310static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003311{
3312 int i, j;
3313
3314 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3315 return -EINVAL;
3316
3317 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003318 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3319 m[i].old_family) &&
3320 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3321 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003322 return -EINVAL;
3323 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3324 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3325 return -EINVAL;
3326
3327 /* check if there is any duplicated entry */
3328 for (j = i + 1; j < num_migrate; j++) {
3329 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3330 sizeof(m[i].old_daddr)) &&
3331 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3332 sizeof(m[i].old_saddr)) &&
3333 m[i].proto == m[j].proto &&
3334 m[i].mode == m[j].mode &&
3335 m[i].reqid == m[j].reqid &&
3336 m[i].old_family == m[j].old_family)
3337 return -EINVAL;
3338 }
3339 }
3340
3341 return 0;
3342}
3343
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003344int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003345 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003346 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003347{
3348 int i, err, nx_cur = 0, nx_new = 0;
3349 struct xfrm_policy *pol = NULL;
3350 struct xfrm_state *x, *xc;
3351 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3352 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3353 struct xfrm_migrate *mp;
3354
3355 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3356 goto out;
3357
3358 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003359 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003360 err = -ENOENT;
3361 goto out;
3362 }
3363
3364 /* Stage 2 - find and update state(s) */
3365 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003366 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003367 x_cur[nx_cur] = x;
3368 nx_cur++;
3369 if ((xc = xfrm_state_migrate(x, mp))) {
3370 x_new[nx_new] = xc;
3371 nx_new++;
3372 } else {
3373 err = -ENODATA;
3374 goto restore_state;
3375 }
3376 }
3377 }
3378
3379 /* Stage 3 - update policy */
3380 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3381 goto restore_state;
3382
3383 /* Stage 4 - delete old state(s) */
3384 if (nx_cur) {
3385 xfrm_states_put(x_cur, nx_cur);
3386 xfrm_states_delete(x_cur, nx_cur);
3387 }
3388
3389 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003390 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003391
3392 xfrm_pol_put(pol);
3393
3394 return 0;
3395out:
3396 return err;
3397
3398restore_state:
3399 if (pol)
3400 xfrm_pol_put(pol);
3401 if (nx_cur)
3402 xfrm_states_put(x_cur, nx_cur);
3403 if (nx_new)
3404 xfrm_states_delete(x_new, nx_new);
3405
3406 return err;
3407}
David S. Millere610e672007-02-08 13:29:15 -08003408EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003409#endif