blob: 45f9cf97ea25d98bea83a7dd48365357fb8f4bee [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
388 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800389 &net->xfrm.policy_inexact[dir] :
390 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700391}
392
David S. Miller5f803b52011-02-24 00:33:19 -0500393static struct hlist_head *policy_hash_direct(struct net *net,
394 const xfrm_address_t *daddr,
395 const xfrm_address_t *saddr,
396 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700397{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800398 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200399 unsigned int hash;
400 u8 dbits;
401 u8 sbits;
402
403 __get_hash_thresh(net, family, dir, &dbits, &sbits);
404 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700405
Alexey Dobriyan11219942008-11-25 17:33:06 -0800406 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407}
408
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200409static void xfrm_dst_hash_transfer(struct net *net,
410 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700411 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200412 unsigned int nhashmask,
413 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700414{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800417 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418 u8 dbits;
419 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700420
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800422 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700423 unsigned int h;
424
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200425 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700426 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200427 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800428 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800429 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430 hlist_add_head(&pol->bydst, ndsttable+h);
431 h0 = h;
432 } else {
433 if (h != h0)
434 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700436 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800438 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800439 }
440 if (!hlist_empty(list)) {
441 entry0 = NULL;
442 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700443 }
444}
445
446static void xfrm_idx_hash_transfer(struct hlist_head *list,
447 struct hlist_head *nidxtable,
448 unsigned int nhashmask)
449{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800450 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700451 struct xfrm_policy *pol;
452
Sasha Levinb67bfe02013-02-27 17:06:00 -0800453 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700454 unsigned int h;
455
456 h = __idx_hash(pol->index, nhashmask);
457 hlist_add_head(&pol->byidx, nidxtable+h);
458 }
459}
460
461static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
462{
463 return ((old_hmask + 1) << 1) - 1;
464}
465
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800466static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700467{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800468 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700469 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
470 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800471 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700472 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700473 int i;
474
475 if (!ndst)
476 return;
477
Fan Du283bc9f2013-11-07 17:47:50 +0800478 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700479
480 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200481 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700482
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800483 net->xfrm.policy_bydst[dir].table = ndst;
484 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700485
Fan Du283bc9f2013-11-07 17:47:50 +0800486 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700487
David S. Miller44e36b42006-08-24 04:50:50 -0700488 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700489}
490
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800491static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700492{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800493 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700494 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
495 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800496 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700497 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498 int i;
499
500 if (!nidx)
501 return;
502
Fan Du283bc9f2013-11-07 17:47:50 +0800503 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504
505 for (i = hmask; i >= 0; i--)
506 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
507
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800508 net->xfrm.policy_byidx = nidx;
509 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700510
Fan Du283bc9f2013-11-07 17:47:50 +0800511 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
David S. Miller44e36b42006-08-24 04:50:50 -0700513 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700514}
515
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700517{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800518 unsigned int cnt = net->xfrm.policy_count[dir];
519 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700520
521 if (total)
522 *total += cnt;
523
524 if ((hmask + 1) < xfrm_policy_hashmax &&
525 cnt > hmask)
526 return 1;
527
528 return 0;
529}
530
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800531static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700532{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800533 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700534
535 if ((hmask + 1) < xfrm_policy_hashmax &&
536 total > hmask)
537 return 1;
538
539 return 0;
540}
541
Alexey Dobriyane0710412010-01-23 13:37:10 +0000542void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700543{
Fan Du283bc9f2013-11-07 17:47:50 +0800544 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000545 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
546 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
547 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
548 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
549 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
550 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
551 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700552 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800553 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700554}
555EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700556
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700557static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800558static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700559{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800560 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700561 int dir, total;
562
563 mutex_lock(&hash_resize_mutex);
564
565 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800566 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800567 if (xfrm_bydst_should_resize(net, dir, &total))
568 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700569 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800570 if (xfrm_byidx_should_resize(net, total))
571 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700572
573 mutex_unlock(&hash_resize_mutex);
574}
575
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200576static void xfrm_hash_rebuild(struct work_struct *work)
577{
578 struct net *net = container_of(work, struct net,
579 xfrm.policy_hthresh.work);
580 unsigned int hmask;
581 struct xfrm_policy *pol;
582 struct xfrm_policy *policy;
583 struct hlist_head *chain;
584 struct hlist_head *odst;
585 struct hlist_node *newpos;
586 int i;
587 int dir;
588 unsigned seq;
589 u8 lbits4, rbits4, lbits6, rbits6;
590
591 mutex_lock(&hash_resize_mutex);
592
593 /* read selector prefixlen thresholds */
594 do {
595 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
596
597 lbits4 = net->xfrm.policy_hthresh.lbits4;
598 rbits4 = net->xfrm.policy_hthresh.rbits4;
599 lbits6 = net->xfrm.policy_hthresh.lbits6;
600 rbits6 = net->xfrm.policy_hthresh.rbits6;
601 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
602
603 write_lock_bh(&net->xfrm.xfrm_policy_lock);
604
605 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800606 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200607 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
608 hmask = net->xfrm.policy_bydst[dir].hmask;
609 odst = net->xfrm.policy_bydst[dir].table;
610 for (i = hmask; i >= 0; i--)
611 INIT_HLIST_HEAD(odst + i);
612 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
613 /* dir out => dst = remote, src = local */
614 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
615 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
616 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
617 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
618 } else {
619 /* dir in/fwd => dst = local, src = remote */
620 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
621 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
622 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
623 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
624 }
625 }
626
627 /* re-insert all policies by order of creation */
628 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
Tobias Brunner6916fb32016-07-29 09:57:32 +0200629 if (xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) {
630 /* skip socket policies */
631 continue;
632 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200633 newpos = NULL;
634 chain = policy_hash_bysel(net, &policy->selector,
635 policy->family,
636 xfrm_policy_id2dir(policy->index));
637 hlist_for_each_entry(pol, chain, bydst) {
638 if (policy->priority >= pol->priority)
639 newpos = &pol->bydst;
640 else
641 break;
642 }
643 if (newpos)
644 hlist_add_behind(&policy->bydst, newpos);
645 else
646 hlist_add_head(&policy->bydst, chain);
647 }
648
649 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
650
651 mutex_unlock(&hash_resize_mutex);
652}
653
654void xfrm_policy_hash_rebuild(struct net *net)
655{
656 schedule_work(&net->xfrm.policy_hthresh.work);
657}
658EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/* Generate new index... KAME seems to generate them ordered by cost
661 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800662static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 static u32 idx_generator;
665
666 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700667 struct hlist_head *list;
668 struct xfrm_policy *p;
669 u32 idx;
670 int found;
671
Fan Due682adf2013-11-07 17:47:48 +0800672 if (!index) {
673 idx = (idx_generator | dir);
674 idx_generator += 8;
675 } else {
676 idx = index;
677 index = 0;
678 }
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (idx == 0)
681 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800682 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700683 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800684 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700685 if (p->index == idx) {
686 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700690 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return idx;
692 }
693}
694
David S. Miller2518c7c2006-08-24 04:45:07 -0700695static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
696{
697 u32 *p1 = (u32 *) s1;
698 u32 *p2 = (u32 *) s2;
699 int len = sizeof(struct xfrm_selector) / sizeof(u32);
700 int i;
701
702 for (i = 0; i < len; i++) {
703 if (p1[i] != p2[i])
704 return 1;
705 }
706
707 return 0;
708}
709
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100710static void xfrm_policy_requeue(struct xfrm_policy *old,
711 struct xfrm_policy *new)
712{
713 struct xfrm_policy_queue *pq = &old->polq;
714 struct sk_buff_head list;
715
Li RongQingde2ad482015-04-30 17:25:19 +0800716 if (skb_queue_empty(&pq->hold_queue))
717 return;
718
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100719 __skb_queue_head_init(&list);
720
721 spin_lock_bh(&pq->hold_queue.lock);
722 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200723 if (del_timer(&pq->hold_timer))
724 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100725 spin_unlock_bh(&pq->hold_queue.lock);
726
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100727 pq = &new->polq;
728
729 spin_lock_bh(&pq->hold_queue.lock);
730 skb_queue_splice(&list, &pq->hold_queue);
731 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200732 if (!mod_timer(&pq->hold_timer, jiffies))
733 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100734 spin_unlock_bh(&pq->hold_queue.lock);
735}
736
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100737static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
738 struct xfrm_policy *pol)
739{
740 u32 mark = policy->mark.v & policy->mark.m;
741
742 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
743 return true;
744
745 if ((mark & pol->mark.m) == pol->mark.v &&
746 policy->priority == pol->priority)
747 return true;
748
749 return false;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
753{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800754 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700755 struct xfrm_policy *pol;
756 struct xfrm_policy *delpol;
757 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800758 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Fan Du283bc9f2013-11-07 17:47:50 +0800760 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800761 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700762 delpol = NULL;
763 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800764 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800765 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700766 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100767 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800768 xfrm_sec_ctx_match(pol->security, policy->security) &&
769 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800771 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EEXIST;
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 delpol = pol;
775 if (policy->priority > pol->priority)
776 continue;
777 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800778 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 continue;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (delpol)
782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700785 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700786 else
787 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800788 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800789 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800790
791 /* After previous checking, family can either be AF_INET or AF_INET6 */
792 if (policy->family == AF_INET)
793 rt_genid_bump_ipv4(net);
794 else
795 rt_genid_bump_ipv6(net);
796
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100797 if (delpol) {
798 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800799 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100800 }
Fan Due682adf2013-11-07 17:47:48 +0800801 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800802 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800803 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 policy->curlft.use_time = 0;
805 if (!mod_timer(&policy->timer, jiffies + HZ))
806 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800807 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
David S. Miller9b78a822005-12-22 07:39:48 -0800809 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800811 else if (xfrm_bydst_should_resize(net, dir, NULL))
812 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return 0;
815}
816EXPORT_SYMBOL(xfrm_policy_insert);
817
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000818struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
819 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800820 struct xfrm_sec_ctx *ctx, int delete,
821 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
David S. Miller2518c7c2006-08-24 04:45:07 -0700823 struct xfrm_policy *pol, *ret;
824 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Eric Parisef41aaa2007-03-07 15:37:58 -0800826 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800827 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800828 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700829 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800830 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700831 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000832 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 !selector_cmp(sel, &pol->selector) &&
834 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700836 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700837 *err = security_xfrm_policy_delete(
838 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800839 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800840 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800841 return pol;
842 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800843 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700844 }
845 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 }
848 }
Fan Du283bc9f2013-11-07 17:47:50 +0800849 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000851 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700852 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700853 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Trent Jaegerdf718372005-12-13 23:12:27 -0800855EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000857struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
858 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
David S. Miller2518c7c2006-08-24 04:45:07 -0700860 struct xfrm_policy *pol, *ret;
861 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Herbert Xub5505c62007-05-14 02:15:47 -0700863 *err = -ENOENT;
864 if (xfrm_policy_id2dir(id) != dir)
865 return NULL;
866
Eric Parisef41aaa2007-03-07 15:37:58 -0800867 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800868 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800869 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800871 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000872 if (pol->type == type && pol->index == id &&
873 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700875 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700876 *err = security_xfrm_policy_delete(
877 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800878 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800879 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800880 return pol;
881 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800882 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700883 }
884 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
886 }
887 }
Fan Du283bc9f2013-11-07 17:47:50 +0800888 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000890 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700891 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894EXPORT_SYMBOL(xfrm_policy_byid);
895
Joy Latten4aa2e622007-06-04 19:05:57 -0400896#ifdef CONFIG_SECURITY_NETWORK_XFRM
897static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900898xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Joy Latten4aa2e622007-06-04 19:05:57 -0400900 int dir, err = 0;
901
902 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
903 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400904 int i;
905
Sasha Levinb67bfe02013-02-27 17:06:00 -0800906 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800907 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400908 if (pol->type != type)
909 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700910 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400911 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900912 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400913 return err;
914 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900915 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800916 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800917 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800918 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400919 bydst) {
920 if (pol->type != type)
921 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700922 err = security_xfrm_policy_delete(
923 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400924 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700925 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900926 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400927 return err;
928 }
929 }
930 }
931 }
932 return err;
933}
934#else
935static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900936xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400937{
938 return 0;
939}
940#endif
941
Tetsuo Handa2e710292014-04-22 21:48:30 +0900942int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400943{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000944 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Fan Du283bc9f2013-11-07 17:47:50 +0800946 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400947
Tetsuo Handa2e710292014-04-22 21:48:30 +0900948 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400949 if (err)
950 goto out;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700953 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800954 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700955
956 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800957 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800958 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700959 if (pol->type != type)
960 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000961 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800962 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000963 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Tetsuo Handa2e710292014-04-22 21:48:30 +0900965 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600966
David S. Miller2518c7c2006-08-24 04:45:07 -0700967 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Fan Du283bc9f2013-11-07 17:47:50 +0800969 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700970 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700972
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800973 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700974 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800975 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800976 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700977 bydst) {
978 if (pol->type != type)
979 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000980 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800981 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000982 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700983
Tetsuo Handa2e710292014-04-22 21:48:30 +0900984 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700985 xfrm_policy_kill(pol);
986
Fan Du283bc9f2013-11-07 17:47:50 +0800987 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700988 goto again2;
989 }
990 }
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000993 if (!cnt)
994 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400995out:
Fan Du283bc9f2013-11-07 17:47:50 +0800996 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400997 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999EXPORT_SYMBOL(xfrm_policy_flush);
1000
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001001int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001002 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 void *data)
1004{
Herbert Xu12a169e2008-10-01 07:03:24 -07001005 struct xfrm_policy *pol;
1006 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001007 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Timo Teras4c563f72008-02-28 21:31:08 -08001009 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1010 walk->type != XFRM_POLICY_TYPE_ANY)
1011 return -EINVAL;
1012
Herbert Xu12a169e2008-10-01 07:03:24 -07001013 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001014 return 0;
1015
Fan Du283bc9f2013-11-07 17:47:50 +08001016 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001017 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001018 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001019 else
Li RongQing80077702015-04-22 17:09:54 +08001020 x = list_first_entry(&walk->walk.all,
1021 struct xfrm_policy_walk_entry, all);
1022
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001023 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001024 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001025 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001026 pol = container_of(x, struct xfrm_policy, walk);
1027 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1028 walk->type != pol->type)
1029 continue;
1030 error = func(pol, xfrm_policy_id2dir(pol->index),
1031 walk->seq, data);
1032 if (error) {
1033 list_move_tail(&walk->walk.all, &x->all);
1034 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001035 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001036 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001038 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001039 error = -ENOENT;
1040 goto out;
1041 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001042 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043out:
Fan Du283bc9f2013-11-07 17:47:50 +08001044 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return error;
1046}
1047EXPORT_SYMBOL(xfrm_policy_walk);
1048
Herbert Xu12a169e2008-10-01 07:03:24 -07001049void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1050{
1051 INIT_LIST_HEAD(&walk->walk.all);
1052 walk->walk.dead = 1;
1053 walk->type = type;
1054 walk->seq = 0;
1055}
1056EXPORT_SYMBOL(xfrm_policy_walk_init);
1057
Fan Du283bc9f2013-11-07 17:47:50 +08001058void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001059{
1060 if (list_empty(&walk->walk.all))
1061 return;
1062
Fan Du283bc9f2013-11-07 17:47:50 +08001063 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001064 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001065 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001066}
1067EXPORT_SYMBOL(xfrm_policy_walk_done);
1068
James Morris134b0fc2006-10-05 15:42:27 -05001069/*
1070 * Find policy to apply to this flow.
1071 *
1072 * Returns 0 if policy found, else an -errno.
1073 */
David S. Millerf299d552011-02-24 01:23:30 -05001074static int xfrm_policy_match(const struct xfrm_policy *pol,
1075 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001076 u8 type, u16 family, int dir)
1077{
David S. Millerf299d552011-02-24 01:23:30 -05001078 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001079 int ret = -ESRCH;
1080 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001081
1082 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001083 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001084 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001085 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001086
1087 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001088 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001089 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001090 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001091
James Morris134b0fc2006-10-05 15:42:27 -05001092 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001093}
1094
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001095static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001096 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001097 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
James Morris134b0fc2006-10-05 15:42:27 -05001099 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001100 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001101 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001102 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001103 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001104
1105 daddr = xfrm_flowi_daddr(fl, family);
1106 saddr = xfrm_flowi_saddr(fl, family);
1107 if (unlikely(!daddr || !saddr))
1108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Fan Du283bc9f2013-11-07 17:47:50 +08001110 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001111 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001112 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001113 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001114 err = xfrm_policy_match(pol, fl, type, family, dir);
1115 if (err) {
1116 if (err == -ESRCH)
1117 continue;
1118 else {
1119 ret = ERR_PTR(err);
1120 goto fail;
1121 }
1122 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001123 ret = pol;
1124 priority = ret->priority;
1125 break;
1126 }
1127 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001128 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001129 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001130 if ((pol->priority >= priority) && ret)
1131 break;
1132
James Morris134b0fc2006-10-05 15:42:27 -05001133 err = xfrm_policy_match(pol, fl, type, family, dir);
1134 if (err) {
1135 if (err == -ESRCH)
1136 continue;
1137 else {
1138 ret = ERR_PTR(err);
1139 goto fail;
1140 }
Li RongQing8faf4912015-05-14 11:16:59 +08001141 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001142 ret = pol;
1143 break;
1144 }
1145 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001146
1147 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001148fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001149 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001150
David S. Miller2518c7c2006-08-24 04:45:07 -07001151 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001152}
1153
Timo Teräs80c802f2010-04-07 00:30:05 +00001154static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001155__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001156{
1157#ifdef CONFIG_XFRM_SUB_POLICY
1158 struct xfrm_policy *pol;
1159
1160 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1161 if (pol != NULL)
1162 return pol;
1163#endif
1164 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1165}
1166
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001167static int flow_to_policy_dir(int dir)
1168{
1169 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1170 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1171 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1172 return dir;
1173
1174 switch (dir) {
1175 default:
1176 case FLOW_DIR_IN:
1177 return XFRM_POLICY_IN;
1178 case FLOW_DIR_OUT:
1179 return XFRM_POLICY_OUT;
1180 case FLOW_DIR_FWD:
1181 return XFRM_POLICY_FWD;
1182 }
1183}
1184
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001185static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001186xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001187 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001188{
1189 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001190
1191 if (old_obj)
1192 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001193
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001194 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001195 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001196 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001197
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001198 /* Resolver returns two references:
1199 * one for cache and one for caller of flow_cache_lookup() */
1200 xfrm_pol_hold(pol);
1201
1202 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Trent Jaegerdf718372005-12-13 23:12:27 -08001205static inline int policy_to_flow_dir(int dir)
1206{
1207 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001208 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1209 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1210 return dir;
1211 switch (dir) {
1212 default:
1213 case XFRM_POLICY_IN:
1214 return FLOW_DIR_IN;
1215 case XFRM_POLICY_OUT:
1216 return FLOW_DIR_OUT;
1217 case XFRM_POLICY_FWD:
1218 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001219 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001220}
1221
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001222static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001223 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001226 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Eric Dumazetd188ba82015-12-08 07:22:02 -08001228 rcu_read_lock();
Fan Du283bc9f2013-11-07 17:47:50 +08001229 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001230 pol = rcu_dereference(sk->sk_policy[dir]);
1231 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001232 bool match = xfrm_selector_match(&pol->selector, fl,
1233 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001234 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001235
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001236 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001237 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1238 pol = NULL;
1239 goto out;
1240 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001241 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001242 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001243 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001244 if (!err)
1245 xfrm_pol_hold(pol);
1246 else if (err == -ESRCH)
1247 pol = NULL;
1248 else
1249 pol = ERR_PTR(err);
1250 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 pol = NULL;
1252 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001253out:
Fan Du283bc9f2013-11-07 17:47:50 +08001254 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001255 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return pol;
1257}
1258
1259static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1260{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001261 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001262
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001263 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001264 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 xfrm_pol_hold(pol);
1266}
1267
1268static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1269 int dir)
1270{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001271 struct net *net = xp_net(pol);
1272
Herbert Xu53c2e282014-11-13 17:09:49 +08001273 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001274 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Herbert Xu53c2e282014-11-13 17:09:49 +08001276 /* Socket policies are not hashed. */
1277 if (!hlist_unhashed(&pol->bydst)) {
1278 hlist_del(&pol->bydst);
1279 hlist_del(&pol->byidx);
1280 }
1281
1282 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001283 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001284
1285 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Herbert Xu53c2e282014-11-13 17:09:49 +08001288static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1289{
1290 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1291}
1292
1293static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1294{
1295 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1296}
1297
Herbert Xu4666faa2005-06-18 22:43:22 -07001298int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Fan Du283bc9f2013-11-07 17:47:50 +08001300 struct net *net = xp_net(pol);
1301
1302 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001304 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001309 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
David S. Millera70fcb02006-03-20 19:18:52 -08001311EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1314{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001315 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct xfrm_policy *old_pol;
1317
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001318#ifdef CONFIG_XFRM_SUB_POLICY
1319 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1320 return -EINVAL;
1321#endif
1322
Fan Du283bc9f2013-11-07 17:47:50 +08001323 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001324 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1325 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001327 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001328 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001329 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001331 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001332 if (old_pol) {
1333 if (pol)
1334 xfrm_policy_requeue(old_pol, pol);
1335
Timo Teräsea2dea92010-03-31 00:17:05 +00001336 /* Unlinking succeeds always. This is the only function
1337 * allowed to delete or replace socket policy.
1338 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001339 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001340 }
Fan Du283bc9f2013-11-07 17:47:50 +08001341 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (old_pol) {
1344 xfrm_policy_kill(old_pol);
1345 }
1346 return 0;
1347}
1348
David S. Millerd3e40a92011-02-24 01:25:41 -05001349static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001351 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001352 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (newp) {
1355 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001356 if (security_xfrm_policy_clone(old->security,
1357 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001358 kfree(newp);
1359 return NULL; /* ENOMEM */
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 newp->lft = old->lft;
1362 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001363 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 newp->action = old->action;
1365 newp->flags = old->flags;
1366 newp->xfrm_nr = old->xfrm_nr;
1367 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001368 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 memcpy(newp->xfrm_vec, old->xfrm_vec,
1370 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001371 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001372 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001373 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 xfrm_pol_put(newp);
1375 }
1376 return newp;
1377}
1378
Eric Dumazetd188ba82015-12-08 07:22:02 -08001379int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001381 const struct xfrm_policy *p;
1382 struct xfrm_policy *np;
1383 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Eric Dumazetd188ba82015-12-08 07:22:02 -08001385 rcu_read_lock();
1386 for (i = 0; i < 2; i++) {
1387 p = rcu_dereference(osk->sk_policy[i]);
1388 if (p) {
1389 np = clone_policy(p, i);
1390 if (unlikely(!np)) {
1391 ret = -ENOMEM;
1392 break;
1393 }
1394 rcu_assign_pointer(sk->sk_policy[i], np);
1395 }
1396 }
1397 rcu_read_unlock();
1398 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001401static int
David Ahern42a7b322015-08-10 16:58:11 -06001402xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1403 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001404{
1405 int err;
1406 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1407
1408 if (unlikely(afinfo == NULL))
1409 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001410 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001411 xfrm_policy_put_afinfo(afinfo);
1412 return err;
1413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/* Resolve list of templates for the flow, given policy. */
1416
1417static int
David S. Millera6c2e612011-02-22 18:35:39 -08001418xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1419 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001421 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int nx;
1423 int i, error;
1424 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1425 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001426 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Weilong Chen9b7a7872013-12-24 09:43:46 +08001428 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct xfrm_state *x;
1430 xfrm_address_t *remote = daddr;
1431 xfrm_address_t *local = saddr;
1432 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1433
Joakim Koskela48b8d782007-07-26 00:08:42 -07001434 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1435 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 remote = &tmpl->id.daddr;
1437 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001438 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001439 error = xfrm_get_saddr(net, fl->flowi_oif,
1440 &tmp, remote,
1441 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001442 if (error)
1443 goto fail;
1444 local = &tmp;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
1448 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1449
1450 if (x && x->km.state == XFRM_STATE_VALID) {
1451 xfrm[nx++] = x;
1452 daddr = remote;
1453 saddr = local;
1454 continue;
1455 }
1456 if (x) {
1457 error = (x->km.state == XFRM_STATE_ERROR ?
1458 -EINVAL : -EAGAIN);
1459 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001460 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001461 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 if (!tmpl->optional)
1465 goto fail;
1466 }
1467 return nx;
1468
1469fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001470 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 xfrm_state_put(xfrm[nx]);
1472 return error;
1473}
1474
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001475static int
David S. Millera6c2e612011-02-22 18:35:39 -08001476xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1477 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001478{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001479 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1480 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001481 int cnx = 0;
1482 int error;
1483 int ret;
1484 int i;
1485
1486 for (i = 0; i < npols; i++) {
1487 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1488 error = -ENOBUFS;
1489 goto fail;
1490 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001491
1492 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001493 if (ret < 0) {
1494 error = ret;
1495 goto fail;
1496 } else
1497 cnx += ret;
1498 }
1499
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001500 /* found states are sorted for outbound processing */
1501 if (npols > 1)
1502 xfrm_state_sort(xfrm, tpp, cnx, family);
1503
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001504 return cnx;
1505
1506 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001507 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001508 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001509 return error;
1510
1511}
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513/* Check that the bundle accepts the flow and its components are
1514 * still valid.
1515 */
1516
David S. Miller05d84022011-02-22 17:47:10 -08001517static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001518{
1519 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1520 int tos;
1521
1522 if (!afinfo)
1523 return -EINVAL;
1524
1525 tos = afinfo->get_tos(fl);
1526
1527 xfrm_policy_put_afinfo(afinfo);
1528
1529 return tos;
1530}
1531
Timo Teräs80c802f2010-04-07 00:30:05 +00001532static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1533{
1534 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1535 struct dst_entry *dst = &xdst->u.dst;
1536
1537 if (xdst->route == NULL) {
1538 /* Dummy bundle - if it has xfrms we were not
1539 * able to build bundle as template resolution failed.
1540 * It means we need to try again resolving. */
1541 if (xdst->num_xfrms > 0)
1542 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001543 } else if (dst->flags & DST_XFRM_QUEUE) {
1544 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001545 } else {
1546 /* Real bundle */
1547 if (stale_bundle(dst))
1548 return NULL;
1549 }
1550
1551 dst_hold(dst);
1552 return flo;
1553}
1554
1555static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1556{
1557 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1558 struct dst_entry *dst = &xdst->u.dst;
1559
1560 if (!xdst->route)
1561 return 0;
1562 if (stale_bundle(dst))
1563 return 0;
1564
1565 return 1;
1566}
1567
1568static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1569{
1570 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1571 struct dst_entry *dst = &xdst->u.dst;
1572
1573 dst_free(dst);
1574}
1575
1576static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1577 .get = xfrm_bundle_flo_get,
1578 .check = xfrm_bundle_flo_check,
1579 .delete = xfrm_bundle_flo_delete,
1580};
1581
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001582static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001583{
1584 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001585 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001586 struct xfrm_dst *xdst;
1587
1588 if (!afinfo)
1589 return ERR_PTR(-EINVAL);
1590
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001591 switch (family) {
1592 case AF_INET:
1593 dst_ops = &net->xfrm.xfrm4_dst_ops;
1594 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001595#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001596 case AF_INET6:
1597 dst_ops = &net->xfrm.xfrm6_dst_ops;
1598 break;
1599#endif
1600 default:
1601 BUG();
1602 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001603 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001604
Madalin Bucurd4cae562011-09-26 07:04:36 +00001605 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001606 struct dst_entry *dst = &xdst->u.dst;
1607
1608 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001609 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001610 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001611 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001612
Madalin Bucurd4cae562011-09-26 07:04:36 +00001613 xfrm_policy_put_afinfo(afinfo);
1614
Herbert Xu25ee3282007-12-11 09:32:34 -08001615 return xdst;
1616}
1617
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001618static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1619 int nfheader_len)
1620{
1621 struct xfrm_policy_afinfo *afinfo =
1622 xfrm_policy_get_afinfo(dst->ops->family);
1623 int err;
1624
1625 if (!afinfo)
1626 return -EINVAL;
1627
1628 err = afinfo->init_path(path, dst, nfheader_len);
1629
1630 xfrm_policy_put_afinfo(afinfo);
1631
1632 return err;
1633}
1634
Herbert Xu87c1e122010-03-02 02:51:56 +00001635static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001636 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001637{
1638 struct xfrm_policy_afinfo *afinfo =
1639 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1640 int err;
1641
1642 if (!afinfo)
1643 return -EINVAL;
1644
Herbert Xu87c1e122010-03-02 02:51:56 +00001645 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001646
1647 xfrm_policy_put_afinfo(afinfo);
1648
1649 return err;
1650}
1651
Timo Teräs80c802f2010-04-07 00:30:05 +00001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1654 * all the metrics... Shortly, bundle a bundle.
1655 */
1656
Herbert Xu25ee3282007-12-11 09:32:34 -08001657static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1658 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001659 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001660 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001662 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001663 unsigned long now = jiffies;
1664 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001665 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001666 struct dst_entry *dst_prev = NULL;
1667 struct dst_entry *dst0 = NULL;
1668 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001670 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001671 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001672 int trailer_len = 0;
1673 int tos;
1674 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001675 xfrm_address_t saddr, daddr;
1676
1677 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001678
1679 tos = xfrm_get_tos(fl, family);
1680 err = tos;
1681 if (tos < 0)
1682 goto put_states;
1683
1684 dst_hold(dst);
1685
1686 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001687 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001688 struct dst_entry *dst1 = &xdst->u.dst;
1689
1690 err = PTR_ERR(xdst);
1691 if (IS_ERR(xdst)) {
1692 dst_release(dst);
1693 goto put_states;
1694 }
1695
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001696 if (xfrm[i]->sel.family == AF_UNSPEC) {
1697 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1698 xfrm_af2proto(family));
1699 if (!inner_mode) {
1700 err = -EAFNOSUPPORT;
1701 dst_release(dst);
1702 goto put_states;
1703 }
1704 } else
1705 inner_mode = xfrm[i]->inner_mode;
1706
Herbert Xu25ee3282007-12-11 09:32:34 -08001707 if (!dst_prev)
1708 dst0 = dst1;
1709 else {
1710 dst_prev->child = dst_clone(dst1);
1711 dst1->flags |= DST_NOHASH;
1712 }
1713
1714 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001715 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001716
1717 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1718 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001719 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1720 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001721 err = PTR_ERR(dst);
1722 if (IS_ERR(dst))
1723 goto put_states;
1724 } else
1725 dst_hold(dst);
1726
1727 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001728 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001729
David S. Millerf5b0a872012-07-19 12:31:33 -07001730 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001731 dst1->flags |= DST_HOST;
1732 dst1->lastuse = now;
1733
1734 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001735 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001736
1737 dst1->next = dst_prev;
1738 dst_prev = dst1;
1739
1740 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001741 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1742 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001743 trailer_len += xfrm[i]->props.trailer_len;
1744 }
1745
1746 dst_prev->child = dst;
1747 dst0->path = dst;
1748
1749 err = -ENODEV;
1750 dev = dst->dev;
1751 if (!dev)
1752 goto free_dst;
1753
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001754 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001755 xfrm_init_pmtu(dst_prev);
1756
1757 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1758 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1759
Herbert Xu87c1e122010-03-02 02:51:56 +00001760 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001761 if (err)
1762 goto free_dst;
1763
1764 dst_prev->header_len = header_len;
1765 dst_prev->trailer_len = trailer_len;
1766 header_len -= xdst->u.dst.xfrm->props.header_len;
1767 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1768 }
1769
1770out:
1771 return dst0;
1772
1773put_states:
1774 for (; i < nx; i++)
1775 xfrm_state_put(xfrm[i]);
1776free_dst:
1777 if (dst0)
1778 dst_free(dst0);
1779 dst0 = ERR_PTR(err);
1780 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Ying Xueda7c2242014-01-08 10:26:39 +08001783#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001784static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001785{
1786 if (!*target) {
1787 *target = kmalloc(size, GFP_ATOMIC);
1788 if (!*target)
1789 return -ENOMEM;
1790 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001791
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001792 memcpy(*target, src, size);
1793 return 0;
1794}
Ying Xueda7c2242014-01-08 10:26:39 +08001795#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001796
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001797static int xfrm_dst_update_parent(struct dst_entry *dst,
1798 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001799{
1800#ifdef CONFIG_XFRM_SUB_POLICY
1801 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1802 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1803 sel, sizeof(*sel));
1804#else
1805 return 0;
1806#endif
1807}
1808
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001809static int xfrm_dst_update_origin(struct dst_entry *dst,
1810 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001811{
1812#ifdef CONFIG_XFRM_SUB_POLICY
1813 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1814 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1815#else
1816 return 0;
1817#endif
1818}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
David S. Miller73ff93c2011-02-22 18:33:42 -08001820static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001821 struct xfrm_policy **pols,
1822 int *num_pols, int *num_xfrms)
1823{
1824 int i;
1825
1826 if (*num_pols == 0 || !pols[0]) {
1827 *num_pols = 0;
1828 *num_xfrms = 0;
1829 return 0;
1830 }
1831 if (IS_ERR(pols[0]))
1832 return PTR_ERR(pols[0]);
1833
1834 *num_xfrms = pols[0]->xfrm_nr;
1835
1836#ifdef CONFIG_XFRM_SUB_POLICY
1837 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1838 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1839 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1840 XFRM_POLICY_TYPE_MAIN,
1841 fl, family,
1842 XFRM_POLICY_OUT);
1843 if (pols[1]) {
1844 if (IS_ERR(pols[1])) {
1845 xfrm_pols_put(pols, *num_pols);
1846 return PTR_ERR(pols[1]);
1847 }
Weilong Chen02d08922013-12-24 09:43:48 +08001848 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001849 (*num_xfrms) += pols[1]->xfrm_nr;
1850 }
1851 }
1852#endif
1853 for (i = 0; i < *num_pols; i++) {
1854 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1855 *num_xfrms = -1;
1856 break;
1857 }
1858 }
1859
1860 return 0;
1861
1862}
1863
1864static struct xfrm_dst *
1865xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001866 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001867 struct dst_entry *dst_orig)
1868{
1869 struct net *net = xp_net(pols[0]);
1870 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1871 struct dst_entry *dst;
1872 struct xfrm_dst *xdst;
1873 int err;
1874
1875 /* Try to instantiate a bundle */
1876 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001877 if (err <= 0) {
1878 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001879 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1880 return ERR_PTR(err);
1881 }
1882
1883 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1884 if (IS_ERR(dst)) {
1885 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1886 return ERR_CAST(dst);
1887 }
1888
1889 xdst = (struct xfrm_dst *)dst;
1890 xdst->num_xfrms = err;
1891 if (num_pols > 1)
1892 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1893 else
1894 err = xfrm_dst_update_origin(dst, fl);
1895 if (unlikely(err)) {
1896 dst_free(dst);
1897 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1898 return ERR_PTR(err);
1899 }
1900
1901 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001902 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001903 xdst->policy_genid = atomic_read(&pols[0]->genid);
1904
1905 return xdst;
1906}
1907
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001908static void xfrm_policy_queue_process(unsigned long arg)
1909{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001910 struct sk_buff *skb;
1911 struct sock *sk;
1912 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001913 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001914 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001915 struct xfrm_policy_queue *pq = &pol->polq;
1916 struct flowi fl;
1917 struct sk_buff_head list;
1918
1919 spin_lock(&pq->hold_queue.lock);
1920 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001921 if (!skb) {
1922 spin_unlock(&pq->hold_queue.lock);
1923 goto out;
1924 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001925 dst = skb_dst(skb);
1926 sk = skb->sk;
1927 xfrm_decode_session(skb, &fl, dst->ops->family);
1928 spin_unlock(&pq->hold_queue.lock);
1929
1930 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001931 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001932 if (IS_ERR(dst))
1933 goto purge_queue;
1934
1935 if (dst->flags & DST_XFRM_QUEUE) {
1936 dst_release(dst);
1937
1938 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1939 goto purge_queue;
1940
1941 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001942 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1943 xfrm_pol_hold(pol);
1944 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001945 }
1946
1947 dst_release(dst);
1948
1949 __skb_queue_head_init(&list);
1950
1951 spin_lock(&pq->hold_queue.lock);
1952 pq->timeout = 0;
1953 skb_queue_splice_init(&pq->hold_queue, &list);
1954 spin_unlock(&pq->hold_queue.lock);
1955
1956 while (!skb_queue_empty(&list)) {
1957 skb = __skb_dequeue(&list);
1958
1959 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1960 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001961 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001962 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001963 kfree_skb(skb);
1964 continue;
1965 }
1966
1967 nf_reset(skb);
1968 skb_dst_drop(skb);
1969 skb_dst_set(skb, dst);
1970
Eric W. Biederman13206b62015-10-07 16:48:35 -05001971 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001972 }
1973
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001974out:
1975 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001976 return;
1977
1978purge_queue:
1979 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001980 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001981 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001982}
1983
Eric W. Biedermanede20592015-10-07 16:48:47 -05001984static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001985{
1986 unsigned long sched_next;
1987 struct dst_entry *dst = skb_dst(skb);
1988 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001989 struct xfrm_policy *pol = xdst->pols[0];
1990 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001991
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001992 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001993 kfree_skb(skb);
1994 return 0;
1995 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001996
1997 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1998 kfree_skb(skb);
1999 return -EAGAIN;
2000 }
2001
2002 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002003
2004 spin_lock_bh(&pq->hold_queue.lock);
2005
2006 if (!pq->timeout)
2007 pq->timeout = XFRM_QUEUE_TMO_MIN;
2008
2009 sched_next = jiffies + pq->timeout;
2010
2011 if (del_timer(&pq->hold_timer)) {
2012 if (time_before(pq->hold_timer.expires, sched_next))
2013 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002014 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002015 }
2016
2017 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002018 if (!mod_timer(&pq->hold_timer, sched_next))
2019 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002020
2021 spin_unlock_bh(&pq->hold_queue.lock);
2022
2023 return 0;
2024}
2025
2026static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002027 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002028 const struct flowi *fl,
2029 int num_xfrms,
2030 u16 family)
2031{
2032 int err;
2033 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002034 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002035 struct dst_entry *dst1;
2036 struct xfrm_dst *xdst;
2037
2038 xdst = xfrm_alloc_dst(net, family);
2039 if (IS_ERR(xdst))
2040 return xdst;
2041
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002042 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2043 net->xfrm.sysctl_larval_drop ||
2044 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002045 return xdst;
2046
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002047 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002048 dst1 = &xdst->u.dst;
2049 dst_hold(dst);
2050 xdst->route = dst;
2051
2052 dst_copy_metrics(dst1, dst);
2053
2054 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2055 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2056 dst1->lastuse = jiffies;
2057
2058 dst1->input = dst_discard;
2059 dst1->output = xdst_queue_output;
2060
2061 dst_hold(dst);
2062 dst1->child = dst;
2063 dst1->path = dst;
2064
2065 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2066
2067 err = -ENODEV;
2068 dev = dst->dev;
2069 if (!dev)
2070 goto free_dst;
2071
2072 err = xfrm_fill_dst(xdst, dev, fl);
2073 if (err)
2074 goto free_dst;
2075
2076out:
2077 return xdst;
2078
2079free_dst:
2080 dst_release(dst1);
2081 xdst = ERR_PTR(err);
2082 goto out;
2083}
2084
Timo Teräs80c802f2010-04-07 00:30:05 +00002085static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002086xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002087 struct flow_cache_object *oldflo, void *ctx)
2088{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002089 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002090 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2091 struct xfrm_dst *xdst, *new_xdst;
2092 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2093
2094 /* Check if the policies from old bundle are usable */
2095 xdst = NULL;
2096 if (oldflo) {
2097 xdst = container_of(oldflo, struct xfrm_dst, flo);
2098 num_pols = xdst->num_pols;
2099 num_xfrms = xdst->num_xfrms;
2100 pol_dead = 0;
2101 for (i = 0; i < num_pols; i++) {
2102 pols[i] = xdst->pols[i];
2103 pol_dead |= pols[i]->walk.dead;
2104 }
2105 if (pol_dead) {
2106 dst_free(&xdst->u.dst);
2107 xdst = NULL;
2108 num_pols = 0;
2109 num_xfrms = 0;
2110 oldflo = NULL;
2111 }
2112 }
2113
2114 /* Resolve policies to use if we couldn't get them from
2115 * previous cache entry */
2116 if (xdst == NULL) {
2117 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002118 pols[0] = __xfrm_policy_lookup(net, fl, family,
2119 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002120 err = xfrm_expand_policies(fl, family, pols,
2121 &num_pols, &num_xfrms);
2122 if (err < 0)
2123 goto inc_error;
2124 if (num_pols == 0)
2125 return NULL;
2126 if (num_xfrms <= 0)
2127 goto make_dummy_bundle;
2128 }
2129
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002130 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2131 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002132 if (IS_ERR(new_xdst)) {
2133 err = PTR_ERR(new_xdst);
2134 if (err != -EAGAIN)
2135 goto error;
2136 if (oldflo == NULL)
2137 goto make_dummy_bundle;
2138 dst_hold(&xdst->u.dst);
2139 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002140 } else if (new_xdst == NULL) {
2141 num_xfrms = 0;
2142 if (oldflo == NULL)
2143 goto make_dummy_bundle;
2144 xdst->num_xfrms = 0;
2145 dst_hold(&xdst->u.dst);
2146 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002147 }
2148
2149 /* Kill the previous bundle */
2150 if (xdst) {
2151 /* The policies were stolen for newly generated bundle */
2152 xdst->num_pols = 0;
2153 dst_free(&xdst->u.dst);
2154 }
2155
2156 /* Flow cache does not have reference, it dst_free()'s,
2157 * but we do need to return one reference for original caller */
2158 dst_hold(&new_xdst->u.dst);
2159 return &new_xdst->flo;
2160
2161make_dummy_bundle:
2162 /* We found policies, but there's no bundles to instantiate:
2163 * either because the policy blocks, has no transformations or
2164 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002165 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002166 if (IS_ERR(xdst)) {
2167 xfrm_pols_put(pols, num_pols);
2168 return ERR_CAST(xdst);
2169 }
2170 xdst->num_pols = num_pols;
2171 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002172 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002173
2174 dst_hold(&xdst->u.dst);
2175 return &xdst->flo;
2176
2177inc_error:
2178 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2179error:
2180 if (xdst != NULL)
2181 dst_free(&xdst->u.dst);
2182 else
2183 xfrm_pols_put(pols, num_pols);
2184 return ERR_PTR(err);
2185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
David S. Miller2774c132011-03-01 14:59:04 -08002187static struct dst_entry *make_blackhole(struct net *net, u16 family,
2188 struct dst_entry *dst_orig)
2189{
2190 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2191 struct dst_entry *ret;
2192
2193 if (!afinfo) {
2194 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002195 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002196 } else {
2197 ret = afinfo->blackhole_route(net, dst_orig);
2198 }
2199 xfrm_policy_put_afinfo(afinfo);
2200
2201 return ret;
2202}
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204/* Main function: finds/creates a bundle for given flow.
2205 *
2206 * At the moment we eat a raw IP route. Mostly to speed up lookups
2207 * on interfaces with disabled IPsec.
2208 */
David S. Miller452edd52011-03-02 13:27:41 -08002209struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2210 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002211 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002213 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002214 struct flow_cache_object *flo;
2215 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002216 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002217 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002218 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002219 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002220
Timo Teräs80c802f2010-04-07 00:30:05 +00002221 dst = NULL;
2222 xdst = NULL;
2223 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002224
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002225 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002226 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002227 num_pols = 1;
2228 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2229 err = xfrm_expand_policies(fl, family, pols,
2230 &num_pols, &num_xfrms);
2231 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002232 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002233
2234 if (num_pols) {
2235 if (num_xfrms <= 0) {
2236 drop_pols = num_pols;
2237 goto no_transform;
2238 }
2239
2240 xdst = xfrm_resolve_and_create_bundle(
2241 pols, num_pols, fl,
2242 family, dst_orig);
2243 if (IS_ERR(xdst)) {
2244 xfrm_pols_put(pols, num_pols);
2245 err = PTR_ERR(xdst);
2246 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002247 } else if (xdst == NULL) {
2248 num_xfrms = 0;
2249 drop_pols = num_pols;
2250 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002251 }
2252
Steffen Klassertb7eea452014-06-18 12:34:21 +02002253 dst_hold(&xdst->u.dst);
2254 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002255 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002256 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Timo Teräs80c802f2010-04-07 00:30:05 +00002259 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002260 struct xfrm_flo xflo;
2261
2262 xflo.dst_orig = dst_orig;
2263 xflo.flags = flags;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002266 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002267 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002268 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Timo Teräs80c802f2010-04-07 00:30:05 +00002270 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002271 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002272 if (flo == NULL)
2273 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002274 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002275 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002276 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002277 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002278 xdst = container_of(flo, struct xfrm_dst, flo);
2279
2280 num_pols = xdst->num_pols;
2281 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002282 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002283 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285
Timo Teräs80c802f2010-04-07 00:30:05 +00002286 dst = &xdst->u.dst;
2287 if (route == NULL && num_xfrms > 0) {
2288 /* The only case when xfrm_bundle_lookup() returns a
2289 * bundle with null route, is when the template could
2290 * not be resolved. It means policies are there, but
2291 * bundle could not be created, since we don't yet
2292 * have the xfrm_state's. We need to wait for KM to
2293 * negotiate new SA's or bail out with error.*/
2294 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002295 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002296 err = -EREMOTE;
2297 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002298 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002299
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002300 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002301
2302 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2303 goto error;
2304 }
2305
2306no_transform:
2307 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002308 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Timo Teräs80c802f2010-04-07 00:30:05 +00002310 if ((flags & XFRM_LOOKUP_ICMP) &&
2311 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2312 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002313 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002314 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002315
Timo Teräs80c802f2010-04-07 00:30:05 +00002316 for (i = 0; i < num_pols; i++)
2317 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002318
Timo Teräs80c802f2010-04-07 00:30:05 +00002319 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002321 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002322 err = -EPERM;
2323 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002324 } else if (num_xfrms > 0) {
2325 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002326 dst_release(dst_orig);
2327 } else {
2328 /* Flow passes untransformed */
2329 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002330 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002332ok:
2333 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002334 if (dst && dst->xfrm &&
2335 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2336 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002337 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Timo Teräs80c802f2010-04-07 00:30:05 +00002339nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002340 if (!(flags & XFRM_LOOKUP_ICMP)) {
2341 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002342 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002343 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002344 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002347dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002348 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2349 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002350 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002351 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353EXPORT_SYMBOL(xfrm_lookup);
2354
Steffen Klassertf92ee612014-09-16 10:08:40 +02002355/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2356 * Otherwise we may send out blackholed packets.
2357 */
2358struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2359 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002360 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002361{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002362 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002363 flags | XFRM_LOOKUP_QUEUE |
2364 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002365
2366 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2367 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2368
2369 return dst;
2370}
2371EXPORT_SYMBOL(xfrm_lookup_route);
2372
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002373static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002374xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002375{
2376 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002377
2378 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2379 return 0;
2380 x = skb->sp->xvec[idx];
2381 if (!x->type->reject)
2382 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002383 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002384}
2385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386/* When skb is transformed back to its "native" form, we have to
2387 * check policy restrictions. At the moment we make this in maximally
2388 * stupid way. Shame on me. :-) Of course, connected sockets must
2389 * have policy cached at them.
2390 */
2391
2392static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002393xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 unsigned short family)
2395{
2396 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002397 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return x->id.proto == tmpl->id.proto &&
2399 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2400 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2401 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002402 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002403 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002404 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2405 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002408/*
2409 * 0 or more than 0 is returned when validation is succeeded (either bypass
2410 * because of optional transport mode, or next index of the mathced secpath
2411 * state with the template.
2412 * -1 is returned when no matching template is found.
2413 * Otherwise "-2 - errored_index" is returned.
2414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002416xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 unsigned short family)
2418{
2419 int idx = start;
2420
2421 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002422 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return start;
2424 } else
2425 start = -1;
2426 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002427 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002429 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2430 if (start == -1)
2431 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435 return start;
2436}
2437
Herbert Xud5422ef2007-12-12 10:44:16 -08002438int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2439 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002442 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 if (unlikely(afinfo == NULL))
2445 return -EAFNOSUPPORT;
2446
Herbert Xud5422ef2007-12-12 10:44:16 -08002447 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002448 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
Herbert Xud5422ef2007-12-12 10:44:16 -08002452EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
David S. Miller9a7386e2011-02-24 01:44:12 -05002454static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002457 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002458 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
2462
2463 return 0;
2464}
2465
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002466int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 unsigned short family)
2468{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002469 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002471 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2472 int npols = 0;
2473 int xfrm_nr;
2474 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002475 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002477 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002478 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Herbert Xud5422ef2007-12-12 10:44:16 -08002480 reverse = dir & ~XFRM_POLICY_MASK;
2481 dir &= XFRM_POLICY_MASK;
2482 fl_dir = policy_to_flow_dir(dir);
2483
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002484 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002485 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002487 }
2488
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002489 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 /* First, check used SA against their selectors. */
2492 if (skb->sp) {
2493 int i;
2494
Weilong Chen9b7a7872013-12-24 09:43:46 +08002495 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002496 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002497 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002498 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502 }
2503
2504 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002505 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002506 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002507 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002508 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002509 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002510 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002511 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002514 if (!pol) {
2515 struct flow_cache_object *flo;
2516
2517 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2518 xfrm_policy_lookup, NULL);
2519 if (IS_ERR_OR_NULL(flo))
2520 pol = ERR_CAST(flo);
2521 else
2522 pol = container_of(flo, struct xfrm_policy, flo);
2523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002525 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002526 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002527 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002528 }
James Morris134b0fc2006-10-05 15:42:27 -05002529
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002530 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002531 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002532 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002533 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002534 return 0;
2535 }
2536 return 1;
2537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
James Morris9d729f72007-03-04 16:12:44 -08002539 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002541 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002542 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002543#ifdef CONFIG_XFRM_SUB_POLICY
2544 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002545 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002546 &fl, family,
2547 XFRM_POLICY_IN);
2548 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002549 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002550 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002551 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002552 }
James Morris9d729f72007-03-04 16:12:44 -08002553 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002554 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002555 }
2556 }
2557#endif
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (pol->action == XFRM_POLICY_ALLOW) {
2560 struct sec_path *sp;
2561 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002562 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002563 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002564 struct xfrm_tmpl **tpp = tp;
2565 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 int i, k;
2567
2568 if ((sp = skb->sp) == NULL)
2569 sp = &dummy;
2570
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002571 for (pi = 0; pi < npols; pi++) {
2572 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002573 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002574 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002575 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002576 }
2577 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002578 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002579 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002580 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002581 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2582 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2583 }
2584 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002585 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002586 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002587 tpp = stp;
2588 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* For each tunnel xfrm, find the first matching tmpl.
2591 * For each tmpl before that, find corresponding xfrm.
2592 * Order is _important_. Later we will implement
2593 * some barriers, but at the moment barriers
2594 * are implied between each two transformations.
2595 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002596 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2597 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002598 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002599 if (k < -1)
2600 /* "-2 - errored_index" returned */
2601 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002602 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002607 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002608 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002612 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return 1;
2614 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002615 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002618 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002619reject_error:
2620 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return 0;
2622}
2623EXPORT_SYMBOL(__xfrm_policy_check);
2624
2625int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2626{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002627 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002629 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002630 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002632 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002633 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002637 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002638
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002639 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002640 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002641 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002642 dst = NULL;
2643 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002644 skb_dst_set(skb, dst);
2645 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647EXPORT_SYMBOL(__xfrm_route_forward);
2648
David S. Millerd49c73c2006-08-13 18:55:53 -07002649/* Optimize later using cookies and generation ids. */
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2652{
David S. Millerd49c73c2006-08-13 18:55:53 -07002653 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002654 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2655 * get validated by dst_ops->check on every use. We do this
2656 * because when a normal route referenced by an XFRM dst is
2657 * obsoleted we do not go looking around for all parent
2658 * referencing XFRM dsts so that we can invalidate them. It
2659 * is just too much work. Instead we make the checks here on
2660 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002661 *
2662 * XFRM dst A --> IPv4 dst X
2663 *
2664 * X is the "xdst->route" of A (X is also the "dst->path" of A
2665 * in this example). If X is marked obsolete, "A" will not
2666 * notice. That's what we are validating here via the
2667 * stale_bundle() check.
2668 *
2669 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002670 * dst which causes it's ->obsolete field to be set to
2671 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2672 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002673 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002674 if (dst->obsolete < 0 && !stale_bundle(dst))
2675 return dst;
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 return NULL;
2678}
2679
2680static int stale_bundle(struct dst_entry *dst)
2681{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002682 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Herbert Xuaabc9762005-05-03 16:27:10 -07002685void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002688 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002689 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 dev_put(dev);
2691 }
2692}
Herbert Xuaabc9762005-05-03 16:27:10 -07002693EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695static void xfrm_link_failure(struct sk_buff *skb)
2696{
2697 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2701{
2702 if (dst) {
2703 if (dst->obsolete) {
2704 dst_release(dst);
2705 dst = NULL;
2706 }
2707 }
2708 return dst;
2709}
2710
Paul Mooree4c17212013-05-29 07:36:25 +00002711void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002712{
Fan Duca925cf2014-01-18 09:55:27 +08002713 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002714}
Paul Mooree4c17212013-05-29 07:36:25 +00002715EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002716
2717static void xfrm_garbage_collect_deferred(struct net *net)
2718{
Fan Duca925cf2014-01-18 09:55:27 +08002719 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002720}
2721
Herbert Xu25ee3282007-12-11 09:32:34 -08002722static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
2724 do {
2725 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2726 u32 pmtu, route_mtu_cached;
2727
2728 pmtu = dst_mtu(dst->child);
2729 xdst->child_mtu_cached = pmtu;
2730
2731 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2732
2733 route_mtu_cached = dst_mtu(xdst->route);
2734 xdst->route_mtu_cached = route_mtu_cached;
2735
2736 if (pmtu > route_mtu_cached)
2737 pmtu = route_mtu_cached;
2738
David S. Millerdefb3512010-12-08 21:16:57 -08002739 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 } while ((dst = dst->next));
2741}
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/* Check that the bundle accepts the flow and its components are
2744 * still valid.
2745 */
2746
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002747static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 struct dst_entry *dst = &first->u.dst;
2750 struct xfrm_dst *last;
2751 u32 mtu;
2752
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002753 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 (dst->dev && !netif_running(dst->dev)))
2755 return 0;
2756
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002757 if (dst->flags & DST_XFRM_QUEUE)
2758 return 1;
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 last = NULL;
2761
2762 do {
2763 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2766 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002767 if (xdst->xfrm_genid != dst->xfrm->genid)
2768 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002769 if (xdst->num_pols > 0 &&
2770 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 mtu = dst_mtu(dst->child);
2774 if (xdst->child_mtu_cached != mtu) {
2775 last = xdst;
2776 xdst->child_mtu_cached = mtu;
2777 }
2778
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002779 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return 0;
2781 mtu = dst_mtu(xdst->route);
2782 if (xdst->route_mtu_cached != mtu) {
2783 last = xdst;
2784 xdst->route_mtu_cached = mtu;
2785 }
2786
2787 dst = dst->child;
2788 } while (dst->xfrm);
2789
2790 if (likely(!last))
2791 return 1;
2792
2793 mtu = last->child_mtu_cached;
2794 for (;;) {
2795 dst = &last->u.dst;
2796
2797 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2798 if (mtu > last->route_mtu_cached)
2799 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002800 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 if (last == first)
2803 break;
2804
Patrick McHardybd0bf072007-07-18 01:55:52 -07002805 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 last->child_mtu_cached = mtu;
2807 }
2808
2809 return 1;
2810}
2811
David S. Miller0dbaee32010-12-13 12:52:14 -08002812static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2813{
2814 return dst_metric_advmss(dst->path);
2815}
2816
Steffen Klassertebb762f2011-11-23 02:12:51 +00002817static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002818{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002819 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2820
2821 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002822}
2823
David S. Millerf894cbf2012-07-02 21:52:24 -07002824static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2825 struct sk_buff *skb,
2826 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002827{
David S. Millerf894cbf2012-07-02 21:52:24 -07002828 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002829}
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2832{
2833 int err = 0;
2834 if (unlikely(afinfo == NULL))
2835 return -EINVAL;
2836 if (unlikely(afinfo->family >= NPROTO))
2837 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002838 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002840 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 else {
2842 struct dst_ops *dst_ops = afinfo->dst_ops;
2843 if (likely(dst_ops->kmem_cachep == NULL))
2844 dst_ops->kmem_cachep = xfrm_dst_cache;
2845 if (likely(dst_ops->check == NULL))
2846 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002847 if (likely(dst_ops->default_advmss == NULL))
2848 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002849 if (likely(dst_ops->mtu == NULL))
2850 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (likely(dst_ops->negative_advice == NULL))
2852 dst_ops->negative_advice = xfrm_negative_advice;
2853 if (likely(dst_ops->link_failure == NULL))
2854 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002855 if (likely(dst_ops->neigh_lookup == NULL))
2856 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002858 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002859 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002861 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return err;
2864}
2865EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2866
2867int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2868{
2869 int err = 0;
2870 if (unlikely(afinfo == NULL))
2871 return -EINVAL;
2872 if (unlikely(afinfo->family >= NPROTO))
2873 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002874 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2876 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2877 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002878 else
2879 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2880 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002882 spin_unlock(&xfrm_policy_afinfo_lock);
2883 if (!err) {
2884 struct dst_ops *dst_ops = afinfo->dst_ops;
2885
2886 synchronize_rcu();
2887
2888 dst_ops->kmem_cachep = NULL;
2889 dst_ops->check = NULL;
2890 dst_ops->negative_advice = NULL;
2891 dst_ops->link_failure = NULL;
2892 afinfo->garbage_collect = NULL;
2893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return err;
2895}
2896EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2899{
Jiri Pirko351638e2013-05-28 01:30:21 +00002900 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 switch (event) {
2903 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002904 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906 return NOTIFY_DONE;
2907}
2908
2909static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002910 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911};
2912
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002913#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002914static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002915{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002916 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002917 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2918 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002919 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002920 rv = xfrm_proc_init(net);
2921 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002922 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002923 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002924}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002925
2926static void xfrm_statistics_fini(struct net *net)
2927{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002928 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002929 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002930}
2931#else
2932static int __net_init xfrm_statistics_init(struct net *net)
2933{
2934 return 0;
2935}
2936
2937static void xfrm_statistics_fini(struct net *net)
2938{
2939}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002940#endif
2941
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002942static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
David S. Miller2518c7c2006-08-24 04:45:07 -07002944 unsigned int hmask, sz;
2945 int dir;
2946
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002947 if (net_eq(net, &init_net))
2948 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002950 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002951 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
David S. Miller2518c7c2006-08-24 04:45:07 -07002953 hmask = 8 - 1;
2954 sz = (hmask+1) * sizeof(struct hlist_head);
2955
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002956 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2957 if (!net->xfrm.policy_byidx)
2958 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002959 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002960
Herbert Xu53c2e282014-11-13 17:09:49 +08002961 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002962 struct xfrm_policy_hash *htab;
2963
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002964 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002965 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002966 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002967
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002968 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002969 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002970 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002971 goto out_bydst;
2972 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002973 htab->dbits4 = 32;
2974 htab->sbits4 = 32;
2975 htab->dbits6 = 128;
2976 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002977 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002978 net->xfrm.policy_hthresh.lbits4 = 32;
2979 net->xfrm.policy_hthresh.rbits4 = 32;
2980 net->xfrm.policy_hthresh.lbits6 = 128;
2981 net->xfrm.policy_hthresh.rbits6 = 128;
2982
2983 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002984
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002985 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002986 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002987 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002988 if (net_eq(net, &init_net))
2989 register_netdevice_notifier(&xfrm_dev_notifier);
2990 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002991
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002992out_bydst:
2993 for (dir--; dir >= 0; dir--) {
2994 struct xfrm_policy_hash *htab;
2995
2996 htab = &net->xfrm.policy_bydst[dir];
2997 xfrm_hash_free(htab->table, sz);
2998 }
2999 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003000out_byidx:
3001 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003004static void xfrm_policy_fini(struct net *net)
3005{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003006 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003007 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003008
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003009 flush_work(&net->xfrm.policy_hash_work);
3010#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003011 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003012#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003013 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003014
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003015 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003016
Herbert Xu53c2e282014-11-13 17:09:49 +08003017 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003018 struct xfrm_policy_hash *htab;
3019
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003020 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003021
3022 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003023 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003024 WARN_ON(!hlist_empty(htab->table));
3025 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003026 }
3027
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003028 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003029 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3030 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003031}
3032
3033static int __net_init xfrm_net_init(struct net *net)
3034{
3035 int rv;
3036
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003037 rv = xfrm_statistics_init(net);
3038 if (rv < 0)
3039 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003040 rv = xfrm_state_init(net);
3041 if (rv < 0)
3042 goto out_state;
3043 rv = xfrm_policy_init(net);
3044 if (rv < 0)
3045 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003046 rv = xfrm_sysctl_init(net);
3047 if (rv < 0)
3048 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003049 rv = flow_cache_init(net);
3050 if (rv < 0)
3051 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003052
3053 /* Initialize the per-net locks here */
3054 spin_lock_init(&net->xfrm.xfrm_state_lock);
3055 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003056 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3057
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003058 return 0;
3059
Steffen Klassert4a93f502014-03-12 09:43:17 +01003060out:
3061 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003062out_sysctl:
3063 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003064out_policy:
3065 xfrm_state_fini(net);
3066out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003067 xfrm_statistics_fini(net);
3068out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003069 return rv;
3070}
3071
3072static void __net_exit xfrm_net_exit(struct net *net)
3073{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003074 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003075 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003076 xfrm_policy_fini(net);
3077 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003078 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003079}
3080
3081static struct pernet_operations __net_initdata xfrm_net_ops = {
3082 .init = xfrm_net_init,
3083 .exit = xfrm_net_exit,
3084};
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086void __init xfrm_init(void)
3087{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003088 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 xfrm_input_init();
3090}
3091
Joy Lattenab5f5e82007-09-17 11:51:22 -07003092#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003093static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3094 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003095{
Paul Moore875179f2007-12-01 23:27:18 +11003096 struct xfrm_sec_ctx *ctx = xp->security;
3097 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003098
Paul Moore875179f2007-12-01 23:27:18 +11003099 if (ctx)
3100 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3101 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3102
Weilong Chen9b7a7872013-12-24 09:43:46 +08003103 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003104 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003105 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003106 if (sel->prefixlen_s != 32)
3107 audit_log_format(audit_buf, " src_prefixlen=%d",
3108 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003109 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003110 if (sel->prefixlen_d != 32)
3111 audit_log_format(audit_buf, " dst_prefixlen=%d",
3112 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003113 break;
3114 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003115 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003116 if (sel->prefixlen_s != 128)
3117 audit_log_format(audit_buf, " src_prefixlen=%d",
3118 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003119 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003120 if (sel->prefixlen_d != 128)
3121 audit_log_format(audit_buf, " dst_prefixlen=%d",
3122 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003123 break;
3124 }
3125}
3126
Tetsuo Handa2e710292014-04-22 21:48:30 +09003127void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003128{
3129 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003130
Paul Mooreafeb14b2007-12-21 14:58:11 -08003131 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003132 if (audit_buf == NULL)
3133 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003134 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003135 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003136 xfrm_audit_common_policyinfo(xp, audit_buf);
3137 audit_log_end(audit_buf);
3138}
3139EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3140
Paul Moore68277ac2007-12-20 20:49:33 -08003141void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003142 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003143{
3144 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003145
Paul Mooreafeb14b2007-12-21 14:58:11 -08003146 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003147 if (audit_buf == NULL)
3148 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003149 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003150 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151 xfrm_audit_common_policyinfo(xp, audit_buf);
3152 audit_log_end(audit_buf);
3153}
3154EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3155#endif
3156
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003157#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003158static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3159 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003160{
3161 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3162 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003163 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3164 sel_cmp->family) &&
3165 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3166 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003167 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3168 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003169 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003170 }
3171 } else {
3172 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003173 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003174 }
3175 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003176 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003177}
3178
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003179static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3180 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003181{
3182 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003183 struct hlist_head *chain;
3184 u32 priority = ~0U;
3185
Fan Du283bc9f2013-11-07 17:47:50 +08003186 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003187 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003188 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003189 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3190 pol->type == type) {
3191 ret = pol;
3192 priority = ret->priority;
3193 break;
3194 }
3195 }
Fan Du8d549c42013-11-07 17:47:49 +08003196 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003197 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003198 if ((pol->priority >= priority) && ret)
3199 break;
3200
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003201 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003202 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003203 ret = pol;
3204 break;
3205 }
3206 }
3207
Li RongQing586f2eb2015-04-30 17:13:41 +08003208 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003209
Fan Du283bc9f2013-11-07 17:47:50 +08003210 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003211
3212 return ret;
3213}
3214
David S. Millerdd701752011-02-24 00:21:08 -05003215static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003216{
3217 int match = 0;
3218
3219 if (t->mode == m->mode && t->id.proto == m->proto &&
3220 (m->reqid == 0 || t->reqid == m->reqid)) {
3221 switch (t->mode) {
3222 case XFRM_MODE_TUNNEL:
3223 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003224 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3225 m->old_family) &&
3226 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3227 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003228 match = 1;
3229 }
3230 break;
3231 case XFRM_MODE_TRANSPORT:
3232 /* in case of transport mode, template does not store
3233 any IP addresses, hence we just compare mode and
3234 protocol */
3235 match = 1;
3236 break;
3237 default:
3238 break;
3239 }
3240 }
3241 return match;
3242}
3243
3244/* update endpoint address(es) of template(s) */
3245static int xfrm_policy_migrate(struct xfrm_policy *pol,
3246 struct xfrm_migrate *m, int num_migrate)
3247{
3248 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003249 int i, j, n = 0;
3250
3251 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003252 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003253 /* target policy has been deleted */
3254 write_unlock_bh(&pol->lock);
3255 return -ENOENT;
3256 }
3257
3258 for (i = 0; i < pol->xfrm_nr; i++) {
3259 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3260 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3261 continue;
3262 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003263 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3264 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003265 continue;
3266 /* update endpoints */
3267 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3268 sizeof(pol->xfrm_vec[i].id.daddr));
3269 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3270 sizeof(pol->xfrm_vec[i].saddr));
3271 pol->xfrm_vec[i].encap_family = mp->new_family;
3272 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003273 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003274 }
3275 }
3276
3277 write_unlock_bh(&pol->lock);
3278
3279 if (!n)
3280 return -ENODATA;
3281
3282 return 0;
3283}
3284
David S. Millerdd701752011-02-24 00:21:08 -05003285static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003286{
3287 int i, j;
3288
3289 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3290 return -EINVAL;
3291
3292 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003293 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3294 m[i].old_family) &&
3295 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3296 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003297 return -EINVAL;
3298 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3299 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3300 return -EINVAL;
3301
3302 /* check if there is any duplicated entry */
3303 for (j = i + 1; j < num_migrate; j++) {
3304 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3305 sizeof(m[i].old_daddr)) &&
3306 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3307 sizeof(m[i].old_saddr)) &&
3308 m[i].proto == m[j].proto &&
3309 m[i].mode == m[j].mode &&
3310 m[i].reqid == m[j].reqid &&
3311 m[i].old_family == m[j].old_family)
3312 return -EINVAL;
3313 }
3314 }
3315
3316 return 0;
3317}
3318
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003319int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003320 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003321 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003322{
3323 int i, err, nx_cur = 0, nx_new = 0;
3324 struct xfrm_policy *pol = NULL;
3325 struct xfrm_state *x, *xc;
3326 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3327 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3328 struct xfrm_migrate *mp;
3329
3330 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3331 goto out;
3332
3333 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003334 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003335 err = -ENOENT;
3336 goto out;
3337 }
3338
3339 /* Stage 2 - find and update state(s) */
3340 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003341 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003342 x_cur[nx_cur] = x;
3343 nx_cur++;
3344 if ((xc = xfrm_state_migrate(x, mp))) {
3345 x_new[nx_new] = xc;
3346 nx_new++;
3347 } else {
3348 err = -ENODATA;
3349 goto restore_state;
3350 }
3351 }
3352 }
3353
3354 /* Stage 3 - update policy */
3355 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3356 goto restore_state;
3357
3358 /* Stage 4 - delete old state(s) */
3359 if (nx_cur) {
3360 xfrm_states_put(x_cur, nx_cur);
3361 xfrm_states_delete(x_cur, nx_cur);
3362 }
3363
3364 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003365 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003366
3367 xfrm_pol_put(pol);
3368
3369 return 0;
3370out:
3371 return err;
3372
3373restore_state:
3374 if (pol)
3375 xfrm_pol_put(pol);
3376 if (nx_cur)
3377 xfrm_states_put(x_cur, nx_cur);
3378 if (nx_new)
3379 xfrm_states_delete(x_new, nx_new);
3380
3381 return err;
3382}
David S. Millere610e672007-02-08 13:29:15 -08003383EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003384#endif