blob: 4a8d90a88c83ed9f44d7874b1c1d5f1bcbf2ae38 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Herbert Xu25ee3282007-12-11 09:32:34 -080053static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000054static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000055static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010056static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Herbert Xu12bfa8b2014-11-13 17:09:50 +080058static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080059static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
60 int dir);
61
David S. Millerbc9b35a2012-05-15 15:04:57 -040062static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050063__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080064{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050065 const struct flowi4 *fl4 = &fl->u.ip4;
66
Alexey Dobriyan26bff942011-11-22 06:46:02 +000067 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
68 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050069 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
71 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
72 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080073}
74
David S. Millerbc9b35a2012-05-15 15:04:57 -040075static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050076__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080077{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050078 const struct flowi6 *fl6 = &fl->u.ip6;
79
80 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
81 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
82 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
83 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
84 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
85 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080086}
87
David S. Millerbc9b35a2012-05-15 15:04:57 -040088bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
89 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080090{
91 switch (family) {
92 case AF_INET:
93 return __xfrm4_selector_match(sel, fl);
94 case AF_INET6:
95 return __xfrm6_selector_match(sel, fl);
96 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040097 return false;
Andrew Morton77681022006-11-08 22:46:26 -080098}
99
Eric Dumazetef8531b2012-08-19 12:31:48 +0200100static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
101{
102 struct xfrm_policy_afinfo *afinfo;
103
104 if (unlikely(family >= NPROTO))
105 return NULL;
106 rcu_read_lock();
107 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
108 if (unlikely(!afinfo))
109 rcu_read_unlock();
110 return afinfo;
111}
112
113static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
114{
115 rcu_read_unlock();
116}
117
David Ahern42a7b322015-08-10 16:58:11 -0600118static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
119 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David Ahern42a7b322015-08-10 16:58:11 -0600131 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
137
David Ahern42a7b322015-08-10 16:58:11 -0600138static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
139 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140 xfrm_address_t *prev_saddr,
141 xfrm_address_t *prev_daddr,
142 int family)
143{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800144 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900145 xfrm_address_t *saddr = &x->props.saddr;
146 xfrm_address_t *daddr = &x->id.daddr;
147 struct dst_entry *dst;
148
149 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
150 saddr = x->coaddr;
151 daddr = prev_daddr;
152 }
153 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
154 saddr = prev_saddr;
155 daddr = x->coaddr;
156 }
157
David Ahern42a7b322015-08-10 16:58:11 -0600158 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900159
160 if (!IS_ERR(dst)) {
161 if (prev_saddr != saddr)
162 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
163 if (prev_daddr != daddr)
164 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
165 }
166
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800167 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static inline unsigned long make_jiffies(long secs)
171{
172 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
173 return MAX_SCHEDULE_TIMEOUT-1;
174 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900175 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static void xfrm_policy_timer(unsigned long data)
179{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800180 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800181 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 long next = LONG_MAX;
183 int warn = 0;
184 int dir;
185
186 read_lock(&xp->lock);
187
Timo Teräsea2dea92010-03-31 00:17:05 +0000188 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out;
190
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700191 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (xp->lft.hard_add_expires_seconds) {
194 long tmo = xp->lft.hard_add_expires_seconds +
195 xp->curlft.add_time - now;
196 if (tmo <= 0)
197 goto expired;
198 if (tmo < next)
199 next = tmo;
200 }
201 if (xp->lft.hard_use_expires_seconds) {
202 long tmo = xp->lft.hard_use_expires_seconds +
203 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
204 if (tmo <= 0)
205 goto expired;
206 if (tmo < next)
207 next = tmo;
208 }
209 if (xp->lft.soft_add_expires_seconds) {
210 long tmo = xp->lft.soft_add_expires_seconds +
211 xp->curlft.add_time - now;
212 if (tmo <= 0) {
213 warn = 1;
214 tmo = XFRM_KM_TIMEOUT;
215 }
216 if (tmo < next)
217 next = tmo;
218 }
219 if (xp->lft.soft_use_expires_seconds) {
220 long tmo = xp->lft.soft_use_expires_seconds +
221 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
222 if (tmo <= 0) {
223 warn = 1;
224 tmo = XFRM_KM_TIMEOUT;
225 }
226 if (tmo < next)
227 next = tmo;
228 }
229
230 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800231 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (next != LONG_MAX &&
233 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
234 xfrm_pol_hold(xp);
235
236out:
237 read_unlock(&xp->lock);
238 xfrm_pol_put(xp);
239 return;
240
241expired:
242 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700243 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800244 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfrm_pol_put(xp);
246}
247
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000248static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
249{
250 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
251
252 if (unlikely(pol->walk.dead))
253 flo = NULL;
254 else
255 xfrm_pol_hold(pol);
256
257 return flo;
258}
259
260static int xfrm_policy_flo_check(struct flow_cache_object *flo)
261{
262 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
263
264 return !pol->walk.dead;
265}
266
267static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
268{
269 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
270}
271
272static const struct flow_cache_ops xfrm_policy_fc_ops = {
273 .get = xfrm_policy_flo_get,
274 .check = xfrm_policy_flo_check,
275 .delete = xfrm_policy_flo_delete,
276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
279 * SPD calls.
280 */
281
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800282struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct xfrm_policy *policy;
285
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700286 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800289 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700290 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700291 INIT_HLIST_NODE(&policy->bydst);
292 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700294 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100295 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800296 setup_timer(&policy->timer, xfrm_policy_timer,
297 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100298 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
299 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000300 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 return policy;
303}
304EXPORT_SYMBOL(xfrm_policy_alloc);
305
Eric Dumazet56f04732015-12-08 07:22:01 -0800306static void xfrm_policy_destroy_rcu(struct rcu_head *head)
307{
308 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
309
310 security_xfrm_policy_free(policy->security);
311 kfree(policy);
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Destroy xfrm_policy: descendant resources must be released to this moment. */
315
WANG Cong64c31b32008-01-07 22:34:29 -0800316void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Herbert Xu12a169e2008-10-01 07:03:24 -0700318 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Fan Du0659eea2013-08-01 18:08:36 +0800320 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 BUG();
322
Eric Dumazet56f04732015-12-08 07:22:01 -0800323 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
WANG Cong64c31b32008-01-07 22:34:29 -0800325EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* Rule must be locked. Release descentant resources, announce
328 * entry dead. The rule must be unlinked from lists to the moment.
329 */
330
331static void xfrm_policy_kill(struct xfrm_policy *policy)
332{
Herbert Xu12a169e2008-10-01 07:03:24 -0700333 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Timo Teräs285ead12010-04-07 00:30:06 +0000335 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200337 if (del_timer(&policy->polq.hold_timer))
338 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800339 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100340
Timo Teräs285ead12010-04-07 00:30:06 +0000341 if (del_timer(&policy->timer))
342 xfrm_pol_put(policy);
343
344 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
David S. Miller2518c7c2006-08-24 04:45:07 -0700347static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
348
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800349static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700350{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800351 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700352}
353
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200354/* calculate policy hash thresholds */
355static void __get_hash_thresh(struct net *net,
356 unsigned short family, int dir,
357 u8 *dbits, u8 *sbits)
358{
359 switch (family) {
360 case AF_INET:
361 *dbits = net->xfrm.policy_bydst[dir].dbits4;
362 *sbits = net->xfrm.policy_bydst[dir].sbits4;
363 break;
364
365 case AF_INET6:
366 *dbits = net->xfrm.policy_bydst[dir].dbits6;
367 *sbits = net->xfrm.policy_bydst[dir].sbits6;
368 break;
369
370 default:
371 *dbits = 0;
372 *sbits = 0;
373 }
374}
375
David S. Miller5f803b52011-02-24 00:33:19 -0500376static struct hlist_head *policy_hash_bysel(struct net *net,
377 const struct xfrm_selector *sel,
378 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700379{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800380 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200381 unsigned int hash;
382 u8 dbits;
383 u8 sbits;
384
385 __get_hash_thresh(net, family, dir, &dbits, &sbits);
386 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700387
Florian Westphale1e551b2016-08-11 15:17:53 +0200388 if (hash == hmask + 1)
389 return &net->xfrm.policy_inexact[dir];
390
391 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
392 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700393}
394
David S. Miller5f803b52011-02-24 00:33:19 -0500395static struct hlist_head *policy_hash_direct(struct net *net,
396 const xfrm_address_t *daddr,
397 const xfrm_address_t *saddr,
398 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700399{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800400 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200401 unsigned int hash;
402 u8 dbits;
403 u8 sbits;
404
405 __get_hash_thresh(net, family, dir, &dbits, &sbits);
406 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700407
Florian Westphale1e551b2016-08-11 15:17:53 +0200408 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
409 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700410}
411
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200412static void xfrm_dst_hash_transfer(struct net *net,
413 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700414 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200415 unsigned int nhashmask,
416 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700417{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800418 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700419 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800420 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200421 u8 dbits;
422 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700423
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800424redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800425 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700426 unsigned int h;
427
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200428 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700429 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200430 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800431 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200432 hlist_del_rcu(&pol->bydst);
433 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800434 h0 = h;
435 } else {
436 if (h != h0)
437 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200438 hlist_del_rcu(&pol->bydst);
439 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800440 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800441 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800442 }
443 if (!hlist_empty(list)) {
444 entry0 = NULL;
445 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700446 }
447}
448
449static void xfrm_idx_hash_transfer(struct hlist_head *list,
450 struct hlist_head *nidxtable,
451 unsigned int nhashmask)
452{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800453 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700454 struct xfrm_policy *pol;
455
Sasha Levinb67bfe02013-02-27 17:06:00 -0800456 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700457 unsigned int h;
458
459 h = __idx_hash(pol->index, nhashmask);
460 hlist_add_head(&pol->byidx, nidxtable+h);
461 }
462}
463
464static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
465{
466 return ((old_hmask + 1) << 1) - 1;
467}
468
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800469static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700470{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800471 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700472 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
473 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700474 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200475 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700476 int i;
477
478 if (!ndst)
479 return;
480
Fan Du283bc9f2013-11-07 17:47:50 +0800481 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700482
Florian Westphale1e551b2016-08-11 15:17:53 +0200483 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
484 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
485
David S. Miller2518c7c2006-08-24 04:45:07 -0700486 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200487 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700488
Florian Westphale1e551b2016-08-11 15:17:53 +0200489 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800490 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700491
Fan Du283bc9f2013-11-07 17:47:50 +0800492 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700493
Florian Westphale1e551b2016-08-11 15:17:53 +0200494 synchronize_rcu();
495
David S. Miller44e36b42006-08-24 04:50:50 -0700496 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700497}
498
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800499static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700500{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800501 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700502 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
503 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800504 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700505 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700506 int i;
507
508 if (!nidx)
509 return;
510
Fan Du283bc9f2013-11-07 17:47:50 +0800511 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
513 for (i = hmask; i >= 0; i--)
514 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
515
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516 net->xfrm.policy_byidx = nidx;
517 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700518
Fan Du283bc9f2013-11-07 17:47:50 +0800519 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700520
David S. Miller44e36b42006-08-24 04:50:50 -0700521 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700522}
523
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700525{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800526 unsigned int cnt = net->xfrm.policy_count[dir];
527 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700528
529 if (total)
530 *total += cnt;
531
532 if ((hmask + 1) < xfrm_policy_hashmax &&
533 cnt > hmask)
534 return 1;
535
536 return 0;
537}
538
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800539static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700540{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800541 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700542
543 if ((hmask + 1) < xfrm_policy_hashmax &&
544 total > hmask)
545 return 1;
546
547 return 0;
548}
549
Alexey Dobriyane0710412010-01-23 13:37:10 +0000550void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700551{
Fan Du283bc9f2013-11-07 17:47:50 +0800552 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000553 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
554 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
555 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
556 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
557 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
558 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
559 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700560 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800561 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700562}
563EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700564
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700565static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800566static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700567{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800568 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700569 int dir, total;
570
571 mutex_lock(&hash_resize_mutex);
572
573 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800574 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800575 if (xfrm_bydst_should_resize(net, dir, &total))
576 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700577 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800578 if (xfrm_byidx_should_resize(net, total))
579 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700580
581 mutex_unlock(&hash_resize_mutex);
582}
583
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200584static void xfrm_hash_rebuild(struct work_struct *work)
585{
586 struct net *net = container_of(work, struct net,
587 xfrm.policy_hthresh.work);
588 unsigned int hmask;
589 struct xfrm_policy *pol;
590 struct xfrm_policy *policy;
591 struct hlist_head *chain;
592 struct hlist_head *odst;
593 struct hlist_node *newpos;
594 int i;
595 int dir;
596 unsigned seq;
597 u8 lbits4, rbits4, lbits6, rbits6;
598
599 mutex_lock(&hash_resize_mutex);
600
601 /* read selector prefixlen thresholds */
602 do {
603 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
604
605 lbits4 = net->xfrm.policy_hthresh.lbits4;
606 rbits4 = net->xfrm.policy_hthresh.rbits4;
607 lbits6 = net->xfrm.policy_hthresh.lbits6;
608 rbits6 = net->xfrm.policy_hthresh.rbits6;
609 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
610
611 write_lock_bh(&net->xfrm.xfrm_policy_lock);
612
613 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800614 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200615 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
616 hmask = net->xfrm.policy_bydst[dir].hmask;
617 odst = net->xfrm.policy_bydst[dir].table;
618 for (i = hmask; i >= 0; i--)
619 INIT_HLIST_HEAD(odst + i);
620 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
621 /* dir out => dst = remote, src = local */
622 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
623 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
624 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
625 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
626 } else {
627 /* dir in/fwd => dst = local, src = remote */
628 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
629 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
630 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
631 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
632 }
633 }
634
635 /* re-insert all policies by order of creation */
636 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
637 newpos = NULL;
638 chain = policy_hash_bysel(net, &policy->selector,
639 policy->family,
640 xfrm_policy_id2dir(policy->index));
641 hlist_for_each_entry(pol, chain, bydst) {
642 if (policy->priority >= pol->priority)
643 newpos = &pol->bydst;
644 else
645 break;
646 }
647 if (newpos)
648 hlist_add_behind(&policy->bydst, newpos);
649 else
650 hlist_add_head(&policy->bydst, chain);
651 }
652
653 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
654
655 mutex_unlock(&hash_resize_mutex);
656}
657
658void xfrm_policy_hash_rebuild(struct net *net)
659{
660 schedule_work(&net->xfrm.policy_hthresh.work);
661}
662EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/* Generate new index... KAME seems to generate them ordered by cost
665 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800666static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 static u32 idx_generator;
669
670 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700671 struct hlist_head *list;
672 struct xfrm_policy *p;
673 u32 idx;
674 int found;
675
Fan Due682adf2013-11-07 17:47:48 +0800676 if (!index) {
677 idx = (idx_generator | dir);
678 idx_generator += 8;
679 } else {
680 idx = index;
681 index = 0;
682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (idx == 0)
685 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800686 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700687 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800688 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700689 if (p->index == idx) {
690 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700694 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return idx;
696 }
697}
698
David S. Miller2518c7c2006-08-24 04:45:07 -0700699static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
700{
701 u32 *p1 = (u32 *) s1;
702 u32 *p2 = (u32 *) s2;
703 int len = sizeof(struct xfrm_selector) / sizeof(u32);
704 int i;
705
706 for (i = 0; i < len; i++) {
707 if (p1[i] != p2[i])
708 return 1;
709 }
710
711 return 0;
712}
713
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100714static void xfrm_policy_requeue(struct xfrm_policy *old,
715 struct xfrm_policy *new)
716{
717 struct xfrm_policy_queue *pq = &old->polq;
718 struct sk_buff_head list;
719
Li RongQingde2ad482015-04-30 17:25:19 +0800720 if (skb_queue_empty(&pq->hold_queue))
721 return;
722
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100723 __skb_queue_head_init(&list);
724
725 spin_lock_bh(&pq->hold_queue.lock);
726 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200727 if (del_timer(&pq->hold_timer))
728 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100729 spin_unlock_bh(&pq->hold_queue.lock);
730
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100731 pq = &new->polq;
732
733 spin_lock_bh(&pq->hold_queue.lock);
734 skb_queue_splice(&list, &pq->hold_queue);
735 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200736 if (!mod_timer(&pq->hold_timer, jiffies))
737 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100738 spin_unlock_bh(&pq->hold_queue.lock);
739}
740
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100741static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
742 struct xfrm_policy *pol)
743{
744 u32 mark = policy->mark.v & policy->mark.m;
745
746 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
747 return true;
748
749 if ((mark & pol->mark.m) == pol->mark.v &&
750 policy->priority == pol->priority)
751 return true;
752
753 return false;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
757{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800758 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700759 struct xfrm_policy *pol;
760 struct xfrm_policy *delpol;
761 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800762 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Fan Du283bc9f2013-11-07 17:47:50 +0800764 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800765 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700766 delpol = NULL;
767 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800768 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800769 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700770 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100771 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800772 xfrm_sec_ctx_match(pol->security, policy->security) &&
773 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800775 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EEXIST;
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 delpol = pol;
779 if (policy->priority > pol->priority)
780 continue;
781 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800782 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 continue;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (delpol)
786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700789 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700790 else
791 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800792 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800793 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800794
795 /* After previous checking, family can either be AF_INET or AF_INET6 */
796 if (policy->family == AF_INET)
797 rt_genid_bump_ipv4(net);
798 else
799 rt_genid_bump_ipv6(net);
800
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100801 if (delpol) {
802 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800803 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100804 }
Fan Due682adf2013-11-07 17:47:48 +0800805 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800806 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800807 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 policy->curlft.use_time = 0;
809 if (!mod_timer(&policy->timer, jiffies + HZ))
810 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800811 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
David S. Miller9b78a822005-12-22 07:39:48 -0800813 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800815 else if (xfrm_bydst_should_resize(net, dir, NULL))
816 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return 0;
819}
820EXPORT_SYMBOL(xfrm_policy_insert);
821
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000822struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
823 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800824 struct xfrm_sec_ctx *ctx, int delete,
825 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 struct xfrm_policy *pol, *ret;
828 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Eric Parisef41aaa2007-03-07 15:37:58 -0800830 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800831 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800832 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800834 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700835 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000836 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700837 !selector_cmp(sel, &pol->selector) &&
838 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700841 *err = security_xfrm_policy_delete(
842 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800843 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800844 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800845 return pol;
846 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800847 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 }
849 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 break;
851 }
852 }
Fan Du283bc9f2013-11-07 17:47:50 +0800853 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000855 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700857 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Trent Jaegerdf718372005-12-13 23:12:27 -0800859EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000861struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
862 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 struct xfrm_policy *pol, *ret;
865 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Herbert Xub5505c62007-05-14 02:15:47 -0700867 *err = -ENOENT;
868 if (xfrm_policy_id2dir(id) != dir)
869 return NULL;
870
Eric Parisef41aaa2007-03-07 15:37:58 -0800871 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800872 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800873 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700874 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800875 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000876 if (pol->type == type && pol->index == id &&
877 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700879 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700880 *err = security_xfrm_policy_delete(
881 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800882 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800883 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800884 return pol;
885 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800886 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700887 }
888 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890 }
891 }
Fan Du283bc9f2013-11-07 17:47:50 +0800892 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000894 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700895 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700896 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898EXPORT_SYMBOL(xfrm_policy_byid);
899
Joy Latten4aa2e622007-06-04 19:05:57 -0400900#ifdef CONFIG_SECURITY_NETWORK_XFRM
901static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900902xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Joy Latten4aa2e622007-06-04 19:05:57 -0400904 int dir, err = 0;
905
906 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
907 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400908 int i;
909
Sasha Levinb67bfe02013-02-27 17:06:00 -0800910 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800911 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400912 if (pol->type != type)
913 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700914 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400915 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900916 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400917 return err;
918 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900919 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800920 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800921 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800922 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400923 bydst) {
924 if (pol->type != type)
925 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700926 err = security_xfrm_policy_delete(
927 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400928 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700929 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900930 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400931 return err;
932 }
933 }
934 }
935 }
936 return err;
937}
938#else
939static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900940xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400941{
942 return 0;
943}
944#endif
945
Tetsuo Handa2e710292014-04-22 21:48:30 +0900946int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400947{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000948 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Fan Du283bc9f2013-11-07 17:47:50 +0800950 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400951
Tetsuo Handa2e710292014-04-22 21:48:30 +0900952 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400953 if (err)
954 goto out;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700957 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800958 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700959
960 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800961 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800962 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700963 if (pol->type != type)
964 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000965 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800966 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000967 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Tetsuo Handa2e710292014-04-22 21:48:30 +0900969 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600970
David S. Miller2518c7c2006-08-24 04:45:07 -0700971 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Fan Du283bc9f2013-11-07 17:47:50 +0800973 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700974 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700976
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800977 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700978 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800979 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800980 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700981 bydst) {
982 if (pol->type != type)
983 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000984 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800985 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000986 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700987
Tetsuo Handa2e710292014-04-22 21:48:30 +0900988 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700989 xfrm_policy_kill(pol);
990
Fan Du283bc9f2013-11-07 17:47:50 +0800991 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700992 goto again2;
993 }
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000997 if (!cnt)
998 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400999out:
Fan Du283bc9f2013-11-07 17:47:50 +08001000 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001001 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003EXPORT_SYMBOL(xfrm_policy_flush);
1004
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001005int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001006 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 void *data)
1008{
Herbert Xu12a169e2008-10-01 07:03:24 -07001009 struct xfrm_policy *pol;
1010 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001011 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Timo Teras4c563f72008-02-28 21:31:08 -08001013 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1014 walk->type != XFRM_POLICY_TYPE_ANY)
1015 return -EINVAL;
1016
Herbert Xu12a169e2008-10-01 07:03:24 -07001017 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001018 return 0;
1019
Fan Du283bc9f2013-11-07 17:47:50 +08001020 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001021 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001022 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001023 else
Li RongQing80077702015-04-22 17:09:54 +08001024 x = list_first_entry(&walk->walk.all,
1025 struct xfrm_policy_walk_entry, all);
1026
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001027 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001028 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001029 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001030 pol = container_of(x, struct xfrm_policy, walk);
1031 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1032 walk->type != pol->type)
1033 continue;
1034 error = func(pol, xfrm_policy_id2dir(pol->index),
1035 walk->seq, data);
1036 if (error) {
1037 list_move_tail(&walk->walk.all, &x->all);
1038 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001039 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001040 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001042 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001043 error = -ENOENT;
1044 goto out;
1045 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001046 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047out:
Fan Du283bc9f2013-11-07 17:47:50 +08001048 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return error;
1050}
1051EXPORT_SYMBOL(xfrm_policy_walk);
1052
Herbert Xu12a169e2008-10-01 07:03:24 -07001053void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1054{
1055 INIT_LIST_HEAD(&walk->walk.all);
1056 walk->walk.dead = 1;
1057 walk->type = type;
1058 walk->seq = 0;
1059}
1060EXPORT_SYMBOL(xfrm_policy_walk_init);
1061
Fan Du283bc9f2013-11-07 17:47:50 +08001062void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001063{
1064 if (list_empty(&walk->walk.all))
1065 return;
1066
Fan Du283bc9f2013-11-07 17:47:50 +08001067 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001068 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001069 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001070}
1071EXPORT_SYMBOL(xfrm_policy_walk_done);
1072
James Morris134b0fc2006-10-05 15:42:27 -05001073/*
1074 * Find policy to apply to this flow.
1075 *
1076 * Returns 0 if policy found, else an -errno.
1077 */
David S. Millerf299d552011-02-24 01:23:30 -05001078static int xfrm_policy_match(const struct xfrm_policy *pol,
1079 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001080 u8 type, u16 family, int dir)
1081{
David S. Millerf299d552011-02-24 01:23:30 -05001082 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001083 int ret = -ESRCH;
1084 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001085
1086 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001087 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001088 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001089 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001090
1091 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001092 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001093 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001094 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001095
James Morris134b0fc2006-10-05 15:42:27 -05001096 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001097}
1098
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001099static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001100 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001101 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
James Morris134b0fc2006-10-05 15:42:27 -05001103 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001104 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001105 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001106 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001107 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001108
1109 daddr = xfrm_flowi_daddr(fl, family);
1110 saddr = xfrm_flowi_saddr(fl, family);
1111 if (unlikely(!daddr || !saddr))
1112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Fan Du283bc9f2013-11-07 17:47:50 +08001114 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001115 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001116 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001117 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001118 err = xfrm_policy_match(pol, fl, type, family, dir);
1119 if (err) {
1120 if (err == -ESRCH)
1121 continue;
1122 else {
1123 ret = ERR_PTR(err);
1124 goto fail;
1125 }
1126 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001127 ret = pol;
1128 priority = ret->priority;
1129 break;
1130 }
1131 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001132 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001133 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001134 if ((pol->priority >= priority) && ret)
1135 break;
1136
James Morris134b0fc2006-10-05 15:42:27 -05001137 err = xfrm_policy_match(pol, fl, type, family, dir);
1138 if (err) {
1139 if (err == -ESRCH)
1140 continue;
1141 else {
1142 ret = ERR_PTR(err);
1143 goto fail;
1144 }
Li RongQing8faf4912015-05-14 11:16:59 +08001145 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001146 ret = pol;
1147 break;
1148 }
1149 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001150
1151 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001152fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001153 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001154
David S. Miller2518c7c2006-08-24 04:45:07 -07001155 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001156}
1157
Timo Teräs80c802f2010-04-07 00:30:05 +00001158static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001159__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001160{
1161#ifdef CONFIG_XFRM_SUB_POLICY
1162 struct xfrm_policy *pol;
1163
1164 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1165 if (pol != NULL)
1166 return pol;
1167#endif
1168 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1169}
1170
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001171static int flow_to_policy_dir(int dir)
1172{
1173 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1174 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1175 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1176 return dir;
1177
1178 switch (dir) {
1179 default:
1180 case FLOW_DIR_IN:
1181 return XFRM_POLICY_IN;
1182 case FLOW_DIR_OUT:
1183 return XFRM_POLICY_OUT;
1184 case FLOW_DIR_FWD:
1185 return XFRM_POLICY_FWD;
1186 }
1187}
1188
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001189static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001190xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001191 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001192{
1193 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001194
1195 if (old_obj)
1196 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001197
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001198 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001199 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001200 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001201
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001202 /* Resolver returns two references:
1203 * one for cache and one for caller of flow_cache_lookup() */
1204 xfrm_pol_hold(pol);
1205
1206 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Trent Jaegerdf718372005-12-13 23:12:27 -08001209static inline int policy_to_flow_dir(int dir)
1210{
1211 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001212 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1213 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1214 return dir;
1215 switch (dir) {
1216 default:
1217 case XFRM_POLICY_IN:
1218 return FLOW_DIR_IN;
1219 case XFRM_POLICY_OUT:
1220 return FLOW_DIR_OUT;
1221 case XFRM_POLICY_FWD:
1222 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001223 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001224}
1225
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001226static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001227 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001230 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Eric Dumazetd188ba82015-12-08 07:22:02 -08001232 rcu_read_lock();
Fan Du283bc9f2013-11-07 17:47:50 +08001233 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001234 pol = rcu_dereference(sk->sk_policy[dir]);
1235 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001236 bool match = xfrm_selector_match(&pol->selector, fl,
1237 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001238 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001239
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001240 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001241 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1242 pol = NULL;
1243 goto out;
1244 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001245 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001246 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001247 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001248 if (!err)
1249 xfrm_pol_hold(pol);
1250 else if (err == -ESRCH)
1251 pol = NULL;
1252 else
1253 pol = ERR_PTR(err);
1254 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 pol = NULL;
1256 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001257out:
Fan Du283bc9f2013-11-07 17:47:50 +08001258 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001259 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return pol;
1261}
1262
1263static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1264{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001265 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001266
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001267 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001268 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 xfrm_pol_hold(pol);
1270}
1271
1272static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1273 int dir)
1274{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001275 struct net *net = xp_net(pol);
1276
Herbert Xu53c2e282014-11-13 17:09:49 +08001277 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001278 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Herbert Xu53c2e282014-11-13 17:09:49 +08001280 /* Socket policies are not hashed. */
1281 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001282 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001283 hlist_del(&pol->byidx);
1284 }
1285
1286 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001287 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001288
1289 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Herbert Xu53c2e282014-11-13 17:09:49 +08001292static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1293{
1294 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1295}
1296
1297static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1298{
1299 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1300}
1301
Herbert Xu4666faa2005-06-18 22:43:22 -07001302int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Fan Du283bc9f2013-11-07 17:47:50 +08001304 struct net *net = xp_net(pol);
1305
1306 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001308 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001313 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
David S. Millera70fcb02006-03-20 19:18:52 -08001315EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1318{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001319 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct xfrm_policy *old_pol;
1321
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001322#ifdef CONFIG_XFRM_SUB_POLICY
1323 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1324 return -EINVAL;
1325#endif
1326
Fan Du283bc9f2013-11-07 17:47:50 +08001327 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001328 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1329 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001331 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001332 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001333 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001335 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001336 if (old_pol) {
1337 if (pol)
1338 xfrm_policy_requeue(old_pol, pol);
1339
Timo Teräsea2dea92010-03-31 00:17:05 +00001340 /* Unlinking succeeds always. This is the only function
1341 * allowed to delete or replace socket policy.
1342 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001343 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001344 }
Fan Du283bc9f2013-11-07 17:47:50 +08001345 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 if (old_pol) {
1348 xfrm_policy_kill(old_pol);
1349 }
1350 return 0;
1351}
1352
David S. Millerd3e40a92011-02-24 01:25:41 -05001353static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001355 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001356 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (newp) {
1359 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001360 if (security_xfrm_policy_clone(old->security,
1361 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001362 kfree(newp);
1363 return NULL; /* ENOMEM */
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 newp->lft = old->lft;
1366 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001367 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 newp->action = old->action;
1369 newp->flags = old->flags;
1370 newp->xfrm_nr = old->xfrm_nr;
1371 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001372 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 memcpy(newp->xfrm_vec, old->xfrm_vec,
1374 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001375 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001376 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001377 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 xfrm_pol_put(newp);
1379 }
1380 return newp;
1381}
1382
Eric Dumazetd188ba82015-12-08 07:22:02 -08001383int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001385 const struct xfrm_policy *p;
1386 struct xfrm_policy *np;
1387 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Eric Dumazetd188ba82015-12-08 07:22:02 -08001389 rcu_read_lock();
1390 for (i = 0; i < 2; i++) {
1391 p = rcu_dereference(osk->sk_policy[i]);
1392 if (p) {
1393 np = clone_policy(p, i);
1394 if (unlikely(!np)) {
1395 ret = -ENOMEM;
1396 break;
1397 }
1398 rcu_assign_pointer(sk->sk_policy[i], np);
1399 }
1400 }
1401 rcu_read_unlock();
1402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001405static int
David Ahern42a7b322015-08-10 16:58:11 -06001406xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1407 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001408{
1409 int err;
1410 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1411
1412 if (unlikely(afinfo == NULL))
1413 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001414 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001415 xfrm_policy_put_afinfo(afinfo);
1416 return err;
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/* Resolve list of templates for the flow, given policy. */
1420
1421static int
David S. Millera6c2e612011-02-22 18:35:39 -08001422xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1423 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001425 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int nx;
1427 int i, error;
1428 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1429 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001430 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Weilong Chen9b7a7872013-12-24 09:43:46 +08001432 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct xfrm_state *x;
1434 xfrm_address_t *remote = daddr;
1435 xfrm_address_t *local = saddr;
1436 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1437
Joakim Koskela48b8d782007-07-26 00:08:42 -07001438 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1439 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 remote = &tmpl->id.daddr;
1441 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001442 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001443 error = xfrm_get_saddr(net, fl->flowi_oif,
1444 &tmp, remote,
1445 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001446 if (error)
1447 goto fail;
1448 local = &tmp;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
1452 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1453
1454 if (x && x->km.state == XFRM_STATE_VALID) {
1455 xfrm[nx++] = x;
1456 daddr = remote;
1457 saddr = local;
1458 continue;
1459 }
1460 if (x) {
1461 error = (x->km.state == XFRM_STATE_ERROR ?
1462 -EINVAL : -EAGAIN);
1463 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001464 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001465 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (!tmpl->optional)
1469 goto fail;
1470 }
1471 return nx;
1472
1473fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001474 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 xfrm_state_put(xfrm[nx]);
1476 return error;
1477}
1478
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001479static int
David S. Millera6c2e612011-02-22 18:35:39 -08001480xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1481 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001482{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001483 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1484 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001485 int cnx = 0;
1486 int error;
1487 int ret;
1488 int i;
1489
1490 for (i = 0; i < npols; i++) {
1491 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1492 error = -ENOBUFS;
1493 goto fail;
1494 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001495
1496 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001497 if (ret < 0) {
1498 error = ret;
1499 goto fail;
1500 } else
1501 cnx += ret;
1502 }
1503
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001504 /* found states are sorted for outbound processing */
1505 if (npols > 1)
1506 xfrm_state_sort(xfrm, tpp, cnx, family);
1507
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001508 return cnx;
1509
1510 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001511 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001512 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001513 return error;
1514
1515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/* Check that the bundle accepts the flow and its components are
1518 * still valid.
1519 */
1520
David S. Miller05d84022011-02-22 17:47:10 -08001521static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001522{
1523 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1524 int tos;
1525
1526 if (!afinfo)
1527 return -EINVAL;
1528
1529 tos = afinfo->get_tos(fl);
1530
1531 xfrm_policy_put_afinfo(afinfo);
1532
1533 return tos;
1534}
1535
Timo Teräs80c802f2010-04-07 00:30:05 +00001536static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1537{
1538 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1539 struct dst_entry *dst = &xdst->u.dst;
1540
1541 if (xdst->route == NULL) {
1542 /* Dummy bundle - if it has xfrms we were not
1543 * able to build bundle as template resolution failed.
1544 * It means we need to try again resolving. */
1545 if (xdst->num_xfrms > 0)
1546 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001547 } else if (dst->flags & DST_XFRM_QUEUE) {
1548 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001549 } else {
1550 /* Real bundle */
1551 if (stale_bundle(dst))
1552 return NULL;
1553 }
1554
1555 dst_hold(dst);
1556 return flo;
1557}
1558
1559static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1560{
1561 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1562 struct dst_entry *dst = &xdst->u.dst;
1563
1564 if (!xdst->route)
1565 return 0;
1566 if (stale_bundle(dst))
1567 return 0;
1568
1569 return 1;
1570}
1571
1572static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1573{
1574 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1575 struct dst_entry *dst = &xdst->u.dst;
1576
1577 dst_free(dst);
1578}
1579
1580static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1581 .get = xfrm_bundle_flo_get,
1582 .check = xfrm_bundle_flo_check,
1583 .delete = xfrm_bundle_flo_delete,
1584};
1585
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001586static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001587{
1588 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001589 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001590 struct xfrm_dst *xdst;
1591
1592 if (!afinfo)
1593 return ERR_PTR(-EINVAL);
1594
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001595 switch (family) {
1596 case AF_INET:
1597 dst_ops = &net->xfrm.xfrm4_dst_ops;
1598 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001599#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001600 case AF_INET6:
1601 dst_ops = &net->xfrm.xfrm6_dst_ops;
1602 break;
1603#endif
1604 default:
1605 BUG();
1606 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001607 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001608
Madalin Bucurd4cae562011-09-26 07:04:36 +00001609 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001610 struct dst_entry *dst = &xdst->u.dst;
1611
1612 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001613 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001614 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001615 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001616
Madalin Bucurd4cae562011-09-26 07:04:36 +00001617 xfrm_policy_put_afinfo(afinfo);
1618
Herbert Xu25ee3282007-12-11 09:32:34 -08001619 return xdst;
1620}
1621
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001622static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1623 int nfheader_len)
1624{
1625 struct xfrm_policy_afinfo *afinfo =
1626 xfrm_policy_get_afinfo(dst->ops->family);
1627 int err;
1628
1629 if (!afinfo)
1630 return -EINVAL;
1631
1632 err = afinfo->init_path(path, dst, nfheader_len);
1633
1634 xfrm_policy_put_afinfo(afinfo);
1635
1636 return err;
1637}
1638
Herbert Xu87c1e122010-03-02 02:51:56 +00001639static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001640 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001641{
1642 struct xfrm_policy_afinfo *afinfo =
1643 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1644 int err;
1645
1646 if (!afinfo)
1647 return -EINVAL;
1648
Herbert Xu87c1e122010-03-02 02:51:56 +00001649 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001650
1651 xfrm_policy_put_afinfo(afinfo);
1652
1653 return err;
1654}
1655
Timo Teräs80c802f2010-04-07 00:30:05 +00001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1658 * all the metrics... Shortly, bundle a bundle.
1659 */
1660
Herbert Xu25ee3282007-12-11 09:32:34 -08001661static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1662 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001663 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001664 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001666 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001667 unsigned long now = jiffies;
1668 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001669 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001670 struct dst_entry *dst_prev = NULL;
1671 struct dst_entry *dst0 = NULL;
1672 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001674 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001675 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001676 int trailer_len = 0;
1677 int tos;
1678 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001679 xfrm_address_t saddr, daddr;
1680
1681 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001682
1683 tos = xfrm_get_tos(fl, family);
1684 err = tos;
1685 if (tos < 0)
1686 goto put_states;
1687
1688 dst_hold(dst);
1689
1690 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001691 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001692 struct dst_entry *dst1 = &xdst->u.dst;
1693
1694 err = PTR_ERR(xdst);
1695 if (IS_ERR(xdst)) {
1696 dst_release(dst);
1697 goto put_states;
1698 }
1699
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001700 if (xfrm[i]->sel.family == AF_UNSPEC) {
1701 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1702 xfrm_af2proto(family));
1703 if (!inner_mode) {
1704 err = -EAFNOSUPPORT;
1705 dst_release(dst);
1706 goto put_states;
1707 }
1708 } else
1709 inner_mode = xfrm[i]->inner_mode;
1710
Herbert Xu25ee3282007-12-11 09:32:34 -08001711 if (!dst_prev)
1712 dst0 = dst1;
1713 else {
1714 dst_prev->child = dst_clone(dst1);
1715 dst1->flags |= DST_NOHASH;
1716 }
1717
1718 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001719 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001720
1721 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1722 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001723 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1724 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001725 err = PTR_ERR(dst);
1726 if (IS_ERR(dst))
1727 goto put_states;
1728 } else
1729 dst_hold(dst);
1730
1731 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001732 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001733
David S. Millerf5b0a872012-07-19 12:31:33 -07001734 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001735 dst1->flags |= DST_HOST;
1736 dst1->lastuse = now;
1737
1738 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001739 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001740
1741 dst1->next = dst_prev;
1742 dst_prev = dst1;
1743
1744 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001745 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1746 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001747 trailer_len += xfrm[i]->props.trailer_len;
1748 }
1749
1750 dst_prev->child = dst;
1751 dst0->path = dst;
1752
1753 err = -ENODEV;
1754 dev = dst->dev;
1755 if (!dev)
1756 goto free_dst;
1757
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001758 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001759 xfrm_init_pmtu(dst_prev);
1760
1761 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1762 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1763
Herbert Xu87c1e122010-03-02 02:51:56 +00001764 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001765 if (err)
1766 goto free_dst;
1767
1768 dst_prev->header_len = header_len;
1769 dst_prev->trailer_len = trailer_len;
1770 header_len -= xdst->u.dst.xfrm->props.header_len;
1771 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1772 }
1773
1774out:
1775 return dst0;
1776
1777put_states:
1778 for (; i < nx; i++)
1779 xfrm_state_put(xfrm[i]);
1780free_dst:
1781 if (dst0)
1782 dst_free(dst0);
1783 dst0 = ERR_PTR(err);
1784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Ying Xueda7c2242014-01-08 10:26:39 +08001787#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001788static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001789{
1790 if (!*target) {
1791 *target = kmalloc(size, GFP_ATOMIC);
1792 if (!*target)
1793 return -ENOMEM;
1794 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001795
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001796 memcpy(*target, src, size);
1797 return 0;
1798}
Ying Xueda7c2242014-01-08 10:26:39 +08001799#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001800
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001801static int xfrm_dst_update_parent(struct dst_entry *dst,
1802 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001803{
1804#ifdef CONFIG_XFRM_SUB_POLICY
1805 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1806 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1807 sel, sizeof(*sel));
1808#else
1809 return 0;
1810#endif
1811}
1812
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001813static int xfrm_dst_update_origin(struct dst_entry *dst,
1814 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001815{
1816#ifdef CONFIG_XFRM_SUB_POLICY
1817 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1818 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1819#else
1820 return 0;
1821#endif
1822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
David S. Miller73ff93c2011-02-22 18:33:42 -08001824static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001825 struct xfrm_policy **pols,
1826 int *num_pols, int *num_xfrms)
1827{
1828 int i;
1829
1830 if (*num_pols == 0 || !pols[0]) {
1831 *num_pols = 0;
1832 *num_xfrms = 0;
1833 return 0;
1834 }
1835 if (IS_ERR(pols[0]))
1836 return PTR_ERR(pols[0]);
1837
1838 *num_xfrms = pols[0]->xfrm_nr;
1839
1840#ifdef CONFIG_XFRM_SUB_POLICY
1841 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1842 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1843 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1844 XFRM_POLICY_TYPE_MAIN,
1845 fl, family,
1846 XFRM_POLICY_OUT);
1847 if (pols[1]) {
1848 if (IS_ERR(pols[1])) {
1849 xfrm_pols_put(pols, *num_pols);
1850 return PTR_ERR(pols[1]);
1851 }
Weilong Chen02d08922013-12-24 09:43:48 +08001852 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001853 (*num_xfrms) += pols[1]->xfrm_nr;
1854 }
1855 }
1856#endif
1857 for (i = 0; i < *num_pols; i++) {
1858 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1859 *num_xfrms = -1;
1860 break;
1861 }
1862 }
1863
1864 return 0;
1865
1866}
1867
1868static struct xfrm_dst *
1869xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001870 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001871 struct dst_entry *dst_orig)
1872{
1873 struct net *net = xp_net(pols[0]);
1874 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1875 struct dst_entry *dst;
1876 struct xfrm_dst *xdst;
1877 int err;
1878
1879 /* Try to instantiate a bundle */
1880 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001881 if (err <= 0) {
1882 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001883 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1884 return ERR_PTR(err);
1885 }
1886
1887 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1888 if (IS_ERR(dst)) {
1889 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1890 return ERR_CAST(dst);
1891 }
1892
1893 xdst = (struct xfrm_dst *)dst;
1894 xdst->num_xfrms = err;
1895 if (num_pols > 1)
1896 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1897 else
1898 err = xfrm_dst_update_origin(dst, fl);
1899 if (unlikely(err)) {
1900 dst_free(dst);
1901 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1902 return ERR_PTR(err);
1903 }
1904
1905 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001906 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001907 xdst->policy_genid = atomic_read(&pols[0]->genid);
1908
1909 return xdst;
1910}
1911
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001912static void xfrm_policy_queue_process(unsigned long arg)
1913{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001914 struct sk_buff *skb;
1915 struct sock *sk;
1916 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001917 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001918 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001919 struct xfrm_policy_queue *pq = &pol->polq;
1920 struct flowi fl;
1921 struct sk_buff_head list;
1922
1923 spin_lock(&pq->hold_queue.lock);
1924 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001925 if (!skb) {
1926 spin_unlock(&pq->hold_queue.lock);
1927 goto out;
1928 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001929 dst = skb_dst(skb);
1930 sk = skb->sk;
1931 xfrm_decode_session(skb, &fl, dst->ops->family);
1932 spin_unlock(&pq->hold_queue.lock);
1933
1934 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001935 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001936 if (IS_ERR(dst))
1937 goto purge_queue;
1938
1939 if (dst->flags & DST_XFRM_QUEUE) {
1940 dst_release(dst);
1941
1942 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1943 goto purge_queue;
1944
1945 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001946 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1947 xfrm_pol_hold(pol);
1948 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001949 }
1950
1951 dst_release(dst);
1952
1953 __skb_queue_head_init(&list);
1954
1955 spin_lock(&pq->hold_queue.lock);
1956 pq->timeout = 0;
1957 skb_queue_splice_init(&pq->hold_queue, &list);
1958 spin_unlock(&pq->hold_queue.lock);
1959
1960 while (!skb_queue_empty(&list)) {
1961 skb = __skb_dequeue(&list);
1962
1963 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1964 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001965 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001966 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001967 kfree_skb(skb);
1968 continue;
1969 }
1970
1971 nf_reset(skb);
1972 skb_dst_drop(skb);
1973 skb_dst_set(skb, dst);
1974
Eric W. Biederman13206b62015-10-07 16:48:35 -05001975 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001976 }
1977
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001978out:
1979 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001980 return;
1981
1982purge_queue:
1983 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001984 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001985 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001986}
1987
Eric W. Biedermanede20592015-10-07 16:48:47 -05001988static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001989{
1990 unsigned long sched_next;
1991 struct dst_entry *dst = skb_dst(skb);
1992 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001993 struct xfrm_policy *pol = xdst->pols[0];
1994 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001995
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001996 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001997 kfree_skb(skb);
1998 return 0;
1999 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002000
2001 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2002 kfree_skb(skb);
2003 return -EAGAIN;
2004 }
2005
2006 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002007
2008 spin_lock_bh(&pq->hold_queue.lock);
2009
2010 if (!pq->timeout)
2011 pq->timeout = XFRM_QUEUE_TMO_MIN;
2012
2013 sched_next = jiffies + pq->timeout;
2014
2015 if (del_timer(&pq->hold_timer)) {
2016 if (time_before(pq->hold_timer.expires, sched_next))
2017 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002018 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002019 }
2020
2021 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002022 if (!mod_timer(&pq->hold_timer, sched_next))
2023 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002024
2025 spin_unlock_bh(&pq->hold_queue.lock);
2026
2027 return 0;
2028}
2029
2030static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002031 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002032 const struct flowi *fl,
2033 int num_xfrms,
2034 u16 family)
2035{
2036 int err;
2037 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002038 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002039 struct dst_entry *dst1;
2040 struct xfrm_dst *xdst;
2041
2042 xdst = xfrm_alloc_dst(net, family);
2043 if (IS_ERR(xdst))
2044 return xdst;
2045
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002046 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2047 net->xfrm.sysctl_larval_drop ||
2048 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002049 return xdst;
2050
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002051 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002052 dst1 = &xdst->u.dst;
2053 dst_hold(dst);
2054 xdst->route = dst;
2055
2056 dst_copy_metrics(dst1, dst);
2057
2058 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2059 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2060 dst1->lastuse = jiffies;
2061
2062 dst1->input = dst_discard;
2063 dst1->output = xdst_queue_output;
2064
2065 dst_hold(dst);
2066 dst1->child = dst;
2067 dst1->path = dst;
2068
2069 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2070
2071 err = -ENODEV;
2072 dev = dst->dev;
2073 if (!dev)
2074 goto free_dst;
2075
2076 err = xfrm_fill_dst(xdst, dev, fl);
2077 if (err)
2078 goto free_dst;
2079
2080out:
2081 return xdst;
2082
2083free_dst:
2084 dst_release(dst1);
2085 xdst = ERR_PTR(err);
2086 goto out;
2087}
2088
Timo Teräs80c802f2010-04-07 00:30:05 +00002089static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002090xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002091 struct flow_cache_object *oldflo, void *ctx)
2092{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002093 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002094 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2095 struct xfrm_dst *xdst, *new_xdst;
2096 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2097
2098 /* Check if the policies from old bundle are usable */
2099 xdst = NULL;
2100 if (oldflo) {
2101 xdst = container_of(oldflo, struct xfrm_dst, flo);
2102 num_pols = xdst->num_pols;
2103 num_xfrms = xdst->num_xfrms;
2104 pol_dead = 0;
2105 for (i = 0; i < num_pols; i++) {
2106 pols[i] = xdst->pols[i];
2107 pol_dead |= pols[i]->walk.dead;
2108 }
2109 if (pol_dead) {
2110 dst_free(&xdst->u.dst);
2111 xdst = NULL;
2112 num_pols = 0;
2113 num_xfrms = 0;
2114 oldflo = NULL;
2115 }
2116 }
2117
2118 /* Resolve policies to use if we couldn't get them from
2119 * previous cache entry */
2120 if (xdst == NULL) {
2121 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002122 pols[0] = __xfrm_policy_lookup(net, fl, family,
2123 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002124 err = xfrm_expand_policies(fl, family, pols,
2125 &num_pols, &num_xfrms);
2126 if (err < 0)
2127 goto inc_error;
2128 if (num_pols == 0)
2129 return NULL;
2130 if (num_xfrms <= 0)
2131 goto make_dummy_bundle;
2132 }
2133
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002134 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2135 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002136 if (IS_ERR(new_xdst)) {
2137 err = PTR_ERR(new_xdst);
2138 if (err != -EAGAIN)
2139 goto error;
2140 if (oldflo == NULL)
2141 goto make_dummy_bundle;
2142 dst_hold(&xdst->u.dst);
2143 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002144 } else if (new_xdst == NULL) {
2145 num_xfrms = 0;
2146 if (oldflo == NULL)
2147 goto make_dummy_bundle;
2148 xdst->num_xfrms = 0;
2149 dst_hold(&xdst->u.dst);
2150 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002151 }
2152
2153 /* Kill the previous bundle */
2154 if (xdst) {
2155 /* The policies were stolen for newly generated bundle */
2156 xdst->num_pols = 0;
2157 dst_free(&xdst->u.dst);
2158 }
2159
2160 /* Flow cache does not have reference, it dst_free()'s,
2161 * but we do need to return one reference for original caller */
2162 dst_hold(&new_xdst->u.dst);
2163 return &new_xdst->flo;
2164
2165make_dummy_bundle:
2166 /* We found policies, but there's no bundles to instantiate:
2167 * either because the policy blocks, has no transformations or
2168 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002169 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002170 if (IS_ERR(xdst)) {
2171 xfrm_pols_put(pols, num_pols);
2172 return ERR_CAST(xdst);
2173 }
2174 xdst->num_pols = num_pols;
2175 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002176 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002177
2178 dst_hold(&xdst->u.dst);
2179 return &xdst->flo;
2180
2181inc_error:
2182 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2183error:
2184 if (xdst != NULL)
2185 dst_free(&xdst->u.dst);
2186 else
2187 xfrm_pols_put(pols, num_pols);
2188 return ERR_PTR(err);
2189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
David S. Miller2774c132011-03-01 14:59:04 -08002191static struct dst_entry *make_blackhole(struct net *net, u16 family,
2192 struct dst_entry *dst_orig)
2193{
2194 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2195 struct dst_entry *ret;
2196
2197 if (!afinfo) {
2198 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002199 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002200 } else {
2201 ret = afinfo->blackhole_route(net, dst_orig);
2202 }
2203 xfrm_policy_put_afinfo(afinfo);
2204
2205 return ret;
2206}
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208/* Main function: finds/creates a bundle for given flow.
2209 *
2210 * At the moment we eat a raw IP route. Mostly to speed up lookups
2211 * on interfaces with disabled IPsec.
2212 */
David S. Miller452edd52011-03-02 13:27:41 -08002213struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2214 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002215 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002217 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002218 struct flow_cache_object *flo;
2219 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002220 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002221 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002222 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002223 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002224
Timo Teräs80c802f2010-04-07 00:30:05 +00002225 dst = NULL;
2226 xdst = NULL;
2227 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002228
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002229 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002230 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002231 num_pols = 1;
2232 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2233 err = xfrm_expand_policies(fl, family, pols,
2234 &num_pols, &num_xfrms);
2235 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002236 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002237
2238 if (num_pols) {
2239 if (num_xfrms <= 0) {
2240 drop_pols = num_pols;
2241 goto no_transform;
2242 }
2243
2244 xdst = xfrm_resolve_and_create_bundle(
2245 pols, num_pols, fl,
2246 family, dst_orig);
2247 if (IS_ERR(xdst)) {
2248 xfrm_pols_put(pols, num_pols);
2249 err = PTR_ERR(xdst);
2250 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002251 } else if (xdst == NULL) {
2252 num_xfrms = 0;
2253 drop_pols = num_pols;
2254 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002255 }
2256
Steffen Klassertb7eea452014-06-18 12:34:21 +02002257 dst_hold(&xdst->u.dst);
2258 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002259 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002260 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Timo Teräs80c802f2010-04-07 00:30:05 +00002263 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002264 struct xfrm_flo xflo;
2265
2266 xflo.dst_orig = dst_orig;
2267 xflo.flags = flags;
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002270 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002271 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002272 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Timo Teräs80c802f2010-04-07 00:30:05 +00002274 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002275 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002276 if (flo == NULL)
2277 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002278 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002279 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002280 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002281 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002282 xdst = container_of(flo, struct xfrm_dst, flo);
2283
2284 num_pols = xdst->num_pols;
2285 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002286 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002287 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289
Timo Teräs80c802f2010-04-07 00:30:05 +00002290 dst = &xdst->u.dst;
2291 if (route == NULL && num_xfrms > 0) {
2292 /* The only case when xfrm_bundle_lookup() returns a
2293 * bundle with null route, is when the template could
2294 * not be resolved. It means policies are there, but
2295 * bundle could not be created, since we don't yet
2296 * have the xfrm_state's. We need to wait for KM to
2297 * negotiate new SA's or bail out with error.*/
2298 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002299 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002300 err = -EREMOTE;
2301 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002302 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002303
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002304 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002305
2306 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2307 goto error;
2308 }
2309
2310no_transform:
2311 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002312 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Timo Teräs80c802f2010-04-07 00:30:05 +00002314 if ((flags & XFRM_LOOKUP_ICMP) &&
2315 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2316 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002317 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002318 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002319
Timo Teräs80c802f2010-04-07 00:30:05 +00002320 for (i = 0; i < num_pols; i++)
2321 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002322
Timo Teräs80c802f2010-04-07 00:30:05 +00002323 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002325 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002326 err = -EPERM;
2327 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002328 } else if (num_xfrms > 0) {
2329 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002330 dst_release(dst_orig);
2331 } else {
2332 /* Flow passes untransformed */
2333 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002334 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002336ok:
2337 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002338 if (dst && dst->xfrm &&
2339 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2340 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002341 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Timo Teräs80c802f2010-04-07 00:30:05 +00002343nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002344 if (!(flags & XFRM_LOOKUP_ICMP)) {
2345 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002347 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002348 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002350 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002351dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002352 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2353 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002354 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002355 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357EXPORT_SYMBOL(xfrm_lookup);
2358
Steffen Klassertf92ee612014-09-16 10:08:40 +02002359/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2360 * Otherwise we may send out blackholed packets.
2361 */
2362struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2363 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002364 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002365{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002366 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002367 flags | XFRM_LOOKUP_QUEUE |
2368 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002369
2370 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2371 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2372
2373 return dst;
2374}
2375EXPORT_SYMBOL(xfrm_lookup_route);
2376
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002377static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002378xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002379{
2380 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002381
2382 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2383 return 0;
2384 x = skb->sp->xvec[idx];
2385 if (!x->type->reject)
2386 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002387 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002388}
2389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390/* When skb is transformed back to its "native" form, we have to
2391 * check policy restrictions. At the moment we make this in maximally
2392 * stupid way. Shame on me. :-) Of course, connected sockets must
2393 * have policy cached at them.
2394 */
2395
2396static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002397xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 unsigned short family)
2399{
2400 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002401 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return x->id.proto == tmpl->id.proto &&
2403 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2404 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2405 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002406 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002407 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002408 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2409 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002412/*
2413 * 0 or more than 0 is returned when validation is succeeded (either bypass
2414 * because of optional transport mode, or next index of the mathced secpath
2415 * state with the template.
2416 * -1 is returned when no matching template is found.
2417 * Otherwise "-2 - errored_index" is returned.
2418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002420xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 unsigned short family)
2422{
2423 int idx = start;
2424
2425 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002426 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return start;
2428 } else
2429 start = -1;
2430 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002431 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002433 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2434 if (start == -1)
2435 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 return start;
2440}
2441
Herbert Xud5422ef2007-12-12 10:44:16 -08002442int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2443 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002446 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 if (unlikely(afinfo == NULL))
2449 return -EAFNOSUPPORT;
2450
Herbert Xud5422ef2007-12-12 10:44:16 -08002451 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002452 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
Herbert Xud5422ef2007-12-12 10:44:16 -08002456EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
David S. Miller9a7386e2011-02-24 01:44:12 -05002458static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002461 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002462 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
2466
2467 return 0;
2468}
2469
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002470int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 unsigned short family)
2472{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002473 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002475 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2476 int npols = 0;
2477 int xfrm_nr;
2478 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002479 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002481 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002482 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Herbert Xud5422ef2007-12-12 10:44:16 -08002484 reverse = dir & ~XFRM_POLICY_MASK;
2485 dir &= XFRM_POLICY_MASK;
2486 fl_dir = policy_to_flow_dir(dir);
2487
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002488 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002489 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002491 }
2492
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002493 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /* First, check used SA against their selectors. */
2496 if (skb->sp) {
2497 int i;
2498
Weilong Chen9b7a7872013-12-24 09:43:46 +08002499 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002500 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002501 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002502 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506 }
2507
2508 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002509 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002510 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002511 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002512 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002513 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002514 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002515 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002518 if (!pol) {
2519 struct flow_cache_object *flo;
2520
2521 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2522 xfrm_policy_lookup, NULL);
2523 if (IS_ERR_OR_NULL(flo))
2524 pol = ERR_CAST(flo);
2525 else
2526 pol = container_of(flo, struct xfrm_policy, flo);
2527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002529 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002530 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002531 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002532 }
James Morris134b0fc2006-10-05 15:42:27 -05002533
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002534 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002535 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002536 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002537 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002538 return 0;
2539 }
2540 return 1;
2541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
James Morris9d729f72007-03-04 16:12:44 -08002543 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002545 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002546 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002547#ifdef CONFIG_XFRM_SUB_POLICY
2548 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002549 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002550 &fl, family,
2551 XFRM_POLICY_IN);
2552 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002553 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002554 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002555 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002556 }
James Morris9d729f72007-03-04 16:12:44 -08002557 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002558 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002559 }
2560 }
2561#endif
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (pol->action == XFRM_POLICY_ALLOW) {
2564 struct sec_path *sp;
2565 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002566 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002567 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002568 struct xfrm_tmpl **tpp = tp;
2569 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 int i, k;
2571
2572 if ((sp = skb->sp) == NULL)
2573 sp = &dummy;
2574
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002575 for (pi = 0; pi < npols; pi++) {
2576 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002577 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002578 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002579 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002580 }
2581 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002582 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002583 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002584 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002585 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2586 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2587 }
2588 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002589 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002590 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002591 tpp = stp;
2592 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /* For each tunnel xfrm, find the first matching tmpl.
2595 * For each tmpl before that, find corresponding xfrm.
2596 * Order is _important_. Later we will implement
2597 * some barriers, but at the moment barriers
2598 * are implied between each two transformations.
2599 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002600 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2601 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002602 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002603 if (k < -1)
2604 /* "-2 - errored_index" returned */
2605 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002606 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002611 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002612 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002616 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return 1;
2618 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002619 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002622 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002623reject_error:
2624 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return 0;
2626}
2627EXPORT_SYMBOL(__xfrm_policy_check);
2628
2629int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2630{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002631 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002633 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002634 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002636 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002637 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002641 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002642
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002643 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002644 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002645 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002646 dst = NULL;
2647 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002648 skb_dst_set(skb, dst);
2649 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651EXPORT_SYMBOL(__xfrm_route_forward);
2652
David S. Millerd49c73c2006-08-13 18:55:53 -07002653/* Optimize later using cookies and generation ids. */
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2656{
David S. Millerd49c73c2006-08-13 18:55:53 -07002657 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002658 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2659 * get validated by dst_ops->check on every use. We do this
2660 * because when a normal route referenced by an XFRM dst is
2661 * obsoleted we do not go looking around for all parent
2662 * referencing XFRM dsts so that we can invalidate them. It
2663 * is just too much work. Instead we make the checks here on
2664 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002665 *
2666 * XFRM dst A --> IPv4 dst X
2667 *
2668 * X is the "xdst->route" of A (X is also the "dst->path" of A
2669 * in this example). If X is marked obsolete, "A" will not
2670 * notice. That's what we are validating here via the
2671 * stale_bundle() check.
2672 *
2673 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002674 * dst which causes it's ->obsolete field to be set to
2675 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2676 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002677 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002678 if (dst->obsolete < 0 && !stale_bundle(dst))
2679 return dst;
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return NULL;
2682}
2683
2684static int stale_bundle(struct dst_entry *dst)
2685{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002686 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Herbert Xuaabc9762005-05-03 16:27:10 -07002689void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002692 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002693 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 dev_put(dev);
2695 }
2696}
Herbert Xuaabc9762005-05-03 16:27:10 -07002697EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699static void xfrm_link_failure(struct sk_buff *skb)
2700{
2701 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
2704static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2705{
2706 if (dst) {
2707 if (dst->obsolete) {
2708 dst_release(dst);
2709 dst = NULL;
2710 }
2711 }
2712 return dst;
2713}
2714
Paul Mooree4c17212013-05-29 07:36:25 +00002715void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002716{
Fan Duca925cf2014-01-18 09:55:27 +08002717 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002718}
Paul Mooree4c17212013-05-29 07:36:25 +00002719EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002720
2721static void xfrm_garbage_collect_deferred(struct net *net)
2722{
Fan Duca925cf2014-01-18 09:55:27 +08002723 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002724}
2725
Herbert Xu25ee3282007-12-11 09:32:34 -08002726static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 do {
2729 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2730 u32 pmtu, route_mtu_cached;
2731
2732 pmtu = dst_mtu(dst->child);
2733 xdst->child_mtu_cached = pmtu;
2734
2735 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2736
2737 route_mtu_cached = dst_mtu(xdst->route);
2738 xdst->route_mtu_cached = route_mtu_cached;
2739
2740 if (pmtu > route_mtu_cached)
2741 pmtu = route_mtu_cached;
2742
David S. Millerdefb3512010-12-08 21:16:57 -08002743 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 } while ((dst = dst->next));
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747/* Check that the bundle accepts the flow and its components are
2748 * still valid.
2749 */
2750
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002751static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 struct dst_entry *dst = &first->u.dst;
2754 struct xfrm_dst *last;
2755 u32 mtu;
2756
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002757 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 (dst->dev && !netif_running(dst->dev)))
2759 return 0;
2760
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002761 if (dst->flags & DST_XFRM_QUEUE)
2762 return 1;
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 last = NULL;
2765
2766 do {
2767 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2770 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002771 if (xdst->xfrm_genid != dst->xfrm->genid)
2772 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002773 if (xdst->num_pols > 0 &&
2774 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 mtu = dst_mtu(dst->child);
2778 if (xdst->child_mtu_cached != mtu) {
2779 last = xdst;
2780 xdst->child_mtu_cached = mtu;
2781 }
2782
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002783 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785 mtu = dst_mtu(xdst->route);
2786 if (xdst->route_mtu_cached != mtu) {
2787 last = xdst;
2788 xdst->route_mtu_cached = mtu;
2789 }
2790
2791 dst = dst->child;
2792 } while (dst->xfrm);
2793
2794 if (likely(!last))
2795 return 1;
2796
2797 mtu = last->child_mtu_cached;
2798 for (;;) {
2799 dst = &last->u.dst;
2800
2801 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2802 if (mtu > last->route_mtu_cached)
2803 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002804 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 if (last == first)
2807 break;
2808
Patrick McHardybd0bf072007-07-18 01:55:52 -07002809 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 last->child_mtu_cached = mtu;
2811 }
2812
2813 return 1;
2814}
2815
David S. Miller0dbaee32010-12-13 12:52:14 -08002816static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2817{
2818 return dst_metric_advmss(dst->path);
2819}
2820
Steffen Klassertebb762f2011-11-23 02:12:51 +00002821static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002822{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002823 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2824
2825 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002826}
2827
David S. Millerf894cbf2012-07-02 21:52:24 -07002828static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2829 struct sk_buff *skb,
2830 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002831{
David S. Millerf894cbf2012-07-02 21:52:24 -07002832 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002833}
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2836{
2837 int err = 0;
2838 if (unlikely(afinfo == NULL))
2839 return -EINVAL;
2840 if (unlikely(afinfo->family >= NPROTO))
2841 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002842 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002844 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 else {
2846 struct dst_ops *dst_ops = afinfo->dst_ops;
2847 if (likely(dst_ops->kmem_cachep == NULL))
2848 dst_ops->kmem_cachep = xfrm_dst_cache;
2849 if (likely(dst_ops->check == NULL))
2850 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002851 if (likely(dst_ops->default_advmss == NULL))
2852 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002853 if (likely(dst_ops->mtu == NULL))
2854 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (likely(dst_ops->negative_advice == NULL))
2856 dst_ops->negative_advice = xfrm_negative_advice;
2857 if (likely(dst_ops->link_failure == NULL))
2858 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002859 if (likely(dst_ops->neigh_lookup == NULL))
2860 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002862 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002863 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002865 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return err;
2868}
2869EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2870
2871int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2872{
2873 int err = 0;
2874 if (unlikely(afinfo == NULL))
2875 return -EINVAL;
2876 if (unlikely(afinfo->family >= NPROTO))
2877 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002878 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2880 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2881 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002882 else
2883 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2884 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002886 spin_unlock(&xfrm_policy_afinfo_lock);
2887 if (!err) {
2888 struct dst_ops *dst_ops = afinfo->dst_ops;
2889
2890 synchronize_rcu();
2891
2892 dst_ops->kmem_cachep = NULL;
2893 dst_ops->check = NULL;
2894 dst_ops->negative_advice = NULL;
2895 dst_ops->link_failure = NULL;
2896 afinfo->garbage_collect = NULL;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return err;
2899}
2900EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2903{
Jiri Pirko351638e2013-05-28 01:30:21 +00002904 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 switch (event) {
2907 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002908 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910 return NOTIFY_DONE;
2911}
2912
2913static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002914 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915};
2916
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002917#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002918static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002919{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002920 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002921 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2922 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002923 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002924 rv = xfrm_proc_init(net);
2925 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002926 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002927 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002928}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002929
2930static void xfrm_statistics_fini(struct net *net)
2931{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002932 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002933 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002934}
2935#else
2936static int __net_init xfrm_statistics_init(struct net *net)
2937{
2938 return 0;
2939}
2940
2941static void xfrm_statistics_fini(struct net *net)
2942{
2943}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002944#endif
2945
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002946static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
David S. Miller2518c7c2006-08-24 04:45:07 -07002948 unsigned int hmask, sz;
2949 int dir;
2950
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002951 if (net_eq(net, &init_net))
2952 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002954 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002955 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
David S. Miller2518c7c2006-08-24 04:45:07 -07002957 hmask = 8 - 1;
2958 sz = (hmask+1) * sizeof(struct hlist_head);
2959
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002960 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2961 if (!net->xfrm.policy_byidx)
2962 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002963 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002964
Herbert Xu53c2e282014-11-13 17:09:49 +08002965 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002966 struct xfrm_policy_hash *htab;
2967
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002968 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002969 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002970 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002971
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002972 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002973 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002974 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002975 goto out_bydst;
2976 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002977 htab->dbits4 = 32;
2978 htab->sbits4 = 32;
2979 htab->dbits6 = 128;
2980 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002981 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002982 net->xfrm.policy_hthresh.lbits4 = 32;
2983 net->xfrm.policy_hthresh.rbits4 = 32;
2984 net->xfrm.policy_hthresh.lbits6 = 128;
2985 net->xfrm.policy_hthresh.rbits6 = 128;
2986
2987 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002988
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002989 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002990 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002991 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002992 if (net_eq(net, &init_net))
2993 register_netdevice_notifier(&xfrm_dev_notifier);
2994 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002995
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002996out_bydst:
2997 for (dir--; dir >= 0; dir--) {
2998 struct xfrm_policy_hash *htab;
2999
3000 htab = &net->xfrm.policy_bydst[dir];
3001 xfrm_hash_free(htab->table, sz);
3002 }
3003 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003004out_byidx:
3005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003008static void xfrm_policy_fini(struct net *net)
3009{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003010 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003011 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003012
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003013 flush_work(&net->xfrm.policy_hash_work);
3014#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003015 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003016#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003017 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003018
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003019 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003020
Herbert Xu53c2e282014-11-13 17:09:49 +08003021 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003022 struct xfrm_policy_hash *htab;
3023
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003024 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003025
3026 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003027 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003028 WARN_ON(!hlist_empty(htab->table));
3029 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003030 }
3031
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003032 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003033 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3034 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003035}
3036
3037static int __net_init xfrm_net_init(struct net *net)
3038{
3039 int rv;
3040
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003041 rv = xfrm_statistics_init(net);
3042 if (rv < 0)
3043 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003044 rv = xfrm_state_init(net);
3045 if (rv < 0)
3046 goto out_state;
3047 rv = xfrm_policy_init(net);
3048 if (rv < 0)
3049 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003050 rv = xfrm_sysctl_init(net);
3051 if (rv < 0)
3052 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003053 rv = flow_cache_init(net);
3054 if (rv < 0)
3055 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003056
3057 /* Initialize the per-net locks here */
3058 spin_lock_init(&net->xfrm.xfrm_state_lock);
3059 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003060 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3061
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003062 return 0;
3063
Steffen Klassert4a93f502014-03-12 09:43:17 +01003064out:
3065 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003066out_sysctl:
3067 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003068out_policy:
3069 xfrm_state_fini(net);
3070out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003071 xfrm_statistics_fini(net);
3072out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003073 return rv;
3074}
3075
3076static void __net_exit xfrm_net_exit(struct net *net)
3077{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003078 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003079 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003080 xfrm_policy_fini(net);
3081 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003082 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003083}
3084
3085static struct pernet_operations __net_initdata xfrm_net_ops = {
3086 .init = xfrm_net_init,
3087 .exit = xfrm_net_exit,
3088};
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090void __init xfrm_init(void)
3091{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003092 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 xfrm_input_init();
3094}
3095
Joy Lattenab5f5e82007-09-17 11:51:22 -07003096#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003097static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3098 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003099{
Paul Moore875179f2007-12-01 23:27:18 +11003100 struct xfrm_sec_ctx *ctx = xp->security;
3101 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003102
Paul Moore875179f2007-12-01 23:27:18 +11003103 if (ctx)
3104 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3105 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3106
Weilong Chen9b7a7872013-12-24 09:43:46 +08003107 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003108 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003109 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003110 if (sel->prefixlen_s != 32)
3111 audit_log_format(audit_buf, " src_prefixlen=%d",
3112 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003113 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003114 if (sel->prefixlen_d != 32)
3115 audit_log_format(audit_buf, " dst_prefixlen=%d",
3116 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003117 break;
3118 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003119 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003120 if (sel->prefixlen_s != 128)
3121 audit_log_format(audit_buf, " src_prefixlen=%d",
3122 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003123 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003124 if (sel->prefixlen_d != 128)
3125 audit_log_format(audit_buf, " dst_prefixlen=%d",
3126 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003127 break;
3128 }
3129}
3130
Tetsuo Handa2e710292014-04-22 21:48:30 +09003131void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003132{
3133 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003134
Paul Mooreafeb14b2007-12-21 14:58:11 -08003135 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003136 if (audit_buf == NULL)
3137 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003138 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003139 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003140 xfrm_audit_common_policyinfo(xp, audit_buf);
3141 audit_log_end(audit_buf);
3142}
3143EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3144
Paul Moore68277ac2007-12-20 20:49:33 -08003145void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003146 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003147{
3148 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003149
Paul Mooreafeb14b2007-12-21 14:58:11 -08003150 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151 if (audit_buf == NULL)
3152 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003153 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003154 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003155 xfrm_audit_common_policyinfo(xp, audit_buf);
3156 audit_log_end(audit_buf);
3157}
3158EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3159#endif
3160
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003161#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003162static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3163 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003164{
3165 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3166 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003167 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3168 sel_cmp->family) &&
3169 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3170 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003171 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3172 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003173 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003174 }
3175 } else {
3176 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003177 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178 }
3179 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003180 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003181}
3182
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003183static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3184 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003185{
3186 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003187 struct hlist_head *chain;
3188 u32 priority = ~0U;
3189
Fan Du283bc9f2013-11-07 17:47:50 +08003190 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003191 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003192 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003193 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3194 pol->type == type) {
3195 ret = pol;
3196 priority = ret->priority;
3197 break;
3198 }
3199 }
Fan Du8d549c42013-11-07 17:47:49 +08003200 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003201 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003202 if ((pol->priority >= priority) && ret)
3203 break;
3204
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003205 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003206 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003207 ret = pol;
3208 break;
3209 }
3210 }
3211
Li RongQing586f2eb2015-04-30 17:13:41 +08003212 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003213
Fan Du283bc9f2013-11-07 17:47:50 +08003214 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003215
3216 return ret;
3217}
3218
David S. Millerdd701752011-02-24 00:21:08 -05003219static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003220{
3221 int match = 0;
3222
3223 if (t->mode == m->mode && t->id.proto == m->proto &&
3224 (m->reqid == 0 || t->reqid == m->reqid)) {
3225 switch (t->mode) {
3226 case XFRM_MODE_TUNNEL:
3227 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003228 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3229 m->old_family) &&
3230 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3231 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232 match = 1;
3233 }
3234 break;
3235 case XFRM_MODE_TRANSPORT:
3236 /* in case of transport mode, template does not store
3237 any IP addresses, hence we just compare mode and
3238 protocol */
3239 match = 1;
3240 break;
3241 default:
3242 break;
3243 }
3244 }
3245 return match;
3246}
3247
3248/* update endpoint address(es) of template(s) */
3249static int xfrm_policy_migrate(struct xfrm_policy *pol,
3250 struct xfrm_migrate *m, int num_migrate)
3251{
3252 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003253 int i, j, n = 0;
3254
3255 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003256 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003257 /* target policy has been deleted */
3258 write_unlock_bh(&pol->lock);
3259 return -ENOENT;
3260 }
3261
3262 for (i = 0; i < pol->xfrm_nr; i++) {
3263 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3264 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3265 continue;
3266 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003267 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3268 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003269 continue;
3270 /* update endpoints */
3271 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3272 sizeof(pol->xfrm_vec[i].id.daddr));
3273 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3274 sizeof(pol->xfrm_vec[i].saddr));
3275 pol->xfrm_vec[i].encap_family = mp->new_family;
3276 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003277 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003278 }
3279 }
3280
3281 write_unlock_bh(&pol->lock);
3282
3283 if (!n)
3284 return -ENODATA;
3285
3286 return 0;
3287}
3288
David S. Millerdd701752011-02-24 00:21:08 -05003289static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003290{
3291 int i, j;
3292
3293 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3294 return -EINVAL;
3295
3296 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003297 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3298 m[i].old_family) &&
3299 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3300 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003301 return -EINVAL;
3302 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3303 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3304 return -EINVAL;
3305
3306 /* check if there is any duplicated entry */
3307 for (j = i + 1; j < num_migrate; j++) {
3308 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3309 sizeof(m[i].old_daddr)) &&
3310 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3311 sizeof(m[i].old_saddr)) &&
3312 m[i].proto == m[j].proto &&
3313 m[i].mode == m[j].mode &&
3314 m[i].reqid == m[j].reqid &&
3315 m[i].old_family == m[j].old_family)
3316 return -EINVAL;
3317 }
3318 }
3319
3320 return 0;
3321}
3322
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003323int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003324 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003325 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003326{
3327 int i, err, nx_cur = 0, nx_new = 0;
3328 struct xfrm_policy *pol = NULL;
3329 struct xfrm_state *x, *xc;
3330 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3331 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3332 struct xfrm_migrate *mp;
3333
3334 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3335 goto out;
3336
3337 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003338 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003339 err = -ENOENT;
3340 goto out;
3341 }
3342
3343 /* Stage 2 - find and update state(s) */
3344 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003345 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003346 x_cur[nx_cur] = x;
3347 nx_cur++;
3348 if ((xc = xfrm_state_migrate(x, mp))) {
3349 x_new[nx_new] = xc;
3350 nx_new++;
3351 } else {
3352 err = -ENODATA;
3353 goto restore_state;
3354 }
3355 }
3356 }
3357
3358 /* Stage 3 - update policy */
3359 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3360 goto restore_state;
3361
3362 /* Stage 4 - delete old state(s) */
3363 if (nx_cur) {
3364 xfrm_states_put(x_cur, nx_cur);
3365 xfrm_states_delete(x_cur, nx_cur);
3366 }
3367
3368 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003369 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003370
3371 xfrm_pol_put(pol);
3372
3373 return 0;
3374out:
3375 return err;
3376
3377restore_state:
3378 if (pol)
3379 xfrm_pol_put(pol);
3380 if (nx_cur)
3381 xfrm_states_put(x_cur, nx_cur);
3382 if (nx_new)
3383 xfrm_states_delete(x_new, nx_new);
3384
3385 return err;
3386}
David S. Millere610e672007-02-08 13:29:15 -08003387EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003388#endif