blob: 18cead7645be0e75621e1b08a9e02621e75b7a4c [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 Due682adf02013-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 Due682adf02013-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
Li RongQingde2ad482015-04-30 17:25:19 +0800703 if (skb_queue_empty(&pq->hold_queue))
704 return;
705
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100706 __skb_queue_head_init(&list);
707
708 spin_lock_bh(&pq->hold_queue.lock);
709 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200710 if (del_timer(&pq->hold_timer))
711 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100712 spin_unlock_bh(&pq->hold_queue.lock);
713
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100714 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 Due682adf02013-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
Li RongQing80077702015-04-22 17:09:54 +08001007 x = list_first_entry(&walk->walk.all,
1008 struct xfrm_policy_walk_entry, all);
1009
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001010 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001011 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001012 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001013 pol = container_of(x, struct xfrm_policy, walk);
1014 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1015 walk->type != pol->type)
1016 continue;
1017 error = func(pol, xfrm_policy_id2dir(pol->index),
1018 walk->seq, data);
1019 if (error) {
1020 list_move_tail(&walk->walk.all, &x->all);
1021 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001022 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001023 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001025 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001026 error = -ENOENT;
1027 goto out;
1028 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001029 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030out:
Fan Du283bc9f2013-11-07 17:47:50 +08001031 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return error;
1033}
1034EXPORT_SYMBOL(xfrm_policy_walk);
1035
Herbert Xu12a169e2008-10-01 07:03:24 -07001036void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1037{
1038 INIT_LIST_HEAD(&walk->walk.all);
1039 walk->walk.dead = 1;
1040 walk->type = type;
1041 walk->seq = 0;
1042}
1043EXPORT_SYMBOL(xfrm_policy_walk_init);
1044
Fan Du283bc9f2013-11-07 17:47:50 +08001045void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001046{
1047 if (list_empty(&walk->walk.all))
1048 return;
1049
Fan Du283bc9f2013-11-07 17:47:50 +08001050 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001051 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001052 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001053}
1054EXPORT_SYMBOL(xfrm_policy_walk_done);
1055
James Morris134b0fc2006-10-05 15:42:27 -05001056/*
1057 * Find policy to apply to this flow.
1058 *
1059 * Returns 0 if policy found, else an -errno.
1060 */
David S. Millerf299d552011-02-24 01:23:30 -05001061static int xfrm_policy_match(const struct xfrm_policy *pol,
1062 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001063 u8 type, u16 family, int dir)
1064{
David S. Millerf299d552011-02-24 01:23:30 -05001065 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001066 int ret = -ESRCH;
1067 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001068
1069 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001070 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001071 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001072 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001073
1074 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001075 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001076 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001077 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001078
James Morris134b0fc2006-10-05 15:42:27 -05001079 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001080}
1081
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001082static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001083 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001084 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
James Morris134b0fc2006-10-05 15:42:27 -05001086 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001087 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001088 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001089 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001090 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001091
1092 daddr = xfrm_flowi_daddr(fl, family);
1093 saddr = xfrm_flowi_saddr(fl, family);
1094 if (unlikely(!daddr || !saddr))
1095 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Fan Du283bc9f2013-11-07 17:47:50 +08001097 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001098 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001099 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001100 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001101 err = xfrm_policy_match(pol, fl, type, family, dir);
1102 if (err) {
1103 if (err == -ESRCH)
1104 continue;
1105 else {
1106 ret = ERR_PTR(err);
1107 goto fail;
1108 }
1109 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001110 ret = pol;
1111 priority = ret->priority;
1112 break;
1113 }
1114 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001115 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001116 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001117 if ((pol->priority >= priority) && ret)
1118 break;
1119
James Morris134b0fc2006-10-05 15:42:27 -05001120 err = xfrm_policy_match(pol, fl, type, family, dir);
1121 if (err) {
1122 if (err == -ESRCH)
1123 continue;
1124 else {
1125 ret = ERR_PTR(err);
1126 goto fail;
1127 }
Li RongQing8faf4912015-05-14 11:16:59 +08001128 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001129 ret = pol;
1130 break;
1131 }
1132 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001133
1134 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001135fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001136 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001137
David S. Miller2518c7c2006-08-24 04:45:07 -07001138 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001139}
1140
Timo Teräs80c802f2010-04-07 00:30:05 +00001141static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001142__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001143{
1144#ifdef CONFIG_XFRM_SUB_POLICY
1145 struct xfrm_policy *pol;
1146
1147 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1148 if (pol != NULL)
1149 return pol;
1150#endif
1151 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1152}
1153
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001154static int flow_to_policy_dir(int dir)
1155{
1156 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1157 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1158 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1159 return dir;
1160
1161 switch (dir) {
1162 default:
1163 case FLOW_DIR_IN:
1164 return XFRM_POLICY_IN;
1165 case FLOW_DIR_OUT:
1166 return XFRM_POLICY_OUT;
1167 case FLOW_DIR_FWD:
1168 return XFRM_POLICY_FWD;
1169 }
1170}
1171
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001172static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001173xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001174 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001175{
1176 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001177
1178 if (old_obj)
1179 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001180
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001181 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001182 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001183 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001184
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001185 /* Resolver returns two references:
1186 * one for cache and one for caller of flow_cache_lookup() */
1187 xfrm_pol_hold(pol);
1188
1189 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Trent Jaegerdf718372005-12-13 23:12:27 -08001192static inline int policy_to_flow_dir(int dir)
1193{
1194 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001195 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1196 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1197 return dir;
1198 switch (dir) {
1199 default:
1200 case XFRM_POLICY_IN:
1201 return FLOW_DIR_IN;
1202 case XFRM_POLICY_OUT:
1203 return FLOW_DIR_OUT;
1204 case XFRM_POLICY_FWD:
1205 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001206 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001207}
1208
David S. Millerdee9f4b2011-02-22 18:44:31 -08001209static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1210 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001213 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Fan Du283bc9f2013-11-07 17:47:50 +08001215 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001217 bool match = xfrm_selector_match(&pol->selector, fl,
1218 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001219 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001220
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001221 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001222 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1223 pol = NULL;
1224 goto out;
1225 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001226 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001227 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001228 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001229 if (!err)
1230 xfrm_pol_hold(pol);
1231 else if (err == -ESRCH)
1232 pol = NULL;
1233 else
1234 pol = ERR_PTR(err);
1235 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 pol = NULL;
1237 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001238out:
Fan Du283bc9f2013-11-07 17:47:50 +08001239 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return pol;
1241}
1242
1243static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1244{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001245 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001246
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001247 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001248 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 xfrm_pol_hold(pol);
1250}
1251
1252static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1253 int dir)
1254{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001255 struct net *net = xp_net(pol);
1256
Herbert Xu53c2e282014-11-13 17:09:49 +08001257 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Herbert Xu53c2e282014-11-13 17:09:49 +08001260 /* Socket policies are not hashed. */
1261 if (!hlist_unhashed(&pol->bydst)) {
1262 hlist_del(&pol->bydst);
1263 hlist_del(&pol->byidx);
1264 }
1265
1266 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001267 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001268
1269 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Herbert Xu53c2e282014-11-13 17:09:49 +08001272static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1273{
1274 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1275}
1276
1277static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1278{
1279 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1280}
1281
Herbert Xu4666faa2005-06-18 22:43:22 -07001282int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Fan Du283bc9f2013-11-07 17:47:50 +08001284 struct net *net = xp_net(pol);
1285
1286 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001288 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001293 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
David S. Millera70fcb02006-03-20 19:18:52 -08001295EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1298{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001299 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct xfrm_policy *old_pol;
1301
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001302#ifdef CONFIG_XFRM_SUB_POLICY
1303 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1304 return -EINVAL;
1305#endif
1306
Fan Du283bc9f2013-11-07 17:47:50 +08001307 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 old_pol = sk->sk_policy[dir];
1309 sk->sk_policy[dir] = pol;
1310 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001311 pol->curlft.add_time = get_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001312 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001313 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001315 if (old_pol) {
1316 if (pol)
1317 xfrm_policy_requeue(old_pol, pol);
1318
Timo Teräsea2dea92010-03-31 00:17:05 +00001319 /* Unlinking succeeds always. This is the only function
1320 * allowed to delete or replace socket policy.
1321 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001322 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001323 }
Fan Du283bc9f2013-11-07 17:47:50 +08001324 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (old_pol) {
1327 xfrm_policy_kill(old_pol);
1328 }
1329 return 0;
1330}
1331
David S. Millerd3e40a92011-02-24 01:25:41 -05001332static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001334 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001335 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (newp) {
1338 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001339 if (security_xfrm_policy_clone(old->security,
1340 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001341 kfree(newp);
1342 return NULL; /* ENOMEM */
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 newp->lft = old->lft;
1345 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001346 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 newp->action = old->action;
1348 newp->flags = old->flags;
1349 newp->xfrm_nr = old->xfrm_nr;
1350 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001351 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 memcpy(newp->xfrm_vec, old->xfrm_vec,
1353 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001354 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001355 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001356 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 xfrm_pol_put(newp);
1358 }
1359 return newp;
1360}
1361
1362int __xfrm_sk_clone_policy(struct sock *sk)
1363{
1364 struct xfrm_policy *p0 = sk->sk_policy[0],
1365 *p1 = sk->sk_policy[1];
1366
1367 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1368 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1369 return -ENOMEM;
1370 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1371 return -ENOMEM;
1372 return 0;
1373}
1374
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001375static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001376xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001377 unsigned short family)
1378{
1379 int err;
1380 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1381
1382 if (unlikely(afinfo == NULL))
1383 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001384 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001385 xfrm_policy_put_afinfo(afinfo);
1386 return err;
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/* Resolve list of templates for the flow, given policy. */
1390
1391static int
David S. Millera6c2e612011-02-22 18:35:39 -08001392xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1393 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001395 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int nx;
1397 int i, error;
1398 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1399 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001400 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Weilong Chen9b7a7872013-12-24 09:43:46 +08001402 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 struct xfrm_state *x;
1404 xfrm_address_t *remote = daddr;
1405 xfrm_address_t *local = saddr;
1406 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1407
Joakim Koskela48b8d782007-07-26 00:08:42 -07001408 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1409 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 remote = &tmpl->id.daddr;
1411 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001412 if (xfrm_addr_any(local, tmpl->encap_family)) {
1413 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001414 if (error)
1415 goto fail;
1416 local = &tmp;
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
1420 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1421
1422 if (x && x->km.state == XFRM_STATE_VALID) {
1423 xfrm[nx++] = x;
1424 daddr = remote;
1425 saddr = local;
1426 continue;
1427 }
1428 if (x) {
1429 error = (x->km.state == XFRM_STATE_ERROR ?
1430 -EINVAL : -EAGAIN);
1431 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001432 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001433 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 if (!tmpl->optional)
1437 goto fail;
1438 }
1439 return nx;
1440
1441fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001442 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 xfrm_state_put(xfrm[nx]);
1444 return error;
1445}
1446
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001447static int
David S. Millera6c2e612011-02-22 18:35:39 -08001448xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1449 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001450{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001451 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1452 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001453 int cnx = 0;
1454 int error;
1455 int ret;
1456 int i;
1457
1458 for (i = 0; i < npols; i++) {
1459 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1460 error = -ENOBUFS;
1461 goto fail;
1462 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001463
1464 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001465 if (ret < 0) {
1466 error = ret;
1467 goto fail;
1468 } else
1469 cnx += ret;
1470 }
1471
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001472 /* found states are sorted for outbound processing */
1473 if (npols > 1)
1474 xfrm_state_sort(xfrm, tpp, cnx, family);
1475
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001476 return cnx;
1477
1478 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001479 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001480 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001481 return error;
1482
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/* Check that the bundle accepts the flow and its components are
1486 * still valid.
1487 */
1488
David S. Miller05d84022011-02-22 17:47:10 -08001489static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001490{
1491 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1492 int tos;
1493
1494 if (!afinfo)
1495 return -EINVAL;
1496
1497 tos = afinfo->get_tos(fl);
1498
1499 xfrm_policy_put_afinfo(afinfo);
1500
1501 return tos;
1502}
1503
Timo Teräs80c802f2010-04-07 00:30:05 +00001504static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1505{
1506 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1507 struct dst_entry *dst = &xdst->u.dst;
1508
1509 if (xdst->route == NULL) {
1510 /* Dummy bundle - if it has xfrms we were not
1511 * able to build bundle as template resolution failed.
1512 * It means we need to try again resolving. */
1513 if (xdst->num_xfrms > 0)
1514 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001515 } else if (dst->flags & DST_XFRM_QUEUE) {
1516 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001517 } else {
1518 /* Real bundle */
1519 if (stale_bundle(dst))
1520 return NULL;
1521 }
1522
1523 dst_hold(dst);
1524 return flo;
1525}
1526
1527static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1528{
1529 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1530 struct dst_entry *dst = &xdst->u.dst;
1531
1532 if (!xdst->route)
1533 return 0;
1534 if (stale_bundle(dst))
1535 return 0;
1536
1537 return 1;
1538}
1539
1540static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1541{
1542 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1543 struct dst_entry *dst = &xdst->u.dst;
1544
1545 dst_free(dst);
1546}
1547
1548static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1549 .get = xfrm_bundle_flo_get,
1550 .check = xfrm_bundle_flo_check,
1551 .delete = xfrm_bundle_flo_delete,
1552};
1553
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001554static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001555{
1556 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001557 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001558 struct xfrm_dst *xdst;
1559
1560 if (!afinfo)
1561 return ERR_PTR(-EINVAL);
1562
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001563 switch (family) {
1564 case AF_INET:
1565 dst_ops = &net->xfrm.xfrm4_dst_ops;
1566 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001567#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001568 case AF_INET6:
1569 dst_ops = &net->xfrm.xfrm6_dst_ops;
1570 break;
1571#endif
1572 default:
1573 BUG();
1574 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001575 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001576
Madalin Bucurd4cae562011-09-26 07:04:36 +00001577 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001578 struct dst_entry *dst = &xdst->u.dst;
1579
1580 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001581 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001582 if (afinfo->init_dst)
1583 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001584 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001585 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001586
Madalin Bucurd4cae562011-09-26 07:04:36 +00001587 xfrm_policy_put_afinfo(afinfo);
1588
Herbert Xu25ee3282007-12-11 09:32:34 -08001589 return xdst;
1590}
1591
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001592static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1593 int nfheader_len)
1594{
1595 struct xfrm_policy_afinfo *afinfo =
1596 xfrm_policy_get_afinfo(dst->ops->family);
1597 int err;
1598
1599 if (!afinfo)
1600 return -EINVAL;
1601
1602 err = afinfo->init_path(path, dst, nfheader_len);
1603
1604 xfrm_policy_put_afinfo(afinfo);
1605
1606 return err;
1607}
1608
Herbert Xu87c1e122010-03-02 02:51:56 +00001609static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001610 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001611{
1612 struct xfrm_policy_afinfo *afinfo =
1613 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1614 int err;
1615
1616 if (!afinfo)
1617 return -EINVAL;
1618
Herbert Xu87c1e122010-03-02 02:51:56 +00001619 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001620
1621 xfrm_policy_put_afinfo(afinfo);
1622
1623 return err;
1624}
1625
Timo Teräs80c802f2010-04-07 00:30:05 +00001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1628 * all the metrics... Shortly, bundle a bundle.
1629 */
1630
Herbert Xu25ee3282007-12-11 09:32:34 -08001631static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1632 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001633 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001634 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001636 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001637 unsigned long now = jiffies;
1638 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001639 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001640 struct dst_entry *dst_prev = NULL;
1641 struct dst_entry *dst0 = NULL;
1642 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001644 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001645 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001646 int trailer_len = 0;
1647 int tos;
1648 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001649 xfrm_address_t saddr, daddr;
1650
1651 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001652
1653 tos = xfrm_get_tos(fl, family);
1654 err = tos;
1655 if (tos < 0)
1656 goto put_states;
1657
1658 dst_hold(dst);
1659
1660 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001661 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001662 struct dst_entry *dst1 = &xdst->u.dst;
1663
1664 err = PTR_ERR(xdst);
1665 if (IS_ERR(xdst)) {
1666 dst_release(dst);
1667 goto put_states;
1668 }
1669
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001670 if (xfrm[i]->sel.family == AF_UNSPEC) {
1671 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1672 xfrm_af2proto(family));
1673 if (!inner_mode) {
1674 err = -EAFNOSUPPORT;
1675 dst_release(dst);
1676 goto put_states;
1677 }
1678 } else
1679 inner_mode = xfrm[i]->inner_mode;
1680
Herbert Xu25ee3282007-12-11 09:32:34 -08001681 if (!dst_prev)
1682 dst0 = dst1;
1683 else {
1684 dst_prev->child = dst_clone(dst1);
1685 dst1->flags |= DST_NOHASH;
1686 }
1687
1688 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001689 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001690
1691 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1692 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001693 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1694 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001695 err = PTR_ERR(dst);
1696 if (IS_ERR(dst))
1697 goto put_states;
1698 } else
1699 dst_hold(dst);
1700
1701 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001702 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001703
David S. Millerf5b0a872012-07-19 12:31:33 -07001704 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001705 dst1->flags |= DST_HOST;
1706 dst1->lastuse = now;
1707
1708 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001709 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001710
1711 dst1->next = dst_prev;
1712 dst_prev = dst1;
1713
1714 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001715 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1716 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001717 trailer_len += xfrm[i]->props.trailer_len;
1718 }
1719
1720 dst_prev->child = dst;
1721 dst0->path = dst;
1722
1723 err = -ENODEV;
1724 dev = dst->dev;
1725 if (!dev)
1726 goto free_dst;
1727
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001728 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001729 xfrm_init_pmtu(dst_prev);
1730
1731 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1732 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1733
Herbert Xu87c1e122010-03-02 02:51:56 +00001734 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001735 if (err)
1736 goto free_dst;
1737
1738 dst_prev->header_len = header_len;
1739 dst_prev->trailer_len = trailer_len;
1740 header_len -= xdst->u.dst.xfrm->props.header_len;
1741 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1742 }
1743
1744out:
1745 return dst0;
1746
1747put_states:
1748 for (; i < nx; i++)
1749 xfrm_state_put(xfrm[i]);
1750free_dst:
1751 if (dst0)
1752 dst_free(dst0);
1753 dst0 = ERR_PTR(err);
1754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Ying Xueda7c2242014-01-08 10:26:39 +08001757#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001758static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001759{
1760 if (!*target) {
1761 *target = kmalloc(size, GFP_ATOMIC);
1762 if (!*target)
1763 return -ENOMEM;
1764 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001765
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001766 memcpy(*target, src, size);
1767 return 0;
1768}
Ying Xueda7c2242014-01-08 10:26:39 +08001769#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001770
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001771static int xfrm_dst_update_parent(struct dst_entry *dst,
1772 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001773{
1774#ifdef CONFIG_XFRM_SUB_POLICY
1775 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1776 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1777 sel, sizeof(*sel));
1778#else
1779 return 0;
1780#endif
1781}
1782
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001783static int xfrm_dst_update_origin(struct dst_entry *dst,
1784 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001785{
1786#ifdef CONFIG_XFRM_SUB_POLICY
1787 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1788 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1789#else
1790 return 0;
1791#endif
1792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
David S. Miller73ff93c2011-02-22 18:33:42 -08001794static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001795 struct xfrm_policy **pols,
1796 int *num_pols, int *num_xfrms)
1797{
1798 int i;
1799
1800 if (*num_pols == 0 || !pols[0]) {
1801 *num_pols = 0;
1802 *num_xfrms = 0;
1803 return 0;
1804 }
1805 if (IS_ERR(pols[0]))
1806 return PTR_ERR(pols[0]);
1807
1808 *num_xfrms = pols[0]->xfrm_nr;
1809
1810#ifdef CONFIG_XFRM_SUB_POLICY
1811 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1812 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1813 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1814 XFRM_POLICY_TYPE_MAIN,
1815 fl, family,
1816 XFRM_POLICY_OUT);
1817 if (pols[1]) {
1818 if (IS_ERR(pols[1])) {
1819 xfrm_pols_put(pols, *num_pols);
1820 return PTR_ERR(pols[1]);
1821 }
Weilong Chen02d08922013-12-24 09:43:48 +08001822 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001823 (*num_xfrms) += pols[1]->xfrm_nr;
1824 }
1825 }
1826#endif
1827 for (i = 0; i < *num_pols; i++) {
1828 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1829 *num_xfrms = -1;
1830 break;
1831 }
1832 }
1833
1834 return 0;
1835
1836}
1837
1838static struct xfrm_dst *
1839xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001840 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001841 struct dst_entry *dst_orig)
1842{
1843 struct net *net = xp_net(pols[0]);
1844 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1845 struct dst_entry *dst;
1846 struct xfrm_dst *xdst;
1847 int err;
1848
1849 /* Try to instantiate a bundle */
1850 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001851 if (err <= 0) {
1852 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001853 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1854 return ERR_PTR(err);
1855 }
1856
1857 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1858 if (IS_ERR(dst)) {
1859 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1860 return ERR_CAST(dst);
1861 }
1862
1863 xdst = (struct xfrm_dst *)dst;
1864 xdst->num_xfrms = err;
1865 if (num_pols > 1)
1866 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1867 else
1868 err = xfrm_dst_update_origin(dst, fl);
1869 if (unlikely(err)) {
1870 dst_free(dst);
1871 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1872 return ERR_PTR(err);
1873 }
1874
1875 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001876 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001877 xdst->policy_genid = atomic_read(&pols[0]->genid);
1878
1879 return xdst;
1880}
1881
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001882static void xfrm_policy_queue_process(unsigned long arg)
1883{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001884 struct sk_buff *skb;
1885 struct sock *sk;
1886 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001887 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1888 struct xfrm_policy_queue *pq = &pol->polq;
1889 struct flowi fl;
1890 struct sk_buff_head list;
1891
1892 spin_lock(&pq->hold_queue.lock);
1893 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001894 if (!skb) {
1895 spin_unlock(&pq->hold_queue.lock);
1896 goto out;
1897 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001898 dst = skb_dst(skb);
1899 sk = skb->sk;
1900 xfrm_decode_session(skb, &fl, dst->ops->family);
1901 spin_unlock(&pq->hold_queue.lock);
1902
1903 dst_hold(dst->path);
1904 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1905 sk, 0);
1906 if (IS_ERR(dst))
1907 goto purge_queue;
1908
1909 if (dst->flags & DST_XFRM_QUEUE) {
1910 dst_release(dst);
1911
1912 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1913 goto purge_queue;
1914
1915 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001916 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1917 xfrm_pol_hold(pol);
1918 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001919 }
1920
1921 dst_release(dst);
1922
1923 __skb_queue_head_init(&list);
1924
1925 spin_lock(&pq->hold_queue.lock);
1926 pq->timeout = 0;
1927 skb_queue_splice_init(&pq->hold_queue, &list);
1928 spin_unlock(&pq->hold_queue.lock);
1929
1930 while (!skb_queue_empty(&list)) {
1931 skb = __skb_dequeue(&list);
1932
1933 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1934 dst_hold(skb_dst(skb)->path);
1935 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1936 &fl, skb->sk, 0);
1937 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001938 kfree_skb(skb);
1939 continue;
1940 }
1941
1942 nf_reset(skb);
1943 skb_dst_drop(skb);
1944 skb_dst_set(skb, dst);
1945
Fabian Frederick5c1e9f22014-10-25 17:27:09 +02001946 dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001947 }
1948
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001949out:
1950 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001951 return;
1952
1953purge_queue:
1954 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001955 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001956 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001957}
1958
Eric Dumazetaad88722014-04-15 13:47:15 -04001959static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001960{
1961 unsigned long sched_next;
1962 struct dst_entry *dst = skb_dst(skb);
1963 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001964 struct xfrm_policy *pol = xdst->pols[0];
1965 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001966
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001967 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001968 kfree_skb(skb);
1969 return 0;
1970 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001971
1972 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1973 kfree_skb(skb);
1974 return -EAGAIN;
1975 }
1976
1977 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001978
1979 spin_lock_bh(&pq->hold_queue.lock);
1980
1981 if (!pq->timeout)
1982 pq->timeout = XFRM_QUEUE_TMO_MIN;
1983
1984 sched_next = jiffies + pq->timeout;
1985
1986 if (del_timer(&pq->hold_timer)) {
1987 if (time_before(pq->hold_timer.expires, sched_next))
1988 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001989 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001990 }
1991
1992 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001993 if (!mod_timer(&pq->hold_timer, sched_next))
1994 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001995
1996 spin_unlock_bh(&pq->hold_queue.lock);
1997
1998 return 0;
1999}
2000
2001static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002002 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002003 const struct flowi *fl,
2004 int num_xfrms,
2005 u16 family)
2006{
2007 int err;
2008 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002009 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002010 struct dst_entry *dst1;
2011 struct xfrm_dst *xdst;
2012
2013 xdst = xfrm_alloc_dst(net, family);
2014 if (IS_ERR(xdst))
2015 return xdst;
2016
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002017 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2018 net->xfrm.sysctl_larval_drop ||
2019 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002020 return xdst;
2021
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002022 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002023 dst1 = &xdst->u.dst;
2024 dst_hold(dst);
2025 xdst->route = dst;
2026
2027 dst_copy_metrics(dst1, dst);
2028
2029 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2030 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2031 dst1->lastuse = jiffies;
2032
2033 dst1->input = dst_discard;
2034 dst1->output = xdst_queue_output;
2035
2036 dst_hold(dst);
2037 dst1->child = dst;
2038 dst1->path = dst;
2039
2040 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2041
2042 err = -ENODEV;
2043 dev = dst->dev;
2044 if (!dev)
2045 goto free_dst;
2046
2047 err = xfrm_fill_dst(xdst, dev, fl);
2048 if (err)
2049 goto free_dst;
2050
2051out:
2052 return xdst;
2053
2054free_dst:
2055 dst_release(dst1);
2056 xdst = ERR_PTR(err);
2057 goto out;
2058}
2059
Timo Teräs80c802f2010-04-07 00:30:05 +00002060static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002061xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002062 struct flow_cache_object *oldflo, void *ctx)
2063{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002064 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002065 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2066 struct xfrm_dst *xdst, *new_xdst;
2067 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2068
2069 /* Check if the policies from old bundle are usable */
2070 xdst = NULL;
2071 if (oldflo) {
2072 xdst = container_of(oldflo, struct xfrm_dst, flo);
2073 num_pols = xdst->num_pols;
2074 num_xfrms = xdst->num_xfrms;
2075 pol_dead = 0;
2076 for (i = 0; i < num_pols; i++) {
2077 pols[i] = xdst->pols[i];
2078 pol_dead |= pols[i]->walk.dead;
2079 }
2080 if (pol_dead) {
2081 dst_free(&xdst->u.dst);
2082 xdst = NULL;
2083 num_pols = 0;
2084 num_xfrms = 0;
2085 oldflo = NULL;
2086 }
2087 }
2088
2089 /* Resolve policies to use if we couldn't get them from
2090 * previous cache entry */
2091 if (xdst == NULL) {
2092 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002093 pols[0] = __xfrm_policy_lookup(net, fl, family,
2094 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002095 err = xfrm_expand_policies(fl, family, pols,
2096 &num_pols, &num_xfrms);
2097 if (err < 0)
2098 goto inc_error;
2099 if (num_pols == 0)
2100 return NULL;
2101 if (num_xfrms <= 0)
2102 goto make_dummy_bundle;
2103 }
2104
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002105 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2106 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002107 if (IS_ERR(new_xdst)) {
2108 err = PTR_ERR(new_xdst);
2109 if (err != -EAGAIN)
2110 goto error;
2111 if (oldflo == NULL)
2112 goto make_dummy_bundle;
2113 dst_hold(&xdst->u.dst);
2114 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002115 } else if (new_xdst == NULL) {
2116 num_xfrms = 0;
2117 if (oldflo == NULL)
2118 goto make_dummy_bundle;
2119 xdst->num_xfrms = 0;
2120 dst_hold(&xdst->u.dst);
2121 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002122 }
2123
2124 /* Kill the previous bundle */
2125 if (xdst) {
2126 /* The policies were stolen for newly generated bundle */
2127 xdst->num_pols = 0;
2128 dst_free(&xdst->u.dst);
2129 }
2130
2131 /* Flow cache does not have reference, it dst_free()'s,
2132 * but we do need to return one reference for original caller */
2133 dst_hold(&new_xdst->u.dst);
2134 return &new_xdst->flo;
2135
2136make_dummy_bundle:
2137 /* We found policies, but there's no bundles to instantiate:
2138 * either because the policy blocks, has no transformations or
2139 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002140 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002141 if (IS_ERR(xdst)) {
2142 xfrm_pols_put(pols, num_pols);
2143 return ERR_CAST(xdst);
2144 }
2145 xdst->num_pols = num_pols;
2146 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002147 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002148
2149 dst_hold(&xdst->u.dst);
2150 return &xdst->flo;
2151
2152inc_error:
2153 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2154error:
2155 if (xdst != NULL)
2156 dst_free(&xdst->u.dst);
2157 else
2158 xfrm_pols_put(pols, num_pols);
2159 return ERR_PTR(err);
2160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
David S. Miller2774c132011-03-01 14:59:04 -08002162static struct dst_entry *make_blackhole(struct net *net, u16 family,
2163 struct dst_entry *dst_orig)
2164{
2165 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2166 struct dst_entry *ret;
2167
2168 if (!afinfo) {
2169 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002170 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002171 } else {
2172 ret = afinfo->blackhole_route(net, dst_orig);
2173 }
2174 xfrm_policy_put_afinfo(afinfo);
2175
2176 return ret;
2177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/* Main function: finds/creates a bundle for given flow.
2180 *
2181 * At the moment we eat a raw IP route. Mostly to speed up lookups
2182 * on interfaces with disabled IPsec.
2183 */
David S. Miller452edd52011-03-02 13:27:41 -08002184struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2185 const struct flowi *fl,
2186 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002188 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002189 struct flow_cache_object *flo;
2190 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002191 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002192 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002193 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002194 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002195
Timo Teräs80c802f2010-04-07 00:30:05 +00002196 dst = NULL;
2197 xdst = NULL;
2198 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002199
Thomas Graff7944fb2007-08-25 13:46:55 -07002200 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002201 num_pols = 1;
2202 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2203 err = xfrm_expand_policies(fl, family, pols,
2204 &num_pols, &num_xfrms);
2205 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002206 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002207
2208 if (num_pols) {
2209 if (num_xfrms <= 0) {
2210 drop_pols = num_pols;
2211 goto no_transform;
2212 }
2213
2214 xdst = xfrm_resolve_and_create_bundle(
2215 pols, num_pols, fl,
2216 family, dst_orig);
2217 if (IS_ERR(xdst)) {
2218 xfrm_pols_put(pols, num_pols);
2219 err = PTR_ERR(xdst);
2220 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002221 } else if (xdst == NULL) {
2222 num_xfrms = 0;
2223 drop_pols = num_pols;
2224 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002225 }
2226
Steffen Klassertb7eea452014-06-18 12:34:21 +02002227 dst_hold(&xdst->u.dst);
2228 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002229 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002230 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Timo Teräs80c802f2010-04-07 00:30:05 +00002233 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002234 struct xfrm_flo xflo;
2235
2236 xflo.dst_orig = dst_orig;
2237 xflo.flags = flags;
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002240 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002241 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002242 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Timo Teräs80c802f2010-04-07 00:30:05 +00002244 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002245 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002246 if (flo == NULL)
2247 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002248 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002249 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002250 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002251 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002252 xdst = container_of(flo, struct xfrm_dst, flo);
2253
2254 num_pols = xdst->num_pols;
2255 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002256 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002257 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259
Timo Teräs80c802f2010-04-07 00:30:05 +00002260 dst = &xdst->u.dst;
2261 if (route == NULL && num_xfrms > 0) {
2262 /* The only case when xfrm_bundle_lookup() returns a
2263 * bundle with null route, is when the template could
2264 * not be resolved. It means policies are there, but
2265 * bundle could not be created, since we don't yet
2266 * have the xfrm_state's. We need to wait for KM to
2267 * negotiate new SA's or bail out with error.*/
2268 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002269 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002270 err = -EREMOTE;
2271 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002272 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002273
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002274 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002275
2276 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2277 goto error;
2278 }
2279
2280no_transform:
2281 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002282 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Timo Teräs80c802f2010-04-07 00:30:05 +00002284 if ((flags & XFRM_LOOKUP_ICMP) &&
2285 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2286 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002287 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002288 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002289
Timo Teräs80c802f2010-04-07 00:30:05 +00002290 for (i = 0; i < num_pols; i++)
2291 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002292
Timo Teräs80c802f2010-04-07 00:30:05 +00002293 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002295 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002296 err = -EPERM;
2297 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002298 } else if (num_xfrms > 0) {
2299 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002300 dst_release(dst_orig);
2301 } else {
2302 /* Flow passes untransformed */
2303 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002304 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002306ok:
2307 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002308 if (dst && dst->xfrm &&
2309 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2310 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002311 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Timo Teräs80c802f2010-04-07 00:30:05 +00002313nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002314 if (!(flags & XFRM_LOOKUP_ICMP)) {
2315 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002316 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002317 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002318 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002320 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002321dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002322 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2323 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002324 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002325 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327EXPORT_SYMBOL(xfrm_lookup);
2328
Steffen Klassertf92ee612014-09-16 10:08:40 +02002329/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2330 * Otherwise we may send out blackholed packets.
2331 */
2332struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2333 const struct flowi *fl,
2334 struct sock *sk, int flags)
2335{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002336 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002337 flags | XFRM_LOOKUP_QUEUE |
2338 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002339
2340 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2341 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2342
2343 return dst;
2344}
2345EXPORT_SYMBOL(xfrm_lookup_route);
2346
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002347static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002348xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002349{
2350 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002351
2352 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2353 return 0;
2354 x = skb->sp->xvec[idx];
2355 if (!x->type->reject)
2356 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002357 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360/* When skb is transformed back to its "native" form, we have to
2361 * check policy restrictions. At the moment we make this in maximally
2362 * stupid way. Shame on me. :-) Of course, connected sockets must
2363 * have policy cached at them.
2364 */
2365
2366static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002367xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 unsigned short family)
2369{
2370 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002371 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return x->id.proto == tmpl->id.proto &&
2373 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2374 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2375 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002376 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002377 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002378 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2379 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002382/*
2383 * 0 or more than 0 is returned when validation is succeeded (either bypass
2384 * because of optional transport mode, or next index of the mathced secpath
2385 * state with the template.
2386 * -1 is returned when no matching template is found.
2387 * Otherwise "-2 - errored_index" is returned.
2388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002390xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 unsigned short family)
2392{
2393 int idx = start;
2394
2395 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002396 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return start;
2398 } else
2399 start = -1;
2400 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002401 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002403 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2404 if (start == -1)
2405 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409 return start;
2410}
2411
Herbert Xud5422ef2007-12-12 10:44:16 -08002412int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2413 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002416 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 if (unlikely(afinfo == NULL))
2419 return -EAFNOSUPPORT;
2420
Herbert Xud5422ef2007-12-12 10:44:16 -08002421 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002422 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
Herbert Xud5422ef2007-12-12 10:44:16 -08002426EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
David S. Miller9a7386e2011-02-24 01:44:12 -05002428static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002431 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002432 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436
2437 return 0;
2438}
2439
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002440int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 unsigned short family)
2442{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002443 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002445 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2446 int npols = 0;
2447 int xfrm_nr;
2448 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002449 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002451 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002452 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Herbert Xud5422ef2007-12-12 10:44:16 -08002454 reverse = dir & ~XFRM_POLICY_MASK;
2455 dir &= XFRM_POLICY_MASK;
2456 fl_dir = policy_to_flow_dir(dir);
2457
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002458 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002459 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002461 }
2462
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002463 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 /* First, check used SA against their selectors. */
2466 if (skb->sp) {
2467 int i;
2468
Weilong Chen9b7a7872013-12-24 09:43:46 +08002469 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002470 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002471 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002472 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 }
2477
2478 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002479 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002480 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002481 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002482 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002483 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002484 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002487 if (!pol) {
2488 struct flow_cache_object *flo;
2489
2490 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2491 xfrm_policy_lookup, NULL);
2492 if (IS_ERR_OR_NULL(flo))
2493 pol = ERR_CAST(flo);
2494 else
2495 pol = container_of(flo, struct xfrm_policy, flo);
2496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002498 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002499 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002500 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002501 }
James Morris134b0fc2006-10-05 15:42:27 -05002502
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002503 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002504 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002505 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002506 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002507 return 0;
2508 }
2509 return 1;
2510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
James Morris9d729f72007-03-04 16:12:44 -08002512 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002514 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002515 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002516#ifdef CONFIG_XFRM_SUB_POLICY
2517 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002518 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002519 &fl, family,
2520 XFRM_POLICY_IN);
2521 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002522 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002523 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002524 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002525 }
James Morris9d729f72007-03-04 16:12:44 -08002526 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002527 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002528 }
2529 }
2530#endif
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (pol->action == XFRM_POLICY_ALLOW) {
2533 struct sec_path *sp;
2534 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002535 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002536 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002537 struct xfrm_tmpl **tpp = tp;
2538 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 int i, k;
2540
2541 if ((sp = skb->sp) == NULL)
2542 sp = &dummy;
2543
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002544 for (pi = 0; pi < npols; pi++) {
2545 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002546 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002547 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002548 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002549 }
2550 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002551 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002552 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002553 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002554 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2555 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2556 }
2557 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002558 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002559 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002560 tpp = stp;
2561 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /* For each tunnel xfrm, find the first matching tmpl.
2564 * For each tmpl before that, find corresponding xfrm.
2565 * Order is _important_. Later we will implement
2566 * some barriers, but at the moment barriers
2567 * are implied between each two transformations.
2568 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002569 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2570 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002571 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002572 if (k < -1)
2573 /* "-2 - errored_index" returned */
2574 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002575 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002580 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002581 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002585 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return 1;
2587 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002588 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002591 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002592reject_error:
2593 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return 0;
2595}
2596EXPORT_SYMBOL(__xfrm_policy_check);
2597
2598int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2599{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002600 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002602 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002603 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002605 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002606 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002610 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002611
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002612 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002613 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002614 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002615 dst = NULL;
2616 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002617 skb_dst_set(skb, dst);
2618 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620EXPORT_SYMBOL(__xfrm_route_forward);
2621
David S. Millerd49c73c2006-08-13 18:55:53 -07002622/* Optimize later using cookies and generation ids. */
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2625{
David S. Millerd49c73c2006-08-13 18:55:53 -07002626 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002627 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2628 * get validated by dst_ops->check on every use. We do this
2629 * because when a normal route referenced by an XFRM dst is
2630 * obsoleted we do not go looking around for all parent
2631 * referencing XFRM dsts so that we can invalidate them. It
2632 * is just too much work. Instead we make the checks here on
2633 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002634 *
2635 * XFRM dst A --> IPv4 dst X
2636 *
2637 * X is the "xdst->route" of A (X is also the "dst->path" of A
2638 * in this example). If X is marked obsolete, "A" will not
2639 * notice. That's what we are validating here via the
2640 * stale_bundle() check.
2641 *
2642 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002643 * dst which causes it's ->obsolete field to be set to
2644 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2645 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002646 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002647 if (dst->obsolete < 0 && !stale_bundle(dst))
2648 return dst;
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return NULL;
2651}
2652
2653static int stale_bundle(struct dst_entry *dst)
2654{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002655 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Herbert Xuaabc9762005-05-03 16:27:10 -07002658void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002661 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002662 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 dev_put(dev);
2664 }
2665}
Herbert Xuaabc9762005-05-03 16:27:10 -07002666EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668static void xfrm_link_failure(struct sk_buff *skb)
2669{
2670 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
2673static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2674{
2675 if (dst) {
2676 if (dst->obsolete) {
2677 dst_release(dst);
2678 dst = NULL;
2679 }
2680 }
2681 return dst;
2682}
2683
Paul Mooree4c17212013-05-29 07:36:25 +00002684void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002685{
Fan Duca925cf2014-01-18 09:55:27 +08002686 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002687}
Paul Mooree4c17212013-05-29 07:36:25 +00002688EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002689
2690static void xfrm_garbage_collect_deferred(struct net *net)
2691{
Fan Duca925cf2014-01-18 09:55:27 +08002692 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002693}
2694
Herbert Xu25ee3282007-12-11 09:32:34 -08002695static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 do {
2698 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2699 u32 pmtu, route_mtu_cached;
2700
2701 pmtu = dst_mtu(dst->child);
2702 xdst->child_mtu_cached = pmtu;
2703
2704 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2705
2706 route_mtu_cached = dst_mtu(xdst->route);
2707 xdst->route_mtu_cached = route_mtu_cached;
2708
2709 if (pmtu > route_mtu_cached)
2710 pmtu = route_mtu_cached;
2711
David S. Millerdefb3512010-12-08 21:16:57 -08002712 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 } while ((dst = dst->next));
2714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/* Check that the bundle accepts the flow and its components are
2717 * still valid.
2718 */
2719
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002720static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 struct dst_entry *dst = &first->u.dst;
2723 struct xfrm_dst *last;
2724 u32 mtu;
2725
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002726 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 (dst->dev && !netif_running(dst->dev)))
2728 return 0;
2729
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002730 if (dst->flags & DST_XFRM_QUEUE)
2731 return 1;
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 last = NULL;
2734
2735 do {
2736 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2739 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002740 if (xdst->xfrm_genid != dst->xfrm->genid)
2741 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002742 if (xdst->num_pols > 0 &&
2743 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 mtu = dst_mtu(dst->child);
2747 if (xdst->child_mtu_cached != mtu) {
2748 last = xdst;
2749 xdst->child_mtu_cached = mtu;
2750 }
2751
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002752 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
2754 mtu = dst_mtu(xdst->route);
2755 if (xdst->route_mtu_cached != mtu) {
2756 last = xdst;
2757 xdst->route_mtu_cached = mtu;
2758 }
2759
2760 dst = dst->child;
2761 } while (dst->xfrm);
2762
2763 if (likely(!last))
2764 return 1;
2765
2766 mtu = last->child_mtu_cached;
2767 for (;;) {
2768 dst = &last->u.dst;
2769
2770 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2771 if (mtu > last->route_mtu_cached)
2772 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002773 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 if (last == first)
2776 break;
2777
Patrick McHardybd0bf072007-07-18 01:55:52 -07002778 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 last->child_mtu_cached = mtu;
2780 }
2781
2782 return 1;
2783}
2784
David S. Miller0dbaee32010-12-13 12:52:14 -08002785static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2786{
2787 return dst_metric_advmss(dst->path);
2788}
2789
Steffen Klassertebb762f2011-11-23 02:12:51 +00002790static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002791{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002792 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2793
2794 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002795}
2796
David S. Millerf894cbf2012-07-02 21:52:24 -07002797static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2798 struct sk_buff *skb,
2799 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002800{
David S. Millerf894cbf2012-07-02 21:52:24 -07002801 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002802}
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2805{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002806 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 int err = 0;
2808 if (unlikely(afinfo == NULL))
2809 return -EINVAL;
2810 if (unlikely(afinfo->family >= NPROTO))
2811 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002812 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002814 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 else {
2816 struct dst_ops *dst_ops = afinfo->dst_ops;
2817 if (likely(dst_ops->kmem_cachep == NULL))
2818 dst_ops->kmem_cachep = xfrm_dst_cache;
2819 if (likely(dst_ops->check == NULL))
2820 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002821 if (likely(dst_ops->default_advmss == NULL))
2822 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002823 if (likely(dst_ops->mtu == NULL))
2824 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (likely(dst_ops->negative_advice == NULL))
2826 dst_ops->negative_advice = xfrm_negative_advice;
2827 if (likely(dst_ops->link_failure == NULL))
2828 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002829 if (likely(dst_ops->neigh_lookup == NULL))
2830 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002832 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002833 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002835 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002836
2837 rtnl_lock();
2838 for_each_net(net) {
2839 struct dst_ops *xfrm_dst_ops;
2840
2841 switch (afinfo->family) {
2842 case AF_INET:
2843 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2844 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002845#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002846 case AF_INET6:
2847 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2848 break;
2849#endif
2850 default:
2851 BUG();
2852 }
2853 *xfrm_dst_ops = *afinfo->dst_ops;
2854 }
2855 rtnl_unlock();
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return err;
2858}
2859EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2860
2861int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2862{
2863 int err = 0;
2864 if (unlikely(afinfo == NULL))
2865 return -EINVAL;
2866 if (unlikely(afinfo->family >= NPROTO))
2867 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002868 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2870 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2871 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002872 else
2873 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2874 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002876 spin_unlock(&xfrm_policy_afinfo_lock);
2877 if (!err) {
2878 struct dst_ops *dst_ops = afinfo->dst_ops;
2879
2880 synchronize_rcu();
2881
2882 dst_ops->kmem_cachep = NULL;
2883 dst_ops->check = NULL;
2884 dst_ops->negative_advice = NULL;
2885 dst_ops->link_failure = NULL;
2886 afinfo->garbage_collect = NULL;
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return err;
2889}
2890EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2891
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002892static void __net_init xfrm_dst_ops_init(struct net *net)
2893{
2894 struct xfrm_policy_afinfo *afinfo;
2895
Eric Dumazetef8531b2012-08-19 12:31:48 +02002896 rcu_read_lock();
2897 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002898 if (afinfo)
2899 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002900#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002901 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002902 if (afinfo)
2903 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2904#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002905 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002906}
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2909{
Jiri Pirko351638e2013-05-28 01:30:21 +00002910 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 switch (event) {
2913 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002914 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916 return NOTIFY_DONE;
2917}
2918
2919static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002920 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921};
2922
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002923#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002924static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002925{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002926 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002927 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2928 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002929 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002930 rv = xfrm_proc_init(net);
2931 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002932 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002933 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002934}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002935
2936static void xfrm_statistics_fini(struct net *net)
2937{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002938 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002939 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002940}
2941#else
2942static int __net_init xfrm_statistics_init(struct net *net)
2943{
2944 return 0;
2945}
2946
2947static void xfrm_statistics_fini(struct net *net)
2948{
2949}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002950#endif
2951
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002952static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
David S. Miller2518c7c2006-08-24 04:45:07 -07002954 unsigned int hmask, sz;
2955 int dir;
2956
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002957 if (net_eq(net, &init_net))
2958 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002960 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002961 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
David S. Miller2518c7c2006-08-24 04:45:07 -07002963 hmask = 8 - 1;
2964 sz = (hmask+1) * sizeof(struct hlist_head);
2965
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002966 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2967 if (!net->xfrm.policy_byidx)
2968 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002969 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002970
Herbert Xu53c2e282014-11-13 17:09:49 +08002971 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002972 struct xfrm_policy_hash *htab;
2973
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002974 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002975 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002976 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002977
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002978 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002979 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002980 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002981 goto out_bydst;
2982 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002983 htab->dbits4 = 32;
2984 htab->sbits4 = 32;
2985 htab->dbits6 = 128;
2986 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002987 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002988 net->xfrm.policy_hthresh.lbits4 = 32;
2989 net->xfrm.policy_hthresh.rbits4 = 32;
2990 net->xfrm.policy_hthresh.lbits6 = 128;
2991 net->xfrm.policy_hthresh.rbits6 = 128;
2992
2993 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002994
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002995 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002996 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002997 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002998 if (net_eq(net, &init_net))
2999 register_netdevice_notifier(&xfrm_dev_notifier);
3000 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003001
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003002out_bydst:
3003 for (dir--; dir >= 0; dir--) {
3004 struct xfrm_policy_hash *htab;
3005
3006 htab = &net->xfrm.policy_bydst[dir];
3007 xfrm_hash_free(htab->table, sz);
3008 }
3009 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003010out_byidx:
3011 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003014static void xfrm_policy_fini(struct net *net)
3015{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003016 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003017 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003018
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003019 flush_work(&net->xfrm.policy_hash_work);
3020#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003021 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003022#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003023 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003024
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003025 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003026
Herbert Xu53c2e282014-11-13 17:09:49 +08003027 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003028 struct xfrm_policy_hash *htab;
3029
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003030 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003031
3032 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003033 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003034 WARN_ON(!hlist_empty(htab->table));
3035 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003036 }
3037
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003038 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003039 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3040 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003041}
3042
3043static int __net_init xfrm_net_init(struct net *net)
3044{
3045 int rv;
3046
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003047 rv = xfrm_statistics_init(net);
3048 if (rv < 0)
3049 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003050 rv = xfrm_state_init(net);
3051 if (rv < 0)
3052 goto out_state;
3053 rv = xfrm_policy_init(net);
3054 if (rv < 0)
3055 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003056 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003057 rv = xfrm_sysctl_init(net);
3058 if (rv < 0)
3059 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003060 rv = flow_cache_init(net);
3061 if (rv < 0)
3062 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003063
3064 /* Initialize the per-net locks here */
3065 spin_lock_init(&net->xfrm.xfrm_state_lock);
3066 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003067 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3068
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003069 return 0;
3070
Steffen Klassert4a93f502014-03-12 09:43:17 +01003071out:
3072 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003073out_sysctl:
3074 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003075out_policy:
3076 xfrm_state_fini(net);
3077out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003078 xfrm_statistics_fini(net);
3079out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003080 return rv;
3081}
3082
3083static void __net_exit xfrm_net_exit(struct net *net)
3084{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003085 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003086 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003087 xfrm_policy_fini(net);
3088 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003089 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003090}
3091
3092static struct pernet_operations __net_initdata xfrm_net_ops = {
3093 .init = xfrm_net_init,
3094 .exit = xfrm_net_exit,
3095};
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097void __init xfrm_init(void)
3098{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003099 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 xfrm_input_init();
3101}
3102
Joy Lattenab5f5e82007-09-17 11:51:22 -07003103#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003104static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3105 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003106{
Paul Moore875179f2007-12-01 23:27:18 +11003107 struct xfrm_sec_ctx *ctx = xp->security;
3108 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003109
Paul Moore875179f2007-12-01 23:27:18 +11003110 if (ctx)
3111 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3112 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3113
Weilong Chen9b7a7872013-12-24 09:43:46 +08003114 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003115 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003116 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003117 if (sel->prefixlen_s != 32)
3118 audit_log_format(audit_buf, " src_prefixlen=%d",
3119 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003120 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003121 if (sel->prefixlen_d != 32)
3122 audit_log_format(audit_buf, " dst_prefixlen=%d",
3123 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003124 break;
3125 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003126 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003127 if (sel->prefixlen_s != 128)
3128 audit_log_format(audit_buf, " src_prefixlen=%d",
3129 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003130 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003131 if (sel->prefixlen_d != 128)
3132 audit_log_format(audit_buf, " dst_prefixlen=%d",
3133 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003134 break;
3135 }
3136}
3137
Tetsuo Handa2e710292014-04-22 21:48:30 +09003138void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003139{
3140 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003141
Paul Mooreafeb14b2007-12-21 14:58:11 -08003142 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003143 if (audit_buf == NULL)
3144 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003145 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003146 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003147 xfrm_audit_common_policyinfo(xp, audit_buf);
3148 audit_log_end(audit_buf);
3149}
3150EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3151
Paul Moore68277ac2007-12-20 20:49:33 -08003152void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003153 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003154{
3155 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003156
Paul Mooreafeb14b2007-12-21 14:58:11 -08003157 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003158 if (audit_buf == NULL)
3159 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003160 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003161 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003162 xfrm_audit_common_policyinfo(xp, audit_buf);
3163 audit_log_end(audit_buf);
3164}
3165EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3166#endif
3167
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003168#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003169static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3170 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003171{
3172 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3173 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003174 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3175 sel_cmp->family) &&
3176 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3177 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3179 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003180 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003181 }
3182 } else {
3183 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003184 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003185 }
3186 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003187 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003188}
3189
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003190static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3191 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003192{
3193 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003194 struct hlist_head *chain;
3195 u32 priority = ~0U;
3196
Fan Du283bc9f2013-11-07 17:47:50 +08003197 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003198 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003199 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003200 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3201 pol->type == type) {
3202 ret = pol;
3203 priority = ret->priority;
3204 break;
3205 }
3206 }
Fan Du8d549c42013-11-07 17:47:49 +08003207 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003208 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003209 if ((pol->priority >= priority) && ret)
3210 break;
3211
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003212 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003213 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003214 ret = pol;
3215 break;
3216 }
3217 }
3218
Li RongQing586f2eb2015-04-30 17:13:41 +08003219 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003220
Fan Du283bc9f2013-11-07 17:47:50 +08003221 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003222
3223 return ret;
3224}
3225
David S. Millerdd701752011-02-24 00:21:08 -05003226static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003227{
3228 int match = 0;
3229
3230 if (t->mode == m->mode && t->id.proto == m->proto &&
3231 (m->reqid == 0 || t->reqid == m->reqid)) {
3232 switch (t->mode) {
3233 case XFRM_MODE_TUNNEL:
3234 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003235 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3236 m->old_family) &&
3237 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3238 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003239 match = 1;
3240 }
3241 break;
3242 case XFRM_MODE_TRANSPORT:
3243 /* in case of transport mode, template does not store
3244 any IP addresses, hence we just compare mode and
3245 protocol */
3246 match = 1;
3247 break;
3248 default:
3249 break;
3250 }
3251 }
3252 return match;
3253}
3254
3255/* update endpoint address(es) of template(s) */
3256static int xfrm_policy_migrate(struct xfrm_policy *pol,
3257 struct xfrm_migrate *m, int num_migrate)
3258{
3259 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003260 int i, j, n = 0;
3261
3262 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003263 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003264 /* target policy has been deleted */
3265 write_unlock_bh(&pol->lock);
3266 return -ENOENT;
3267 }
3268
3269 for (i = 0; i < pol->xfrm_nr; i++) {
3270 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3271 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3272 continue;
3273 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003274 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3275 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003276 continue;
3277 /* update endpoints */
3278 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3279 sizeof(pol->xfrm_vec[i].id.daddr));
3280 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3281 sizeof(pol->xfrm_vec[i].saddr));
3282 pol->xfrm_vec[i].encap_family = mp->new_family;
3283 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003284 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003285 }
3286 }
3287
3288 write_unlock_bh(&pol->lock);
3289
3290 if (!n)
3291 return -ENODATA;
3292
3293 return 0;
3294}
3295
David S. Millerdd701752011-02-24 00:21:08 -05003296static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003297{
3298 int i, j;
3299
3300 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3301 return -EINVAL;
3302
3303 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003304 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3305 m[i].old_family) &&
3306 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3307 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003308 return -EINVAL;
3309 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3310 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3311 return -EINVAL;
3312
3313 /* check if there is any duplicated entry */
3314 for (j = i + 1; j < num_migrate; j++) {
3315 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3316 sizeof(m[i].old_daddr)) &&
3317 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3318 sizeof(m[i].old_saddr)) &&
3319 m[i].proto == m[j].proto &&
3320 m[i].mode == m[j].mode &&
3321 m[i].reqid == m[j].reqid &&
3322 m[i].old_family == m[j].old_family)
3323 return -EINVAL;
3324 }
3325 }
3326
3327 return 0;
3328}
3329
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003330int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003331 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003332 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003333{
3334 int i, err, nx_cur = 0, nx_new = 0;
3335 struct xfrm_policy *pol = NULL;
3336 struct xfrm_state *x, *xc;
3337 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3338 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3339 struct xfrm_migrate *mp;
3340
3341 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3342 goto out;
3343
3344 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003345 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003346 err = -ENOENT;
3347 goto out;
3348 }
3349
3350 /* Stage 2 - find and update state(s) */
3351 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003352 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003353 x_cur[nx_cur] = x;
3354 nx_cur++;
3355 if ((xc = xfrm_state_migrate(x, mp))) {
3356 x_new[nx_new] = xc;
3357 nx_new++;
3358 } else {
3359 err = -ENODATA;
3360 goto restore_state;
3361 }
3362 }
3363 }
3364
3365 /* Stage 3 - update policy */
3366 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3367 goto restore_state;
3368
3369 /* Stage 4 - delete old state(s) */
3370 if (nx_cur) {
3371 xfrm_states_put(x_cur, nx_cur);
3372 xfrm_states_delete(x_cur, nx_cur);
3373 }
3374
3375 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003376 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003377
3378 xfrm_pol_put(pol);
3379
3380 return 0;
3381out:
3382 return err;
3383
3384restore_state:
3385 if (pol)
3386 xfrm_pol_put(pol);
3387 if (nx_cur)
3388 xfrm_states_put(x_cur, nx_cur);
3389 if (nx_new)
3390 xfrm_states_delete(x_new, nx_new);
3391
3392 return err;
3393}
David S. Millere610e672007-02-08 13:29:15 -08003394EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003395#endif