blob: 576d903210686470a6dbef78be3d42f1b7cbced3 [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
David S. Millerbc9b35a2012-05-15 15:04:57 -040063static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050064__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080065{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050066 const struct flowi4 *fl4 = &fl->u.ip4;
67
Alexey Dobriyan26bff942011-11-22 06:46:02 +000068 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
69 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050070 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
72 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
73 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080074}
75
David S. Millerbc9b35a2012-05-15 15:04:57 -040076static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050077__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080078{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050079 const struct flowi6 *fl6 = &fl->u.ip6;
80
81 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
82 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
83 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
84 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
85 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
86 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080087}
88
David S. Millerbc9b35a2012-05-15 15:04:57 -040089bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
90 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080091{
92 switch (family) {
93 case AF_INET:
94 return __xfrm4_selector_match(sel, fl);
95 case AF_INET6:
96 return __xfrm6_selector_match(sel, fl);
97 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040098 return false;
Andrew Morton77681022006-11-08 22:46:26 -080099}
100
Eric Dumazetef8531b2012-08-19 12:31:48 +0200101static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
102{
103 struct xfrm_policy_afinfo *afinfo;
104
105 if (unlikely(family >= NPROTO))
106 return NULL;
107 rcu_read_lock();
108 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
109 if (unlikely(!afinfo))
110 rcu_read_unlock();
111 return afinfo;
112}
113
114static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
115{
116 rcu_read_unlock();
117}
118
David Ahern42a7b322015-08-10 16:58:11 -0600119static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
120 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500121 const xfrm_address_t *saddr,
122 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900123 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800125 struct xfrm_policy_afinfo *afinfo;
126 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Herbert Xu25ee3282007-12-11 09:32:34 -0800128 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800130 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Ahern42a7b322015-08-10 16:58:11 -0600132 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900135
136 return dst;
137}
138
David Ahern42a7b322015-08-10 16:58:11 -0600139static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
140 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900141 xfrm_address_t *prev_saddr,
142 xfrm_address_t *prev_daddr,
143 int family)
144{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800145 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900146 xfrm_address_t *saddr = &x->props.saddr;
147 xfrm_address_t *daddr = &x->id.daddr;
148 struct dst_entry *dst;
149
150 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
151 saddr = x->coaddr;
152 daddr = prev_daddr;
153 }
154 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
155 saddr = prev_saddr;
156 daddr = x->coaddr;
157 }
158
David Ahern42a7b322015-08-10 16:58:11 -0600159 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900160
161 if (!IS_ERR(dst)) {
162 if (prev_saddr != saddr)
163 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
164 if (prev_daddr != daddr)
165 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
166 }
167
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800168 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static inline unsigned long make_jiffies(long secs)
172{
173 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
174 return MAX_SCHEDULE_TIMEOUT-1;
175 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900176 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static void xfrm_policy_timer(unsigned long data)
180{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800181 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800182 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 long next = LONG_MAX;
184 int warn = 0;
185 int dir;
186
187 read_lock(&xp->lock);
188
Timo Teräsea2dea92010-03-31 00:17:05 +0000189 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto out;
191
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700192 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (xp->lft.hard_add_expires_seconds) {
195 long tmo = xp->lft.hard_add_expires_seconds +
196 xp->curlft.add_time - now;
197 if (tmo <= 0)
198 goto expired;
199 if (tmo < next)
200 next = tmo;
201 }
202 if (xp->lft.hard_use_expires_seconds) {
203 long tmo = xp->lft.hard_use_expires_seconds +
204 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
205 if (tmo <= 0)
206 goto expired;
207 if (tmo < next)
208 next = tmo;
209 }
210 if (xp->lft.soft_add_expires_seconds) {
211 long tmo = xp->lft.soft_add_expires_seconds +
212 xp->curlft.add_time - now;
213 if (tmo <= 0) {
214 warn = 1;
215 tmo = XFRM_KM_TIMEOUT;
216 }
217 if (tmo < next)
218 next = tmo;
219 }
220 if (xp->lft.soft_use_expires_seconds) {
221 long tmo = xp->lft.soft_use_expires_seconds +
222 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
223 if (tmo <= 0) {
224 warn = 1;
225 tmo = XFRM_KM_TIMEOUT;
226 }
227 if (tmo < next)
228 next = tmo;
229 }
230
231 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800232 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (next != LONG_MAX &&
234 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
235 xfrm_pol_hold(xp);
236
237out:
238 read_unlock(&xp->lock);
239 xfrm_pol_put(xp);
240 return;
241
242expired:
243 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700244 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800245 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 xfrm_pol_put(xp);
247}
248
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000249static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
250{
251 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
252
253 if (unlikely(pol->walk.dead))
254 flo = NULL;
255 else
256 xfrm_pol_hold(pol);
257
258 return flo;
259}
260
261static int xfrm_policy_flo_check(struct flow_cache_object *flo)
262{
263 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
264
265 return !pol->walk.dead;
266}
267
268static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
269{
270 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
271}
272
273static const struct flow_cache_ops xfrm_policy_fc_ops = {
274 .get = xfrm_policy_flo_get,
275 .check = xfrm_policy_flo_check,
276 .delete = xfrm_policy_flo_delete,
277};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
280 * SPD calls.
281 */
282
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800283struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct xfrm_policy *policy;
286
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700287 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800290 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700291 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700292 INIT_HLIST_NODE(&policy->bydst);
293 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700295 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100296 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800297 setup_timer(&policy->timer, xfrm_policy_timer,
298 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100299 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
300 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000301 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 return policy;
304}
305EXPORT_SYMBOL(xfrm_policy_alloc);
306
Eric Dumazet56f04732015-12-08 07:22:01 -0800307static void xfrm_policy_destroy_rcu(struct rcu_head *head)
308{
309 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
310
311 security_xfrm_policy_free(policy->security);
312 kfree(policy);
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/* Destroy xfrm_policy: descendant resources must be released to this moment. */
316
WANG Cong64c31b32008-01-07 22:34:29 -0800317void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Herbert Xu12a169e2008-10-01 07:03:24 -0700319 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Fan Du0659eea2013-08-01 18:08:36 +0800321 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 BUG();
323
Eric Dumazet56f04732015-12-08 07:22:01 -0800324 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
WANG Cong64c31b32008-01-07 22:34:29 -0800326EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/* Rule must be locked. Release descentant resources, announce
329 * entry dead. The rule must be unlinked from lists to the moment.
330 */
331
332static void xfrm_policy_kill(struct xfrm_policy *policy)
333{
Herbert Xu12a169e2008-10-01 07:03:24 -0700334 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Timo Teräs285ead12010-04-07 00:30:06 +0000336 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200338 if (del_timer(&policy->polq.hold_timer))
339 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800340 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100341
Timo Teräs285ead12010-04-07 00:30:06 +0000342 if (del_timer(&policy->timer))
343 xfrm_pol_put(policy);
344
345 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
David S. Miller2518c7c2006-08-24 04:45:07 -0700348static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
349
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800350static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700351{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800352 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700353}
354
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200355/* calculate policy hash thresholds */
356static void __get_hash_thresh(struct net *net,
357 unsigned short family, int dir,
358 u8 *dbits, u8 *sbits)
359{
360 switch (family) {
361 case AF_INET:
362 *dbits = net->xfrm.policy_bydst[dir].dbits4;
363 *sbits = net->xfrm.policy_bydst[dir].sbits4;
364 break;
365
366 case AF_INET6:
367 *dbits = net->xfrm.policy_bydst[dir].dbits6;
368 *sbits = net->xfrm.policy_bydst[dir].sbits6;
369 break;
370
371 default:
372 *dbits = 0;
373 *sbits = 0;
374 }
375}
376
David S. Miller5f803b52011-02-24 00:33:19 -0500377static struct hlist_head *policy_hash_bysel(struct net *net,
378 const struct xfrm_selector *sel,
379 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700380{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800381 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200382 unsigned int hash;
383 u8 dbits;
384 u8 sbits;
385
386 __get_hash_thresh(net, family, dir, &dbits, &sbits);
387 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700388
Florian Westphale1e551b2016-08-11 15:17:53 +0200389 if (hash == hmask + 1)
390 return &net->xfrm.policy_inexact[dir];
391
392 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
393 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700394}
395
David S. Miller5f803b52011-02-24 00:33:19 -0500396static struct hlist_head *policy_hash_direct(struct net *net,
397 const xfrm_address_t *daddr,
398 const xfrm_address_t *saddr,
399 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700400{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800401 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200402 unsigned int hash;
403 u8 dbits;
404 u8 sbits;
405
406 __get_hash_thresh(net, family, dir, &dbits, &sbits);
407 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700408
Florian Westphale1e551b2016-08-11 15:17:53 +0200409 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
410 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700411}
412
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200413static void xfrm_dst_hash_transfer(struct net *net,
414 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700415 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200416 unsigned int nhashmask,
417 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700418{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800419 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200422 u8 dbits;
423 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700424
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800425redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800426 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700427 unsigned int h;
428
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200429 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700430 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200431 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800432 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200433 hlist_del_rcu(&pol->bydst);
434 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800435 h0 = h;
436 } else {
437 if (h != h0)
438 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200439 hlist_del_rcu(&pol->bydst);
440 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800441 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800442 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800443 }
444 if (!hlist_empty(list)) {
445 entry0 = NULL;
446 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700447 }
448}
449
450static void xfrm_idx_hash_transfer(struct hlist_head *list,
451 struct hlist_head *nidxtable,
452 unsigned int nhashmask)
453{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800454 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700455 struct xfrm_policy *pol;
456
Sasha Levinb67bfe02013-02-27 17:06:00 -0800457 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700458 unsigned int h;
459
460 h = __idx_hash(pol->index, nhashmask);
461 hlist_add_head(&pol->byidx, nidxtable+h);
462 }
463}
464
465static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
466{
467 return ((old_hmask + 1) << 1) - 1;
468}
469
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800470static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700471{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800472 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700473 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
474 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700475 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200476 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700477 int i;
478
479 if (!ndst)
480 return;
481
Fan Du283bc9f2013-11-07 17:47:50 +0800482 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200483 write_seqcount_begin(&xfrm_policy_hash_generation);
484
485 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
486 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700487
Florian Westphale1e551b2016-08-11 15:17:53 +0200488 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
489 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
490
David S. Miller2518c7c2006-08-24 04:45:07 -0700491 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200492 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700493
Florian Westphale1e551b2016-08-11 15:17:53 +0200494 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800495 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700496
Florian Westphal30846092016-08-11 15:17:54 +0200497 write_seqcount_end(&xfrm_policy_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800498 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700499
Florian Westphale1e551b2016-08-11 15:17:53 +0200500 synchronize_rcu();
501
David S. Miller44e36b42006-08-24 04:50:50 -0700502 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700503}
504
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800505static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700506{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800507 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700508 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
509 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700511 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512 int i;
513
514 if (!nidx)
515 return;
516
Fan Du283bc9f2013-11-07 17:47:50 +0800517 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700518
519 for (i = hmask; i >= 0; i--)
520 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
521
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800522 net->xfrm.policy_byidx = nidx;
523 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700524
Fan Du283bc9f2013-11-07 17:47:50 +0800525 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700526
David S. Miller44e36b42006-08-24 04:50:50 -0700527 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700528}
529
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800530static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700531{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800532 unsigned int cnt = net->xfrm.policy_count[dir];
533 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700534
535 if (total)
536 *total += cnt;
537
538 if ((hmask + 1) < xfrm_policy_hashmax &&
539 cnt > hmask)
540 return 1;
541
542 return 0;
543}
544
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800545static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700546{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800547 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700548
549 if ((hmask + 1) < xfrm_policy_hashmax &&
550 total > hmask)
551 return 1;
552
553 return 0;
554}
555
Alexey Dobriyane0710412010-01-23 13:37:10 +0000556void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700557{
Fan Du283bc9f2013-11-07 17:47:50 +0800558 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000559 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
560 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
561 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
562 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
563 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
564 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
565 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700566 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800567 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700568}
569EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700570
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700571static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800572static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700573{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800574 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700575 int dir, total;
576
577 mutex_lock(&hash_resize_mutex);
578
579 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800580 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800581 if (xfrm_bydst_should_resize(net, dir, &total))
582 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700583 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800584 if (xfrm_byidx_should_resize(net, total))
585 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700586
587 mutex_unlock(&hash_resize_mutex);
588}
589
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200590static void xfrm_hash_rebuild(struct work_struct *work)
591{
592 struct net *net = container_of(work, struct net,
593 xfrm.policy_hthresh.work);
594 unsigned int hmask;
595 struct xfrm_policy *pol;
596 struct xfrm_policy *policy;
597 struct hlist_head *chain;
598 struct hlist_head *odst;
599 struct hlist_node *newpos;
600 int i;
601 int dir;
602 unsigned seq;
603 u8 lbits4, rbits4, lbits6, rbits6;
604
605 mutex_lock(&hash_resize_mutex);
606
607 /* read selector prefixlen thresholds */
608 do {
609 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
610
611 lbits4 = net->xfrm.policy_hthresh.lbits4;
612 rbits4 = net->xfrm.policy_hthresh.rbits4;
613 lbits6 = net->xfrm.policy_hthresh.lbits6;
614 rbits6 = net->xfrm.policy_hthresh.rbits6;
615 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
616
617 write_lock_bh(&net->xfrm.xfrm_policy_lock);
618
619 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800620 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200621 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
622 hmask = net->xfrm.policy_bydst[dir].hmask;
623 odst = net->xfrm.policy_bydst[dir].table;
624 for (i = hmask; i >= 0; i--)
625 INIT_HLIST_HEAD(odst + i);
626 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
627 /* dir out => dst = remote, src = local */
628 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
629 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
630 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
631 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
632 } else {
633 /* dir in/fwd => dst = local, src = remote */
634 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
635 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
636 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
637 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
638 }
639 }
640
641 /* re-insert all policies by order of creation */
642 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
643 newpos = NULL;
644 chain = policy_hash_bysel(net, &policy->selector,
645 policy->family,
646 xfrm_policy_id2dir(policy->index));
647 hlist_for_each_entry(pol, chain, bydst) {
648 if (policy->priority >= pol->priority)
649 newpos = &pol->bydst;
650 else
651 break;
652 }
653 if (newpos)
654 hlist_add_behind(&policy->bydst, newpos);
655 else
656 hlist_add_head(&policy->bydst, chain);
657 }
658
659 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
660
661 mutex_unlock(&hash_resize_mutex);
662}
663
664void xfrm_policy_hash_rebuild(struct net *net)
665{
666 schedule_work(&net->xfrm.policy_hthresh.work);
667}
668EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/* Generate new index... KAME seems to generate them ordered by cost
671 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800672static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 static u32 idx_generator;
675
676 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 struct hlist_head *list;
678 struct xfrm_policy *p;
679 u32 idx;
680 int found;
681
Fan Due682adf2013-11-07 17:47:48 +0800682 if (!index) {
683 idx = (idx_generator | dir);
684 idx_generator += 8;
685 } else {
686 idx = index;
687 index = 0;
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (idx == 0)
691 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800692 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700693 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800694 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700695 if (p->index == idx) {
696 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return idx;
702 }
703}
704
David S. Miller2518c7c2006-08-24 04:45:07 -0700705static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
706{
707 u32 *p1 = (u32 *) s1;
708 u32 *p2 = (u32 *) s2;
709 int len = sizeof(struct xfrm_selector) / sizeof(u32);
710 int i;
711
712 for (i = 0; i < len; i++) {
713 if (p1[i] != p2[i])
714 return 1;
715 }
716
717 return 0;
718}
719
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100720static void xfrm_policy_requeue(struct xfrm_policy *old,
721 struct xfrm_policy *new)
722{
723 struct xfrm_policy_queue *pq = &old->polq;
724 struct sk_buff_head list;
725
Li RongQingde2ad482015-04-30 17:25:19 +0800726 if (skb_queue_empty(&pq->hold_queue))
727 return;
728
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100729 __skb_queue_head_init(&list);
730
731 spin_lock_bh(&pq->hold_queue.lock);
732 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200733 if (del_timer(&pq->hold_timer))
734 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100735 spin_unlock_bh(&pq->hold_queue.lock);
736
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100737 pq = &new->polq;
738
739 spin_lock_bh(&pq->hold_queue.lock);
740 skb_queue_splice(&list, &pq->hold_queue);
741 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200742 if (!mod_timer(&pq->hold_timer, jiffies))
743 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100744 spin_unlock_bh(&pq->hold_queue.lock);
745}
746
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100747static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
748 struct xfrm_policy *pol)
749{
750 u32 mark = policy->mark.v & policy->mark.m;
751
752 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
753 return true;
754
755 if ((mark & pol->mark.m) == pol->mark.v &&
756 policy->priority == pol->priority)
757 return true;
758
759 return false;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
763{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800764 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700765 struct xfrm_policy *pol;
766 struct xfrm_policy *delpol;
767 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800768 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Fan Du283bc9f2013-11-07 17:47:50 +0800770 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800771 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700772 delpol = NULL;
773 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800774 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800775 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700776 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100777 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800778 xfrm_sec_ctx_match(pol->security, policy->security) &&
779 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800781 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EEXIST;
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 delpol = pol;
785 if (policy->priority > pol->priority)
786 continue;
787 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800788 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 continue;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (delpol)
792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700795 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700796 else
797 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800798 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800799 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800800
801 /* After previous checking, family can either be AF_INET or AF_INET6 */
802 if (policy->family == AF_INET)
803 rt_genid_bump_ipv4(net);
804 else
805 rt_genid_bump_ipv6(net);
806
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100807 if (delpol) {
808 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800809 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100810 }
Fan Due682adf2013-11-07 17:47:48 +0800811 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800812 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800813 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 policy->curlft.use_time = 0;
815 if (!mod_timer(&policy->timer, jiffies + HZ))
816 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800817 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
David S. Miller9b78a822005-12-22 07:39:48 -0800819 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800821 else if (xfrm_bydst_should_resize(net, dir, NULL))
822 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825}
826EXPORT_SYMBOL(xfrm_policy_insert);
827
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000828struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
829 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800830 struct xfrm_sec_ctx *ctx, int delete,
831 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 struct xfrm_policy *pol, *ret;
834 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Eric Parisef41aaa2007-03-07 15:37:58 -0800836 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800837 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800838 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700839 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800840 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000842 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700843 !selector_cmp(sel, &pol->selector) &&
844 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700847 *err = security_xfrm_policy_delete(
848 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800849 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800850 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800851 return pol;
852 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800853 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700854 }
855 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 break;
857 }
858 }
Fan Du283bc9f2013-11-07 17:47:50 +0800859 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000861 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700863 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Trent Jaegerdf718372005-12-13 23:12:27 -0800865EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000867struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
868 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 struct xfrm_policy *pol, *ret;
871 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Herbert Xub5505c62007-05-14 02:15:47 -0700873 *err = -ENOENT;
874 if (xfrm_policy_id2dir(id) != dir)
875 return NULL;
876
Eric Parisef41aaa2007-03-07 15:37:58 -0800877 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800878 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800879 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700880 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800881 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000882 if (pol->type == type && pol->index == id &&
883 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700885 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700886 *err = security_xfrm_policy_delete(
887 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800888 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800889 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800890 return pol;
891 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800892 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700893 }
894 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 break;
896 }
897 }
Fan Du283bc9f2013-11-07 17:47:50 +0800898 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000900 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700901 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700902 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904EXPORT_SYMBOL(xfrm_policy_byid);
905
Joy Latten4aa2e622007-06-04 19:05:57 -0400906#ifdef CONFIG_SECURITY_NETWORK_XFRM
907static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900908xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Joy Latten4aa2e622007-06-04 19:05:57 -0400910 int dir, err = 0;
911
912 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
913 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400914 int i;
915
Sasha Levinb67bfe02013-02-27 17:06:00 -0800916 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800917 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400918 if (pol->type != type)
919 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700920 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400921 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900922 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400923 return err;
924 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900925 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800926 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800927 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800928 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400929 bydst) {
930 if (pol->type != type)
931 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700932 err = security_xfrm_policy_delete(
933 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400934 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700935 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900936 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400937 return err;
938 }
939 }
940 }
941 }
942 return err;
943}
944#else
945static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900946xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400947{
948 return 0;
949}
950#endif
951
Tetsuo Handa2e710292014-04-22 21:48:30 +0900952int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400953{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000954 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Fan Du283bc9f2013-11-07 17:47:50 +0800956 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400957
Tetsuo Handa2e710292014-04-22 21:48:30 +0900958 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400959 if (err)
960 goto out;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700963 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800964 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700965
966 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800967 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800968 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700969 if (pol->type != type)
970 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000971 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800972 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000973 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Tetsuo Handa2e710292014-04-22 21:48:30 +0900975 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600976
David S. Miller2518c7c2006-08-24 04:45:07 -0700977 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Fan Du283bc9f2013-11-07 17:47:50 +0800979 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700980 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700982
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800983 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800985 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800986 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700987 bydst) {
988 if (pol->type != type)
989 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000990 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800991 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000992 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700993
Tetsuo Handa2e710292014-04-22 21:48:30 +0900994 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700995 xfrm_policy_kill(pol);
996
Fan Du283bc9f2013-11-07 17:47:50 +0800997 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700998 goto again2;
999 }
1000 }
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001003 if (!cnt)
1004 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001005out:
Fan Du283bc9f2013-11-07 17:47:50 +08001006 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009EXPORT_SYMBOL(xfrm_policy_flush);
1010
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001011int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001012 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 void *data)
1014{
Herbert Xu12a169e2008-10-01 07:03:24 -07001015 struct xfrm_policy *pol;
1016 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001017 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Timo Teras4c563f72008-02-28 21:31:08 -08001019 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1020 walk->type != XFRM_POLICY_TYPE_ANY)
1021 return -EINVAL;
1022
Herbert Xu12a169e2008-10-01 07:03:24 -07001023 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001024 return 0;
1025
Fan Du283bc9f2013-11-07 17:47:50 +08001026 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001027 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001028 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001029 else
Li RongQing80077702015-04-22 17:09:54 +08001030 x = list_first_entry(&walk->walk.all,
1031 struct xfrm_policy_walk_entry, all);
1032
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001033 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001034 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001035 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001036 pol = container_of(x, struct xfrm_policy, walk);
1037 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1038 walk->type != pol->type)
1039 continue;
1040 error = func(pol, xfrm_policy_id2dir(pol->index),
1041 walk->seq, data);
1042 if (error) {
1043 list_move_tail(&walk->walk.all, &x->all);
1044 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001045 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001046 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001048 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001049 error = -ENOENT;
1050 goto out;
1051 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001052 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053out:
Fan Du283bc9f2013-11-07 17:47:50 +08001054 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return error;
1056}
1057EXPORT_SYMBOL(xfrm_policy_walk);
1058
Herbert Xu12a169e2008-10-01 07:03:24 -07001059void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1060{
1061 INIT_LIST_HEAD(&walk->walk.all);
1062 walk->walk.dead = 1;
1063 walk->type = type;
1064 walk->seq = 0;
1065}
1066EXPORT_SYMBOL(xfrm_policy_walk_init);
1067
Fan Du283bc9f2013-11-07 17:47:50 +08001068void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001069{
1070 if (list_empty(&walk->walk.all))
1071 return;
1072
Fan Du283bc9f2013-11-07 17:47:50 +08001073 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001074 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001075 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001076}
1077EXPORT_SYMBOL(xfrm_policy_walk_done);
1078
James Morris134b0fc2006-10-05 15:42:27 -05001079/*
1080 * Find policy to apply to this flow.
1081 *
1082 * Returns 0 if policy found, else an -errno.
1083 */
David S. Millerf299d552011-02-24 01:23:30 -05001084static int xfrm_policy_match(const struct xfrm_policy *pol,
1085 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001086 u8 type, u16 family, int dir)
1087{
David S. Millerf299d552011-02-24 01:23:30 -05001088 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001089 int ret = -ESRCH;
1090 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001091
1092 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001093 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001094 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001095 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001096
1097 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001098 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001099 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001100 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001101
James Morris134b0fc2006-10-05 15:42:27 -05001102 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001103}
1104
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001105static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001106 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001107 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
James Morris134b0fc2006-10-05 15:42:27 -05001109 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001110 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001111 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001112 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001113 unsigned int sequence;
1114 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001115
1116 daddr = xfrm_flowi_daddr(fl, family);
1117 saddr = xfrm_flowi_saddr(fl, family);
1118 if (unlikely(!daddr || !saddr))
1119 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Fan Du283bc9f2013-11-07 17:47:50 +08001121 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +02001122 retry:
1123 do {
1124 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1125 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1126 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1127
1128 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001129 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001130 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001131 err = xfrm_policy_match(pol, fl, type, family, dir);
1132 if (err) {
1133 if (err == -ESRCH)
1134 continue;
1135 else {
1136 ret = ERR_PTR(err);
1137 goto fail;
1138 }
1139 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001140 ret = pol;
1141 priority = ret->priority;
1142 break;
1143 }
1144 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001145 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001146 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001147 if ((pol->priority >= priority) && ret)
1148 break;
1149
James Morris134b0fc2006-10-05 15:42:27 -05001150 err = xfrm_policy_match(pol, fl, type, family, dir);
1151 if (err) {
1152 if (err == -ESRCH)
1153 continue;
1154 else {
1155 ret = ERR_PTR(err);
1156 goto fail;
1157 }
Li RongQing8faf4912015-05-14 11:16:59 +08001158 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001159 ret = pol;
1160 break;
1161 }
1162 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001163
Florian Westphal30846092016-08-11 15:17:54 +02001164 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1165 goto retry;
1166
Li RongQing586f2eb2015-04-30 17:13:41 +08001167 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001168fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001169 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001170
David S. Miller2518c7c2006-08-24 04:45:07 -07001171 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001172}
1173
Timo Teräs80c802f2010-04-07 00:30:05 +00001174static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001175__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001176{
1177#ifdef CONFIG_XFRM_SUB_POLICY
1178 struct xfrm_policy *pol;
1179
1180 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1181 if (pol != NULL)
1182 return pol;
1183#endif
1184 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1185}
1186
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001187static int flow_to_policy_dir(int dir)
1188{
1189 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1190 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1191 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1192 return dir;
1193
1194 switch (dir) {
1195 default:
1196 case FLOW_DIR_IN:
1197 return XFRM_POLICY_IN;
1198 case FLOW_DIR_OUT:
1199 return XFRM_POLICY_OUT;
1200 case FLOW_DIR_FWD:
1201 return XFRM_POLICY_FWD;
1202 }
1203}
1204
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001205static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001206xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001207 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001208{
1209 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001210
1211 if (old_obj)
1212 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001213
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001214 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001215 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001216 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001217
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001218 /* Resolver returns two references:
1219 * one for cache and one for caller of flow_cache_lookup() */
1220 xfrm_pol_hold(pol);
1221
1222 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Trent Jaegerdf718372005-12-13 23:12:27 -08001225static inline int policy_to_flow_dir(int dir)
1226{
1227 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001228 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1229 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1230 return dir;
1231 switch (dir) {
1232 default:
1233 case XFRM_POLICY_IN:
1234 return FLOW_DIR_IN;
1235 case XFRM_POLICY_OUT:
1236 return FLOW_DIR_OUT;
1237 case XFRM_POLICY_FWD:
1238 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001239 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001240}
1241
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001242static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001243 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001246 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Eric Dumazetd188ba82015-12-08 07:22:02 -08001248 rcu_read_lock();
Fan Du283bc9f2013-11-07 17:47:50 +08001249 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001250 pol = rcu_dereference(sk->sk_policy[dir]);
1251 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001252 bool match = xfrm_selector_match(&pol->selector, fl,
1253 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001254 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001255
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001256 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001257 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1258 pol = NULL;
1259 goto out;
1260 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001261 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001262 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001263 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001264 if (!err)
1265 xfrm_pol_hold(pol);
1266 else if (err == -ESRCH)
1267 pol = NULL;
1268 else
1269 pol = ERR_PTR(err);
1270 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 pol = NULL;
1272 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001273out:
Fan Du283bc9f2013-11-07 17:47:50 +08001274 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001275 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return pol;
1277}
1278
1279static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1280{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001281 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001282
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001283 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001284 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 xfrm_pol_hold(pol);
1286}
1287
1288static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1289 int dir)
1290{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001291 struct net *net = xp_net(pol);
1292
Herbert Xu53c2e282014-11-13 17:09:49 +08001293 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001294 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Herbert Xu53c2e282014-11-13 17:09:49 +08001296 /* Socket policies are not hashed. */
1297 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001298 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001299 hlist_del(&pol->byidx);
1300 }
1301
1302 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001303 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001304
1305 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Herbert Xu53c2e282014-11-13 17:09:49 +08001308static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1309{
1310 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1311}
1312
1313static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1314{
1315 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1316}
1317
Herbert Xu4666faa2005-06-18 22:43:22 -07001318int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Fan Du283bc9f2013-11-07 17:47:50 +08001320 struct net *net = xp_net(pol);
1321
1322 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001324 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001329 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
David S. Millera70fcb02006-03-20 19:18:52 -08001331EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1334{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001335 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct xfrm_policy *old_pol;
1337
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001338#ifdef CONFIG_XFRM_SUB_POLICY
1339 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1340 return -EINVAL;
1341#endif
1342
Fan Du283bc9f2013-11-07 17:47:50 +08001343 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001344 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1345 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001347 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001348 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001349 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001351 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001352 if (old_pol) {
1353 if (pol)
1354 xfrm_policy_requeue(old_pol, pol);
1355
Timo Teräsea2dea92010-03-31 00:17:05 +00001356 /* Unlinking succeeds always. This is the only function
1357 * allowed to delete or replace socket policy.
1358 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001359 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001360 }
Fan Du283bc9f2013-11-07 17:47:50 +08001361 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (old_pol) {
1364 xfrm_policy_kill(old_pol);
1365 }
1366 return 0;
1367}
1368
David S. Millerd3e40a92011-02-24 01:25:41 -05001369static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001371 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001372 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 if (newp) {
1375 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001376 if (security_xfrm_policy_clone(old->security,
1377 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001378 kfree(newp);
1379 return NULL; /* ENOMEM */
1380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 newp->lft = old->lft;
1382 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001383 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 newp->action = old->action;
1385 newp->flags = old->flags;
1386 newp->xfrm_nr = old->xfrm_nr;
1387 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001388 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 memcpy(newp->xfrm_vec, old->xfrm_vec,
1390 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001391 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001392 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001393 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 xfrm_pol_put(newp);
1395 }
1396 return newp;
1397}
1398
Eric Dumazetd188ba82015-12-08 07:22:02 -08001399int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001401 const struct xfrm_policy *p;
1402 struct xfrm_policy *np;
1403 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Eric Dumazetd188ba82015-12-08 07:22:02 -08001405 rcu_read_lock();
1406 for (i = 0; i < 2; i++) {
1407 p = rcu_dereference(osk->sk_policy[i]);
1408 if (p) {
1409 np = clone_policy(p, i);
1410 if (unlikely(!np)) {
1411 ret = -ENOMEM;
1412 break;
1413 }
1414 rcu_assign_pointer(sk->sk_policy[i], np);
1415 }
1416 }
1417 rcu_read_unlock();
1418 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001421static int
David Ahern42a7b322015-08-10 16:58:11 -06001422xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1423 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001424{
1425 int err;
1426 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1427
1428 if (unlikely(afinfo == NULL))
1429 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001430 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001431 xfrm_policy_put_afinfo(afinfo);
1432 return err;
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/* Resolve list of templates for the flow, given policy. */
1436
1437static int
David S. Millera6c2e612011-02-22 18:35:39 -08001438xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1439 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001441 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int nx;
1443 int i, error;
1444 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1445 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001446 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Weilong Chen9b7a7872013-12-24 09:43:46 +08001448 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct xfrm_state *x;
1450 xfrm_address_t *remote = daddr;
1451 xfrm_address_t *local = saddr;
1452 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1453
Joakim Koskela48b8d782007-07-26 00:08:42 -07001454 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1455 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 remote = &tmpl->id.daddr;
1457 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001458 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001459 error = xfrm_get_saddr(net, fl->flowi_oif,
1460 &tmp, remote,
1461 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001462 if (error)
1463 goto fail;
1464 local = &tmp;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1469
1470 if (x && x->km.state == XFRM_STATE_VALID) {
1471 xfrm[nx++] = x;
1472 daddr = remote;
1473 saddr = local;
1474 continue;
1475 }
1476 if (x) {
1477 error = (x->km.state == XFRM_STATE_ERROR ?
1478 -EINVAL : -EAGAIN);
1479 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001480 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001481 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 if (!tmpl->optional)
1485 goto fail;
1486 }
1487 return nx;
1488
1489fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001490 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xfrm_state_put(xfrm[nx]);
1492 return error;
1493}
1494
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001495static int
David S. Millera6c2e612011-02-22 18:35:39 -08001496xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1497 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001498{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001499 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1500 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001501 int cnx = 0;
1502 int error;
1503 int ret;
1504 int i;
1505
1506 for (i = 0; i < npols; i++) {
1507 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1508 error = -ENOBUFS;
1509 goto fail;
1510 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001511
1512 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001513 if (ret < 0) {
1514 error = ret;
1515 goto fail;
1516 } else
1517 cnx += ret;
1518 }
1519
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001520 /* found states are sorted for outbound processing */
1521 if (npols > 1)
1522 xfrm_state_sort(xfrm, tpp, cnx, family);
1523
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001524 return cnx;
1525
1526 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001527 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001528 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001529 return error;
1530
1531}
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/* Check that the bundle accepts the flow and its components are
1534 * still valid.
1535 */
1536
David S. Miller05d84022011-02-22 17:47:10 -08001537static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001538{
1539 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1540 int tos;
1541
1542 if (!afinfo)
1543 return -EINVAL;
1544
1545 tos = afinfo->get_tos(fl);
1546
1547 xfrm_policy_put_afinfo(afinfo);
1548
1549 return tos;
1550}
1551
Timo Teräs80c802f2010-04-07 00:30:05 +00001552static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1553{
1554 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1555 struct dst_entry *dst = &xdst->u.dst;
1556
1557 if (xdst->route == NULL) {
1558 /* Dummy bundle - if it has xfrms we were not
1559 * able to build bundle as template resolution failed.
1560 * It means we need to try again resolving. */
1561 if (xdst->num_xfrms > 0)
1562 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001563 } else if (dst->flags & DST_XFRM_QUEUE) {
1564 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001565 } else {
1566 /* Real bundle */
1567 if (stale_bundle(dst))
1568 return NULL;
1569 }
1570
1571 dst_hold(dst);
1572 return flo;
1573}
1574
1575static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1576{
1577 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1578 struct dst_entry *dst = &xdst->u.dst;
1579
1580 if (!xdst->route)
1581 return 0;
1582 if (stale_bundle(dst))
1583 return 0;
1584
1585 return 1;
1586}
1587
1588static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1589{
1590 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1591 struct dst_entry *dst = &xdst->u.dst;
1592
1593 dst_free(dst);
1594}
1595
1596static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1597 .get = xfrm_bundle_flo_get,
1598 .check = xfrm_bundle_flo_check,
1599 .delete = xfrm_bundle_flo_delete,
1600};
1601
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001602static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001603{
1604 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001605 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001606 struct xfrm_dst *xdst;
1607
1608 if (!afinfo)
1609 return ERR_PTR(-EINVAL);
1610
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001611 switch (family) {
1612 case AF_INET:
1613 dst_ops = &net->xfrm.xfrm4_dst_ops;
1614 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001615#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001616 case AF_INET6:
1617 dst_ops = &net->xfrm.xfrm6_dst_ops;
1618 break;
1619#endif
1620 default:
1621 BUG();
1622 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001623 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001624
Madalin Bucurd4cae562011-09-26 07:04:36 +00001625 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001626 struct dst_entry *dst = &xdst->u.dst;
1627
1628 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001629 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001630 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001631 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001632
Madalin Bucurd4cae562011-09-26 07:04:36 +00001633 xfrm_policy_put_afinfo(afinfo);
1634
Herbert Xu25ee3282007-12-11 09:32:34 -08001635 return xdst;
1636}
1637
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001638static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1639 int nfheader_len)
1640{
1641 struct xfrm_policy_afinfo *afinfo =
1642 xfrm_policy_get_afinfo(dst->ops->family);
1643 int err;
1644
1645 if (!afinfo)
1646 return -EINVAL;
1647
1648 err = afinfo->init_path(path, dst, nfheader_len);
1649
1650 xfrm_policy_put_afinfo(afinfo);
1651
1652 return err;
1653}
1654
Herbert Xu87c1e122010-03-02 02:51:56 +00001655static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001656 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001657{
1658 struct xfrm_policy_afinfo *afinfo =
1659 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1660 int err;
1661
1662 if (!afinfo)
1663 return -EINVAL;
1664
Herbert Xu87c1e122010-03-02 02:51:56 +00001665 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001666
1667 xfrm_policy_put_afinfo(afinfo);
1668
1669 return err;
1670}
1671
Timo Teräs80c802f2010-04-07 00:30:05 +00001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1674 * all the metrics... Shortly, bundle a bundle.
1675 */
1676
Herbert Xu25ee3282007-12-11 09:32:34 -08001677static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1678 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001679 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001680 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001682 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001683 unsigned long now = jiffies;
1684 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001685 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001686 struct dst_entry *dst_prev = NULL;
1687 struct dst_entry *dst0 = NULL;
1688 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001690 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001691 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001692 int trailer_len = 0;
1693 int tos;
1694 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001695 xfrm_address_t saddr, daddr;
1696
1697 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001698
1699 tos = xfrm_get_tos(fl, family);
1700 err = tos;
1701 if (tos < 0)
1702 goto put_states;
1703
1704 dst_hold(dst);
1705
1706 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001707 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001708 struct dst_entry *dst1 = &xdst->u.dst;
1709
1710 err = PTR_ERR(xdst);
1711 if (IS_ERR(xdst)) {
1712 dst_release(dst);
1713 goto put_states;
1714 }
1715
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001716 if (xfrm[i]->sel.family == AF_UNSPEC) {
1717 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1718 xfrm_af2proto(family));
1719 if (!inner_mode) {
1720 err = -EAFNOSUPPORT;
1721 dst_release(dst);
1722 goto put_states;
1723 }
1724 } else
1725 inner_mode = xfrm[i]->inner_mode;
1726
Herbert Xu25ee3282007-12-11 09:32:34 -08001727 if (!dst_prev)
1728 dst0 = dst1;
1729 else {
1730 dst_prev->child = dst_clone(dst1);
1731 dst1->flags |= DST_NOHASH;
1732 }
1733
1734 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001735 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001736
1737 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1738 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001739 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1740 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001741 err = PTR_ERR(dst);
1742 if (IS_ERR(dst))
1743 goto put_states;
1744 } else
1745 dst_hold(dst);
1746
1747 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001748 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001749
David S. Millerf5b0a872012-07-19 12:31:33 -07001750 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001751 dst1->flags |= DST_HOST;
1752 dst1->lastuse = now;
1753
1754 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001755 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001756
1757 dst1->next = dst_prev;
1758 dst_prev = dst1;
1759
1760 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001761 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1762 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001763 trailer_len += xfrm[i]->props.trailer_len;
1764 }
1765
1766 dst_prev->child = dst;
1767 dst0->path = dst;
1768
1769 err = -ENODEV;
1770 dev = dst->dev;
1771 if (!dev)
1772 goto free_dst;
1773
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001774 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001775 xfrm_init_pmtu(dst_prev);
1776
1777 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1778 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1779
Herbert Xu87c1e122010-03-02 02:51:56 +00001780 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001781 if (err)
1782 goto free_dst;
1783
1784 dst_prev->header_len = header_len;
1785 dst_prev->trailer_len = trailer_len;
1786 header_len -= xdst->u.dst.xfrm->props.header_len;
1787 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1788 }
1789
1790out:
1791 return dst0;
1792
1793put_states:
1794 for (; i < nx; i++)
1795 xfrm_state_put(xfrm[i]);
1796free_dst:
1797 if (dst0)
1798 dst_free(dst0);
1799 dst0 = ERR_PTR(err);
1800 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Ying Xueda7c2242014-01-08 10:26:39 +08001803#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001804static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001805{
1806 if (!*target) {
1807 *target = kmalloc(size, GFP_ATOMIC);
1808 if (!*target)
1809 return -ENOMEM;
1810 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001811
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001812 memcpy(*target, src, size);
1813 return 0;
1814}
Ying Xueda7c2242014-01-08 10:26:39 +08001815#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001816
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001817static int xfrm_dst_update_parent(struct dst_entry *dst,
1818 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001819{
1820#ifdef CONFIG_XFRM_SUB_POLICY
1821 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1822 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1823 sel, sizeof(*sel));
1824#else
1825 return 0;
1826#endif
1827}
1828
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001829static int xfrm_dst_update_origin(struct dst_entry *dst,
1830 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001831{
1832#ifdef CONFIG_XFRM_SUB_POLICY
1833 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1834 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1835#else
1836 return 0;
1837#endif
1838}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
David S. Miller73ff93c2011-02-22 18:33:42 -08001840static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001841 struct xfrm_policy **pols,
1842 int *num_pols, int *num_xfrms)
1843{
1844 int i;
1845
1846 if (*num_pols == 0 || !pols[0]) {
1847 *num_pols = 0;
1848 *num_xfrms = 0;
1849 return 0;
1850 }
1851 if (IS_ERR(pols[0]))
1852 return PTR_ERR(pols[0]);
1853
1854 *num_xfrms = pols[0]->xfrm_nr;
1855
1856#ifdef CONFIG_XFRM_SUB_POLICY
1857 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1858 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1859 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1860 XFRM_POLICY_TYPE_MAIN,
1861 fl, family,
1862 XFRM_POLICY_OUT);
1863 if (pols[1]) {
1864 if (IS_ERR(pols[1])) {
1865 xfrm_pols_put(pols, *num_pols);
1866 return PTR_ERR(pols[1]);
1867 }
Weilong Chen02d08922013-12-24 09:43:48 +08001868 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001869 (*num_xfrms) += pols[1]->xfrm_nr;
1870 }
1871 }
1872#endif
1873 for (i = 0; i < *num_pols; i++) {
1874 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1875 *num_xfrms = -1;
1876 break;
1877 }
1878 }
1879
1880 return 0;
1881
1882}
1883
1884static struct xfrm_dst *
1885xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001886 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001887 struct dst_entry *dst_orig)
1888{
1889 struct net *net = xp_net(pols[0]);
1890 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1891 struct dst_entry *dst;
1892 struct xfrm_dst *xdst;
1893 int err;
1894
1895 /* Try to instantiate a bundle */
1896 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001897 if (err <= 0) {
1898 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001899 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1900 return ERR_PTR(err);
1901 }
1902
1903 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1904 if (IS_ERR(dst)) {
1905 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1906 return ERR_CAST(dst);
1907 }
1908
1909 xdst = (struct xfrm_dst *)dst;
1910 xdst->num_xfrms = err;
1911 if (num_pols > 1)
1912 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1913 else
1914 err = xfrm_dst_update_origin(dst, fl);
1915 if (unlikely(err)) {
1916 dst_free(dst);
1917 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1918 return ERR_PTR(err);
1919 }
1920
1921 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001922 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001923 xdst->policy_genid = atomic_read(&pols[0]->genid);
1924
1925 return xdst;
1926}
1927
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001928static void xfrm_policy_queue_process(unsigned long arg)
1929{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001930 struct sk_buff *skb;
1931 struct sock *sk;
1932 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001933 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001934 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001935 struct xfrm_policy_queue *pq = &pol->polq;
1936 struct flowi fl;
1937 struct sk_buff_head list;
1938
1939 spin_lock(&pq->hold_queue.lock);
1940 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001941 if (!skb) {
1942 spin_unlock(&pq->hold_queue.lock);
1943 goto out;
1944 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001945 dst = skb_dst(skb);
1946 sk = skb->sk;
1947 xfrm_decode_session(skb, &fl, dst->ops->family);
1948 spin_unlock(&pq->hold_queue.lock);
1949
1950 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001951 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001952 if (IS_ERR(dst))
1953 goto purge_queue;
1954
1955 if (dst->flags & DST_XFRM_QUEUE) {
1956 dst_release(dst);
1957
1958 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1959 goto purge_queue;
1960
1961 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001962 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1963 xfrm_pol_hold(pol);
1964 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001965 }
1966
1967 dst_release(dst);
1968
1969 __skb_queue_head_init(&list);
1970
1971 spin_lock(&pq->hold_queue.lock);
1972 pq->timeout = 0;
1973 skb_queue_splice_init(&pq->hold_queue, &list);
1974 spin_unlock(&pq->hold_queue.lock);
1975
1976 while (!skb_queue_empty(&list)) {
1977 skb = __skb_dequeue(&list);
1978
1979 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1980 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001981 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001982 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001983 kfree_skb(skb);
1984 continue;
1985 }
1986
1987 nf_reset(skb);
1988 skb_dst_drop(skb);
1989 skb_dst_set(skb, dst);
1990
Eric W. Biederman13206b62015-10-07 16:48:35 -05001991 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001992 }
1993
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001994out:
1995 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001996 return;
1997
1998purge_queue:
1999 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002000 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002001 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002002}
2003
Eric W. Biedermanede20592015-10-07 16:48:47 -05002004static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002005{
2006 unsigned long sched_next;
2007 struct dst_entry *dst = skb_dst(skb);
2008 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002009 struct xfrm_policy *pol = xdst->pols[0];
2010 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002011
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002012 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002013 kfree_skb(skb);
2014 return 0;
2015 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002016
2017 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2018 kfree_skb(skb);
2019 return -EAGAIN;
2020 }
2021
2022 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002023
2024 spin_lock_bh(&pq->hold_queue.lock);
2025
2026 if (!pq->timeout)
2027 pq->timeout = XFRM_QUEUE_TMO_MIN;
2028
2029 sched_next = jiffies + pq->timeout;
2030
2031 if (del_timer(&pq->hold_timer)) {
2032 if (time_before(pq->hold_timer.expires, sched_next))
2033 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002034 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002035 }
2036
2037 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002038 if (!mod_timer(&pq->hold_timer, sched_next))
2039 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002040
2041 spin_unlock_bh(&pq->hold_queue.lock);
2042
2043 return 0;
2044}
2045
2046static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002047 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002048 const struct flowi *fl,
2049 int num_xfrms,
2050 u16 family)
2051{
2052 int err;
2053 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002054 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002055 struct dst_entry *dst1;
2056 struct xfrm_dst *xdst;
2057
2058 xdst = xfrm_alloc_dst(net, family);
2059 if (IS_ERR(xdst))
2060 return xdst;
2061
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002062 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2063 net->xfrm.sysctl_larval_drop ||
2064 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002065 return xdst;
2066
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002067 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002068 dst1 = &xdst->u.dst;
2069 dst_hold(dst);
2070 xdst->route = dst;
2071
2072 dst_copy_metrics(dst1, dst);
2073
2074 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2075 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2076 dst1->lastuse = jiffies;
2077
2078 dst1->input = dst_discard;
2079 dst1->output = xdst_queue_output;
2080
2081 dst_hold(dst);
2082 dst1->child = dst;
2083 dst1->path = dst;
2084
2085 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2086
2087 err = -ENODEV;
2088 dev = dst->dev;
2089 if (!dev)
2090 goto free_dst;
2091
2092 err = xfrm_fill_dst(xdst, dev, fl);
2093 if (err)
2094 goto free_dst;
2095
2096out:
2097 return xdst;
2098
2099free_dst:
2100 dst_release(dst1);
2101 xdst = ERR_PTR(err);
2102 goto out;
2103}
2104
Timo Teräs80c802f2010-04-07 00:30:05 +00002105static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002106xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002107 struct flow_cache_object *oldflo, void *ctx)
2108{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002109 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002110 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2111 struct xfrm_dst *xdst, *new_xdst;
2112 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2113
2114 /* Check if the policies from old bundle are usable */
2115 xdst = NULL;
2116 if (oldflo) {
2117 xdst = container_of(oldflo, struct xfrm_dst, flo);
2118 num_pols = xdst->num_pols;
2119 num_xfrms = xdst->num_xfrms;
2120 pol_dead = 0;
2121 for (i = 0; i < num_pols; i++) {
2122 pols[i] = xdst->pols[i];
2123 pol_dead |= pols[i]->walk.dead;
2124 }
2125 if (pol_dead) {
2126 dst_free(&xdst->u.dst);
2127 xdst = NULL;
2128 num_pols = 0;
2129 num_xfrms = 0;
2130 oldflo = NULL;
2131 }
2132 }
2133
2134 /* Resolve policies to use if we couldn't get them from
2135 * previous cache entry */
2136 if (xdst == NULL) {
2137 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002138 pols[0] = __xfrm_policy_lookup(net, fl, family,
2139 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002140 err = xfrm_expand_policies(fl, family, pols,
2141 &num_pols, &num_xfrms);
2142 if (err < 0)
2143 goto inc_error;
2144 if (num_pols == 0)
2145 return NULL;
2146 if (num_xfrms <= 0)
2147 goto make_dummy_bundle;
2148 }
2149
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002150 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2151 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002152 if (IS_ERR(new_xdst)) {
2153 err = PTR_ERR(new_xdst);
2154 if (err != -EAGAIN)
2155 goto error;
2156 if (oldflo == NULL)
2157 goto make_dummy_bundle;
2158 dst_hold(&xdst->u.dst);
2159 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002160 } else if (new_xdst == NULL) {
2161 num_xfrms = 0;
2162 if (oldflo == NULL)
2163 goto make_dummy_bundle;
2164 xdst->num_xfrms = 0;
2165 dst_hold(&xdst->u.dst);
2166 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002167 }
2168
2169 /* Kill the previous bundle */
2170 if (xdst) {
2171 /* The policies were stolen for newly generated bundle */
2172 xdst->num_pols = 0;
2173 dst_free(&xdst->u.dst);
2174 }
2175
2176 /* Flow cache does not have reference, it dst_free()'s,
2177 * but we do need to return one reference for original caller */
2178 dst_hold(&new_xdst->u.dst);
2179 return &new_xdst->flo;
2180
2181make_dummy_bundle:
2182 /* We found policies, but there's no bundles to instantiate:
2183 * either because the policy blocks, has no transformations or
2184 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002185 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002186 if (IS_ERR(xdst)) {
2187 xfrm_pols_put(pols, num_pols);
2188 return ERR_CAST(xdst);
2189 }
2190 xdst->num_pols = num_pols;
2191 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002192 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002193
2194 dst_hold(&xdst->u.dst);
2195 return &xdst->flo;
2196
2197inc_error:
2198 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2199error:
2200 if (xdst != NULL)
2201 dst_free(&xdst->u.dst);
2202 else
2203 xfrm_pols_put(pols, num_pols);
2204 return ERR_PTR(err);
2205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
David S. Miller2774c132011-03-01 14:59:04 -08002207static struct dst_entry *make_blackhole(struct net *net, u16 family,
2208 struct dst_entry *dst_orig)
2209{
2210 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2211 struct dst_entry *ret;
2212
2213 if (!afinfo) {
2214 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002215 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002216 } else {
2217 ret = afinfo->blackhole_route(net, dst_orig);
2218 }
2219 xfrm_policy_put_afinfo(afinfo);
2220
2221 return ret;
2222}
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224/* Main function: finds/creates a bundle for given flow.
2225 *
2226 * At the moment we eat a raw IP route. Mostly to speed up lookups
2227 * on interfaces with disabled IPsec.
2228 */
David S. Miller452edd52011-03-02 13:27:41 -08002229struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2230 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002231 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002233 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002234 struct flow_cache_object *flo;
2235 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002236 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002237 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002238 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002239 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002240
Timo Teräs80c802f2010-04-07 00:30:05 +00002241 dst = NULL;
2242 xdst = NULL;
2243 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002244
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002245 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002246 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002247 num_pols = 1;
2248 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2249 err = xfrm_expand_policies(fl, family, pols,
2250 &num_pols, &num_xfrms);
2251 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002252 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002253
2254 if (num_pols) {
2255 if (num_xfrms <= 0) {
2256 drop_pols = num_pols;
2257 goto no_transform;
2258 }
2259
2260 xdst = xfrm_resolve_and_create_bundle(
2261 pols, num_pols, fl,
2262 family, dst_orig);
2263 if (IS_ERR(xdst)) {
2264 xfrm_pols_put(pols, num_pols);
2265 err = PTR_ERR(xdst);
2266 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002267 } else if (xdst == NULL) {
2268 num_xfrms = 0;
2269 drop_pols = num_pols;
2270 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002271 }
2272
Steffen Klassertb7eea452014-06-18 12:34:21 +02002273 dst_hold(&xdst->u.dst);
2274 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002275 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002276 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Timo Teräs80c802f2010-04-07 00:30:05 +00002279 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002280 struct xfrm_flo xflo;
2281
2282 xflo.dst_orig = dst_orig;
2283 xflo.flags = flags;
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002286 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002287 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002288 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Timo Teräs80c802f2010-04-07 00:30:05 +00002290 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002291 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002292 if (flo == NULL)
2293 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002294 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002295 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002296 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002297 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002298 xdst = container_of(flo, struct xfrm_dst, flo);
2299
2300 num_pols = xdst->num_pols;
2301 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002302 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002303 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305
Timo Teräs80c802f2010-04-07 00:30:05 +00002306 dst = &xdst->u.dst;
2307 if (route == NULL && num_xfrms > 0) {
2308 /* The only case when xfrm_bundle_lookup() returns a
2309 * bundle with null route, is when the template could
2310 * not be resolved. It means policies are there, but
2311 * bundle could not be created, since we don't yet
2312 * have the xfrm_state's. We need to wait for KM to
2313 * negotiate new SA's or bail out with error.*/
2314 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002315 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002316 err = -EREMOTE;
2317 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002318 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002319
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002320 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002321
2322 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2323 goto error;
2324 }
2325
2326no_transform:
2327 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002328 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Timo Teräs80c802f2010-04-07 00:30:05 +00002330 if ((flags & XFRM_LOOKUP_ICMP) &&
2331 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2332 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002333 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002334 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002335
Timo Teräs80c802f2010-04-07 00:30:05 +00002336 for (i = 0; i < num_pols; i++)
2337 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002338
Timo Teräs80c802f2010-04-07 00:30:05 +00002339 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002341 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002342 err = -EPERM;
2343 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002344 } else if (num_xfrms > 0) {
2345 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 dst_release(dst_orig);
2347 } else {
2348 /* Flow passes untransformed */
2349 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002350 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002352ok:
2353 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002354 if (dst && dst->xfrm &&
2355 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2356 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002357 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Timo Teräs80c802f2010-04-07 00:30:05 +00002359nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002360 if (!(flags & XFRM_LOOKUP_ICMP)) {
2361 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002362 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002363 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002364 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002366 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002367dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002368 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2369 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002370 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002371 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373EXPORT_SYMBOL(xfrm_lookup);
2374
Steffen Klassertf92ee612014-09-16 10:08:40 +02002375/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2376 * Otherwise we may send out blackholed packets.
2377 */
2378struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2379 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002380 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002381{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002382 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002383 flags | XFRM_LOOKUP_QUEUE |
2384 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002385
2386 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2387 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2388
2389 return dst;
2390}
2391EXPORT_SYMBOL(xfrm_lookup_route);
2392
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002393static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002394xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002395{
2396 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002397
2398 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2399 return 0;
2400 x = skb->sp->xvec[idx];
2401 if (!x->type->reject)
2402 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002403 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002404}
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406/* When skb is transformed back to its "native" form, we have to
2407 * check policy restrictions. At the moment we make this in maximally
2408 * stupid way. Shame on me. :-) Of course, connected sockets must
2409 * have policy cached at them.
2410 */
2411
2412static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002413xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 unsigned short family)
2415{
2416 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002417 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return x->id.proto == tmpl->id.proto &&
2419 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2420 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2421 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002422 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002423 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002424 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2425 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002428/*
2429 * 0 or more than 0 is returned when validation is succeeded (either bypass
2430 * because of optional transport mode, or next index of the mathced secpath
2431 * state with the template.
2432 * -1 is returned when no matching template is found.
2433 * Otherwise "-2 - errored_index" is returned.
2434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002436xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 unsigned short family)
2438{
2439 int idx = start;
2440
2441 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002442 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return start;
2444 } else
2445 start = -1;
2446 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002447 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002449 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2450 if (start == -1)
2451 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
2455 return start;
2456}
2457
Herbert Xud5422ef2007-12-12 10:44:16 -08002458int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2459 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002462 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 if (unlikely(afinfo == NULL))
2465 return -EAFNOSUPPORT;
2466
Herbert Xud5422ef2007-12-12 10:44:16 -08002467 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002468 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
Herbert Xud5422ef2007-12-12 10:44:16 -08002472EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
David S. Miller9a7386e2011-02-24 01:44:12 -05002474static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002477 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002478 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482
2483 return 0;
2484}
2485
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002486int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 unsigned short family)
2488{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002489 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002491 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2492 int npols = 0;
2493 int xfrm_nr;
2494 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002495 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002497 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002498 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Herbert Xud5422ef2007-12-12 10:44:16 -08002500 reverse = dir & ~XFRM_POLICY_MASK;
2501 dir &= XFRM_POLICY_MASK;
2502 fl_dir = policy_to_flow_dir(dir);
2503
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002504 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002505 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002507 }
2508
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002509 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* First, check used SA against their selectors. */
2512 if (skb->sp) {
2513 int i;
2514
Weilong Chen9b7a7872013-12-24 09:43:46 +08002515 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002516 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002517 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002518 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522 }
2523
2524 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002525 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002526 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002527 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002528 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002529 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002530 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002531 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002534 if (!pol) {
2535 struct flow_cache_object *flo;
2536
2537 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2538 xfrm_policy_lookup, NULL);
2539 if (IS_ERR_OR_NULL(flo))
2540 pol = ERR_CAST(flo);
2541 else
2542 pol = container_of(flo, struct xfrm_policy, flo);
2543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002545 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002546 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002547 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002548 }
James Morris134b0fc2006-10-05 15:42:27 -05002549
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002550 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002551 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002552 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002553 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002554 return 0;
2555 }
2556 return 1;
2557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
James Morris9d729f72007-03-04 16:12:44 -08002559 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002561 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002562 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002563#ifdef CONFIG_XFRM_SUB_POLICY
2564 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002565 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002566 &fl, family,
2567 XFRM_POLICY_IN);
2568 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002569 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002570 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002571 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002572 }
James Morris9d729f72007-03-04 16:12:44 -08002573 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002574 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002575 }
2576 }
2577#endif
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (pol->action == XFRM_POLICY_ALLOW) {
2580 struct sec_path *sp;
2581 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002582 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002583 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002584 struct xfrm_tmpl **tpp = tp;
2585 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 int i, k;
2587
2588 if ((sp = skb->sp) == NULL)
2589 sp = &dummy;
2590
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002591 for (pi = 0; pi < npols; pi++) {
2592 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002593 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002594 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002595 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002596 }
2597 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002598 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002599 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002600 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002601 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2602 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2603 }
2604 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002605 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002606 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002607 tpp = stp;
2608 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* For each tunnel xfrm, find the first matching tmpl.
2611 * For each tmpl before that, find corresponding xfrm.
2612 * Order is _important_. Later we will implement
2613 * some barriers, but at the moment barriers
2614 * are implied between each two transformations.
2615 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002616 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2617 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002618 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002619 if (k < -1)
2620 /* "-2 - errored_index" returned */
2621 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002622 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002627 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002628 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002632 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return 1;
2634 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002635 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002638 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002639reject_error:
2640 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return 0;
2642}
2643EXPORT_SYMBOL(__xfrm_policy_check);
2644
2645int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2646{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002647 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002649 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002650 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002652 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002653 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002657 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002658
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002659 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002660 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002661 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002662 dst = NULL;
2663 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002664 skb_dst_set(skb, dst);
2665 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667EXPORT_SYMBOL(__xfrm_route_forward);
2668
David S. Millerd49c73c2006-08-13 18:55:53 -07002669/* Optimize later using cookies and generation ids. */
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2672{
David S. Millerd49c73c2006-08-13 18:55:53 -07002673 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002674 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2675 * get validated by dst_ops->check on every use. We do this
2676 * because when a normal route referenced by an XFRM dst is
2677 * obsoleted we do not go looking around for all parent
2678 * referencing XFRM dsts so that we can invalidate them. It
2679 * is just too much work. Instead we make the checks here on
2680 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002681 *
2682 * XFRM dst A --> IPv4 dst X
2683 *
2684 * X is the "xdst->route" of A (X is also the "dst->path" of A
2685 * in this example). If X is marked obsolete, "A" will not
2686 * notice. That's what we are validating here via the
2687 * stale_bundle() check.
2688 *
2689 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002690 * dst which causes it's ->obsolete field to be set to
2691 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2692 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002693 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002694 if (dst->obsolete < 0 && !stale_bundle(dst))
2695 return dst;
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return NULL;
2698}
2699
2700static int stale_bundle(struct dst_entry *dst)
2701{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002702 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
Herbert Xuaabc9762005-05-03 16:27:10 -07002705void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002708 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002709 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 dev_put(dev);
2711 }
2712}
Herbert Xuaabc9762005-05-03 16:27:10 -07002713EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715static void xfrm_link_failure(struct sk_buff *skb)
2716{
2717 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2721{
2722 if (dst) {
2723 if (dst->obsolete) {
2724 dst_release(dst);
2725 dst = NULL;
2726 }
2727 }
2728 return dst;
2729}
2730
Paul Mooree4c17212013-05-29 07:36:25 +00002731void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002732{
Fan Duca925cf2014-01-18 09:55:27 +08002733 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002734}
Paul Mooree4c17212013-05-29 07:36:25 +00002735EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002736
2737static void xfrm_garbage_collect_deferred(struct net *net)
2738{
Fan Duca925cf2014-01-18 09:55:27 +08002739 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002740}
2741
Herbert Xu25ee3282007-12-11 09:32:34 -08002742static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 do {
2745 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2746 u32 pmtu, route_mtu_cached;
2747
2748 pmtu = dst_mtu(dst->child);
2749 xdst->child_mtu_cached = pmtu;
2750
2751 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2752
2753 route_mtu_cached = dst_mtu(xdst->route);
2754 xdst->route_mtu_cached = route_mtu_cached;
2755
2756 if (pmtu > route_mtu_cached)
2757 pmtu = route_mtu_cached;
2758
David S. Millerdefb3512010-12-08 21:16:57 -08002759 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 } while ((dst = dst->next));
2761}
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763/* Check that the bundle accepts the flow and its components are
2764 * still valid.
2765 */
2766
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002767static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
2769 struct dst_entry *dst = &first->u.dst;
2770 struct xfrm_dst *last;
2771 u32 mtu;
2772
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002773 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 (dst->dev && !netif_running(dst->dev)))
2775 return 0;
2776
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002777 if (dst->flags & DST_XFRM_QUEUE)
2778 return 1;
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 last = NULL;
2781
2782 do {
2783 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2786 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002787 if (xdst->xfrm_genid != dst->xfrm->genid)
2788 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002789 if (xdst->num_pols > 0 &&
2790 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 mtu = dst_mtu(dst->child);
2794 if (xdst->child_mtu_cached != mtu) {
2795 last = xdst;
2796 xdst->child_mtu_cached = mtu;
2797 }
2798
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002799 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
2801 mtu = dst_mtu(xdst->route);
2802 if (xdst->route_mtu_cached != mtu) {
2803 last = xdst;
2804 xdst->route_mtu_cached = mtu;
2805 }
2806
2807 dst = dst->child;
2808 } while (dst->xfrm);
2809
2810 if (likely(!last))
2811 return 1;
2812
2813 mtu = last->child_mtu_cached;
2814 for (;;) {
2815 dst = &last->u.dst;
2816
2817 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2818 if (mtu > last->route_mtu_cached)
2819 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002820 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 if (last == first)
2823 break;
2824
Patrick McHardybd0bf072007-07-18 01:55:52 -07002825 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 last->child_mtu_cached = mtu;
2827 }
2828
2829 return 1;
2830}
2831
David S. Miller0dbaee32010-12-13 12:52:14 -08002832static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2833{
2834 return dst_metric_advmss(dst->path);
2835}
2836
Steffen Klassertebb762f2011-11-23 02:12:51 +00002837static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002838{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002839 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2840
2841 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002842}
2843
David S. Millerf894cbf2012-07-02 21:52:24 -07002844static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2845 struct sk_buff *skb,
2846 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002847{
David S. Millerf894cbf2012-07-02 21:52:24 -07002848 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2852{
2853 int err = 0;
2854 if (unlikely(afinfo == NULL))
2855 return -EINVAL;
2856 if (unlikely(afinfo->family >= NPROTO))
2857 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002858 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002860 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 else {
2862 struct dst_ops *dst_ops = afinfo->dst_ops;
2863 if (likely(dst_ops->kmem_cachep == NULL))
2864 dst_ops->kmem_cachep = xfrm_dst_cache;
2865 if (likely(dst_ops->check == NULL))
2866 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002867 if (likely(dst_ops->default_advmss == NULL))
2868 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002869 if (likely(dst_ops->mtu == NULL))
2870 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (likely(dst_ops->negative_advice == NULL))
2872 dst_ops->negative_advice = xfrm_negative_advice;
2873 if (likely(dst_ops->link_failure == NULL))
2874 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002875 if (likely(dst_ops->neigh_lookup == NULL))
2876 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002878 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002879 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002881 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return err;
2884}
2885EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2886
2887int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2888{
2889 int err = 0;
2890 if (unlikely(afinfo == NULL))
2891 return -EINVAL;
2892 if (unlikely(afinfo->family >= NPROTO))
2893 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002894 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2896 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2897 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002898 else
2899 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2900 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002902 spin_unlock(&xfrm_policy_afinfo_lock);
2903 if (!err) {
2904 struct dst_ops *dst_ops = afinfo->dst_ops;
2905
2906 synchronize_rcu();
2907
2908 dst_ops->kmem_cachep = NULL;
2909 dst_ops->check = NULL;
2910 dst_ops->negative_advice = NULL;
2911 dst_ops->link_failure = NULL;
2912 afinfo->garbage_collect = NULL;
2913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return err;
2915}
2916EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2919{
Jiri Pirko351638e2013-05-28 01:30:21 +00002920 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 switch (event) {
2923 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002924 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926 return NOTIFY_DONE;
2927}
2928
2929static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002930 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931};
2932
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002933#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002934static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002935{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002936 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002937 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2938 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002939 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002940 rv = xfrm_proc_init(net);
2941 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002942 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002943 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002944}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002945
2946static void xfrm_statistics_fini(struct net *net)
2947{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002948 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002949 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002950}
2951#else
2952static int __net_init xfrm_statistics_init(struct net *net)
2953{
2954 return 0;
2955}
2956
2957static void xfrm_statistics_fini(struct net *net)
2958{
2959}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002960#endif
2961
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002962static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
David S. Miller2518c7c2006-08-24 04:45:07 -07002964 unsigned int hmask, sz;
2965 int dir;
2966
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002967 if (net_eq(net, &init_net))
2968 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002970 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002971 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
David S. Miller2518c7c2006-08-24 04:45:07 -07002973 hmask = 8 - 1;
2974 sz = (hmask+1) * sizeof(struct hlist_head);
2975
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002976 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2977 if (!net->xfrm.policy_byidx)
2978 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002979 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002980
Herbert Xu53c2e282014-11-13 17:09:49 +08002981 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002982 struct xfrm_policy_hash *htab;
2983
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002984 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002985 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002986 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002987
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002988 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002989 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002990 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002991 goto out_bydst;
2992 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002993 htab->dbits4 = 32;
2994 htab->sbits4 = 32;
2995 htab->dbits6 = 128;
2996 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002997 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002998 net->xfrm.policy_hthresh.lbits4 = 32;
2999 net->xfrm.policy_hthresh.rbits4 = 32;
3000 net->xfrm.policy_hthresh.lbits6 = 128;
3001 net->xfrm.policy_hthresh.rbits6 = 128;
3002
3003 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003004
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003005 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003006 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003007 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003008 if (net_eq(net, &init_net))
3009 register_netdevice_notifier(&xfrm_dev_notifier);
3010 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003011
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003012out_bydst:
3013 for (dir--; dir >= 0; dir--) {
3014 struct xfrm_policy_hash *htab;
3015
3016 htab = &net->xfrm.policy_bydst[dir];
3017 xfrm_hash_free(htab->table, sz);
3018 }
3019 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003020out_byidx:
3021 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003024static void xfrm_policy_fini(struct net *net)
3025{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003026 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003027 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003028
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003029 flush_work(&net->xfrm.policy_hash_work);
3030#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003031 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003032#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003033 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003034
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003035 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003036
Herbert Xu53c2e282014-11-13 17:09:49 +08003037 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003038 struct xfrm_policy_hash *htab;
3039
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003040 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003041
3042 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003043 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003044 WARN_ON(!hlist_empty(htab->table));
3045 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003046 }
3047
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003048 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003049 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3050 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003051}
3052
3053static int __net_init xfrm_net_init(struct net *net)
3054{
3055 int rv;
3056
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003057 rv = xfrm_statistics_init(net);
3058 if (rv < 0)
3059 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003060 rv = xfrm_state_init(net);
3061 if (rv < 0)
3062 goto out_state;
3063 rv = xfrm_policy_init(net);
3064 if (rv < 0)
3065 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003066 rv = xfrm_sysctl_init(net);
3067 if (rv < 0)
3068 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003069 rv = flow_cache_init(net);
3070 if (rv < 0)
3071 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003072
3073 /* Initialize the per-net locks here */
3074 spin_lock_init(&net->xfrm.xfrm_state_lock);
3075 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003076 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3077
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003078 return 0;
3079
Steffen Klassert4a93f502014-03-12 09:43:17 +01003080out:
3081 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003082out_sysctl:
3083 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003084out_policy:
3085 xfrm_state_fini(net);
3086out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003087 xfrm_statistics_fini(net);
3088out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003089 return rv;
3090}
3091
3092static void __net_exit xfrm_net_exit(struct net *net)
3093{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003094 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003095 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003096 xfrm_policy_fini(net);
3097 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003098 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003099}
3100
3101static struct pernet_operations __net_initdata xfrm_net_ops = {
3102 .init = xfrm_net_init,
3103 .exit = xfrm_net_exit,
3104};
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106void __init xfrm_init(void)
3107{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003108 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003109 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 xfrm_input_init();
3111}
3112
Joy Lattenab5f5e82007-09-17 11:51:22 -07003113#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003114static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3115 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003116{
Paul Moore875179f2007-12-01 23:27:18 +11003117 struct xfrm_sec_ctx *ctx = xp->security;
3118 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003119
Paul Moore875179f2007-12-01 23:27:18 +11003120 if (ctx)
3121 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3122 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3123
Weilong Chen9b7a7872013-12-24 09:43:46 +08003124 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003125 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003126 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003127 if (sel->prefixlen_s != 32)
3128 audit_log_format(audit_buf, " src_prefixlen=%d",
3129 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003130 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003131 if (sel->prefixlen_d != 32)
3132 audit_log_format(audit_buf, " dst_prefixlen=%d",
3133 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003134 break;
3135 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003136 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003137 if (sel->prefixlen_s != 128)
3138 audit_log_format(audit_buf, " src_prefixlen=%d",
3139 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003140 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003141 if (sel->prefixlen_d != 128)
3142 audit_log_format(audit_buf, " dst_prefixlen=%d",
3143 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003144 break;
3145 }
3146}
3147
Tetsuo Handa2e710292014-04-22 21:48:30 +09003148void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003149{
3150 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151
Paul Mooreafeb14b2007-12-21 14:58:11 -08003152 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153 if (audit_buf == NULL)
3154 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003155 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003156 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003157 xfrm_audit_common_policyinfo(xp, audit_buf);
3158 audit_log_end(audit_buf);
3159}
3160EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3161
Paul Moore68277ac2007-12-20 20:49:33 -08003162void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003163 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003164{
3165 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003166
Paul Mooreafeb14b2007-12-21 14:58:11 -08003167 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003168 if (audit_buf == NULL)
3169 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003170 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003171 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003172 xfrm_audit_common_policyinfo(xp, audit_buf);
3173 audit_log_end(audit_buf);
3174}
3175EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3176#endif
3177
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003179static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3180 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003181{
3182 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3183 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003184 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3185 sel_cmp->family) &&
3186 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3187 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003188 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3189 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003190 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003191 }
3192 } else {
3193 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003194 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003195 }
3196 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003197 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003198}
3199
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003200static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3201 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003202{
3203 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204 struct hlist_head *chain;
3205 u32 priority = ~0U;
3206
Fan Du283bc9f2013-11-07 17:47:50 +08003207 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003208 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003209 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003210 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3211 pol->type == type) {
3212 ret = pol;
3213 priority = ret->priority;
3214 break;
3215 }
3216 }
Fan Du8d549c42013-11-07 17:47:49 +08003217 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003218 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003219 if ((pol->priority >= priority) && ret)
3220 break;
3221
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003222 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003223 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003224 ret = pol;
3225 break;
3226 }
3227 }
3228
Li RongQing586f2eb2015-04-30 17:13:41 +08003229 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003230
Fan Du283bc9f2013-11-07 17:47:50 +08003231 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232
3233 return ret;
3234}
3235
David S. Millerdd701752011-02-24 00:21:08 -05003236static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003237{
3238 int match = 0;
3239
3240 if (t->mode == m->mode && t->id.proto == m->proto &&
3241 (m->reqid == 0 || t->reqid == m->reqid)) {
3242 switch (t->mode) {
3243 case XFRM_MODE_TUNNEL:
3244 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003245 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3246 m->old_family) &&
3247 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3248 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003249 match = 1;
3250 }
3251 break;
3252 case XFRM_MODE_TRANSPORT:
3253 /* in case of transport mode, template does not store
3254 any IP addresses, hence we just compare mode and
3255 protocol */
3256 match = 1;
3257 break;
3258 default:
3259 break;
3260 }
3261 }
3262 return match;
3263}
3264
3265/* update endpoint address(es) of template(s) */
3266static int xfrm_policy_migrate(struct xfrm_policy *pol,
3267 struct xfrm_migrate *m, int num_migrate)
3268{
3269 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003270 int i, j, n = 0;
3271
3272 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003273 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003274 /* target policy has been deleted */
3275 write_unlock_bh(&pol->lock);
3276 return -ENOENT;
3277 }
3278
3279 for (i = 0; i < pol->xfrm_nr; i++) {
3280 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3281 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3282 continue;
3283 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003284 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3285 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003286 continue;
3287 /* update endpoints */
3288 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3289 sizeof(pol->xfrm_vec[i].id.daddr));
3290 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3291 sizeof(pol->xfrm_vec[i].saddr));
3292 pol->xfrm_vec[i].encap_family = mp->new_family;
3293 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003294 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003295 }
3296 }
3297
3298 write_unlock_bh(&pol->lock);
3299
3300 if (!n)
3301 return -ENODATA;
3302
3303 return 0;
3304}
3305
David S. Millerdd701752011-02-24 00:21:08 -05003306static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003307{
3308 int i, j;
3309
3310 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3311 return -EINVAL;
3312
3313 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003314 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3315 m[i].old_family) &&
3316 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3317 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003318 return -EINVAL;
3319 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3320 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3321 return -EINVAL;
3322
3323 /* check if there is any duplicated entry */
3324 for (j = i + 1; j < num_migrate; j++) {
3325 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3326 sizeof(m[i].old_daddr)) &&
3327 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3328 sizeof(m[i].old_saddr)) &&
3329 m[i].proto == m[j].proto &&
3330 m[i].mode == m[j].mode &&
3331 m[i].reqid == m[j].reqid &&
3332 m[i].old_family == m[j].old_family)
3333 return -EINVAL;
3334 }
3335 }
3336
3337 return 0;
3338}
3339
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003340int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003341 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003342 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003343{
3344 int i, err, nx_cur = 0, nx_new = 0;
3345 struct xfrm_policy *pol = NULL;
3346 struct xfrm_state *x, *xc;
3347 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3348 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3349 struct xfrm_migrate *mp;
3350
3351 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3352 goto out;
3353
3354 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003355 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003356 err = -ENOENT;
3357 goto out;
3358 }
3359
3360 /* Stage 2 - find and update state(s) */
3361 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003362 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003363 x_cur[nx_cur] = x;
3364 nx_cur++;
3365 if ((xc = xfrm_state_migrate(x, mp))) {
3366 x_new[nx_new] = xc;
3367 nx_new++;
3368 } else {
3369 err = -ENODATA;
3370 goto restore_state;
3371 }
3372 }
3373 }
3374
3375 /* Stage 3 - update policy */
3376 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3377 goto restore_state;
3378
3379 /* Stage 4 - delete old state(s) */
3380 if (nx_cur) {
3381 xfrm_states_put(x_cur, nx_cur);
3382 xfrm_states_delete(x_cur, nx_cur);
3383 }
3384
3385 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003386 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003387
3388 xfrm_pol_put(pol);
3389
3390 return 0;
3391out:
3392 return err;
3393
3394restore_state:
3395 if (pol)
3396 xfrm_pol_put(pol);
3397 if (nx_cur)
3398 xfrm_states_put(x_cur, nx_cur);
3399 if (nx_new)
3400 xfrm_states_delete(x_new, nx_new);
3401
3402 return err;
3403}
David S. Millere610e672007-02-08 13:29:15 -08003404EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003405#endif