blob: f4d3a125e49cf9c462753783340ed26dc16077a0 [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
Wei Yongjun29fa0b302008-12-03 00:33:09 -080058static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
59 int dir);
60
David S. Millerbc9b35a2012-05-15 15:04:57 -040061static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050062__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080063{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050064 const struct flowi4 *fl4 = &fl->u.ip4;
65
Alexey Dobriyan26bff942011-11-22 06:46:02 +000066 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
67 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050068 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
69 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
70 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
71 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080072}
73
David S. Millerbc9b35a2012-05-15 15:04:57 -040074static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050075__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080076{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050077 const struct flowi6 *fl6 = &fl->u.ip6;
78
79 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
80 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
81 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
82 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
83 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
84 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080085}
86
David S. Millerbc9b35a2012-05-15 15:04:57 -040087bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
88 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080089{
90 switch (family) {
91 case AF_INET:
92 return __xfrm4_selector_match(sel, fl);
93 case AF_INET6:
94 return __xfrm6_selector_match(sel, fl);
95 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040096 return false;
Andrew Morton77681022006-11-08 22:46:26 -080097}
98
Eric Dumazetef8531b2012-08-19 12:31:48 +020099static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
100{
101 struct xfrm_policy_afinfo *afinfo;
102
103 if (unlikely(family >= NPROTO))
104 return NULL;
105 rcu_read_lock();
106 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
107 if (unlikely(!afinfo))
108 rcu_read_unlock();
109 return afinfo;
110}
111
112static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
113{
114 rcu_read_unlock();
115}
116
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800117static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500118 const xfrm_address_t *saddr,
119 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900120 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800122 struct xfrm_policy_afinfo *afinfo;
123 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Herbert Xu25ee3282007-12-11 09:32:34 -0800125 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800127 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800129 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
133 return dst;
134}
135
136static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
137 xfrm_address_t *prev_saddr,
138 xfrm_address_t *prev_daddr,
139 int family)
140{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800141 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900142 xfrm_address_t *saddr = &x->props.saddr;
143 xfrm_address_t *daddr = &x->id.daddr;
144 struct dst_entry *dst;
145
146 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
147 saddr = x->coaddr;
148 daddr = prev_daddr;
149 }
150 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
151 saddr = prev_saddr;
152 daddr = x->coaddr;
153 }
154
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800155 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900156
157 if (!IS_ERR(dst)) {
158 if (prev_saddr != saddr)
159 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
160 if (prev_daddr != daddr)
161 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
162 }
163
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800164 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static inline unsigned long make_jiffies(long secs)
168{
169 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
170 return MAX_SCHEDULE_TIMEOUT-1;
171 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900172 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static void xfrm_policy_timer(unsigned long data)
176{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800177 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800178 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 long next = LONG_MAX;
180 int warn = 0;
181 int dir;
182
183 read_lock(&xp->lock);
184
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000185 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out;
187
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700188 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 if (xp->lft.hard_add_expires_seconds) {
191 long tmo = xp->lft.hard_add_expires_seconds +
192 xp->curlft.add_time - now;
193 if (tmo <= 0)
194 goto expired;
195 if (tmo < next)
196 next = tmo;
197 }
198 if (xp->lft.hard_use_expires_seconds) {
199 long tmo = xp->lft.hard_use_expires_seconds +
200 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
201 if (tmo <= 0)
202 goto expired;
203 if (tmo < next)
204 next = tmo;
205 }
206 if (xp->lft.soft_add_expires_seconds) {
207 long tmo = xp->lft.soft_add_expires_seconds +
208 xp->curlft.add_time - now;
209 if (tmo <= 0) {
210 warn = 1;
211 tmo = XFRM_KM_TIMEOUT;
212 }
213 if (tmo < next)
214 next = tmo;
215 }
216 if (xp->lft.soft_use_expires_seconds) {
217 long tmo = xp->lft.soft_use_expires_seconds +
218 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
219 if (tmo <= 0) {
220 warn = 1;
221 tmo = XFRM_KM_TIMEOUT;
222 }
223 if (tmo < next)
224 next = tmo;
225 }
226
227 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800228 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (next != LONG_MAX &&
230 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
231 xfrm_pol_hold(xp);
232
233out:
234 read_unlock(&xp->lock);
235 xfrm_pol_put(xp);
236 return;
237
238expired:
239 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700240 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800241 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 xfrm_pol_put(xp);
243}
244
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000245static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
246{
247 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
248
249 if (unlikely(pol->walk.dead))
250 flo = NULL;
251 else
252 xfrm_pol_hold(pol);
253
254 return flo;
255}
256
257static int xfrm_policy_flo_check(struct flow_cache_object *flo)
258{
259 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
260
261 return !pol->walk.dead;
262}
263
264static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
265{
266 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
267}
268
269static const struct flow_cache_ops xfrm_policy_fc_ops = {
270 .get = xfrm_policy_flo_get,
271 .check = xfrm_policy_flo_check,
272 .delete = xfrm_policy_flo_delete,
273};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
276 * SPD calls.
277 */
278
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800279struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct xfrm_policy *policy;
282
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700283 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800286 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700287 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700288 INIT_HLIST_NODE(&policy->bydst);
289 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700291 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100292 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800293 setup_timer(&policy->timer, xfrm_policy_timer,
294 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100295 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
296 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000297 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299 return policy;
300}
301EXPORT_SYMBOL(xfrm_policy_alloc);
302
303/* Destroy xfrm_policy: descendant resources must be released to this moment. */
304
WANG Cong64c31b32008-01-07 22:34:29 -0800305void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Herbert Xu12a169e2008-10-01 07:03:24 -0700307 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Fan Du0659eea2013-08-01 18:08:36 +0800309 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 BUG();
311
Paul Moore03e1ad72008-04-12 19:07:52 -0700312 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 kfree(policy);
314}
WANG Cong64c31b32008-01-07 22:34:29 -0800315EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100317static void xfrm_queue_purge(struct sk_buff_head *list)
318{
319 struct sk_buff *skb;
320
Steffen Klassert302a50b2013-08-28 08:47:14 +0200321 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100322 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/* Rule must be locked. Release descentant resources, announce
326 * entry dead. The rule must be unlinked from lists to the moment.
327 */
328
329static void xfrm_policy_kill(struct xfrm_policy *policy)
330{
Herbert Xu12a169e2008-10-01 07:03:24 -0700331 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Timo Teräs285ead12010-04-07 00:30:06 +0000333 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200335 if (del_timer(&policy->polq.hold_timer))
336 xfrm_pol_put(policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100337 xfrm_queue_purge(&policy->polq.hold_queue);
338
Timo Teräs285ead12010-04-07 00:30:06 +0000339 if (del_timer(&policy->timer))
340 xfrm_pol_put(policy);
341
342 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
David S. Miller2518c7c2006-08-24 04:45:07 -0700345static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
346
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800347static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700348{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800349 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700350}
351
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200352/* calculate policy hash thresholds */
353static void __get_hash_thresh(struct net *net,
354 unsigned short family, int dir,
355 u8 *dbits, u8 *sbits)
356{
357 switch (family) {
358 case AF_INET:
359 *dbits = net->xfrm.policy_bydst[dir].dbits4;
360 *sbits = net->xfrm.policy_bydst[dir].sbits4;
361 break;
362
363 case AF_INET6:
364 *dbits = net->xfrm.policy_bydst[dir].dbits6;
365 *sbits = net->xfrm.policy_bydst[dir].sbits6;
366 break;
367
368 default:
369 *dbits = 0;
370 *sbits = 0;
371 }
372}
373
David S. Miller5f803b52011-02-24 00:33:19 -0500374static struct hlist_head *policy_hash_bysel(struct net *net,
375 const struct xfrm_selector *sel,
376 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700377{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800378 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200379 unsigned int hash;
380 u8 dbits;
381 u8 sbits;
382
383 __get_hash_thresh(net, family, dir, &dbits, &sbits);
384 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700385
386 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800387 &net->xfrm.policy_inexact[dir] :
388 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700389}
390
David S. Miller5f803b52011-02-24 00:33:19 -0500391static struct hlist_head *policy_hash_direct(struct net *net,
392 const xfrm_address_t *daddr,
393 const xfrm_address_t *saddr,
394 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700395{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800396 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200397 unsigned int hash;
398 u8 dbits;
399 u8 sbits;
400
401 __get_hash_thresh(net, family, dir, &dbits, &sbits);
402 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700403
Alexey Dobriyan11219942008-11-25 17:33:06 -0800404 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700405}
406
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200407static void xfrm_dst_hash_transfer(struct net *net,
408 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700409 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200410 unsigned int nhashmask,
411 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700412{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800413 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700414 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800415 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200416 u8 dbits;
417 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700418
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800419redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800420 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700421 unsigned int h;
422
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200423 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700424 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200425 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800426 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800427 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800428 hlist_add_head(&pol->bydst, ndsttable+h);
429 h0 = h;
430 } else {
431 if (h != h0)
432 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800433 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700434 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800435 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800436 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 }
438 if (!hlist_empty(list)) {
439 entry0 = NULL;
440 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700441 }
442}
443
444static void xfrm_idx_hash_transfer(struct hlist_head *list,
445 struct hlist_head *nidxtable,
446 unsigned int nhashmask)
447{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800448 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700449 struct xfrm_policy *pol;
450
Sasha Levinb67bfe02013-02-27 17:06:00 -0800451 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700452 unsigned int h;
453
454 h = __idx_hash(pol->index, nhashmask);
455 hlist_add_head(&pol->byidx, nidxtable+h);
456 }
457}
458
459static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
460{
461 return ((old_hmask + 1) << 1) - 1;
462}
463
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800464static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700465{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800466 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700467 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
468 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800469 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700470 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700471 int i;
472
473 if (!ndst)
474 return;
475
Fan Du283bc9f2013-11-07 17:47:50 +0800476 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700477
478 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200479 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700480
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800481 net->xfrm.policy_bydst[dir].table = ndst;
482 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700483
Fan Du283bc9f2013-11-07 17:47:50 +0800484 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700485
David S. Miller44e36b42006-08-24 04:50:50 -0700486 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700487}
488
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800489static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700490{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800491 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700492 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
493 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800494 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700495 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700496 int i;
497
498 if (!nidx)
499 return;
500
Fan Du283bc9f2013-11-07 17:47:50 +0800501 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700502
503 for (i = hmask; i >= 0; i--)
504 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
505
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800506 net->xfrm.policy_byidx = nidx;
507 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700508
Fan Du283bc9f2013-11-07 17:47:50 +0800509 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700510
David S. Miller44e36b42006-08-24 04:50:50 -0700511 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700512}
513
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800514static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700515{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516 unsigned int cnt = net->xfrm.policy_count[dir];
517 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700518
519 if (total)
520 *total += cnt;
521
522 if ((hmask + 1) < xfrm_policy_hashmax &&
523 cnt > hmask)
524 return 1;
525
526 return 0;
527}
528
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800529static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700530{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800531 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700532
533 if ((hmask + 1) < xfrm_policy_hashmax &&
534 total > hmask)
535 return 1;
536
537 return 0;
538}
539
Alexey Dobriyane0710412010-01-23 13:37:10 +0000540void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700541{
Fan Du283bc9f2013-11-07 17:47:50 +0800542 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000543 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
544 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
545 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
546 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
547 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
548 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
549 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700550 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800551 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700552}
553EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700554
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700555static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800556static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700557{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800558 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700559 int dir, total;
560
561 mutex_lock(&hash_resize_mutex);
562
563 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800564 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800565 if (xfrm_bydst_should_resize(net, dir, &total))
566 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700567 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800568 if (xfrm_byidx_should_resize(net, total))
569 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700570
571 mutex_unlock(&hash_resize_mutex);
572}
573
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200574static void xfrm_hash_rebuild(struct work_struct *work)
575{
576 struct net *net = container_of(work, struct net,
577 xfrm.policy_hthresh.work);
578 unsigned int hmask;
579 struct xfrm_policy *pol;
580 struct xfrm_policy *policy;
581 struct hlist_head *chain;
582 struct hlist_head *odst;
583 struct hlist_node *newpos;
584 int i;
585 int dir;
586 unsigned seq;
587 u8 lbits4, rbits4, lbits6, rbits6;
588
589 mutex_lock(&hash_resize_mutex);
590
591 /* read selector prefixlen thresholds */
592 do {
593 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
594
595 lbits4 = net->xfrm.policy_hthresh.lbits4;
596 rbits4 = net->xfrm.policy_hthresh.rbits4;
597 lbits6 = net->xfrm.policy_hthresh.lbits6;
598 rbits6 = net->xfrm.policy_hthresh.rbits6;
599 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
600
601 write_lock_bh(&net->xfrm.xfrm_policy_lock);
602
603 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800604 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200605 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
606 hmask = net->xfrm.policy_bydst[dir].hmask;
607 odst = net->xfrm.policy_bydst[dir].table;
608 for (i = hmask; i >= 0; i--)
609 INIT_HLIST_HEAD(odst + i);
610 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
611 /* dir out => dst = remote, src = local */
612 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
613 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
614 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
615 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
616 } else {
617 /* dir in/fwd => dst = local, src = remote */
618 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
619 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
620 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
621 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
622 }
623 }
624
625 /* re-insert all policies by order of creation */
626 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
627 newpos = NULL;
628 chain = policy_hash_bysel(net, &policy->selector,
629 policy->family,
630 xfrm_policy_id2dir(policy->index));
631 hlist_for_each_entry(pol, chain, bydst) {
632 if (policy->priority >= pol->priority)
633 newpos = &pol->bydst;
634 else
635 break;
636 }
637 if (newpos)
638 hlist_add_behind(&policy->bydst, newpos);
639 else
640 hlist_add_head(&policy->bydst, chain);
641 }
642
643 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
644
645 mutex_unlock(&hash_resize_mutex);
646}
647
648void xfrm_policy_hash_rebuild(struct net *net)
649{
650 schedule_work(&net->xfrm.policy_hthresh.work);
651}
652EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/* Generate new index... KAME seems to generate them ordered by cost
655 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800656static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 static u32 idx_generator;
659
660 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700661 struct hlist_head *list;
662 struct xfrm_policy *p;
663 u32 idx;
664 int found;
665
Fan Due682adf02013-11-07 17:47:48 +0800666 if (!index) {
667 idx = (idx_generator | dir);
668 idx_generator += 8;
669 } else {
670 idx = index;
671 index = 0;
672 }
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (idx == 0)
675 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800676 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800678 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 if (p->index == idx) {
680 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700684 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return idx;
686 }
687}
688
David S. Miller2518c7c2006-08-24 04:45:07 -0700689static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
690{
691 u32 *p1 = (u32 *) s1;
692 u32 *p2 = (u32 *) s2;
693 int len = sizeof(struct xfrm_selector) / sizeof(u32);
694 int i;
695
696 for (i = 0; i < len; i++) {
697 if (p1[i] != p2[i])
698 return 1;
699 }
700
701 return 0;
702}
703
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100704static void xfrm_policy_requeue(struct xfrm_policy *old,
705 struct xfrm_policy *new)
706{
707 struct xfrm_policy_queue *pq = &old->polq;
708 struct sk_buff_head list;
709
710 __skb_queue_head_init(&list);
711
712 spin_lock_bh(&pq->hold_queue.lock);
713 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200714 if (del_timer(&pq->hold_timer))
715 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100716 spin_unlock_bh(&pq->hold_queue.lock);
717
718 if (skb_queue_empty(&list))
719 return;
720
721 pq = &new->polq;
722
723 spin_lock_bh(&pq->hold_queue.lock);
724 skb_queue_splice(&list, &pq->hold_queue);
725 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200726 if (!mod_timer(&pq->hold_timer, jiffies))
727 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100728 spin_unlock_bh(&pq->hold_queue.lock);
729}
730
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100731static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
732 struct xfrm_policy *pol)
733{
734 u32 mark = policy->mark.v & policy->mark.m;
735
736 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
737 return true;
738
739 if ((mark & pol->mark.m) == pol->mark.v &&
740 policy->priority == pol->priority)
741 return true;
742
743 return false;
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
747{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800748 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 struct xfrm_policy *pol;
750 struct xfrm_policy *delpol;
751 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800752 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Fan Du283bc9f2013-11-07 17:47:50 +0800754 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800755 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700756 delpol = NULL;
757 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800758 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800759 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700760 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100761 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800762 xfrm_sec_ctx_match(pol->security, policy->security) &&
763 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800765 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EEXIST;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 delpol = pol;
769 if (policy->priority > pol->priority)
770 continue;
771 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800772 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 continue;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (delpol)
776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700779 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700780 else
781 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800783 net->xfrm.policy_count[dir]++;
Fan Duca925cf2014-01-18 09:55:27 +0800784 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800785
786 /* After previous checking, family can either be AF_INET or AF_INET6 */
787 if (policy->family == AF_INET)
788 rt_genid_bump_ipv4(net);
789 else
790 rt_genid_bump_ipv6(net);
791
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100792 if (delpol) {
793 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800794 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100795 }
Fan Due682adf02013-11-07 17:47:48 +0800796 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800797 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800798 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 policy->curlft.use_time = 0;
800 if (!mod_timer(&policy->timer, jiffies + HZ))
801 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800802 list_add(&policy->walk.all, &net->xfrm.policy_all);
Fan Du283bc9f2013-11-07 17:47:50 +0800803 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
David S. Miller9b78a822005-12-22 07:39:48 -0800805 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800807 else if (xfrm_bydst_should_resize(net, dir, NULL))
808 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0;
811}
812EXPORT_SYMBOL(xfrm_policy_insert);
813
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000814struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
815 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800816 struct xfrm_sec_ctx *ctx, int delete,
817 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
David S. Miller2518c7c2006-08-24 04:45:07 -0700819 struct xfrm_policy *pol, *ret;
820 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Eric Parisef41aaa2007-03-07 15:37:58 -0800822 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800823 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800824 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700825 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800826 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000828 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700829 !selector_cmp(sel, &pol->selector) &&
830 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700832 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700833 *err = security_xfrm_policy_delete(
834 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800835 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800836 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800837 return pol;
838 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800839 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 }
841 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 break;
843 }
844 }
Fan Du283bc9f2013-11-07 17:47:50 +0800845 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000847 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Trent Jaegerdf718372005-12-13 23:12:27 -0800851EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000853struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
854 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 struct xfrm_policy *pol, *ret;
857 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Herbert Xub5505c62007-05-14 02:15:47 -0700859 *err = -ENOENT;
860 if (xfrm_policy_id2dir(id) != dir)
861 return NULL;
862
Eric Parisef41aaa2007-03-07 15:37:58 -0800863 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800864 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800865 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800867 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000868 if (pol->type == type && pol->index == id &&
869 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700872 *err = security_xfrm_policy_delete(
873 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800874 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800875 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800876 return pol;
877 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800878 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700879 }
880 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 }
883 }
Fan Du283bc9f2013-11-07 17:47:50 +0800884 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000886 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700887 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700888 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890EXPORT_SYMBOL(xfrm_policy_byid);
891
Joy Latten4aa2e622007-06-04 19:05:57 -0400892#ifdef CONFIG_SECURITY_NETWORK_XFRM
893static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900894xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Joy Latten4aa2e622007-06-04 19:05:57 -0400896 int dir, err = 0;
897
898 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
899 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400900 int i;
901
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800903 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400904 if (pol->type != type)
905 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700906 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400907 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900908 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400909 return err;
910 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900911 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800912 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800913 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800914 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400915 bydst) {
916 if (pol->type != type)
917 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700918 err = security_xfrm_policy_delete(
919 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400920 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700921 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900922 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400923 return err;
924 }
925 }
926 }
927 }
928 return err;
929}
930#else
931static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900932xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400933{
934 return 0;
935}
936#endif
937
Tetsuo Handa2e710292014-04-22 21:48:30 +0900938int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400939{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000940 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Fan Du283bc9f2013-11-07 17:47:50 +0800942 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400943
Tetsuo Handa2e710292014-04-22 21:48:30 +0900944 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400945 if (err)
946 goto out;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700949 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800950 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700951
952 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800953 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800954 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700955 if (pol->type != type)
956 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000957 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800958 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000959 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Tetsuo Handa2e710292014-04-22 21:48:30 +0900961 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600962
David S. Miller2518c7c2006-08-24 04:45:07 -0700963 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Fan Du283bc9f2013-11-07 17:47:50 +0800965 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700966 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700968
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800969 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700970 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800971 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800972 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700973 bydst) {
974 if (pol->type != type)
975 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000976 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800977 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000978 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700979
Tetsuo Handa2e710292014-04-22 21:48:30 +0900980 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700981 xfrm_policy_kill(pol);
982
Fan Du283bc9f2013-11-07 17:47:50 +0800983 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 goto again2;
985 }
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000989 if (!cnt)
990 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400991out:
Fan Du283bc9f2013-11-07 17:47:50 +0800992 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995EXPORT_SYMBOL(xfrm_policy_flush);
996
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800997int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800998 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 void *data)
1000{
Herbert Xu12a169e2008-10-01 07:03:24 -07001001 struct xfrm_policy *pol;
1002 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001003 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Timo Teras4c563f72008-02-28 21:31:08 -08001005 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1006 walk->type != XFRM_POLICY_TYPE_ANY)
1007 return -EINVAL;
1008
Herbert Xu12a169e2008-10-01 07:03:24 -07001009 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001010 return 0;
1011
Fan Du283bc9f2013-11-07 17:47:50 +08001012 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001013 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001014 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001015 else
1016 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001017 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001018 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001019 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001020 pol = container_of(x, struct xfrm_policy, walk);
1021 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1022 walk->type != pol->type)
1023 continue;
1024 error = func(pol, xfrm_policy_id2dir(pol->index),
1025 walk->seq, data);
1026 if (error) {
1027 list_move_tail(&walk->walk.all, &x->all);
1028 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001029 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001030 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001032 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001033 error = -ENOENT;
1034 goto out;
1035 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001036 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037out:
Fan Du283bc9f2013-11-07 17:47:50 +08001038 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return error;
1040}
1041EXPORT_SYMBOL(xfrm_policy_walk);
1042
Herbert Xu12a169e2008-10-01 07:03:24 -07001043void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1044{
1045 INIT_LIST_HEAD(&walk->walk.all);
1046 walk->walk.dead = 1;
1047 walk->type = type;
1048 walk->seq = 0;
1049}
1050EXPORT_SYMBOL(xfrm_policy_walk_init);
1051
Fan Du283bc9f2013-11-07 17:47:50 +08001052void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001053{
1054 if (list_empty(&walk->walk.all))
1055 return;
1056
Fan Du283bc9f2013-11-07 17:47:50 +08001057 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001058 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001059 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001060}
1061EXPORT_SYMBOL(xfrm_policy_walk_done);
1062
James Morris134b0fc2006-10-05 15:42:27 -05001063/*
1064 * Find policy to apply to this flow.
1065 *
1066 * Returns 0 if policy found, else an -errno.
1067 */
David S. Millerf299d552011-02-24 01:23:30 -05001068static int xfrm_policy_match(const struct xfrm_policy *pol,
1069 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001070 u8 type, u16 family, int dir)
1071{
David S. Millerf299d552011-02-24 01:23:30 -05001072 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001073 int ret = -ESRCH;
1074 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001075
1076 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001077 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001078 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001079 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001080
1081 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001082 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001083 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001084 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001085
James Morris134b0fc2006-10-05 15:42:27 -05001086 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001087}
1088
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001089static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001090 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001091 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
James Morris134b0fc2006-10-05 15:42:27 -05001093 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001094 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001095 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001096 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001097 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001098
1099 daddr = xfrm_flowi_daddr(fl, family);
1100 saddr = xfrm_flowi_saddr(fl, family);
1101 if (unlikely(!daddr || !saddr))
1102 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Fan Du283bc9f2013-11-07 17:47:50 +08001104 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001105 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001106 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001107 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001108 err = xfrm_policy_match(pol, fl, type, family, dir);
1109 if (err) {
1110 if (err == -ESRCH)
1111 continue;
1112 else {
1113 ret = ERR_PTR(err);
1114 goto fail;
1115 }
1116 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001117 ret = pol;
1118 priority = ret->priority;
1119 break;
1120 }
1121 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001122 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001123 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001124 err = xfrm_policy_match(pol, fl, type, family, dir);
1125 if (err) {
1126 if (err == -ESRCH)
1127 continue;
1128 else {
1129 ret = ERR_PTR(err);
1130 goto fail;
1131 }
1132 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001133 ret = pol;
1134 break;
1135 }
1136 }
David S. Milleracba48e2006-08-25 15:46:46 -07001137 if (ret)
1138 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001139fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001140 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001141
David S. Miller2518c7c2006-08-24 04:45:07 -07001142 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001143}
1144
Timo Teräs80c802f2010-04-07 00:30:05 +00001145static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001146__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001147{
1148#ifdef CONFIG_XFRM_SUB_POLICY
1149 struct xfrm_policy *pol;
1150
1151 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1152 if (pol != NULL)
1153 return pol;
1154#endif
1155 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1156}
1157
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001158static int flow_to_policy_dir(int dir)
1159{
1160 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1161 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1162 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1163 return dir;
1164
1165 switch (dir) {
1166 default:
1167 case FLOW_DIR_IN:
1168 return XFRM_POLICY_IN;
1169 case FLOW_DIR_OUT:
1170 return XFRM_POLICY_OUT;
1171 case FLOW_DIR_FWD:
1172 return XFRM_POLICY_FWD;
1173 }
1174}
1175
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001176static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001177xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001178 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001179{
1180 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001181
1182 if (old_obj)
1183 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001184
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001185 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001186 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001187 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001188
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001189 /* Resolver returns two references:
1190 * one for cache and one for caller of flow_cache_lookup() */
1191 xfrm_pol_hold(pol);
1192
1193 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Trent Jaegerdf718372005-12-13 23:12:27 -08001196static inline int policy_to_flow_dir(int dir)
1197{
1198 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001199 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1200 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1201 return dir;
1202 switch (dir) {
1203 default:
1204 case XFRM_POLICY_IN:
1205 return FLOW_DIR_IN;
1206 case XFRM_POLICY_OUT:
1207 return FLOW_DIR_OUT;
1208 case XFRM_POLICY_FWD:
1209 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001210 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001211}
1212
David S. Millerdee9f4b2011-02-22 18:44:31 -08001213static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1214 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001217 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Fan Du283bc9f2013-11-07 17:47:50 +08001219 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001221 bool match = xfrm_selector_match(&pol->selector, fl,
1222 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001223 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001224
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001225 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001226 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1227 pol = NULL;
1228 goto out;
1229 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001230 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001231 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001232 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001233 if (!err)
1234 xfrm_pol_hold(pol);
1235 else if (err == -ESRCH)
1236 pol = NULL;
1237 else
1238 pol = ERR_PTR(err);
1239 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 pol = NULL;
1241 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001242out:
Fan Du283bc9f2013-11-07 17:47:50 +08001243 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return pol;
1245}
1246
1247static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1248{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001249 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001250
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001251 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001252 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 xfrm_pol_hold(pol);
1254}
1255
1256static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1257 int dir)
1258{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001259 struct net *net = xp_net(pol);
1260
Herbert Xu53c2e282014-11-13 17:09:49 +08001261 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001262 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Herbert Xu53c2e282014-11-13 17:09:49 +08001264 /* Socket policies are not hashed. */
1265 if (!hlist_unhashed(&pol->bydst)) {
1266 hlist_del(&pol->bydst);
1267 hlist_del(&pol->byidx);
1268 }
1269
1270 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001271 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001272
1273 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Herbert Xu53c2e282014-11-13 17:09:49 +08001276static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1277{
1278 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1279}
1280
1281static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1282{
1283 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1284}
1285
Herbert Xu4666faa2005-06-18 22:43:22 -07001286int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Fan Du283bc9f2013-11-07 17:47:50 +08001288 struct net *net = xp_net(pol);
1289
1290 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001292 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001297 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
David S. Millera70fcb02006-03-20 19:18:52 -08001299EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1302{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001303 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 struct xfrm_policy *old_pol;
1305
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001306#ifdef CONFIG_XFRM_SUB_POLICY
1307 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1308 return -EINVAL;
1309#endif
1310
Fan Du283bc9f2013-11-07 17:47:50 +08001311 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 old_pol = sk->sk_policy[dir];
1313 sk->sk_policy[dir] = pol;
1314 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001315 pol->curlft.add_time = get_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001316 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001317 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001319 if (old_pol) {
1320 if (pol)
1321 xfrm_policy_requeue(old_pol, pol);
1322
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001323 /* Unlinking succeeds always. This is the only function
1324 * allowed to delete or replace socket policy.
1325 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001326 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001327 }
Fan Du283bc9f2013-11-07 17:47:50 +08001328 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (old_pol) {
1331 xfrm_policy_kill(old_pol);
1332 }
1333 return 0;
1334}
1335
David S. Millerd3e40a92011-02-24 01:25:41 -05001336static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001338 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001339 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (newp) {
1342 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001343 if (security_xfrm_policy_clone(old->security,
1344 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001345 kfree(newp);
1346 return NULL; /* ENOMEM */
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 newp->lft = old->lft;
1349 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001350 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 newp->action = old->action;
1352 newp->flags = old->flags;
1353 newp->xfrm_nr = old->xfrm_nr;
1354 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001355 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 memcpy(newp->xfrm_vec, old->xfrm_vec,
1357 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001358 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001359 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001360 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 xfrm_pol_put(newp);
1362 }
1363 return newp;
1364}
1365
1366int __xfrm_sk_clone_policy(struct sock *sk)
1367{
1368 struct xfrm_policy *p0 = sk->sk_policy[0],
1369 *p1 = sk->sk_policy[1];
1370
1371 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1372 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1373 return -ENOMEM;
1374 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1375 return -ENOMEM;
1376 return 0;
1377}
1378
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001379static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001380xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001381 unsigned short family)
1382{
1383 int err;
1384 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1385
1386 if (unlikely(afinfo == NULL))
1387 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001388 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001389 xfrm_policy_put_afinfo(afinfo);
1390 return err;
1391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/* Resolve list of templates for the flow, given policy. */
1394
1395static int
David S. Millera6c2e612011-02-22 18:35:39 -08001396xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1397 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001399 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int nx;
1401 int i, error;
1402 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1403 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001404 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Weilong Chen9b7a7872013-12-24 09:43:46 +08001406 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct xfrm_state *x;
1408 xfrm_address_t *remote = daddr;
1409 xfrm_address_t *local = saddr;
1410 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1411
Joakim Koskela48b8d782007-07-26 00:08:42 -07001412 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1413 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 remote = &tmpl->id.daddr;
1415 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001416 if (xfrm_addr_any(local, tmpl->encap_family)) {
1417 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001418 if (error)
1419 goto fail;
1420 local = &tmp;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1425
1426 if (x && x->km.state == XFRM_STATE_VALID) {
1427 xfrm[nx++] = x;
1428 daddr = remote;
1429 saddr = local;
1430 continue;
1431 }
1432 if (x) {
1433 error = (x->km.state == XFRM_STATE_ERROR ?
1434 -EINVAL : -EAGAIN);
1435 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001436 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001437 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (!tmpl->optional)
1441 goto fail;
1442 }
1443 return nx;
1444
1445fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001446 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 xfrm_state_put(xfrm[nx]);
1448 return error;
1449}
1450
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001451static int
David S. Millera6c2e612011-02-22 18:35:39 -08001452xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1453 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001454{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001455 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1456 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001457 int cnx = 0;
1458 int error;
1459 int ret;
1460 int i;
1461
1462 for (i = 0; i < npols; i++) {
1463 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1464 error = -ENOBUFS;
1465 goto fail;
1466 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001467
1468 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001469 if (ret < 0) {
1470 error = ret;
1471 goto fail;
1472 } else
1473 cnx += ret;
1474 }
1475
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001476 /* found states are sorted for outbound processing */
1477 if (npols > 1)
1478 xfrm_state_sort(xfrm, tpp, cnx, family);
1479
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001480 return cnx;
1481
1482 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001483 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001484 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001485 return error;
1486
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/* Check that the bundle accepts the flow and its components are
1490 * still valid.
1491 */
1492
David S. Miller05d84022011-02-22 17:47:10 -08001493static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001494{
1495 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1496 int tos;
1497
1498 if (!afinfo)
1499 return -EINVAL;
1500
1501 tos = afinfo->get_tos(fl);
1502
1503 xfrm_policy_put_afinfo(afinfo);
1504
1505 return tos;
1506}
1507
Timo Teräs80c802f2010-04-07 00:30:05 +00001508static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1509{
1510 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1511 struct dst_entry *dst = &xdst->u.dst;
1512
1513 if (xdst->route == NULL) {
1514 /* Dummy bundle - if it has xfrms we were not
1515 * able to build bundle as template resolution failed.
1516 * It means we need to try again resolving. */
1517 if (xdst->num_xfrms > 0)
1518 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001519 } else if (dst->flags & DST_XFRM_QUEUE) {
1520 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001521 } else {
1522 /* Real bundle */
1523 if (stale_bundle(dst))
1524 return NULL;
1525 }
1526
1527 dst_hold(dst);
1528 return flo;
1529}
1530
1531static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1532{
1533 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1534 struct dst_entry *dst = &xdst->u.dst;
1535
1536 if (!xdst->route)
1537 return 0;
1538 if (stale_bundle(dst))
1539 return 0;
1540
1541 return 1;
1542}
1543
1544static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1545{
1546 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1547 struct dst_entry *dst = &xdst->u.dst;
1548
1549 dst_free(dst);
1550}
1551
1552static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1553 .get = xfrm_bundle_flo_get,
1554 .check = xfrm_bundle_flo_check,
1555 .delete = xfrm_bundle_flo_delete,
1556};
1557
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001558static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001559{
1560 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001561 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001562 struct xfrm_dst *xdst;
1563
1564 if (!afinfo)
1565 return ERR_PTR(-EINVAL);
1566
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001567 switch (family) {
1568 case AF_INET:
1569 dst_ops = &net->xfrm.xfrm4_dst_ops;
1570 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001571#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001572 case AF_INET6:
1573 dst_ops = &net->xfrm.xfrm6_dst_ops;
1574 break;
1575#endif
1576 default:
1577 BUG();
1578 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001579 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001580
Madalin Bucurd4cae562011-09-26 07:04:36 +00001581 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001582 struct dst_entry *dst = &xdst->u.dst;
1583
1584 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001585 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001586 if (afinfo->init_dst)
1587 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001588 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001589 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001590
Madalin Bucurd4cae562011-09-26 07:04:36 +00001591 xfrm_policy_put_afinfo(afinfo);
1592
Herbert Xu25ee3282007-12-11 09:32:34 -08001593 return xdst;
1594}
1595
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001596static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1597 int nfheader_len)
1598{
1599 struct xfrm_policy_afinfo *afinfo =
1600 xfrm_policy_get_afinfo(dst->ops->family);
1601 int err;
1602
1603 if (!afinfo)
1604 return -EINVAL;
1605
1606 err = afinfo->init_path(path, dst, nfheader_len);
1607
1608 xfrm_policy_put_afinfo(afinfo);
1609
1610 return err;
1611}
1612
Herbert Xu87c1e122010-03-02 02:51:56 +00001613static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001614 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001615{
1616 struct xfrm_policy_afinfo *afinfo =
1617 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1618 int err;
1619
1620 if (!afinfo)
1621 return -EINVAL;
1622
Herbert Xu87c1e122010-03-02 02:51:56 +00001623 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001624
1625 xfrm_policy_put_afinfo(afinfo);
1626
1627 return err;
1628}
1629
Timo Teräs80c802f2010-04-07 00:30:05 +00001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1632 * all the metrics... Shortly, bundle a bundle.
1633 */
1634
Herbert Xu25ee3282007-12-11 09:32:34 -08001635static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1636 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001637 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001638 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001640 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001641 unsigned long now = jiffies;
1642 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001643 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001644 struct dst_entry *dst_prev = NULL;
1645 struct dst_entry *dst0 = NULL;
1646 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001648 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001649 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001650 int trailer_len = 0;
1651 int tos;
1652 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001653 xfrm_address_t saddr, daddr;
1654
1655 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001656
1657 tos = xfrm_get_tos(fl, family);
1658 err = tos;
1659 if (tos < 0)
1660 goto put_states;
1661
1662 dst_hold(dst);
1663
1664 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001665 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001666 struct dst_entry *dst1 = &xdst->u.dst;
1667
1668 err = PTR_ERR(xdst);
1669 if (IS_ERR(xdst)) {
1670 dst_release(dst);
1671 goto put_states;
1672 }
1673
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001674 if (xfrm[i]->sel.family == AF_UNSPEC) {
1675 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1676 xfrm_af2proto(family));
1677 if (!inner_mode) {
1678 err = -EAFNOSUPPORT;
1679 dst_release(dst);
1680 goto put_states;
1681 }
1682 } else
1683 inner_mode = xfrm[i]->inner_mode;
1684
Herbert Xu25ee3282007-12-11 09:32:34 -08001685 if (!dst_prev)
1686 dst0 = dst1;
1687 else {
1688 dst_prev->child = dst_clone(dst1);
1689 dst1->flags |= DST_NOHASH;
1690 }
1691
1692 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001693 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001694
1695 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1696 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001697 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1698 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001699 err = PTR_ERR(dst);
1700 if (IS_ERR(dst))
1701 goto put_states;
1702 } else
1703 dst_hold(dst);
1704
1705 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001706 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001707
David S. Millerf5b0a872012-07-19 12:31:33 -07001708 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001709 dst1->flags |= DST_HOST;
1710 dst1->lastuse = now;
1711
1712 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001713 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001714
1715 dst1->next = dst_prev;
1716 dst_prev = dst1;
1717
1718 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001719 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1720 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001721 trailer_len += xfrm[i]->props.trailer_len;
1722 }
1723
1724 dst_prev->child = dst;
1725 dst0->path = dst;
1726
1727 err = -ENODEV;
1728 dev = dst->dev;
1729 if (!dev)
1730 goto free_dst;
1731
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001732 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001733 xfrm_init_pmtu(dst_prev);
1734
1735 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1736 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1737
Herbert Xu87c1e122010-03-02 02:51:56 +00001738 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001739 if (err)
1740 goto free_dst;
1741
1742 dst_prev->header_len = header_len;
1743 dst_prev->trailer_len = trailer_len;
1744 header_len -= xdst->u.dst.xfrm->props.header_len;
1745 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1746 }
1747
1748out:
1749 return dst0;
1750
1751put_states:
1752 for (; i < nx; i++)
1753 xfrm_state_put(xfrm[i]);
1754free_dst:
1755 if (dst0)
1756 dst_free(dst0);
1757 dst0 = ERR_PTR(err);
1758 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Ying Xueda7c2242014-01-08 10:26:39 +08001761#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001762static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001763{
1764 if (!*target) {
1765 *target = kmalloc(size, GFP_ATOMIC);
1766 if (!*target)
1767 return -ENOMEM;
1768 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001769
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001770 memcpy(*target, src, size);
1771 return 0;
1772}
Ying Xueda7c2242014-01-08 10:26:39 +08001773#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001774
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001775static int xfrm_dst_update_parent(struct dst_entry *dst,
1776 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001777{
1778#ifdef CONFIG_XFRM_SUB_POLICY
1779 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1780 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1781 sel, sizeof(*sel));
1782#else
1783 return 0;
1784#endif
1785}
1786
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001787static int xfrm_dst_update_origin(struct dst_entry *dst,
1788 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001789{
1790#ifdef CONFIG_XFRM_SUB_POLICY
1791 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1792 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1793#else
1794 return 0;
1795#endif
1796}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David S. Miller73ff93c2011-02-22 18:33:42 -08001798static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001799 struct xfrm_policy **pols,
1800 int *num_pols, int *num_xfrms)
1801{
1802 int i;
1803
1804 if (*num_pols == 0 || !pols[0]) {
1805 *num_pols = 0;
1806 *num_xfrms = 0;
1807 return 0;
1808 }
1809 if (IS_ERR(pols[0]))
1810 return PTR_ERR(pols[0]);
1811
1812 *num_xfrms = pols[0]->xfrm_nr;
1813
1814#ifdef CONFIG_XFRM_SUB_POLICY
1815 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1816 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1817 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1818 XFRM_POLICY_TYPE_MAIN,
1819 fl, family,
1820 XFRM_POLICY_OUT);
1821 if (pols[1]) {
1822 if (IS_ERR(pols[1])) {
1823 xfrm_pols_put(pols, *num_pols);
1824 return PTR_ERR(pols[1]);
1825 }
Weilong Chen02d08922013-12-24 09:43:48 +08001826 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001827 (*num_xfrms) += pols[1]->xfrm_nr;
1828 }
1829 }
1830#endif
1831 for (i = 0; i < *num_pols; i++) {
1832 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1833 *num_xfrms = -1;
1834 break;
1835 }
1836 }
1837
1838 return 0;
1839
1840}
1841
1842static struct xfrm_dst *
1843xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001844 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001845 struct dst_entry *dst_orig)
1846{
1847 struct net *net = xp_net(pols[0]);
1848 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1849 struct dst_entry *dst;
1850 struct xfrm_dst *xdst;
1851 int err;
1852
1853 /* Try to instantiate a bundle */
1854 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001855 if (err <= 0) {
1856 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001857 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1858 return ERR_PTR(err);
1859 }
1860
1861 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1862 if (IS_ERR(dst)) {
1863 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1864 return ERR_CAST(dst);
1865 }
1866
1867 xdst = (struct xfrm_dst *)dst;
1868 xdst->num_xfrms = err;
1869 if (num_pols > 1)
1870 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1871 else
1872 err = xfrm_dst_update_origin(dst, fl);
1873 if (unlikely(err)) {
1874 dst_free(dst);
1875 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1876 return ERR_PTR(err);
1877 }
1878
1879 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001880 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001881 xdst->policy_genid = atomic_read(&pols[0]->genid);
1882
1883 return xdst;
1884}
1885
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001886static void xfrm_policy_queue_process(unsigned long arg)
1887{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001888 struct sk_buff *skb;
1889 struct sock *sk;
1890 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001891 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1892 struct xfrm_policy_queue *pq = &pol->polq;
1893 struct flowi fl;
1894 struct sk_buff_head list;
1895
1896 spin_lock(&pq->hold_queue.lock);
1897 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001898 if (!skb) {
1899 spin_unlock(&pq->hold_queue.lock);
1900 goto out;
1901 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001902 dst = skb_dst(skb);
1903 sk = skb->sk;
1904 xfrm_decode_session(skb, &fl, dst->ops->family);
1905 spin_unlock(&pq->hold_queue.lock);
1906
1907 dst_hold(dst->path);
1908 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1909 sk, 0);
1910 if (IS_ERR(dst))
1911 goto purge_queue;
1912
1913 if (dst->flags & DST_XFRM_QUEUE) {
1914 dst_release(dst);
1915
1916 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1917 goto purge_queue;
1918
1919 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001920 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1921 xfrm_pol_hold(pol);
1922 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001923 }
1924
1925 dst_release(dst);
1926
1927 __skb_queue_head_init(&list);
1928
1929 spin_lock(&pq->hold_queue.lock);
1930 pq->timeout = 0;
1931 skb_queue_splice_init(&pq->hold_queue, &list);
1932 spin_unlock(&pq->hold_queue.lock);
1933
1934 while (!skb_queue_empty(&list)) {
1935 skb = __skb_dequeue(&list);
1936
1937 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1938 dst_hold(skb_dst(skb)->path);
1939 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1940 &fl, skb->sk, 0);
1941 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001942 kfree_skb(skb);
1943 continue;
1944 }
1945
1946 nf_reset(skb);
1947 skb_dst_drop(skb);
1948 skb_dst_set(skb, dst);
1949
Fabian Frederick5c1e9f22014-10-25 17:27:09 +02001950 dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001951 }
1952
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001953out:
1954 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001955 return;
1956
1957purge_queue:
1958 pq->timeout = 0;
1959 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001960 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001961}
1962
Eric Dumazetaad88722014-04-15 13:47:15 -04001963static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001964{
1965 unsigned long sched_next;
1966 struct dst_entry *dst = skb_dst(skb);
1967 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001968 struct xfrm_policy *pol = xdst->pols[0];
1969 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001970
Eric Dumazetd0bf4a92014-09-29 13:29:15 -07001971 if (unlikely(skb_fclone_busy(skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001972 kfree_skb(skb);
1973 return 0;
1974 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001975
1976 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1977 kfree_skb(skb);
1978 return -EAGAIN;
1979 }
1980
1981 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001982
1983 spin_lock_bh(&pq->hold_queue.lock);
1984
1985 if (!pq->timeout)
1986 pq->timeout = XFRM_QUEUE_TMO_MIN;
1987
1988 sched_next = jiffies + pq->timeout;
1989
1990 if (del_timer(&pq->hold_timer)) {
1991 if (time_before(pq->hold_timer.expires, sched_next))
1992 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001993 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001994 }
1995
1996 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001997 if (!mod_timer(&pq->hold_timer, sched_next))
1998 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001999
2000 spin_unlock_bh(&pq->hold_queue.lock);
2001
2002 return 0;
2003}
2004
2005static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002006 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002007 const struct flowi *fl,
2008 int num_xfrms,
2009 u16 family)
2010{
2011 int err;
2012 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002013 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002014 struct dst_entry *dst1;
2015 struct xfrm_dst *xdst;
2016
2017 xdst = xfrm_alloc_dst(net, family);
2018 if (IS_ERR(xdst))
2019 return xdst;
2020
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002021 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2022 net->xfrm.sysctl_larval_drop ||
2023 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002024 return xdst;
2025
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002026 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002027 dst1 = &xdst->u.dst;
2028 dst_hold(dst);
2029 xdst->route = dst;
2030
2031 dst_copy_metrics(dst1, dst);
2032
2033 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2034 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2035 dst1->lastuse = jiffies;
2036
2037 dst1->input = dst_discard;
2038 dst1->output = xdst_queue_output;
2039
2040 dst_hold(dst);
2041 dst1->child = dst;
2042 dst1->path = dst;
2043
2044 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2045
2046 err = -ENODEV;
2047 dev = dst->dev;
2048 if (!dev)
2049 goto free_dst;
2050
2051 err = xfrm_fill_dst(xdst, dev, fl);
2052 if (err)
2053 goto free_dst;
2054
2055out:
2056 return xdst;
2057
2058free_dst:
2059 dst_release(dst1);
2060 xdst = ERR_PTR(err);
2061 goto out;
2062}
2063
Timo Teräs80c802f2010-04-07 00:30:05 +00002064static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002065xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002066 struct flow_cache_object *oldflo, void *ctx)
2067{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002068 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002069 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2070 struct xfrm_dst *xdst, *new_xdst;
2071 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2072
2073 /* Check if the policies from old bundle are usable */
2074 xdst = NULL;
2075 if (oldflo) {
2076 xdst = container_of(oldflo, struct xfrm_dst, flo);
2077 num_pols = xdst->num_pols;
2078 num_xfrms = xdst->num_xfrms;
2079 pol_dead = 0;
2080 for (i = 0; i < num_pols; i++) {
2081 pols[i] = xdst->pols[i];
2082 pol_dead |= pols[i]->walk.dead;
2083 }
2084 if (pol_dead) {
2085 dst_free(&xdst->u.dst);
2086 xdst = NULL;
2087 num_pols = 0;
2088 num_xfrms = 0;
2089 oldflo = NULL;
2090 }
2091 }
2092
2093 /* Resolve policies to use if we couldn't get them from
2094 * previous cache entry */
2095 if (xdst == NULL) {
2096 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002097 pols[0] = __xfrm_policy_lookup(net, fl, family,
2098 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002099 err = xfrm_expand_policies(fl, family, pols,
2100 &num_pols, &num_xfrms);
2101 if (err < 0)
2102 goto inc_error;
2103 if (num_pols == 0)
2104 return NULL;
2105 if (num_xfrms <= 0)
2106 goto make_dummy_bundle;
2107 }
2108
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002109 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2110 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002111 if (IS_ERR(new_xdst)) {
2112 err = PTR_ERR(new_xdst);
2113 if (err != -EAGAIN)
2114 goto error;
2115 if (oldflo == NULL)
2116 goto make_dummy_bundle;
2117 dst_hold(&xdst->u.dst);
2118 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002119 } else if (new_xdst == NULL) {
2120 num_xfrms = 0;
2121 if (oldflo == NULL)
2122 goto make_dummy_bundle;
2123 xdst->num_xfrms = 0;
2124 dst_hold(&xdst->u.dst);
2125 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002126 }
2127
2128 /* Kill the previous bundle */
2129 if (xdst) {
2130 /* The policies were stolen for newly generated bundle */
2131 xdst->num_pols = 0;
2132 dst_free(&xdst->u.dst);
2133 }
2134
2135 /* Flow cache does not have reference, it dst_free()'s,
2136 * but we do need to return one reference for original caller */
2137 dst_hold(&new_xdst->u.dst);
2138 return &new_xdst->flo;
2139
2140make_dummy_bundle:
2141 /* We found policies, but there's no bundles to instantiate:
2142 * either because the policy blocks, has no transformations or
2143 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002144 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002145 if (IS_ERR(xdst)) {
2146 xfrm_pols_put(pols, num_pols);
2147 return ERR_CAST(xdst);
2148 }
2149 xdst->num_pols = num_pols;
2150 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002151 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002152
2153 dst_hold(&xdst->u.dst);
2154 return &xdst->flo;
2155
2156inc_error:
2157 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2158error:
2159 if (xdst != NULL)
2160 dst_free(&xdst->u.dst);
2161 else
2162 xfrm_pols_put(pols, num_pols);
2163 return ERR_PTR(err);
2164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
David S. Miller2774c132011-03-01 14:59:04 -08002166static struct dst_entry *make_blackhole(struct net *net, u16 family,
2167 struct dst_entry *dst_orig)
2168{
2169 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2170 struct dst_entry *ret;
2171
2172 if (!afinfo) {
2173 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002174 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002175 } else {
2176 ret = afinfo->blackhole_route(net, dst_orig);
2177 }
2178 xfrm_policy_put_afinfo(afinfo);
2179
2180 return ret;
2181}
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183/* Main function: finds/creates a bundle for given flow.
2184 *
2185 * At the moment we eat a raw IP route. Mostly to speed up lookups
2186 * on interfaces with disabled IPsec.
2187 */
David S. Miller452edd52011-03-02 13:27:41 -08002188struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2189 const struct flowi *fl,
2190 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002192 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002193 struct flow_cache_object *flo;
2194 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002195 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002196 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002197 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002198 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002199
Timo Teräs80c802f2010-04-07 00:30:05 +00002200 dst = NULL;
2201 xdst = NULL;
2202 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002203
Thomas Graff7944fb2007-08-25 13:46:55 -07002204 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002205 num_pols = 1;
2206 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2207 err = xfrm_expand_policies(fl, family, pols,
2208 &num_pols, &num_xfrms);
2209 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002210 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002211
2212 if (num_pols) {
2213 if (num_xfrms <= 0) {
2214 drop_pols = num_pols;
2215 goto no_transform;
2216 }
2217
2218 xdst = xfrm_resolve_and_create_bundle(
2219 pols, num_pols, fl,
2220 family, dst_orig);
2221 if (IS_ERR(xdst)) {
2222 xfrm_pols_put(pols, num_pols);
2223 err = PTR_ERR(xdst);
2224 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002225 } else if (xdst == NULL) {
2226 num_xfrms = 0;
2227 drop_pols = num_pols;
2228 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002229 }
2230
Steffen Klassertb7eea452014-06-18 12:34:21 +02002231 dst_hold(&xdst->u.dst);
2232 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002233 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002234 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Timo Teräs80c802f2010-04-07 00:30:05 +00002237 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002238 struct xfrm_flo xflo;
2239
2240 xflo.dst_orig = dst_orig;
2241 xflo.flags = flags;
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002244 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002245 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002246 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Timo Teräs80c802f2010-04-07 00:30:05 +00002248 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002249 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002250 if (flo == NULL)
2251 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002252 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002253 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002254 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002255 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002256 xdst = container_of(flo, struct xfrm_dst, flo);
2257
2258 num_pols = xdst->num_pols;
2259 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002260 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002261 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
Timo Teräs80c802f2010-04-07 00:30:05 +00002264 dst = &xdst->u.dst;
2265 if (route == NULL && num_xfrms > 0) {
2266 /* The only case when xfrm_bundle_lookup() returns a
2267 * bundle with null route, is when the template could
2268 * not be resolved. It means policies are there, but
2269 * bundle could not be created, since we don't yet
2270 * have the xfrm_state's. We need to wait for KM to
2271 * negotiate new SA's or bail out with error.*/
2272 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002273 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002274 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002275 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002276
Steffen Klassertf92ee612014-09-16 10:08:40 +02002277 return ERR_PTR(-EREMOTE);
Timo Teräs80c802f2010-04-07 00:30:05 +00002278 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002279
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002280 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002281
2282 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2283 goto error;
2284 }
2285
2286no_transform:
2287 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002288 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Timo Teräs80c802f2010-04-07 00:30:05 +00002290 if ((flags & XFRM_LOOKUP_ICMP) &&
2291 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2292 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002293 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002294 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002295
Timo Teräs80c802f2010-04-07 00:30:05 +00002296 for (i = 0; i < num_pols; i++)
2297 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002298
Timo Teräs80c802f2010-04-07 00:30:05 +00002299 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002301 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002302 err = -EPERM;
2303 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002304 } else if (num_xfrms > 0) {
2305 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002306 dst_release(dst_orig);
2307 } else {
2308 /* Flow passes untransformed */
2309 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002310 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002312ok:
2313 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002314 if (dst && dst->xfrm &&
2315 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2316 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002317 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Timo Teräs80c802f2010-04-07 00:30:05 +00002319nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002320 if (!(flags & XFRM_LOOKUP_ICMP)) {
2321 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002322 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002323 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002324 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002326 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002327dropdst:
2328 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002329 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002330 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332EXPORT_SYMBOL(xfrm_lookup);
2333
Steffen Klassertf92ee612014-09-16 10:08:40 +02002334/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2335 * Otherwise we may send out blackholed packets.
2336 */
2337struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2338 const struct flowi *fl,
2339 struct sock *sk, int flags)
2340{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002341 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
2342 flags | XFRM_LOOKUP_QUEUE);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002343
2344 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2345 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2346
2347 return dst;
2348}
2349EXPORT_SYMBOL(xfrm_lookup_route);
2350
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002351static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002352xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002353{
2354 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002355
2356 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2357 return 0;
2358 x = skb->sp->xvec[idx];
2359 if (!x->type->reject)
2360 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002361 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002362}
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364/* When skb is transformed back to its "native" form, we have to
2365 * check policy restrictions. At the moment we make this in maximally
2366 * stupid way. Shame on me. :-) Of course, connected sockets must
2367 * have policy cached at them.
2368 */
2369
2370static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002371xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 unsigned short family)
2373{
2374 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002375 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return x->id.proto == tmpl->id.proto &&
2377 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2378 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2379 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002380 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002381 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002382 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2383 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002386/*
2387 * 0 or more than 0 is returned when validation is succeeded (either bypass
2388 * because of optional transport mode, or next index of the mathced secpath
2389 * state with the template.
2390 * -1 is returned when no matching template is found.
2391 * Otherwise "-2 - errored_index" is returned.
2392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002394xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 unsigned short family)
2396{
2397 int idx = start;
2398
2399 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002400 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return start;
2402 } else
2403 start = -1;
2404 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002405 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002407 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2408 if (start == -1)
2409 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413 return start;
2414}
2415
Herbert Xud5422ef2007-12-12 10:44:16 -08002416int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2417 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002420 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (unlikely(afinfo == NULL))
2423 return -EAFNOSUPPORT;
2424
Herbert Xud5422ef2007-12-12 10:44:16 -08002425 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002426 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002428 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
Herbert Xud5422ef2007-12-12 10:44:16 -08002430EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
David S. Miller9a7386e2011-02-24 01:44:12 -05002432static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
2434 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002435 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002436 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440
2441 return 0;
2442}
2443
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002444int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 unsigned short family)
2446{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002447 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002449 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2450 int npols = 0;
2451 int xfrm_nr;
2452 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002453 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002455 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002456 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Herbert Xud5422ef2007-12-12 10:44:16 -08002458 reverse = dir & ~XFRM_POLICY_MASK;
2459 dir &= XFRM_POLICY_MASK;
2460 fl_dir = policy_to_flow_dir(dir);
2461
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002462 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002463 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002465 }
2466
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002467 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 /* First, check used SA against their selectors. */
2470 if (skb->sp) {
2471 int i;
2472
Weilong Chen9b7a7872013-12-24 09:43:46 +08002473 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002474 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002475 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002476 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
2480 }
2481
2482 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002483 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002484 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002485 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002486 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002487 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002488 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002491 if (!pol) {
2492 struct flow_cache_object *flo;
2493
2494 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2495 xfrm_policy_lookup, NULL);
2496 if (IS_ERR_OR_NULL(flo))
2497 pol = ERR_CAST(flo);
2498 else
2499 pol = container_of(flo, struct xfrm_policy, flo);
2500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002502 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002503 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002504 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002505 }
James Morris134b0fc2006-10-05 15:42:27 -05002506
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002507 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002508 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002509 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002510 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002511 return 0;
2512 }
2513 return 1;
2514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
James Morris9d729f72007-03-04 16:12:44 -08002516 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002518 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002519 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002520#ifdef CONFIG_XFRM_SUB_POLICY
2521 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002522 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002523 &fl, family,
2524 XFRM_POLICY_IN);
2525 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002526 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002527 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002528 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002529 }
James Morris9d729f72007-03-04 16:12:44 -08002530 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002531 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002532 }
2533 }
2534#endif
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (pol->action == XFRM_POLICY_ALLOW) {
2537 struct sec_path *sp;
2538 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002539 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002540 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002541 struct xfrm_tmpl **tpp = tp;
2542 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 int i, k;
2544
2545 if ((sp = skb->sp) == NULL)
2546 sp = &dummy;
2547
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002548 for (pi = 0; pi < npols; pi++) {
2549 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002550 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002551 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002552 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002553 }
2554 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002555 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002556 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002557 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002558 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2559 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2560 }
2561 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002562 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002563 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002564 tpp = stp;
2565 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /* For each tunnel xfrm, find the first matching tmpl.
2568 * For each tmpl before that, find corresponding xfrm.
2569 * Order is _important_. Later we will implement
2570 * some barriers, but at the moment barriers
2571 * are implied between each two transformations.
2572 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002573 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2574 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002575 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002576 if (k < -1)
2577 /* "-2 - errored_index" returned */
2578 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002579 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002584 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002585 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002589 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return 1;
2591 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002592 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002595 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002596reject_error:
2597 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return 0;
2599}
2600EXPORT_SYMBOL(__xfrm_policy_check);
2601
2602int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2603{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002604 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002606 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002607 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002609 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002610 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002614 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002615
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002616 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002617 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002618 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002619 dst = NULL;
2620 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002621 skb_dst_set(skb, dst);
2622 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624EXPORT_SYMBOL(__xfrm_route_forward);
2625
David S. Millerd49c73c2006-08-13 18:55:53 -07002626/* Optimize later using cookies and generation ids. */
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2629{
David S. Millerd49c73c2006-08-13 18:55:53 -07002630 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002631 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2632 * get validated by dst_ops->check on every use. We do this
2633 * because when a normal route referenced by an XFRM dst is
2634 * obsoleted we do not go looking around for all parent
2635 * referencing XFRM dsts so that we can invalidate them. It
2636 * is just too much work. Instead we make the checks here on
2637 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002638 *
2639 * XFRM dst A --> IPv4 dst X
2640 *
2641 * X is the "xdst->route" of A (X is also the "dst->path" of A
2642 * in this example). If X is marked obsolete, "A" will not
2643 * notice. That's what we are validating here via the
2644 * stale_bundle() check.
2645 *
2646 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002647 * dst which causes it's ->obsolete field to be set to
2648 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2649 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002650 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002651 if (dst->obsolete < 0 && !stale_bundle(dst))
2652 return dst;
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return NULL;
2655}
2656
2657static int stale_bundle(struct dst_entry *dst)
2658{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002659 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Herbert Xuaabc9762005-05-03 16:27:10 -07002662void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002665 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002666 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 dev_put(dev);
2668 }
2669}
Herbert Xuaabc9762005-05-03 16:27:10 -07002670EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672static void xfrm_link_failure(struct sk_buff *skb)
2673{
2674 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2678{
2679 if (dst) {
2680 if (dst->obsolete) {
2681 dst_release(dst);
2682 dst = NULL;
2683 }
2684 }
2685 return dst;
2686}
2687
Paul Mooree4c17212013-05-29 07:36:25 +00002688void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002689{
Fan Duca925cf2014-01-18 09:55:27 +08002690 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002691}
Paul Mooree4c17212013-05-29 07:36:25 +00002692EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002693
2694static void xfrm_garbage_collect_deferred(struct net *net)
2695{
Fan Duca925cf2014-01-18 09:55:27 +08002696 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002697}
2698
Herbert Xu25ee3282007-12-11 09:32:34 -08002699static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701 do {
2702 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2703 u32 pmtu, route_mtu_cached;
2704
2705 pmtu = dst_mtu(dst->child);
2706 xdst->child_mtu_cached = pmtu;
2707
2708 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2709
2710 route_mtu_cached = dst_mtu(xdst->route);
2711 xdst->route_mtu_cached = route_mtu_cached;
2712
2713 if (pmtu > route_mtu_cached)
2714 pmtu = route_mtu_cached;
2715
David S. Millerdefb3512010-12-08 21:16:57 -08002716 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 } while ((dst = dst->next));
2718}
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720/* Check that the bundle accepts the flow and its components are
2721 * still valid.
2722 */
2723
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002724static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 struct dst_entry *dst = &first->u.dst;
2727 struct xfrm_dst *last;
2728 u32 mtu;
2729
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002730 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 (dst->dev && !netif_running(dst->dev)))
2732 return 0;
2733
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002734 if (dst->flags & DST_XFRM_QUEUE)
2735 return 1;
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 last = NULL;
2738
2739 do {
2740 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2743 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002744 if (xdst->xfrm_genid != dst->xfrm->genid)
2745 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002746 if (xdst->num_pols > 0 &&
2747 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 mtu = dst_mtu(dst->child);
2751 if (xdst->child_mtu_cached != mtu) {
2752 last = xdst;
2753 xdst->child_mtu_cached = mtu;
2754 }
2755
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002756 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return 0;
2758 mtu = dst_mtu(xdst->route);
2759 if (xdst->route_mtu_cached != mtu) {
2760 last = xdst;
2761 xdst->route_mtu_cached = mtu;
2762 }
2763
2764 dst = dst->child;
2765 } while (dst->xfrm);
2766
2767 if (likely(!last))
2768 return 1;
2769
2770 mtu = last->child_mtu_cached;
2771 for (;;) {
2772 dst = &last->u.dst;
2773
2774 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2775 if (mtu > last->route_mtu_cached)
2776 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002777 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 if (last == first)
2780 break;
2781
Patrick McHardybd0bf072007-07-18 01:55:52 -07002782 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 last->child_mtu_cached = mtu;
2784 }
2785
2786 return 1;
2787}
2788
David S. Miller0dbaee32010-12-13 12:52:14 -08002789static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2790{
2791 return dst_metric_advmss(dst->path);
2792}
2793
Steffen Klassertebb762f2011-11-23 02:12:51 +00002794static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002795{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002796 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2797
2798 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002799}
2800
David S. Millerf894cbf2012-07-02 21:52:24 -07002801static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2802 struct sk_buff *skb,
2803 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002804{
David S. Millerf894cbf2012-07-02 21:52:24 -07002805 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002806}
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2809{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002810 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int err = 0;
2812 if (unlikely(afinfo == NULL))
2813 return -EINVAL;
2814 if (unlikely(afinfo->family >= NPROTO))
2815 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002816 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2818 err = -ENOBUFS;
2819 else {
2820 struct dst_ops *dst_ops = afinfo->dst_ops;
2821 if (likely(dst_ops->kmem_cachep == NULL))
2822 dst_ops->kmem_cachep = xfrm_dst_cache;
2823 if (likely(dst_ops->check == NULL))
2824 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002825 if (likely(dst_ops->default_advmss == NULL))
2826 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002827 if (likely(dst_ops->mtu == NULL))
2828 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (likely(dst_ops->negative_advice == NULL))
2830 dst_ops->negative_advice = xfrm_negative_advice;
2831 if (likely(dst_ops->link_failure == NULL))
2832 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002833 if (likely(dst_ops->neigh_lookup == NULL))
2834 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002836 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002837 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002839 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002840
2841 rtnl_lock();
2842 for_each_net(net) {
2843 struct dst_ops *xfrm_dst_ops;
2844
2845 switch (afinfo->family) {
2846 case AF_INET:
2847 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2848 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002849#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002850 case AF_INET6:
2851 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2852 break;
2853#endif
2854 default:
2855 BUG();
2856 }
2857 *xfrm_dst_ops = *afinfo->dst_ops;
2858 }
2859 rtnl_unlock();
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return err;
2862}
2863EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2864
2865int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2866{
2867 int err = 0;
2868 if (unlikely(afinfo == NULL))
2869 return -EINVAL;
2870 if (unlikely(afinfo->family >= NPROTO))
2871 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002872 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2874 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2875 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002876 else
2877 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2878 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002880 spin_unlock(&xfrm_policy_afinfo_lock);
2881 if (!err) {
2882 struct dst_ops *dst_ops = afinfo->dst_ops;
2883
2884 synchronize_rcu();
2885
2886 dst_ops->kmem_cachep = NULL;
2887 dst_ops->check = NULL;
2888 dst_ops->negative_advice = NULL;
2889 dst_ops->link_failure = NULL;
2890 afinfo->garbage_collect = NULL;
2891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return err;
2893}
2894EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2895
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002896static void __net_init xfrm_dst_ops_init(struct net *net)
2897{
2898 struct xfrm_policy_afinfo *afinfo;
2899
Eric Dumazetef8531b2012-08-19 12:31:48 +02002900 rcu_read_lock();
2901 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002902 if (afinfo)
2903 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002904#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002905 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002906 if (afinfo)
2907 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2908#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002909 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002910}
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2913{
Jiri Pirko351638e2013-05-28 01:30:21 +00002914 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 switch (event) {
2917 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002918 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
2920 return NOTIFY_DONE;
2921}
2922
2923static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002924 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925};
2926
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002927#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002928static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002929{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002930 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002931 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2932 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002933 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002934 rv = xfrm_proc_init(net);
2935 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002936 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002937 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002938}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002939
2940static void xfrm_statistics_fini(struct net *net)
2941{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002942 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002943 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002944}
2945#else
2946static int __net_init xfrm_statistics_init(struct net *net)
2947{
2948 return 0;
2949}
2950
2951static void xfrm_statistics_fini(struct net *net)
2952{
2953}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002954#endif
2955
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002956static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
David S. Miller2518c7c2006-08-24 04:45:07 -07002958 unsigned int hmask, sz;
2959 int dir;
2960
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002961 if (net_eq(net, &init_net))
2962 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002964 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002965 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
David S. Miller2518c7c2006-08-24 04:45:07 -07002967 hmask = 8 - 1;
2968 sz = (hmask+1) * sizeof(struct hlist_head);
2969
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002970 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2971 if (!net->xfrm.policy_byidx)
2972 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002973 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002974
Herbert Xu53c2e282014-11-13 17:09:49 +08002975 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002976 struct xfrm_policy_hash *htab;
2977
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002978 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002979 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002980 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002981
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002982 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002983 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002984 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002985 goto out_bydst;
2986 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002987 htab->dbits4 = 32;
2988 htab->sbits4 = 32;
2989 htab->dbits6 = 128;
2990 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002991 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002992 net->xfrm.policy_hthresh.lbits4 = 32;
2993 net->xfrm.policy_hthresh.rbits4 = 32;
2994 net->xfrm.policy_hthresh.lbits6 = 128;
2995 net->xfrm.policy_hthresh.rbits6 = 128;
2996
2997 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002998
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002999 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003000 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003001 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003002 if (net_eq(net, &init_net))
3003 register_netdevice_notifier(&xfrm_dev_notifier);
3004 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003005
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003006out_bydst:
3007 for (dir--; dir >= 0; dir--) {
3008 struct xfrm_policy_hash *htab;
3009
3010 htab = &net->xfrm.policy_bydst[dir];
3011 xfrm_hash_free(htab->table, sz);
3012 }
3013 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003014out_byidx:
3015 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003018static void xfrm_policy_fini(struct net *net)
3019{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003020 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003021 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003022
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003023 flush_work(&net->xfrm.policy_hash_work);
3024#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003025 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003026#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003027 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003028
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003029 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003030
Herbert Xu53c2e282014-11-13 17:09:49 +08003031 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003032 struct xfrm_policy_hash *htab;
3033
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003034 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003035
3036 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003037 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003038 WARN_ON(!hlist_empty(htab->table));
3039 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003040 }
3041
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003042 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003043 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3044 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003045}
3046
3047static int __net_init xfrm_net_init(struct net *net)
3048{
3049 int rv;
3050
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003051 rv = xfrm_statistics_init(net);
3052 if (rv < 0)
3053 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003054 rv = xfrm_state_init(net);
3055 if (rv < 0)
3056 goto out_state;
3057 rv = xfrm_policy_init(net);
3058 if (rv < 0)
3059 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003060 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003061 rv = xfrm_sysctl_init(net);
3062 if (rv < 0)
3063 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003064 rv = flow_cache_init(net);
3065 if (rv < 0)
3066 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003067
3068 /* Initialize the per-net locks here */
3069 spin_lock_init(&net->xfrm.xfrm_state_lock);
3070 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003071 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3072
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003073 return 0;
3074
Steffen Klassert4a93f502014-03-12 09:43:17 +01003075out:
3076 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003077out_sysctl:
3078 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003079out_policy:
3080 xfrm_state_fini(net);
3081out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003082 xfrm_statistics_fini(net);
3083out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003084 return rv;
3085}
3086
3087static void __net_exit xfrm_net_exit(struct net *net)
3088{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003089 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003090 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003091 xfrm_policy_fini(net);
3092 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003093 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003094}
3095
3096static struct pernet_operations __net_initdata xfrm_net_ops = {
3097 .init = xfrm_net_init,
3098 .exit = xfrm_net_exit,
3099};
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101void __init xfrm_init(void)
3102{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003103 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 xfrm_input_init();
3105}
3106
Joy Lattenab5f5e82007-09-17 11:51:22 -07003107#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003108static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3109 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003110{
Paul Moore875179f2007-12-01 23:27:18 +11003111 struct xfrm_sec_ctx *ctx = xp->security;
3112 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003113
Paul Moore875179f2007-12-01 23:27:18 +11003114 if (ctx)
3115 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3116 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3117
Weilong Chen9b7a7872013-12-24 09:43:46 +08003118 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003119 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003120 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003121 if (sel->prefixlen_s != 32)
3122 audit_log_format(audit_buf, " src_prefixlen=%d",
3123 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003124 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003125 if (sel->prefixlen_d != 32)
3126 audit_log_format(audit_buf, " dst_prefixlen=%d",
3127 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003128 break;
3129 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003130 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003131 if (sel->prefixlen_s != 128)
3132 audit_log_format(audit_buf, " src_prefixlen=%d",
3133 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003134 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003135 if (sel->prefixlen_d != 128)
3136 audit_log_format(audit_buf, " dst_prefixlen=%d",
3137 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003138 break;
3139 }
3140}
3141
Tetsuo Handa2e710292014-04-22 21:48:30 +09003142void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003143{
3144 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003145
Paul Mooreafeb14b2007-12-21 14:58:11 -08003146 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003147 if (audit_buf == NULL)
3148 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003149 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003150 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151 xfrm_audit_common_policyinfo(xp, audit_buf);
3152 audit_log_end(audit_buf);
3153}
3154EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3155
Paul Moore68277ac2007-12-20 20:49:33 -08003156void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003157 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003158{
3159 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003160
Paul Mooreafeb14b2007-12-21 14:58:11 -08003161 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003162 if (audit_buf == NULL)
3163 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003164 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003165 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003166 xfrm_audit_common_policyinfo(xp, audit_buf);
3167 audit_log_end(audit_buf);
3168}
3169EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3170#endif
3171
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003172#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003173static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3174 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003175{
3176 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3177 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003178 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3179 sel_cmp->family) &&
3180 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3181 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003182 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3183 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003184 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003185 }
3186 } else {
3187 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003188 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003189 }
3190 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003191 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003192}
3193
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003194static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3195 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003196{
3197 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003198 struct hlist_head *chain;
3199 u32 priority = ~0U;
3200
Fan Du283bc9f2013-11-07 17:47:50 +08003201 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003202 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003203 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3205 pol->type == type) {
3206 ret = pol;
3207 priority = ret->priority;
3208 break;
3209 }
3210 }
Fan Du8d549c42013-11-07 17:47:49 +08003211 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003212 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003213 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3214 pol->type == type &&
3215 pol->priority < priority) {
3216 ret = pol;
3217 break;
3218 }
3219 }
3220
3221 if (ret)
3222 xfrm_pol_hold(ret);
3223
Fan Du283bc9f2013-11-07 17:47:50 +08003224 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003225
3226 return ret;
3227}
3228
David S. Millerdd701752011-02-24 00:21:08 -05003229static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003230{
3231 int match = 0;
3232
3233 if (t->mode == m->mode && t->id.proto == m->proto &&
3234 (m->reqid == 0 || t->reqid == m->reqid)) {
3235 switch (t->mode) {
3236 case XFRM_MODE_TUNNEL:
3237 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003238 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3239 m->old_family) &&
3240 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3241 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003242 match = 1;
3243 }
3244 break;
3245 case XFRM_MODE_TRANSPORT:
3246 /* in case of transport mode, template does not store
3247 any IP addresses, hence we just compare mode and
3248 protocol */
3249 match = 1;
3250 break;
3251 default:
3252 break;
3253 }
3254 }
3255 return match;
3256}
3257
3258/* update endpoint address(es) of template(s) */
3259static int xfrm_policy_migrate(struct xfrm_policy *pol,
3260 struct xfrm_migrate *m, int num_migrate)
3261{
3262 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003263 int i, j, n = 0;
3264
3265 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003266 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003267 /* target policy has been deleted */
3268 write_unlock_bh(&pol->lock);
3269 return -ENOENT;
3270 }
3271
3272 for (i = 0; i < pol->xfrm_nr; i++) {
3273 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3274 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3275 continue;
3276 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003277 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3278 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003279 continue;
3280 /* update endpoints */
3281 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3282 sizeof(pol->xfrm_vec[i].id.daddr));
3283 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3284 sizeof(pol->xfrm_vec[i].saddr));
3285 pol->xfrm_vec[i].encap_family = mp->new_family;
3286 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003287 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003288 }
3289 }
3290
3291 write_unlock_bh(&pol->lock);
3292
3293 if (!n)
3294 return -ENODATA;
3295
3296 return 0;
3297}
3298
David S. Millerdd701752011-02-24 00:21:08 -05003299static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003300{
3301 int i, j;
3302
3303 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3304 return -EINVAL;
3305
3306 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003307 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3308 m[i].old_family) &&
3309 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3310 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003311 return -EINVAL;
3312 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3313 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3314 return -EINVAL;
3315
3316 /* check if there is any duplicated entry */
3317 for (j = i + 1; j < num_migrate; j++) {
3318 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3319 sizeof(m[i].old_daddr)) &&
3320 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3321 sizeof(m[i].old_saddr)) &&
3322 m[i].proto == m[j].proto &&
3323 m[i].mode == m[j].mode &&
3324 m[i].reqid == m[j].reqid &&
3325 m[i].old_family == m[j].old_family)
3326 return -EINVAL;
3327 }
3328 }
3329
3330 return 0;
3331}
3332
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003333int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003334 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003335 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003336{
3337 int i, err, nx_cur = 0, nx_new = 0;
3338 struct xfrm_policy *pol = NULL;
3339 struct xfrm_state *x, *xc;
3340 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3341 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3342 struct xfrm_migrate *mp;
3343
3344 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3345 goto out;
3346
3347 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003348 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003349 err = -ENOENT;
3350 goto out;
3351 }
3352
3353 /* Stage 2 - find and update state(s) */
3354 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003355 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003356 x_cur[nx_cur] = x;
3357 nx_cur++;
3358 if ((xc = xfrm_state_migrate(x, mp))) {
3359 x_new[nx_new] = xc;
3360 nx_new++;
3361 } else {
3362 err = -ENODATA;
3363 goto restore_state;
3364 }
3365 }
3366 }
3367
3368 /* Stage 3 - update policy */
3369 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3370 goto restore_state;
3371
3372 /* Stage 4 - delete old state(s) */
3373 if (nx_cur) {
3374 xfrm_states_put(x_cur, nx_cur);
3375 xfrm_states_delete(x_cur, nx_cur);
3376 }
3377
3378 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003379 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003380
3381 xfrm_pol_put(pol);
3382
3383 return 0;
3384out:
3385 return err;
3386
3387restore_state:
3388 if (pol)
3389 xfrm_pol_put(pol);
3390 if (nx_cur)
3391 xfrm_states_put(x_cur, nx_cur);
3392 if (nx_new)
3393 xfrm_states_delete(x_new, nx_new);
3394
3395 return err;
3396}
David S. Millere610e672007-02-08 13:29:15 -08003397EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003398#endif