blob: d8c35ad4210daabf773d9b8ae1f52d9b2b3d5176 [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
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800118static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500119 const xfrm_address_t *saddr,
120 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900121 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800123 struct xfrm_policy_afinfo *afinfo;
124 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Herbert Xu25ee3282007-12-11 09:32:34 -0800126 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800128 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800130 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900133
134 return dst;
135}
136
137static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
138 xfrm_address_t *prev_saddr,
139 xfrm_address_t *prev_daddr,
140 int family)
141{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800142 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900143 xfrm_address_t *saddr = &x->props.saddr;
144 xfrm_address_t *daddr = &x->id.daddr;
145 struct dst_entry *dst;
146
147 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
148 saddr = x->coaddr;
149 daddr = prev_daddr;
150 }
151 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
152 saddr = prev_saddr;
153 daddr = x->coaddr;
154 }
155
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800156 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900157
158 if (!IS_ERR(dst)) {
159 if (prev_saddr != saddr)
160 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
161 if (prev_daddr != daddr)
162 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
163 }
164
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800165 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static inline unsigned long make_jiffies(long secs)
169{
170 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
171 return MAX_SCHEDULE_TIMEOUT-1;
172 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900173 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176static void xfrm_policy_timer(unsigned long data)
177{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800178 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800179 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 long next = LONG_MAX;
181 int warn = 0;
182 int dir;
183
184 read_lock(&xp->lock);
185
Timo Teräsea2dea92010-03-31 00:17:05 +0000186 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto out;
188
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700189 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (xp->lft.hard_add_expires_seconds) {
192 long tmo = xp->lft.hard_add_expires_seconds +
193 xp->curlft.add_time - now;
194 if (tmo <= 0)
195 goto expired;
196 if (tmo < next)
197 next = tmo;
198 }
199 if (xp->lft.hard_use_expires_seconds) {
200 long tmo = xp->lft.hard_use_expires_seconds +
201 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
202 if (tmo <= 0)
203 goto expired;
204 if (tmo < next)
205 next = tmo;
206 }
207 if (xp->lft.soft_add_expires_seconds) {
208 long tmo = xp->lft.soft_add_expires_seconds +
209 xp->curlft.add_time - now;
210 if (tmo <= 0) {
211 warn = 1;
212 tmo = XFRM_KM_TIMEOUT;
213 }
214 if (tmo < next)
215 next = tmo;
216 }
217 if (xp->lft.soft_use_expires_seconds) {
218 long tmo = xp->lft.soft_use_expires_seconds +
219 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
220 if (tmo <= 0) {
221 warn = 1;
222 tmo = XFRM_KM_TIMEOUT;
223 }
224 if (tmo < next)
225 next = tmo;
226 }
227
228 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800229 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (next != LONG_MAX &&
231 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
232 xfrm_pol_hold(xp);
233
234out:
235 read_unlock(&xp->lock);
236 xfrm_pol_put(xp);
237 return;
238
239expired:
240 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700241 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800242 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xfrm_pol_put(xp);
244}
245
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000246static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
247{
248 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
249
250 if (unlikely(pol->walk.dead))
251 flo = NULL;
252 else
253 xfrm_pol_hold(pol);
254
255 return flo;
256}
257
258static int xfrm_policy_flo_check(struct flow_cache_object *flo)
259{
260 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
261
262 return !pol->walk.dead;
263}
264
265static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
266{
267 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
268}
269
270static const struct flow_cache_ops xfrm_policy_fc_ops = {
271 .get = xfrm_policy_flo_get,
272 .check = xfrm_policy_flo_check,
273 .delete = xfrm_policy_flo_delete,
274};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
277 * SPD calls.
278 */
279
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800280struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct xfrm_policy *policy;
283
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700284 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800287 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700288 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700289 INIT_HLIST_NODE(&policy->bydst);
290 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700292 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100293 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800294 setup_timer(&policy->timer, xfrm_policy_timer,
295 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100296 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
297 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000298 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 return policy;
301}
302EXPORT_SYMBOL(xfrm_policy_alloc);
303
304/* Destroy xfrm_policy: descendant resources must be released to this moment. */
305
WANG Cong64c31b32008-01-07 22:34:29 -0800306void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Herbert Xu12a169e2008-10-01 07:03:24 -0700308 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Fan Du0659eea2013-08-01 18:08:36 +0800310 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 BUG();
312
Paul Moore03e1ad72008-04-12 19:07:52 -0700313 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 kfree(policy);
315}
WANG Cong64c31b32008-01-07 22:34:29 -0800316EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* Rule must be locked. Release descentant resources, announce
319 * entry dead. The rule must be unlinked from lists to the moment.
320 */
321
322static void xfrm_policy_kill(struct xfrm_policy *policy)
323{
Herbert Xu12a169e2008-10-01 07:03:24 -0700324 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Timo Teräs285ead12010-04-07 00:30:06 +0000326 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200328 if (del_timer(&policy->polq.hold_timer))
329 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800330 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100331
Timo Teräs285ead12010-04-07 00:30:06 +0000332 if (del_timer(&policy->timer))
333 xfrm_pol_put(policy);
334
335 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
David S. Miller2518c7c2006-08-24 04:45:07 -0700338static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
339
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800340static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700341{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800342 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700343}
344
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200345/* calculate policy hash thresholds */
346static void __get_hash_thresh(struct net *net,
347 unsigned short family, int dir,
348 u8 *dbits, u8 *sbits)
349{
350 switch (family) {
351 case AF_INET:
352 *dbits = net->xfrm.policy_bydst[dir].dbits4;
353 *sbits = net->xfrm.policy_bydst[dir].sbits4;
354 break;
355
356 case AF_INET6:
357 *dbits = net->xfrm.policy_bydst[dir].dbits6;
358 *sbits = net->xfrm.policy_bydst[dir].sbits6;
359 break;
360
361 default:
362 *dbits = 0;
363 *sbits = 0;
364 }
365}
366
David S. Miller5f803b52011-02-24 00:33:19 -0500367static struct hlist_head *policy_hash_bysel(struct net *net,
368 const struct xfrm_selector *sel,
369 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700370{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800371 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200372 unsigned int hash;
373 u8 dbits;
374 u8 sbits;
375
376 __get_hash_thresh(net, family, dir, &dbits, &sbits);
377 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700378
379 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800380 &net->xfrm.policy_inexact[dir] :
381 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700382}
383
David S. Miller5f803b52011-02-24 00:33:19 -0500384static struct hlist_head *policy_hash_direct(struct net *net,
385 const xfrm_address_t *daddr,
386 const xfrm_address_t *saddr,
387 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700388{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800389 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200390 unsigned int hash;
391 u8 dbits;
392 u8 sbits;
393
394 __get_hash_thresh(net, family, dir, &dbits, &sbits);
395 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700396
Alexey Dobriyan11219942008-11-25 17:33:06 -0800397 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700398}
399
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200400static void xfrm_dst_hash_transfer(struct net *net,
401 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700402 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200403 unsigned int nhashmask,
404 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700405{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800406 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800408 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200409 u8 dbits;
410 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700411
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800412redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800413 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700414 unsigned int h;
415
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200416 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700417 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800419 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800420 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421 hlist_add_head(&pol->bydst, ndsttable+h);
422 h0 = h;
423 } else {
424 if (h != h0)
425 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800426 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700427 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800428 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800429 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430 }
431 if (!hlist_empty(list)) {
432 entry0 = NULL;
433 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700434 }
435}
436
437static void xfrm_idx_hash_transfer(struct hlist_head *list,
438 struct hlist_head *nidxtable,
439 unsigned int nhashmask)
440{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800441 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700442 struct xfrm_policy *pol;
443
Sasha Levinb67bfe02013-02-27 17:06:00 -0800444 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700445 unsigned int h;
446
447 h = __idx_hash(pol->index, nhashmask);
448 hlist_add_head(&pol->byidx, nidxtable+h);
449 }
450}
451
452static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
453{
454 return ((old_hmask + 1) << 1) - 1;
455}
456
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800457static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700458{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800459 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
461 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800462 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700463 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700464 int i;
465
466 if (!ndst)
467 return;
468
Fan Du283bc9f2013-11-07 17:47:50 +0800469 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700470
471 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200472 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700473
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800474 net->xfrm.policy_bydst[dir].table = ndst;
475 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700476
Fan Du283bc9f2013-11-07 17:47:50 +0800477 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
David S. Miller44e36b42006-08-24 04:50:50 -0700479 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700480}
481
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800482static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700483{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800484 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700485 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
486 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800487 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700488 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700489 int i;
490
491 if (!nidx)
492 return;
493
Fan Du283bc9f2013-11-07 17:47:50 +0800494 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700495
496 for (i = hmask; i >= 0; i--)
497 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
498
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800499 net->xfrm.policy_byidx = nidx;
500 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700501
Fan Du283bc9f2013-11-07 17:47:50 +0800502 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
David S. Miller44e36b42006-08-24 04:50:50 -0700504 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700505}
506
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800507static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700508{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800509 unsigned int cnt = net->xfrm.policy_count[dir];
510 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700511
512 if (total)
513 *total += cnt;
514
515 if ((hmask + 1) < xfrm_policy_hashmax &&
516 cnt > hmask)
517 return 1;
518
519 return 0;
520}
521
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800522static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700523{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700525
526 if ((hmask + 1) < xfrm_policy_hashmax &&
527 total > hmask)
528 return 1;
529
530 return 0;
531}
532
Alexey Dobriyane0710412010-01-23 13:37:10 +0000533void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700534{
Fan Du283bc9f2013-11-07 17:47:50 +0800535 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000536 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
537 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
538 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
539 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
540 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
541 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
542 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700543 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800544 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700545}
546EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700547
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700548static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800549static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700550{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800551 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700552 int dir, total;
553
554 mutex_lock(&hash_resize_mutex);
555
556 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800557 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800558 if (xfrm_bydst_should_resize(net, dir, &total))
559 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800561 if (xfrm_byidx_should_resize(net, total))
562 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700563
564 mutex_unlock(&hash_resize_mutex);
565}
566
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200567static void xfrm_hash_rebuild(struct work_struct *work)
568{
569 struct net *net = container_of(work, struct net,
570 xfrm.policy_hthresh.work);
571 unsigned int hmask;
572 struct xfrm_policy *pol;
573 struct xfrm_policy *policy;
574 struct hlist_head *chain;
575 struct hlist_head *odst;
576 struct hlist_node *newpos;
577 int i;
578 int dir;
579 unsigned seq;
580 u8 lbits4, rbits4, lbits6, rbits6;
581
582 mutex_lock(&hash_resize_mutex);
583
584 /* read selector prefixlen thresholds */
585 do {
586 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
587
588 lbits4 = net->xfrm.policy_hthresh.lbits4;
589 rbits4 = net->xfrm.policy_hthresh.rbits4;
590 lbits6 = net->xfrm.policy_hthresh.lbits6;
591 rbits6 = net->xfrm.policy_hthresh.rbits6;
592 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
593
594 write_lock_bh(&net->xfrm.xfrm_policy_lock);
595
596 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800597 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200598 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
599 hmask = net->xfrm.policy_bydst[dir].hmask;
600 odst = net->xfrm.policy_bydst[dir].table;
601 for (i = hmask; i >= 0; i--)
602 INIT_HLIST_HEAD(odst + i);
603 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
604 /* dir out => dst = remote, src = local */
605 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
606 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
607 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
608 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
609 } else {
610 /* dir in/fwd => dst = local, src = remote */
611 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
612 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
613 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
614 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
615 }
616 }
617
618 /* re-insert all policies by order of creation */
619 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
620 newpos = NULL;
621 chain = policy_hash_bysel(net, &policy->selector,
622 policy->family,
623 xfrm_policy_id2dir(policy->index));
624 hlist_for_each_entry(pol, chain, bydst) {
625 if (policy->priority >= pol->priority)
626 newpos = &pol->bydst;
627 else
628 break;
629 }
630 if (newpos)
631 hlist_add_behind(&policy->bydst, newpos);
632 else
633 hlist_add_head(&policy->bydst, chain);
634 }
635
636 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
637
638 mutex_unlock(&hash_resize_mutex);
639}
640
641void xfrm_policy_hash_rebuild(struct net *net)
642{
643 schedule_work(&net->xfrm.policy_hthresh.work);
644}
645EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/* Generate new index... KAME seems to generate them ordered by cost
648 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800649static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 static u32 idx_generator;
652
653 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700654 struct hlist_head *list;
655 struct xfrm_policy *p;
656 u32 idx;
657 int found;
658
Fan Due682adf2013-11-07 17:47:48 +0800659 if (!index) {
660 idx = (idx_generator | dir);
661 idx_generator += 8;
662 } else {
663 idx = index;
664 index = 0;
665 }
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (idx == 0)
668 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800669 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700670 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800671 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700672 if (p->index == idx) {
673 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return idx;
679 }
680}
681
David S. Miller2518c7c2006-08-24 04:45:07 -0700682static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
683{
684 u32 *p1 = (u32 *) s1;
685 u32 *p2 = (u32 *) s2;
686 int len = sizeof(struct xfrm_selector) / sizeof(u32);
687 int i;
688
689 for (i = 0; i < len; i++) {
690 if (p1[i] != p2[i])
691 return 1;
692 }
693
694 return 0;
695}
696
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100697static void xfrm_policy_requeue(struct xfrm_policy *old,
698 struct xfrm_policy *new)
699{
700 struct xfrm_policy_queue *pq = &old->polq;
701 struct sk_buff_head list;
702
703 __skb_queue_head_init(&list);
704
705 spin_lock_bh(&pq->hold_queue.lock);
706 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200707 if (del_timer(&pq->hold_timer))
708 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100709 spin_unlock_bh(&pq->hold_queue.lock);
710
711 if (skb_queue_empty(&list))
712 return;
713
714 pq = &new->polq;
715
716 spin_lock_bh(&pq->hold_queue.lock);
717 skb_queue_splice(&list, &pq->hold_queue);
718 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200719 if (!mod_timer(&pq->hold_timer, jiffies))
720 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100721 spin_unlock_bh(&pq->hold_queue.lock);
722}
723
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100724static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
725 struct xfrm_policy *pol)
726{
727 u32 mark = policy->mark.v & policy->mark.m;
728
729 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
730 return true;
731
732 if ((mark & pol->mark.m) == pol->mark.v &&
733 policy->priority == pol->priority)
734 return true;
735
736 return false;
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
740{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800741 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700742 struct xfrm_policy *pol;
743 struct xfrm_policy *delpol;
744 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800745 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Fan Du283bc9f2013-11-07 17:47:50 +0800747 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800748 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 delpol = NULL;
750 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800751 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800752 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700753 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100754 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800755 xfrm_sec_ctx_match(pol->security, policy->security) &&
756 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800758 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -EEXIST;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 delpol = pol;
762 if (policy->priority > pol->priority)
763 continue;
764 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800765 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 continue;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (delpol)
769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700772 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700773 else
774 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800775 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800776 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800777
778 /* After previous checking, family can either be AF_INET or AF_INET6 */
779 if (policy->family == AF_INET)
780 rt_genid_bump_ipv4(net);
781 else
782 rt_genid_bump_ipv6(net);
783
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100784 if (delpol) {
785 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800786 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100787 }
Fan Due682adf2013-11-07 17:47:48 +0800788 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800789 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800790 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 policy->curlft.use_time = 0;
792 if (!mod_timer(&policy->timer, jiffies + HZ))
793 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800794 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
David S. Miller9b78a822005-12-22 07:39:48 -0800796 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800798 else if (xfrm_bydst_should_resize(net, dir, NULL))
799 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803EXPORT_SYMBOL(xfrm_policy_insert);
804
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000805struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
806 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800807 struct xfrm_sec_ctx *ctx, int delete,
808 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
David S. Miller2518c7c2006-08-24 04:45:07 -0700810 struct xfrm_policy *pol, *ret;
811 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Eric Parisef41aaa2007-03-07 15:37:58 -0800813 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800814 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800815 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700816 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800817 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700818 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000819 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700820 !selector_cmp(sel, &pol->selector) &&
821 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700823 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700824 *err = security_xfrm_policy_delete(
825 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800826 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800827 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800828 return pol;
829 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800830 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700831 }
832 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 break;
834 }
835 }
Fan Du283bc9f2013-11-07 17:47:50 +0800836 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000838 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700839 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
Trent Jaegerdf718372005-12-13 23:12:27 -0800842EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000844struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
845 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
David S. Miller2518c7c2006-08-24 04:45:07 -0700847 struct xfrm_policy *pol, *ret;
848 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Herbert Xub5505c62007-05-14 02:15:47 -0700850 *err = -ENOENT;
851 if (xfrm_policy_id2dir(id) != dir)
852 return NULL;
853
Eric Parisef41aaa2007-03-07 15:37:58 -0800854 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800855 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800856 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700857 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800858 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000859 if (pol->type == type && pol->index == id &&
860 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700863 *err = security_xfrm_policy_delete(
864 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800865 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800866 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800867 return pol;
868 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800869 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700870 }
871 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873 }
874 }
Fan Du283bc9f2013-11-07 17:47:50 +0800875 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000877 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700878 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700879 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881EXPORT_SYMBOL(xfrm_policy_byid);
882
Joy Latten4aa2e622007-06-04 19:05:57 -0400883#ifdef CONFIG_SECURITY_NETWORK_XFRM
884static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900885xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Joy Latten4aa2e622007-06-04 19:05:57 -0400887 int dir, err = 0;
888
889 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
890 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400891 int i;
892
Sasha Levinb67bfe02013-02-27 17:06:00 -0800893 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800894 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400895 if (pol->type != type)
896 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700897 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400898 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900899 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400900 return err;
901 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900902 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800903 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800904 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800905 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400906 bydst) {
907 if (pol->type != type)
908 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700909 err = security_xfrm_policy_delete(
910 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400911 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700912 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900913 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400914 return err;
915 }
916 }
917 }
918 }
919 return err;
920}
921#else
922static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900923xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400924{
925 return 0;
926}
927#endif
928
Tetsuo Handa2e710292014-04-22 21:48:30 +0900929int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400930{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000931 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Fan Du283bc9f2013-11-07 17:47:50 +0800933 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400934
Tetsuo Handa2e710292014-04-22 21:48:30 +0900935 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400936 if (err)
937 goto out;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700940 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800941 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700942
943 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800944 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800945 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700946 if (pol->type != type)
947 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000948 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800949 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000950 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Tetsuo Handa2e710292014-04-22 21:48:30 +0900952 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600953
David S. Miller2518c7c2006-08-24 04:45:07 -0700954 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Fan Du283bc9f2013-11-07 17:47:50 +0800956 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700957 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700959
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800960 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700961 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800962 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800963 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700964 bydst) {
965 if (pol->type != type)
966 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000967 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800968 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000969 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700970
Tetsuo Handa2e710292014-04-22 21:48:30 +0900971 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700972 xfrm_policy_kill(pol);
973
Fan Du283bc9f2013-11-07 17:47:50 +0800974 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700975 goto again2;
976 }
977 }
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000980 if (!cnt)
981 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400982out:
Fan Du283bc9f2013-11-07 17:47:50 +0800983 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986EXPORT_SYMBOL(xfrm_policy_flush);
987
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800988int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800989 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 void *data)
991{
Herbert Xu12a169e2008-10-01 07:03:24 -0700992 struct xfrm_policy *pol;
993 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800994 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Timo Teras4c563f72008-02-28 21:31:08 -0800996 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
997 walk->type != XFRM_POLICY_TYPE_ANY)
998 return -EINVAL;
999
Herbert Xu12a169e2008-10-01 07:03:24 -07001000 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001001 return 0;
1002
Fan Du283bc9f2013-11-07 17:47:50 +08001003 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001004 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001005 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001006 else
1007 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001008 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001009 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001010 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001011 pol = container_of(x, struct xfrm_policy, walk);
1012 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1013 walk->type != pol->type)
1014 continue;
1015 error = func(pol, xfrm_policy_id2dir(pol->index),
1016 walk->seq, data);
1017 if (error) {
1018 list_move_tail(&walk->walk.all, &x->all);
1019 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001020 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001021 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001023 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001024 error = -ENOENT;
1025 goto out;
1026 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001027 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028out:
Fan Du283bc9f2013-11-07 17:47:50 +08001029 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return error;
1031}
1032EXPORT_SYMBOL(xfrm_policy_walk);
1033
Herbert Xu12a169e2008-10-01 07:03:24 -07001034void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1035{
1036 INIT_LIST_HEAD(&walk->walk.all);
1037 walk->walk.dead = 1;
1038 walk->type = type;
1039 walk->seq = 0;
1040}
1041EXPORT_SYMBOL(xfrm_policy_walk_init);
1042
Fan Du283bc9f2013-11-07 17:47:50 +08001043void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001044{
1045 if (list_empty(&walk->walk.all))
1046 return;
1047
Fan Du283bc9f2013-11-07 17:47:50 +08001048 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001049 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001050 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001051}
1052EXPORT_SYMBOL(xfrm_policy_walk_done);
1053
James Morris134b0fc2006-10-05 15:42:27 -05001054/*
1055 * Find policy to apply to this flow.
1056 *
1057 * Returns 0 if policy found, else an -errno.
1058 */
David S. Millerf299d552011-02-24 01:23:30 -05001059static int xfrm_policy_match(const struct xfrm_policy *pol,
1060 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001061 u8 type, u16 family, int dir)
1062{
David S. Millerf299d552011-02-24 01:23:30 -05001063 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001064 int ret = -ESRCH;
1065 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001066
1067 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001068 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001069 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001070 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001071
1072 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001073 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001074 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001075 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001076
James Morris134b0fc2006-10-05 15:42:27 -05001077 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001078}
1079
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001080static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001081 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001082 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
James Morris134b0fc2006-10-05 15:42:27 -05001084 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001085 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001086 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001087 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001088 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001089
1090 daddr = xfrm_flowi_daddr(fl, family);
1091 saddr = xfrm_flowi_saddr(fl, family);
1092 if (unlikely(!daddr || !saddr))
1093 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Fan Du283bc9f2013-11-07 17:47:50 +08001095 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001096 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001097 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001098 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001099 err = xfrm_policy_match(pol, fl, type, family, dir);
1100 if (err) {
1101 if (err == -ESRCH)
1102 continue;
1103 else {
1104 ret = ERR_PTR(err);
1105 goto fail;
1106 }
1107 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001108 ret = pol;
1109 priority = ret->priority;
1110 break;
1111 }
1112 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001113 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001114 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001115 err = xfrm_policy_match(pol, fl, type, family, dir);
1116 if (err) {
1117 if (err == -ESRCH)
1118 continue;
1119 else {
1120 ret = ERR_PTR(err);
1121 goto fail;
1122 }
1123 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001124 ret = pol;
1125 break;
1126 }
1127 }
David S. Milleracba48e2006-08-25 15:46:46 -07001128 if (ret)
1129 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001130fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001131 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001132
David S. Miller2518c7c2006-08-24 04:45:07 -07001133 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001134}
1135
Timo Teräs80c802f2010-04-07 00:30:05 +00001136static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001137__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001138{
1139#ifdef CONFIG_XFRM_SUB_POLICY
1140 struct xfrm_policy *pol;
1141
1142 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1143 if (pol != NULL)
1144 return pol;
1145#endif
1146 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1147}
1148
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001149static int flow_to_policy_dir(int dir)
1150{
1151 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1152 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1153 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1154 return dir;
1155
1156 switch (dir) {
1157 default:
1158 case FLOW_DIR_IN:
1159 return XFRM_POLICY_IN;
1160 case FLOW_DIR_OUT:
1161 return XFRM_POLICY_OUT;
1162 case FLOW_DIR_FWD:
1163 return XFRM_POLICY_FWD;
1164 }
1165}
1166
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001167static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001168xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001169 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001170{
1171 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001172
1173 if (old_obj)
1174 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001175
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001176 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001177 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001178 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001179
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001180 /* Resolver returns two references:
1181 * one for cache and one for caller of flow_cache_lookup() */
1182 xfrm_pol_hold(pol);
1183
1184 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Trent Jaegerdf718372005-12-13 23:12:27 -08001187static inline int policy_to_flow_dir(int dir)
1188{
1189 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001190 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1191 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1192 return dir;
1193 switch (dir) {
1194 default:
1195 case XFRM_POLICY_IN:
1196 return FLOW_DIR_IN;
1197 case XFRM_POLICY_OUT:
1198 return FLOW_DIR_OUT;
1199 case XFRM_POLICY_FWD:
1200 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001201 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001202}
1203
David S. Millerdee9f4b2011-02-22 18:44:31 -08001204static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1205 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001208 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Fan Du283bc9f2013-11-07 17:47:50 +08001210 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001212 bool match = xfrm_selector_match(&pol->selector, fl,
1213 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001214 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001215
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001216 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001217 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1218 pol = NULL;
1219 goto out;
1220 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001221 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001222 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001223 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001224 if (!err)
1225 xfrm_pol_hold(pol);
1226 else if (err == -ESRCH)
1227 pol = NULL;
1228 else
1229 pol = ERR_PTR(err);
1230 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 pol = NULL;
1232 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001233out:
Fan Du283bc9f2013-11-07 17:47:50 +08001234 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return pol;
1236}
1237
1238static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1239{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001240 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001241
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001242 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001243 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 xfrm_pol_hold(pol);
1245}
1246
1247static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1248 int dir)
1249{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001250 struct net *net = xp_net(pol);
1251
Herbert Xu53c2e282014-11-13 17:09:49 +08001252 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Herbert Xu53c2e282014-11-13 17:09:49 +08001255 /* Socket policies are not hashed. */
1256 if (!hlist_unhashed(&pol->bydst)) {
1257 hlist_del(&pol->bydst);
1258 hlist_del(&pol->byidx);
1259 }
1260
1261 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001262 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001263
1264 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Herbert Xu53c2e282014-11-13 17:09:49 +08001267static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1268{
1269 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1270}
1271
1272static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1273{
1274 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1275}
1276
Herbert Xu4666faa2005-06-18 22:43:22 -07001277int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Fan Du283bc9f2013-11-07 17:47:50 +08001279 struct net *net = xp_net(pol);
1280
1281 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001283 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001288 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
David S. Millera70fcb02006-03-20 19:18:52 -08001290EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1293{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001294 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct xfrm_policy *old_pol;
1296
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001297#ifdef CONFIG_XFRM_SUB_POLICY
1298 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1299 return -EINVAL;
1300#endif
1301
Fan Du283bc9f2013-11-07 17:47:50 +08001302 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 old_pol = sk->sk_policy[dir];
1304 sk->sk_policy[dir] = pol;
1305 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001306 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001307 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001308 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001310 if (old_pol) {
1311 if (pol)
1312 xfrm_policy_requeue(old_pol, pol);
1313
Timo Teräsea2dea92010-03-31 00:17:05 +00001314 /* Unlinking succeeds always. This is the only function
1315 * allowed to delete or replace socket policy.
1316 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001317 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001318 }
Fan Du283bc9f2013-11-07 17:47:50 +08001319 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (old_pol) {
1322 xfrm_policy_kill(old_pol);
1323 }
1324 return 0;
1325}
1326
David S. Millerd3e40a92011-02-24 01:25:41 -05001327static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001329 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001330 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (newp) {
1333 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001334 if (security_xfrm_policy_clone(old->security,
1335 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001336 kfree(newp);
1337 return NULL; /* ENOMEM */
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 newp->lft = old->lft;
1340 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001341 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 newp->action = old->action;
1343 newp->flags = old->flags;
1344 newp->xfrm_nr = old->xfrm_nr;
1345 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001346 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 memcpy(newp->xfrm_vec, old->xfrm_vec,
1348 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001349 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001350 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001351 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 xfrm_pol_put(newp);
1353 }
1354 return newp;
1355}
1356
1357int __xfrm_sk_clone_policy(struct sock *sk)
1358{
1359 struct xfrm_policy *p0 = sk->sk_policy[0],
1360 *p1 = sk->sk_policy[1];
1361
1362 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1363 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1364 return -ENOMEM;
1365 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1366 return -ENOMEM;
1367 return 0;
1368}
1369
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001370static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001371xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001372 unsigned short family)
1373{
1374 int err;
1375 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1376
1377 if (unlikely(afinfo == NULL))
1378 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001379 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001380 xfrm_policy_put_afinfo(afinfo);
1381 return err;
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/* Resolve list of templates for the flow, given policy. */
1385
1386static int
David S. Millera6c2e612011-02-22 18:35:39 -08001387xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1388 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001390 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int nx;
1392 int i, error;
1393 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1394 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001395 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Weilong Chen9b7a7872013-12-24 09:43:46 +08001397 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 struct xfrm_state *x;
1399 xfrm_address_t *remote = daddr;
1400 xfrm_address_t *local = saddr;
1401 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1402
Joakim Koskela48b8d782007-07-26 00:08:42 -07001403 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1404 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 remote = &tmpl->id.daddr;
1406 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001407 if (xfrm_addr_any(local, tmpl->encap_family)) {
1408 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001409 if (error)
1410 goto fail;
1411 local = &tmp;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1416
1417 if (x && x->km.state == XFRM_STATE_VALID) {
1418 xfrm[nx++] = x;
1419 daddr = remote;
1420 saddr = local;
1421 continue;
1422 }
1423 if (x) {
1424 error = (x->km.state == XFRM_STATE_ERROR ?
1425 -EINVAL : -EAGAIN);
1426 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001427 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001428 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (!tmpl->optional)
1432 goto fail;
1433 }
1434 return nx;
1435
1436fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001437 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 xfrm_state_put(xfrm[nx]);
1439 return error;
1440}
1441
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001442static int
David S. Millera6c2e612011-02-22 18:35:39 -08001443xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1444 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001445{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001446 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1447 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001448 int cnx = 0;
1449 int error;
1450 int ret;
1451 int i;
1452
1453 for (i = 0; i < npols; i++) {
1454 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1455 error = -ENOBUFS;
1456 goto fail;
1457 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001458
1459 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001460 if (ret < 0) {
1461 error = ret;
1462 goto fail;
1463 } else
1464 cnx += ret;
1465 }
1466
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001467 /* found states are sorted for outbound processing */
1468 if (npols > 1)
1469 xfrm_state_sort(xfrm, tpp, cnx, family);
1470
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001471 return cnx;
1472
1473 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001474 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001475 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001476 return error;
1477
1478}
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/* Check that the bundle accepts the flow and its components are
1481 * still valid.
1482 */
1483
David S. Miller05d84022011-02-22 17:47:10 -08001484static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001485{
1486 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1487 int tos;
1488
1489 if (!afinfo)
1490 return -EINVAL;
1491
1492 tos = afinfo->get_tos(fl);
1493
1494 xfrm_policy_put_afinfo(afinfo);
1495
1496 return tos;
1497}
1498
Timo Teräs80c802f2010-04-07 00:30:05 +00001499static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1500{
1501 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1502 struct dst_entry *dst = &xdst->u.dst;
1503
1504 if (xdst->route == NULL) {
1505 /* Dummy bundle - if it has xfrms we were not
1506 * able to build bundle as template resolution failed.
1507 * It means we need to try again resolving. */
1508 if (xdst->num_xfrms > 0)
1509 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001510 } else if (dst->flags & DST_XFRM_QUEUE) {
1511 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001512 } else {
1513 /* Real bundle */
1514 if (stale_bundle(dst))
1515 return NULL;
1516 }
1517
1518 dst_hold(dst);
1519 return flo;
1520}
1521
1522static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1523{
1524 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1525 struct dst_entry *dst = &xdst->u.dst;
1526
1527 if (!xdst->route)
1528 return 0;
1529 if (stale_bundle(dst))
1530 return 0;
1531
1532 return 1;
1533}
1534
1535static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1536{
1537 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1538 struct dst_entry *dst = &xdst->u.dst;
1539
1540 dst_free(dst);
1541}
1542
1543static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1544 .get = xfrm_bundle_flo_get,
1545 .check = xfrm_bundle_flo_check,
1546 .delete = xfrm_bundle_flo_delete,
1547};
1548
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001549static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001550{
1551 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001552 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001553 struct xfrm_dst *xdst;
1554
1555 if (!afinfo)
1556 return ERR_PTR(-EINVAL);
1557
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001558 switch (family) {
1559 case AF_INET:
1560 dst_ops = &net->xfrm.xfrm4_dst_ops;
1561 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001562#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001563 case AF_INET6:
1564 dst_ops = &net->xfrm.xfrm6_dst_ops;
1565 break;
1566#endif
1567 default:
1568 BUG();
1569 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001570 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001571
Madalin Bucurd4cae562011-09-26 07:04:36 +00001572 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001573 struct dst_entry *dst = &xdst->u.dst;
1574
1575 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001576 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001577 if (afinfo->init_dst)
1578 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001579 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001580 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001581
Madalin Bucurd4cae562011-09-26 07:04:36 +00001582 xfrm_policy_put_afinfo(afinfo);
1583
Herbert Xu25ee3282007-12-11 09:32:34 -08001584 return xdst;
1585}
1586
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001587static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1588 int nfheader_len)
1589{
1590 struct xfrm_policy_afinfo *afinfo =
1591 xfrm_policy_get_afinfo(dst->ops->family);
1592 int err;
1593
1594 if (!afinfo)
1595 return -EINVAL;
1596
1597 err = afinfo->init_path(path, dst, nfheader_len);
1598
1599 xfrm_policy_put_afinfo(afinfo);
1600
1601 return err;
1602}
1603
Herbert Xu87c1e122010-03-02 02:51:56 +00001604static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001605 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001606{
1607 struct xfrm_policy_afinfo *afinfo =
1608 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1609 int err;
1610
1611 if (!afinfo)
1612 return -EINVAL;
1613
Herbert Xu87c1e122010-03-02 02:51:56 +00001614 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001615
1616 xfrm_policy_put_afinfo(afinfo);
1617
1618 return err;
1619}
1620
Timo Teräs80c802f2010-04-07 00:30:05 +00001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1623 * all the metrics... Shortly, bundle a bundle.
1624 */
1625
Herbert Xu25ee3282007-12-11 09:32:34 -08001626static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1627 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001628 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001629 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001631 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001632 unsigned long now = jiffies;
1633 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001634 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001635 struct dst_entry *dst_prev = NULL;
1636 struct dst_entry *dst0 = NULL;
1637 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001639 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001640 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001641 int trailer_len = 0;
1642 int tos;
1643 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001644 xfrm_address_t saddr, daddr;
1645
1646 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001647
1648 tos = xfrm_get_tos(fl, family);
1649 err = tos;
1650 if (tos < 0)
1651 goto put_states;
1652
1653 dst_hold(dst);
1654
1655 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001656 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001657 struct dst_entry *dst1 = &xdst->u.dst;
1658
1659 err = PTR_ERR(xdst);
1660 if (IS_ERR(xdst)) {
1661 dst_release(dst);
1662 goto put_states;
1663 }
1664
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001665 if (xfrm[i]->sel.family == AF_UNSPEC) {
1666 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1667 xfrm_af2proto(family));
1668 if (!inner_mode) {
1669 err = -EAFNOSUPPORT;
1670 dst_release(dst);
1671 goto put_states;
1672 }
1673 } else
1674 inner_mode = xfrm[i]->inner_mode;
1675
Herbert Xu25ee3282007-12-11 09:32:34 -08001676 if (!dst_prev)
1677 dst0 = dst1;
1678 else {
1679 dst_prev->child = dst_clone(dst1);
1680 dst1->flags |= DST_NOHASH;
1681 }
1682
1683 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001684 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001685
1686 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1687 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001688 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1689 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001690 err = PTR_ERR(dst);
1691 if (IS_ERR(dst))
1692 goto put_states;
1693 } else
1694 dst_hold(dst);
1695
1696 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001697 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001698
David S. Millerf5b0a872012-07-19 12:31:33 -07001699 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001700 dst1->flags |= DST_HOST;
1701 dst1->lastuse = now;
1702
1703 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001704 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001705
1706 dst1->next = dst_prev;
1707 dst_prev = dst1;
1708
1709 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001710 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1711 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001712 trailer_len += xfrm[i]->props.trailer_len;
1713 }
1714
1715 dst_prev->child = dst;
1716 dst0->path = dst;
1717
1718 err = -ENODEV;
1719 dev = dst->dev;
1720 if (!dev)
1721 goto free_dst;
1722
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001723 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001724 xfrm_init_pmtu(dst_prev);
1725
1726 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1727 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1728
Herbert Xu87c1e122010-03-02 02:51:56 +00001729 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001730 if (err)
1731 goto free_dst;
1732
1733 dst_prev->header_len = header_len;
1734 dst_prev->trailer_len = trailer_len;
1735 header_len -= xdst->u.dst.xfrm->props.header_len;
1736 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1737 }
1738
1739out:
1740 return dst0;
1741
1742put_states:
1743 for (; i < nx; i++)
1744 xfrm_state_put(xfrm[i]);
1745free_dst:
1746 if (dst0)
1747 dst_free(dst0);
1748 dst0 = ERR_PTR(err);
1749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Ying Xueda7c2242014-01-08 10:26:39 +08001752#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001753static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001754{
1755 if (!*target) {
1756 *target = kmalloc(size, GFP_ATOMIC);
1757 if (!*target)
1758 return -ENOMEM;
1759 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001760
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001761 memcpy(*target, src, size);
1762 return 0;
1763}
Ying Xueda7c2242014-01-08 10:26:39 +08001764#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001765
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001766static int xfrm_dst_update_parent(struct dst_entry *dst,
1767 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001768{
1769#ifdef CONFIG_XFRM_SUB_POLICY
1770 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1771 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1772 sel, sizeof(*sel));
1773#else
1774 return 0;
1775#endif
1776}
1777
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001778static int xfrm_dst_update_origin(struct dst_entry *dst,
1779 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001780{
1781#ifdef CONFIG_XFRM_SUB_POLICY
1782 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1783 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1784#else
1785 return 0;
1786#endif
1787}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
David S. Miller73ff93c2011-02-22 18:33:42 -08001789static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001790 struct xfrm_policy **pols,
1791 int *num_pols, int *num_xfrms)
1792{
1793 int i;
1794
1795 if (*num_pols == 0 || !pols[0]) {
1796 *num_pols = 0;
1797 *num_xfrms = 0;
1798 return 0;
1799 }
1800 if (IS_ERR(pols[0]))
1801 return PTR_ERR(pols[0]);
1802
1803 *num_xfrms = pols[0]->xfrm_nr;
1804
1805#ifdef CONFIG_XFRM_SUB_POLICY
1806 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1807 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1808 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1809 XFRM_POLICY_TYPE_MAIN,
1810 fl, family,
1811 XFRM_POLICY_OUT);
1812 if (pols[1]) {
1813 if (IS_ERR(pols[1])) {
1814 xfrm_pols_put(pols, *num_pols);
1815 return PTR_ERR(pols[1]);
1816 }
Weilong Chen02d08922013-12-24 09:43:48 +08001817 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001818 (*num_xfrms) += pols[1]->xfrm_nr;
1819 }
1820 }
1821#endif
1822 for (i = 0; i < *num_pols; i++) {
1823 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1824 *num_xfrms = -1;
1825 break;
1826 }
1827 }
1828
1829 return 0;
1830
1831}
1832
1833static struct xfrm_dst *
1834xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001835 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001836 struct dst_entry *dst_orig)
1837{
1838 struct net *net = xp_net(pols[0]);
1839 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1840 struct dst_entry *dst;
1841 struct xfrm_dst *xdst;
1842 int err;
1843
1844 /* Try to instantiate a bundle */
1845 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001846 if (err <= 0) {
1847 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001848 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1849 return ERR_PTR(err);
1850 }
1851
1852 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1853 if (IS_ERR(dst)) {
1854 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1855 return ERR_CAST(dst);
1856 }
1857
1858 xdst = (struct xfrm_dst *)dst;
1859 xdst->num_xfrms = err;
1860 if (num_pols > 1)
1861 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1862 else
1863 err = xfrm_dst_update_origin(dst, fl);
1864 if (unlikely(err)) {
1865 dst_free(dst);
1866 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1867 return ERR_PTR(err);
1868 }
1869
1870 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001871 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001872 xdst->policy_genid = atomic_read(&pols[0]->genid);
1873
1874 return xdst;
1875}
1876
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001877static void xfrm_policy_queue_process(unsigned long arg)
1878{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001879 struct sk_buff *skb;
1880 struct sock *sk;
1881 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001882 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1883 struct xfrm_policy_queue *pq = &pol->polq;
1884 struct flowi fl;
1885 struct sk_buff_head list;
1886
1887 spin_lock(&pq->hold_queue.lock);
1888 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001889 if (!skb) {
1890 spin_unlock(&pq->hold_queue.lock);
1891 goto out;
1892 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001893 dst = skb_dst(skb);
1894 sk = skb->sk;
1895 xfrm_decode_session(skb, &fl, dst->ops->family);
1896 spin_unlock(&pq->hold_queue.lock);
1897
1898 dst_hold(dst->path);
1899 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1900 sk, 0);
1901 if (IS_ERR(dst))
1902 goto purge_queue;
1903
1904 if (dst->flags & DST_XFRM_QUEUE) {
1905 dst_release(dst);
1906
1907 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1908 goto purge_queue;
1909
1910 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001911 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1912 xfrm_pol_hold(pol);
1913 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001914 }
1915
1916 dst_release(dst);
1917
1918 __skb_queue_head_init(&list);
1919
1920 spin_lock(&pq->hold_queue.lock);
1921 pq->timeout = 0;
1922 skb_queue_splice_init(&pq->hold_queue, &list);
1923 spin_unlock(&pq->hold_queue.lock);
1924
1925 while (!skb_queue_empty(&list)) {
1926 skb = __skb_dequeue(&list);
1927
1928 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1929 dst_hold(skb_dst(skb)->path);
1930 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1931 &fl, skb->sk, 0);
1932 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001933 kfree_skb(skb);
1934 continue;
1935 }
1936
1937 nf_reset(skb);
1938 skb_dst_drop(skb);
1939 skb_dst_set(skb, dst);
1940
Fabian Frederick5c1e9f22014-10-25 17:27:09 +02001941 dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001942 }
1943
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001944out:
1945 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001946 return;
1947
1948purge_queue:
1949 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001950 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001951 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001952}
1953
Eric Dumazetaad88722014-04-15 13:47:15 -04001954static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001955{
1956 unsigned long sched_next;
1957 struct dst_entry *dst = skb_dst(skb);
1958 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001959 struct xfrm_policy *pol = xdst->pols[0];
1960 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001961
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001962 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001963 kfree_skb(skb);
1964 return 0;
1965 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001966
1967 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1968 kfree_skb(skb);
1969 return -EAGAIN;
1970 }
1971
1972 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001973
1974 spin_lock_bh(&pq->hold_queue.lock);
1975
1976 if (!pq->timeout)
1977 pq->timeout = XFRM_QUEUE_TMO_MIN;
1978
1979 sched_next = jiffies + pq->timeout;
1980
1981 if (del_timer(&pq->hold_timer)) {
1982 if (time_before(pq->hold_timer.expires, sched_next))
1983 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001984 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001985 }
1986
1987 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001988 if (!mod_timer(&pq->hold_timer, sched_next))
1989 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001990
1991 spin_unlock_bh(&pq->hold_queue.lock);
1992
1993 return 0;
1994}
1995
1996static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02001997 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001998 const struct flowi *fl,
1999 int num_xfrms,
2000 u16 family)
2001{
2002 int err;
2003 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002004 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002005 struct dst_entry *dst1;
2006 struct xfrm_dst *xdst;
2007
2008 xdst = xfrm_alloc_dst(net, family);
2009 if (IS_ERR(xdst))
2010 return xdst;
2011
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002012 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2013 net->xfrm.sysctl_larval_drop ||
2014 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002015 return xdst;
2016
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002017 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002018 dst1 = &xdst->u.dst;
2019 dst_hold(dst);
2020 xdst->route = dst;
2021
2022 dst_copy_metrics(dst1, dst);
2023
2024 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2025 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2026 dst1->lastuse = jiffies;
2027
2028 dst1->input = dst_discard;
2029 dst1->output = xdst_queue_output;
2030
2031 dst_hold(dst);
2032 dst1->child = dst;
2033 dst1->path = dst;
2034
2035 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2036
2037 err = -ENODEV;
2038 dev = dst->dev;
2039 if (!dev)
2040 goto free_dst;
2041
2042 err = xfrm_fill_dst(xdst, dev, fl);
2043 if (err)
2044 goto free_dst;
2045
2046out:
2047 return xdst;
2048
2049free_dst:
2050 dst_release(dst1);
2051 xdst = ERR_PTR(err);
2052 goto out;
2053}
2054
Timo Teräs80c802f2010-04-07 00:30:05 +00002055static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002056xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002057 struct flow_cache_object *oldflo, void *ctx)
2058{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002059 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002060 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2061 struct xfrm_dst *xdst, *new_xdst;
2062 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2063
2064 /* Check if the policies from old bundle are usable */
2065 xdst = NULL;
2066 if (oldflo) {
2067 xdst = container_of(oldflo, struct xfrm_dst, flo);
2068 num_pols = xdst->num_pols;
2069 num_xfrms = xdst->num_xfrms;
2070 pol_dead = 0;
2071 for (i = 0; i < num_pols; i++) {
2072 pols[i] = xdst->pols[i];
2073 pol_dead |= pols[i]->walk.dead;
2074 }
2075 if (pol_dead) {
2076 dst_free(&xdst->u.dst);
2077 xdst = NULL;
2078 num_pols = 0;
2079 num_xfrms = 0;
2080 oldflo = NULL;
2081 }
2082 }
2083
2084 /* Resolve policies to use if we couldn't get them from
2085 * previous cache entry */
2086 if (xdst == NULL) {
2087 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002088 pols[0] = __xfrm_policy_lookup(net, fl, family,
2089 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002090 err = xfrm_expand_policies(fl, family, pols,
2091 &num_pols, &num_xfrms);
2092 if (err < 0)
2093 goto inc_error;
2094 if (num_pols == 0)
2095 return NULL;
2096 if (num_xfrms <= 0)
2097 goto make_dummy_bundle;
2098 }
2099
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002100 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2101 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002102 if (IS_ERR(new_xdst)) {
2103 err = PTR_ERR(new_xdst);
2104 if (err != -EAGAIN)
2105 goto error;
2106 if (oldflo == NULL)
2107 goto make_dummy_bundle;
2108 dst_hold(&xdst->u.dst);
2109 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002110 } else if (new_xdst == NULL) {
2111 num_xfrms = 0;
2112 if (oldflo == NULL)
2113 goto make_dummy_bundle;
2114 xdst->num_xfrms = 0;
2115 dst_hold(&xdst->u.dst);
2116 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002117 }
2118
2119 /* Kill the previous bundle */
2120 if (xdst) {
2121 /* The policies were stolen for newly generated bundle */
2122 xdst->num_pols = 0;
2123 dst_free(&xdst->u.dst);
2124 }
2125
2126 /* Flow cache does not have reference, it dst_free()'s,
2127 * but we do need to return one reference for original caller */
2128 dst_hold(&new_xdst->u.dst);
2129 return &new_xdst->flo;
2130
2131make_dummy_bundle:
2132 /* We found policies, but there's no bundles to instantiate:
2133 * either because the policy blocks, has no transformations or
2134 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002135 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002136 if (IS_ERR(xdst)) {
2137 xfrm_pols_put(pols, num_pols);
2138 return ERR_CAST(xdst);
2139 }
2140 xdst->num_pols = num_pols;
2141 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002142 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002143
2144 dst_hold(&xdst->u.dst);
2145 return &xdst->flo;
2146
2147inc_error:
2148 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2149error:
2150 if (xdst != NULL)
2151 dst_free(&xdst->u.dst);
2152 else
2153 xfrm_pols_put(pols, num_pols);
2154 return ERR_PTR(err);
2155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
David S. Miller2774c132011-03-01 14:59:04 -08002157static struct dst_entry *make_blackhole(struct net *net, u16 family,
2158 struct dst_entry *dst_orig)
2159{
2160 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2161 struct dst_entry *ret;
2162
2163 if (!afinfo) {
2164 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002165 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002166 } else {
2167 ret = afinfo->blackhole_route(net, dst_orig);
2168 }
2169 xfrm_policy_put_afinfo(afinfo);
2170
2171 return ret;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/* Main function: finds/creates a bundle for given flow.
2175 *
2176 * At the moment we eat a raw IP route. Mostly to speed up lookups
2177 * on interfaces with disabled IPsec.
2178 */
David S. Miller452edd52011-03-02 13:27:41 -08002179struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2180 const struct flowi *fl,
2181 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002183 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002184 struct flow_cache_object *flo;
2185 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002186 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002187 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002188 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002189 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002190
Timo Teräs80c802f2010-04-07 00:30:05 +00002191 dst = NULL;
2192 xdst = NULL;
2193 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002194
Thomas Graff7944fb2007-08-25 13:46:55 -07002195 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002196 num_pols = 1;
2197 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2198 err = xfrm_expand_policies(fl, family, pols,
2199 &num_pols, &num_xfrms);
2200 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002201 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002202
2203 if (num_pols) {
2204 if (num_xfrms <= 0) {
2205 drop_pols = num_pols;
2206 goto no_transform;
2207 }
2208
2209 xdst = xfrm_resolve_and_create_bundle(
2210 pols, num_pols, fl,
2211 family, dst_orig);
2212 if (IS_ERR(xdst)) {
2213 xfrm_pols_put(pols, num_pols);
2214 err = PTR_ERR(xdst);
2215 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002216 } else if (xdst == NULL) {
2217 num_xfrms = 0;
2218 drop_pols = num_pols;
2219 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002220 }
2221
Steffen Klassertb7eea452014-06-18 12:34:21 +02002222 dst_hold(&xdst->u.dst);
2223 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002224 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002225 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Timo Teräs80c802f2010-04-07 00:30:05 +00002228 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002229 struct xfrm_flo xflo;
2230
2231 xflo.dst_orig = dst_orig;
2232 xflo.flags = flags;
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002235 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002236 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002237 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Timo Teräs80c802f2010-04-07 00:30:05 +00002239 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002240 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002241 if (flo == NULL)
2242 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002243 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002244 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002245 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002246 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002247 xdst = container_of(flo, struct xfrm_dst, flo);
2248
2249 num_pols = xdst->num_pols;
2250 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002251 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002252 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254
Timo Teräs80c802f2010-04-07 00:30:05 +00002255 dst = &xdst->u.dst;
2256 if (route == NULL && num_xfrms > 0) {
2257 /* The only case when xfrm_bundle_lookup() returns a
2258 * bundle with null route, is when the template could
2259 * not be resolved. It means policies are there, but
2260 * bundle could not be created, since we don't yet
2261 * have the xfrm_state's. We need to wait for KM to
2262 * negotiate new SA's or bail out with error.*/
2263 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002264 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002265 err = -EREMOTE;
2266 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002267 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002268
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002269 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002270
2271 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2272 goto error;
2273 }
2274
2275no_transform:
2276 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002277 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Timo Teräs80c802f2010-04-07 00:30:05 +00002279 if ((flags & XFRM_LOOKUP_ICMP) &&
2280 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2281 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002282 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002283 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002284
Timo Teräs80c802f2010-04-07 00:30:05 +00002285 for (i = 0; i < num_pols; i++)
2286 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002287
Timo Teräs80c802f2010-04-07 00:30:05 +00002288 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002290 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002291 err = -EPERM;
2292 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002293 } else if (num_xfrms > 0) {
2294 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002295 dst_release(dst_orig);
2296 } else {
2297 /* Flow passes untransformed */
2298 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002299 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002301ok:
2302 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002303 if (dst && dst->xfrm &&
2304 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2305 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002306 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Timo Teräs80c802f2010-04-07 00:30:05 +00002308nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002309 if (!(flags & XFRM_LOOKUP_ICMP)) {
2310 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002311 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002312 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002313 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002315 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002316dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002317 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2318 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002319 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002320 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322EXPORT_SYMBOL(xfrm_lookup);
2323
Steffen Klassertf92ee612014-09-16 10:08:40 +02002324/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2325 * Otherwise we may send out blackholed packets.
2326 */
2327struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2328 const struct flowi *fl,
2329 struct sock *sk, int flags)
2330{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002331 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002332 flags | XFRM_LOOKUP_QUEUE |
2333 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002334
2335 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2336 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2337
2338 return dst;
2339}
2340EXPORT_SYMBOL(xfrm_lookup_route);
2341
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002342static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002343xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002344{
2345 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002346
2347 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2348 return 0;
2349 x = skb->sp->xvec[idx];
2350 if (!x->type->reject)
2351 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002352 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002353}
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/* When skb is transformed back to its "native" form, we have to
2356 * check policy restrictions. At the moment we make this in maximally
2357 * stupid way. Shame on me. :-) Of course, connected sockets must
2358 * have policy cached at them.
2359 */
2360
2361static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002362xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 unsigned short family)
2364{
2365 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002366 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return x->id.proto == tmpl->id.proto &&
2368 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2369 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2370 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002371 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002372 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002373 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2374 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002377/*
2378 * 0 or more than 0 is returned when validation is succeeded (either bypass
2379 * because of optional transport mode, or next index of the mathced secpath
2380 * state with the template.
2381 * -1 is returned when no matching template is found.
2382 * Otherwise "-2 - errored_index" is returned.
2383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002385xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 unsigned short family)
2387{
2388 int idx = start;
2389
2390 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002391 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return start;
2393 } else
2394 start = -1;
2395 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002396 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002398 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2399 if (start == -1)
2400 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404 return start;
2405}
2406
Herbert Xud5422ef2007-12-12 10:44:16 -08002407int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2408 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 if (unlikely(afinfo == NULL))
2414 return -EAFNOSUPPORT;
2415
Herbert Xud5422ef2007-12-12 10:44:16 -08002416 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002417 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
Herbert Xud5422ef2007-12-12 10:44:16 -08002421EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
David S. Miller9a7386e2011-02-24 01:44:12 -05002423static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
2425 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002426 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002427 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431
2432 return 0;
2433}
2434
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002435int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 unsigned short family)
2437{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002438 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002440 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2441 int npols = 0;
2442 int xfrm_nr;
2443 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002444 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002446 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002447 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Herbert Xud5422ef2007-12-12 10:44:16 -08002449 reverse = dir & ~XFRM_POLICY_MASK;
2450 dir &= XFRM_POLICY_MASK;
2451 fl_dir = policy_to_flow_dir(dir);
2452
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002453 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002454 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002456 }
2457
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002458 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /* First, check used SA against their selectors. */
2461 if (skb->sp) {
2462 int i;
2463
Weilong Chen9b7a7872013-12-24 09:43:46 +08002464 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002465 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002466 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002467 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 }
2472
2473 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002474 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002475 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002476 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002477 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002478 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002479 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002482 if (!pol) {
2483 struct flow_cache_object *flo;
2484
2485 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2486 xfrm_policy_lookup, NULL);
2487 if (IS_ERR_OR_NULL(flo))
2488 pol = ERR_CAST(flo);
2489 else
2490 pol = container_of(flo, struct xfrm_policy, flo);
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002493 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002494 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002495 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002496 }
James Morris134b0fc2006-10-05 15:42:27 -05002497
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002498 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002499 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002500 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002501 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002502 return 0;
2503 }
2504 return 1;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
James Morris9d729f72007-03-04 16:12:44 -08002507 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002509 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002510 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002511#ifdef CONFIG_XFRM_SUB_POLICY
2512 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002513 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002514 &fl, family,
2515 XFRM_POLICY_IN);
2516 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002517 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002518 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002519 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002520 }
James Morris9d729f72007-03-04 16:12:44 -08002521 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002522 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002523 }
2524 }
2525#endif
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (pol->action == XFRM_POLICY_ALLOW) {
2528 struct sec_path *sp;
2529 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002530 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002531 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002532 struct xfrm_tmpl **tpp = tp;
2533 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 int i, k;
2535
2536 if ((sp = skb->sp) == NULL)
2537 sp = &dummy;
2538
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002539 for (pi = 0; pi < npols; pi++) {
2540 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002541 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002542 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002543 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002544 }
2545 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002546 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002547 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002548 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002549 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2550 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2551 }
2552 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002553 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002554 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002555 tpp = stp;
2556 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* For each tunnel xfrm, find the first matching tmpl.
2559 * For each tmpl before that, find corresponding xfrm.
2560 * Order is _important_. Later we will implement
2561 * some barriers, but at the moment barriers
2562 * are implied between each two transformations.
2563 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002564 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2565 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002566 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002567 if (k < -1)
2568 /* "-2 - errored_index" returned */
2569 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002570 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002575 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002576 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002580 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 1;
2582 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002583 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002586 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002587reject_error:
2588 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return 0;
2590}
2591EXPORT_SYMBOL(__xfrm_policy_check);
2592
2593int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2594{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002595 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002597 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002598 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002600 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002601 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002605 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002606
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002607 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002608 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002609 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002610 dst = NULL;
2611 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002612 skb_dst_set(skb, dst);
2613 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615EXPORT_SYMBOL(__xfrm_route_forward);
2616
David S. Millerd49c73c2006-08-13 18:55:53 -07002617/* Optimize later using cookies and generation ids. */
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2620{
David S. Millerd49c73c2006-08-13 18:55:53 -07002621 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002622 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2623 * get validated by dst_ops->check on every use. We do this
2624 * because when a normal route referenced by an XFRM dst is
2625 * obsoleted we do not go looking around for all parent
2626 * referencing XFRM dsts so that we can invalidate them. It
2627 * is just too much work. Instead we make the checks here on
2628 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002629 *
2630 * XFRM dst A --> IPv4 dst X
2631 *
2632 * X is the "xdst->route" of A (X is also the "dst->path" of A
2633 * in this example). If X is marked obsolete, "A" will not
2634 * notice. That's what we are validating here via the
2635 * stale_bundle() check.
2636 *
2637 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002638 * dst which causes it's ->obsolete field to be set to
2639 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2640 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002641 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002642 if (dst->obsolete < 0 && !stale_bundle(dst))
2643 return dst;
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return NULL;
2646}
2647
2648static int stale_bundle(struct dst_entry *dst)
2649{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002650 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
Herbert Xuaabc9762005-05-03 16:27:10 -07002653void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002656 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002657 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 dev_put(dev);
2659 }
2660}
Herbert Xuaabc9762005-05-03 16:27:10 -07002661EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663static void xfrm_link_failure(struct sk_buff *skb)
2664{
2665 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2669{
2670 if (dst) {
2671 if (dst->obsolete) {
2672 dst_release(dst);
2673 dst = NULL;
2674 }
2675 }
2676 return dst;
2677}
2678
Paul Mooree4c17212013-05-29 07:36:25 +00002679void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002680{
Fan Duca925cf2014-01-18 09:55:27 +08002681 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002682}
Paul Mooree4c17212013-05-29 07:36:25 +00002683EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002684
2685static void xfrm_garbage_collect_deferred(struct net *net)
2686{
Fan Duca925cf2014-01-18 09:55:27 +08002687 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002688}
2689
Herbert Xu25ee3282007-12-11 09:32:34 -08002690static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 do {
2693 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2694 u32 pmtu, route_mtu_cached;
2695
2696 pmtu = dst_mtu(dst->child);
2697 xdst->child_mtu_cached = pmtu;
2698
2699 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2700
2701 route_mtu_cached = dst_mtu(xdst->route);
2702 xdst->route_mtu_cached = route_mtu_cached;
2703
2704 if (pmtu > route_mtu_cached)
2705 pmtu = route_mtu_cached;
2706
David S. Millerdefb3512010-12-08 21:16:57 -08002707 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 } while ((dst = dst->next));
2709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711/* Check that the bundle accepts the flow and its components are
2712 * still valid.
2713 */
2714
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002715static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
2717 struct dst_entry *dst = &first->u.dst;
2718 struct xfrm_dst *last;
2719 u32 mtu;
2720
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002721 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 (dst->dev && !netif_running(dst->dev)))
2723 return 0;
2724
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002725 if (dst->flags & DST_XFRM_QUEUE)
2726 return 1;
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 last = NULL;
2729
2730 do {
2731 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2734 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002735 if (xdst->xfrm_genid != dst->xfrm->genid)
2736 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002737 if (xdst->num_pols > 0 &&
2738 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 mtu = dst_mtu(dst->child);
2742 if (xdst->child_mtu_cached != mtu) {
2743 last = xdst;
2744 xdst->child_mtu_cached = mtu;
2745 }
2746
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002747 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return 0;
2749 mtu = dst_mtu(xdst->route);
2750 if (xdst->route_mtu_cached != mtu) {
2751 last = xdst;
2752 xdst->route_mtu_cached = mtu;
2753 }
2754
2755 dst = dst->child;
2756 } while (dst->xfrm);
2757
2758 if (likely(!last))
2759 return 1;
2760
2761 mtu = last->child_mtu_cached;
2762 for (;;) {
2763 dst = &last->u.dst;
2764
2765 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2766 if (mtu > last->route_mtu_cached)
2767 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002768 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 if (last == first)
2771 break;
2772
Patrick McHardybd0bf072007-07-18 01:55:52 -07002773 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 last->child_mtu_cached = mtu;
2775 }
2776
2777 return 1;
2778}
2779
David S. Miller0dbaee32010-12-13 12:52:14 -08002780static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2781{
2782 return dst_metric_advmss(dst->path);
2783}
2784
Steffen Klassertebb762f2011-11-23 02:12:51 +00002785static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002786{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002787 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2788
2789 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002790}
2791
David S. Millerf894cbf2012-07-02 21:52:24 -07002792static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2793 struct sk_buff *skb,
2794 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002795{
David S. Millerf894cbf2012-07-02 21:52:24 -07002796 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002797}
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2800{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002801 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 int err = 0;
2803 if (unlikely(afinfo == NULL))
2804 return -EINVAL;
2805 if (unlikely(afinfo->family >= NPROTO))
2806 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002807 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2809 err = -ENOBUFS;
2810 else {
2811 struct dst_ops *dst_ops = afinfo->dst_ops;
2812 if (likely(dst_ops->kmem_cachep == NULL))
2813 dst_ops->kmem_cachep = xfrm_dst_cache;
2814 if (likely(dst_ops->check == NULL))
2815 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002816 if (likely(dst_ops->default_advmss == NULL))
2817 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002818 if (likely(dst_ops->mtu == NULL))
2819 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (likely(dst_ops->negative_advice == NULL))
2821 dst_ops->negative_advice = xfrm_negative_advice;
2822 if (likely(dst_ops->link_failure == NULL))
2823 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002824 if (likely(dst_ops->neigh_lookup == NULL))
2825 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002827 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002828 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002830 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002831
2832 rtnl_lock();
2833 for_each_net(net) {
2834 struct dst_ops *xfrm_dst_ops;
2835
2836 switch (afinfo->family) {
2837 case AF_INET:
2838 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2839 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002840#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002841 case AF_INET6:
2842 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2843 break;
2844#endif
2845 default:
2846 BUG();
2847 }
2848 *xfrm_dst_ops = *afinfo->dst_ops;
2849 }
2850 rtnl_unlock();
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return err;
2853}
2854EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2855
2856int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2857{
2858 int err = 0;
2859 if (unlikely(afinfo == NULL))
2860 return -EINVAL;
2861 if (unlikely(afinfo->family >= NPROTO))
2862 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002863 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2865 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2866 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002867 else
2868 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2869 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002871 spin_unlock(&xfrm_policy_afinfo_lock);
2872 if (!err) {
2873 struct dst_ops *dst_ops = afinfo->dst_ops;
2874
2875 synchronize_rcu();
2876
2877 dst_ops->kmem_cachep = NULL;
2878 dst_ops->check = NULL;
2879 dst_ops->negative_advice = NULL;
2880 dst_ops->link_failure = NULL;
2881 afinfo->garbage_collect = NULL;
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return err;
2884}
2885EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2886
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002887static void __net_init xfrm_dst_ops_init(struct net *net)
2888{
2889 struct xfrm_policy_afinfo *afinfo;
2890
Eric Dumazetef8531b2012-08-19 12:31:48 +02002891 rcu_read_lock();
2892 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002893 if (afinfo)
2894 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002895#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002896 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002897 if (afinfo)
2898 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2899#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002900 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2904{
Jiri Pirko351638e2013-05-28 01:30:21 +00002905 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 switch (event) {
2908 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002909 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911 return NOTIFY_DONE;
2912}
2913
2914static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002915 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916};
2917
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002918#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002919static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002920{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002921 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002922 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2923 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002924 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002925 rv = xfrm_proc_init(net);
2926 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002927 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002928 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002929}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002930
2931static void xfrm_statistics_fini(struct net *net)
2932{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002933 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002934 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002935}
2936#else
2937static int __net_init xfrm_statistics_init(struct net *net)
2938{
2939 return 0;
2940}
2941
2942static void xfrm_statistics_fini(struct net *net)
2943{
2944}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002945#endif
2946
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002947static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
David S. Miller2518c7c2006-08-24 04:45:07 -07002949 unsigned int hmask, sz;
2950 int dir;
2951
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002952 if (net_eq(net, &init_net))
2953 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002955 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002956 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
David S. Miller2518c7c2006-08-24 04:45:07 -07002958 hmask = 8 - 1;
2959 sz = (hmask+1) * sizeof(struct hlist_head);
2960
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002961 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2962 if (!net->xfrm.policy_byidx)
2963 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002964 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002965
Herbert Xu53c2e282014-11-13 17:09:49 +08002966 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002967 struct xfrm_policy_hash *htab;
2968
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002969 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002970 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002971 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002972
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002973 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002974 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002975 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002976 goto out_bydst;
2977 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002978 htab->dbits4 = 32;
2979 htab->sbits4 = 32;
2980 htab->dbits6 = 128;
2981 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002982 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002983 net->xfrm.policy_hthresh.lbits4 = 32;
2984 net->xfrm.policy_hthresh.rbits4 = 32;
2985 net->xfrm.policy_hthresh.lbits6 = 128;
2986 net->xfrm.policy_hthresh.rbits6 = 128;
2987
2988 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002989
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002990 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002991 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002992 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002993 if (net_eq(net, &init_net))
2994 register_netdevice_notifier(&xfrm_dev_notifier);
2995 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002996
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002997out_bydst:
2998 for (dir--; dir >= 0; dir--) {
2999 struct xfrm_policy_hash *htab;
3000
3001 htab = &net->xfrm.policy_bydst[dir];
3002 xfrm_hash_free(htab->table, sz);
3003 }
3004 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003005out_byidx:
3006 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003009static void xfrm_policy_fini(struct net *net)
3010{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003011 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003012 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003013
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003014 flush_work(&net->xfrm.policy_hash_work);
3015#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003016 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003017#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003018 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003019
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003020 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003021
Herbert Xu53c2e282014-11-13 17:09:49 +08003022 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003023 struct xfrm_policy_hash *htab;
3024
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003025 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003026
3027 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003028 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003029 WARN_ON(!hlist_empty(htab->table));
3030 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003031 }
3032
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003033 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003034 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3035 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003036}
3037
3038static int __net_init xfrm_net_init(struct net *net)
3039{
3040 int rv;
3041
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003042 rv = xfrm_statistics_init(net);
3043 if (rv < 0)
3044 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003045 rv = xfrm_state_init(net);
3046 if (rv < 0)
3047 goto out_state;
3048 rv = xfrm_policy_init(net);
3049 if (rv < 0)
3050 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003051 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003052 rv = xfrm_sysctl_init(net);
3053 if (rv < 0)
3054 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003055 rv = flow_cache_init(net);
3056 if (rv < 0)
3057 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003058
3059 /* Initialize the per-net locks here */
3060 spin_lock_init(&net->xfrm.xfrm_state_lock);
3061 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003062 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3063
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003064 return 0;
3065
Steffen Klassert4a93f502014-03-12 09:43:17 +01003066out:
3067 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003068out_sysctl:
3069 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003070out_policy:
3071 xfrm_state_fini(net);
3072out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003073 xfrm_statistics_fini(net);
3074out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003075 return rv;
3076}
3077
3078static void __net_exit xfrm_net_exit(struct net *net)
3079{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003080 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003081 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003082 xfrm_policy_fini(net);
3083 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003084 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003085}
3086
3087static struct pernet_operations __net_initdata xfrm_net_ops = {
3088 .init = xfrm_net_init,
3089 .exit = xfrm_net_exit,
3090};
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092void __init xfrm_init(void)
3093{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003094 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 xfrm_input_init();
3096}
3097
Joy Lattenab5f5e82007-09-17 11:51:22 -07003098#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003099static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3100 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003101{
Paul Moore875179f2007-12-01 23:27:18 +11003102 struct xfrm_sec_ctx *ctx = xp->security;
3103 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003104
Paul Moore875179f2007-12-01 23:27:18 +11003105 if (ctx)
3106 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3107 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3108
Weilong Chen9b7a7872013-12-24 09:43:46 +08003109 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003110 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003111 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003112 if (sel->prefixlen_s != 32)
3113 audit_log_format(audit_buf, " src_prefixlen=%d",
3114 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003115 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003116 if (sel->prefixlen_d != 32)
3117 audit_log_format(audit_buf, " dst_prefixlen=%d",
3118 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003119 break;
3120 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003121 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003122 if (sel->prefixlen_s != 128)
3123 audit_log_format(audit_buf, " src_prefixlen=%d",
3124 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003125 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003126 if (sel->prefixlen_d != 128)
3127 audit_log_format(audit_buf, " dst_prefixlen=%d",
3128 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003129 break;
3130 }
3131}
3132
Tetsuo Handa2e710292014-04-22 21:48:30 +09003133void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003134{
3135 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003136
Paul Mooreafeb14b2007-12-21 14:58:11 -08003137 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003138 if (audit_buf == NULL)
3139 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003140 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003141 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003142 xfrm_audit_common_policyinfo(xp, audit_buf);
3143 audit_log_end(audit_buf);
3144}
3145EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3146
Paul Moore68277ac2007-12-20 20:49:33 -08003147void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003148 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003149{
3150 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151
Paul Mooreafeb14b2007-12-21 14:58:11 -08003152 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153 if (audit_buf == NULL)
3154 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003155 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003156 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003157 xfrm_audit_common_policyinfo(xp, audit_buf);
3158 audit_log_end(audit_buf);
3159}
3160EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3161#endif
3162
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003163#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003164static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3165 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003166{
3167 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3168 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003169 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3170 sel_cmp->family) &&
3171 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3172 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003173 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3174 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003175 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003176 }
3177 } else {
3178 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003179 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003180 }
3181 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003182 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003183}
3184
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003185static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3186 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003187{
3188 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003189 struct hlist_head *chain;
3190 u32 priority = ~0U;
3191
Fan Du283bc9f2013-11-07 17:47:50 +08003192 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003193 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003194 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003195 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3196 pol->type == type) {
3197 ret = pol;
3198 priority = ret->priority;
3199 break;
3200 }
3201 }
Fan Du8d549c42013-11-07 17:47:49 +08003202 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003203 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3205 pol->type == type &&
3206 pol->priority < priority) {
3207 ret = pol;
3208 break;
3209 }
3210 }
3211
3212 if (ret)
3213 xfrm_pol_hold(ret);
3214
Fan Du283bc9f2013-11-07 17:47:50 +08003215 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003216
3217 return ret;
3218}
3219
David S. Millerdd701752011-02-24 00:21:08 -05003220static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003221{
3222 int match = 0;
3223
3224 if (t->mode == m->mode && t->id.proto == m->proto &&
3225 (m->reqid == 0 || t->reqid == m->reqid)) {
3226 switch (t->mode) {
3227 case XFRM_MODE_TUNNEL:
3228 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003229 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3230 m->old_family) &&
3231 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3232 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003233 match = 1;
3234 }
3235 break;
3236 case XFRM_MODE_TRANSPORT:
3237 /* in case of transport mode, template does not store
3238 any IP addresses, hence we just compare mode and
3239 protocol */
3240 match = 1;
3241 break;
3242 default:
3243 break;
3244 }
3245 }
3246 return match;
3247}
3248
3249/* update endpoint address(es) of template(s) */
3250static int xfrm_policy_migrate(struct xfrm_policy *pol,
3251 struct xfrm_migrate *m, int num_migrate)
3252{
3253 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003254 int i, j, n = 0;
3255
3256 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003257 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003258 /* target policy has been deleted */
3259 write_unlock_bh(&pol->lock);
3260 return -ENOENT;
3261 }
3262
3263 for (i = 0; i < pol->xfrm_nr; i++) {
3264 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3265 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3266 continue;
3267 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003268 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3269 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003270 continue;
3271 /* update endpoints */
3272 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3273 sizeof(pol->xfrm_vec[i].id.daddr));
3274 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3275 sizeof(pol->xfrm_vec[i].saddr));
3276 pol->xfrm_vec[i].encap_family = mp->new_family;
3277 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003278 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003279 }
3280 }
3281
3282 write_unlock_bh(&pol->lock);
3283
3284 if (!n)
3285 return -ENODATA;
3286
3287 return 0;
3288}
3289
David S. Millerdd701752011-02-24 00:21:08 -05003290static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003291{
3292 int i, j;
3293
3294 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3295 return -EINVAL;
3296
3297 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003298 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3299 m[i].old_family) &&
3300 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3301 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003302 return -EINVAL;
3303 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3304 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3305 return -EINVAL;
3306
3307 /* check if there is any duplicated entry */
3308 for (j = i + 1; j < num_migrate; j++) {
3309 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3310 sizeof(m[i].old_daddr)) &&
3311 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3312 sizeof(m[i].old_saddr)) &&
3313 m[i].proto == m[j].proto &&
3314 m[i].mode == m[j].mode &&
3315 m[i].reqid == m[j].reqid &&
3316 m[i].old_family == m[j].old_family)
3317 return -EINVAL;
3318 }
3319 }
3320
3321 return 0;
3322}
3323
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003324int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003325 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003326 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003327{
3328 int i, err, nx_cur = 0, nx_new = 0;
3329 struct xfrm_policy *pol = NULL;
3330 struct xfrm_state *x, *xc;
3331 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3332 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3333 struct xfrm_migrate *mp;
3334
3335 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3336 goto out;
3337
3338 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003339 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003340 err = -ENOENT;
3341 goto out;
3342 }
3343
3344 /* Stage 2 - find and update state(s) */
3345 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003346 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003347 x_cur[nx_cur] = x;
3348 nx_cur++;
3349 if ((xc = xfrm_state_migrate(x, mp))) {
3350 x_new[nx_new] = xc;
3351 nx_new++;
3352 } else {
3353 err = -ENODATA;
3354 goto restore_state;
3355 }
3356 }
3357 }
3358
3359 /* Stage 3 - update policy */
3360 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3361 goto restore_state;
3362
3363 /* Stage 4 - delete old state(s) */
3364 if (nx_cur) {
3365 xfrm_states_put(x_cur, nx_cur);
3366 xfrm_states_delete(x_cur, nx_cur);
3367 }
3368
3369 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003370 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003371
3372 xfrm_pol_put(pol);
3373
3374 return 0;
3375out:
3376 return err;
3377
3378restore_state:
3379 if (pol)
3380 xfrm_pol_put(pol);
3381 if (nx_cur)
3382 xfrm_states_put(x_cur, nx_cur);
3383 if (nx_new)
3384 xfrm_states_delete(x_new, nx_new);
3385
3386 return err;
3387}
David S. Millere610e672007-02-08 13:29:15 -08003388EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003389#endif