blob: 418daa038edf4444520ebcfb15627d4e0679d402 [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Steffen Klassertb8c203b2014-09-16 10:08:49 +020042struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
Priyanka Jain418a99a2012-08-12 21:22:29 +000047static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Herbert Xu25ee3282007-12-11 09:32:34 -080053static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000054static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000055static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010056static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Herbert Xu12bfa8b2014-11-13 17:09:50 +080058static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080059static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
60 int dir);
61
David S. Millerbc9b35a2012-05-15 15:04:57 -040062static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050063__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080064{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050065 const struct flowi4 *fl4 = &fl->u.ip4;
66
Alexey Dobriyan26bff942011-11-22 06:46:02 +000067 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
68 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050069 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
71 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
72 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080073}
74
David S. Millerbc9b35a2012-05-15 15:04:57 -040075static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050076__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080077{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050078 const struct flowi6 *fl6 = &fl->u.ip6;
79
80 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
81 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
82 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
83 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
84 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
85 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080086}
87
David S. Millerbc9b35a2012-05-15 15:04:57 -040088bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
89 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080090{
91 switch (family) {
92 case AF_INET:
93 return __xfrm4_selector_match(sel, fl);
94 case AF_INET6:
95 return __xfrm6_selector_match(sel, fl);
96 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040097 return false;
Andrew Morton77681022006-11-08 22:46:26 -080098}
99
Eric Dumazetef8531b2012-08-19 12:31:48 +0200100static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
101{
102 struct xfrm_policy_afinfo *afinfo;
103
104 if (unlikely(family >= NPROTO))
105 return NULL;
106 rcu_read_lock();
107 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
108 if (unlikely(!afinfo))
109 rcu_read_unlock();
110 return afinfo;
111}
112
113static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
114{
115 rcu_read_unlock();
116}
117
David Ahern42a7b322015-08-10 16:58:11 -0600118static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
119 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David Ahern42a7b322015-08-10 16:58:11 -0600131 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
137
David Ahern42a7b322015-08-10 16:58:11 -0600138static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
139 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140 xfrm_address_t *prev_saddr,
141 xfrm_address_t *prev_daddr,
142 int family)
143{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800144 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900145 xfrm_address_t *saddr = &x->props.saddr;
146 xfrm_address_t *daddr = &x->id.daddr;
147 struct dst_entry *dst;
148
149 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
150 saddr = x->coaddr;
151 daddr = prev_daddr;
152 }
153 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
154 saddr = prev_saddr;
155 daddr = x->coaddr;
156 }
157
David Ahern42a7b322015-08-10 16:58:11 -0600158 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900159
160 if (!IS_ERR(dst)) {
161 if (prev_saddr != saddr)
162 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
163 if (prev_daddr != daddr)
164 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
165 }
166
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800167 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static inline unsigned long make_jiffies(long secs)
171{
172 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
173 return MAX_SCHEDULE_TIMEOUT-1;
174 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900175 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static void xfrm_policy_timer(unsigned long data)
179{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800180 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800181 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 long next = LONG_MAX;
183 int warn = 0;
184 int dir;
185
186 read_lock(&xp->lock);
187
Timo Teräsea2dea92010-03-31 00:17:05 +0000188 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out;
190
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700191 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (xp->lft.hard_add_expires_seconds) {
194 long tmo = xp->lft.hard_add_expires_seconds +
195 xp->curlft.add_time - now;
196 if (tmo <= 0)
197 goto expired;
198 if (tmo < next)
199 next = tmo;
200 }
201 if (xp->lft.hard_use_expires_seconds) {
202 long tmo = xp->lft.hard_use_expires_seconds +
203 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
204 if (tmo <= 0)
205 goto expired;
206 if (tmo < next)
207 next = tmo;
208 }
209 if (xp->lft.soft_add_expires_seconds) {
210 long tmo = xp->lft.soft_add_expires_seconds +
211 xp->curlft.add_time - now;
212 if (tmo <= 0) {
213 warn = 1;
214 tmo = XFRM_KM_TIMEOUT;
215 }
216 if (tmo < next)
217 next = tmo;
218 }
219 if (xp->lft.soft_use_expires_seconds) {
220 long tmo = xp->lft.soft_use_expires_seconds +
221 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
222 if (tmo <= 0) {
223 warn = 1;
224 tmo = XFRM_KM_TIMEOUT;
225 }
226 if (tmo < next)
227 next = tmo;
228 }
229
230 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800231 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (next != LONG_MAX &&
233 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
234 xfrm_pol_hold(xp);
235
236out:
237 read_unlock(&xp->lock);
238 xfrm_pol_put(xp);
239 return;
240
241expired:
242 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700243 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800244 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfrm_pol_put(xp);
246}
247
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000248static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
249{
250 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
251
252 if (unlikely(pol->walk.dead))
253 flo = NULL;
254 else
255 xfrm_pol_hold(pol);
256
257 return flo;
258}
259
260static int xfrm_policy_flo_check(struct flow_cache_object *flo)
261{
262 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
263
264 return !pol->walk.dead;
265}
266
267static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
268{
269 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
270}
271
272static const struct flow_cache_ops xfrm_policy_fc_ops = {
273 .get = xfrm_policy_flo_get,
274 .check = xfrm_policy_flo_check,
275 .delete = xfrm_policy_flo_delete,
276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
279 * SPD calls.
280 */
281
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800282struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct xfrm_policy *policy;
285
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700286 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800289 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700290 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700291 INIT_HLIST_NODE(&policy->bydst);
292 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700294 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100295 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800296 setup_timer(&policy->timer, xfrm_policy_timer,
297 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100298 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
299 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000300 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 return policy;
303}
304EXPORT_SYMBOL(xfrm_policy_alloc);
305
306/* Destroy xfrm_policy: descendant resources must be released to this moment. */
307
WANG Cong64c31b32008-01-07 22:34:29 -0800308void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Herbert Xu12a169e2008-10-01 07:03:24 -0700310 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Fan Du0659eea2013-08-01 18:08:36 +0800312 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 BUG();
314
Paul Moore03e1ad72008-04-12 19:07:52 -0700315 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kfree(policy);
317}
WANG Cong64c31b32008-01-07 22:34:29 -0800318EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Rule must be locked. Release descentant resources, announce
321 * entry dead. The rule must be unlinked from lists to the moment.
322 */
323
324static void xfrm_policy_kill(struct xfrm_policy *policy)
325{
Herbert Xu12a169e2008-10-01 07:03:24 -0700326 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Timo Teräs285ead12010-04-07 00:30:06 +0000328 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200330 if (del_timer(&policy->polq.hold_timer))
331 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800332 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100333
Timo Teräs285ead12010-04-07 00:30:06 +0000334 if (del_timer(&policy->timer))
335 xfrm_pol_put(policy);
336
337 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
David S. Miller2518c7c2006-08-24 04:45:07 -0700340static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
341
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800342static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700343{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800344 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700345}
346
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200347/* calculate policy hash thresholds */
348static void __get_hash_thresh(struct net *net,
349 unsigned short family, int dir,
350 u8 *dbits, u8 *sbits)
351{
352 switch (family) {
353 case AF_INET:
354 *dbits = net->xfrm.policy_bydst[dir].dbits4;
355 *sbits = net->xfrm.policy_bydst[dir].sbits4;
356 break;
357
358 case AF_INET6:
359 *dbits = net->xfrm.policy_bydst[dir].dbits6;
360 *sbits = net->xfrm.policy_bydst[dir].sbits6;
361 break;
362
363 default:
364 *dbits = 0;
365 *sbits = 0;
366 }
367}
368
David S. Miller5f803b52011-02-24 00:33:19 -0500369static struct hlist_head *policy_hash_bysel(struct net *net,
370 const struct xfrm_selector *sel,
371 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700372{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800373 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200374 unsigned int hash;
375 u8 dbits;
376 u8 sbits;
377
378 __get_hash_thresh(net, family, dir, &dbits, &sbits);
379 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700380
381 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800382 &net->xfrm.policy_inexact[dir] :
383 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700384}
385
David S. Miller5f803b52011-02-24 00:33:19 -0500386static struct hlist_head *policy_hash_direct(struct net *net,
387 const xfrm_address_t *daddr,
388 const xfrm_address_t *saddr,
389 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700390{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800391 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200392 unsigned int hash;
393 u8 dbits;
394 u8 sbits;
395
396 __get_hash_thresh(net, family, dir, &dbits, &sbits);
397 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700398
Alexey Dobriyan11219942008-11-25 17:33:06 -0800399 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700400}
401
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200402static void xfrm_dst_hash_transfer(struct net *net,
403 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700404 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200405 unsigned int nhashmask,
406 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700407{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800408 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700409 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800410 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200411 u8 dbits;
412 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700413
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800414redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 unsigned int h;
417
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700419 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200420 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800422 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800423 hlist_add_head(&pol->bydst, ndsttable+h);
424 h0 = h;
425 } else {
426 if (h != h0)
427 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800428 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700429 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800431 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800432 }
433 if (!hlist_empty(list)) {
434 entry0 = NULL;
435 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700436 }
437}
438
439static void xfrm_idx_hash_transfer(struct hlist_head *list,
440 struct hlist_head *nidxtable,
441 unsigned int nhashmask)
442{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800443 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700444 struct xfrm_policy *pol;
445
Sasha Levinb67bfe02013-02-27 17:06:00 -0800446 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700447 unsigned int h;
448
449 h = __idx_hash(pol->index, nhashmask);
450 hlist_add_head(&pol->byidx, nidxtable+h);
451 }
452}
453
454static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
455{
456 return ((old_hmask + 1) << 1) - 1;
457}
458
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800459static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700460{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800461 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700462 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
463 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800464 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700465 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700466 int i;
467
468 if (!ndst)
469 return;
470
Fan Du283bc9f2013-11-07 17:47:50 +0800471 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700472
473 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200474 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700475
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800476 net->xfrm.policy_bydst[dir].table = ndst;
477 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
Fan Du283bc9f2013-11-07 17:47:50 +0800479 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700480
David S. Miller44e36b42006-08-24 04:50:50 -0700481 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700482}
483
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800484static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700485{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800486 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700487 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
488 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800489 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700490 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700491 int i;
492
493 if (!nidx)
494 return;
495
Fan Du283bc9f2013-11-07 17:47:50 +0800496 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700497
498 for (i = hmask; i >= 0; i--)
499 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
500
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800501 net->xfrm.policy_byidx = nidx;
502 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700503
Fan Du283bc9f2013-11-07 17:47:50 +0800504 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700505
David S. Miller44e36b42006-08-24 04:50:50 -0700506 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700507}
508
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800509static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700510{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800511 unsigned int cnt = net->xfrm.policy_count[dir];
512 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700513
514 if (total)
515 *total += cnt;
516
517 if ((hmask + 1) < xfrm_policy_hashmax &&
518 cnt > hmask)
519 return 1;
520
521 return 0;
522}
523
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700525{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800526 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700527
528 if ((hmask + 1) < xfrm_policy_hashmax &&
529 total > hmask)
530 return 1;
531
532 return 0;
533}
534
Alexey Dobriyane0710412010-01-23 13:37:10 +0000535void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700536{
Fan Du283bc9f2013-11-07 17:47:50 +0800537 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000538 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
539 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
540 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
541 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
542 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
543 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
544 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700545 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800546 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700547}
548EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700549
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700550static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800551static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700552{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800553 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700554 int dir, total;
555
556 mutex_lock(&hash_resize_mutex);
557
558 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800559 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800560 if (xfrm_bydst_should_resize(net, dir, &total))
561 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800563 if (xfrm_byidx_should_resize(net, total))
564 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700565
566 mutex_unlock(&hash_resize_mutex);
567}
568
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200569static void xfrm_hash_rebuild(struct work_struct *work)
570{
571 struct net *net = container_of(work, struct net,
572 xfrm.policy_hthresh.work);
573 unsigned int hmask;
574 struct xfrm_policy *pol;
575 struct xfrm_policy *policy;
576 struct hlist_head *chain;
577 struct hlist_head *odst;
578 struct hlist_node *newpos;
579 int i;
580 int dir;
581 unsigned seq;
582 u8 lbits4, rbits4, lbits6, rbits6;
583
584 mutex_lock(&hash_resize_mutex);
585
586 /* read selector prefixlen thresholds */
587 do {
588 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
589
590 lbits4 = net->xfrm.policy_hthresh.lbits4;
591 rbits4 = net->xfrm.policy_hthresh.rbits4;
592 lbits6 = net->xfrm.policy_hthresh.lbits6;
593 rbits6 = net->xfrm.policy_hthresh.rbits6;
594 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
595
596 write_lock_bh(&net->xfrm.xfrm_policy_lock);
597
598 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800599 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200600 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
601 hmask = net->xfrm.policy_bydst[dir].hmask;
602 odst = net->xfrm.policy_bydst[dir].table;
603 for (i = hmask; i >= 0; i--)
604 INIT_HLIST_HEAD(odst + i);
605 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
606 /* dir out => dst = remote, src = local */
607 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
608 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
609 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
610 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
611 } else {
612 /* dir in/fwd => dst = local, src = remote */
613 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
614 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
615 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
616 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
617 }
618 }
619
620 /* re-insert all policies by order of creation */
621 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
622 newpos = NULL;
623 chain = policy_hash_bysel(net, &policy->selector,
624 policy->family,
625 xfrm_policy_id2dir(policy->index));
626 hlist_for_each_entry(pol, chain, bydst) {
627 if (policy->priority >= pol->priority)
628 newpos = &pol->bydst;
629 else
630 break;
631 }
632 if (newpos)
633 hlist_add_behind(&policy->bydst, newpos);
634 else
635 hlist_add_head(&policy->bydst, chain);
636 }
637
638 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
639
640 mutex_unlock(&hash_resize_mutex);
641}
642
643void xfrm_policy_hash_rebuild(struct net *net)
644{
645 schedule_work(&net->xfrm.policy_hthresh.work);
646}
647EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/* Generate new index... KAME seems to generate them ordered by cost
650 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800651static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 static u32 idx_generator;
654
655 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700656 struct hlist_head *list;
657 struct xfrm_policy *p;
658 u32 idx;
659 int found;
660
Fan Due682adf2013-11-07 17:47:48 +0800661 if (!index) {
662 idx = (idx_generator | dir);
663 idx_generator += 8;
664 } else {
665 idx = index;
666 index = 0;
667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (idx == 0)
670 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800671 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700672 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800673 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700674 if (p->index == idx) {
675 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return idx;
681 }
682}
683
David S. Miller2518c7c2006-08-24 04:45:07 -0700684static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
685{
686 u32 *p1 = (u32 *) s1;
687 u32 *p2 = (u32 *) s2;
688 int len = sizeof(struct xfrm_selector) / sizeof(u32);
689 int i;
690
691 for (i = 0; i < len; i++) {
692 if (p1[i] != p2[i])
693 return 1;
694 }
695
696 return 0;
697}
698
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100699static void xfrm_policy_requeue(struct xfrm_policy *old,
700 struct xfrm_policy *new)
701{
702 struct xfrm_policy_queue *pq = &old->polq;
703 struct sk_buff_head list;
704
Li RongQingde2ad482015-04-30 17:25:19 +0800705 if (skb_queue_empty(&pq->hold_queue))
706 return;
707
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100708 __skb_queue_head_init(&list);
709
710 spin_lock_bh(&pq->hold_queue.lock);
711 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200712 if (del_timer(&pq->hold_timer))
713 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100714 spin_unlock_bh(&pq->hold_queue.lock);
715
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100716 pq = &new->polq;
717
718 spin_lock_bh(&pq->hold_queue.lock);
719 skb_queue_splice(&list, &pq->hold_queue);
720 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200721 if (!mod_timer(&pq->hold_timer, jiffies))
722 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100723 spin_unlock_bh(&pq->hold_queue.lock);
724}
725
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100726static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
727 struct xfrm_policy *pol)
728{
729 u32 mark = policy->mark.v & policy->mark.m;
730
731 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
732 return true;
733
734 if ((mark & pol->mark.m) == pol->mark.v &&
735 policy->priority == pol->priority)
736 return true;
737
738 return false;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
742{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800743 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700744 struct xfrm_policy *pol;
745 struct xfrm_policy *delpol;
746 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800747 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Fan Du283bc9f2013-11-07 17:47:50 +0800749 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800750 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700751 delpol = NULL;
752 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800753 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800754 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700755 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100756 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800757 xfrm_sec_ctx_match(pol->security, policy->security) &&
758 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800760 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EEXIST;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 delpol = pol;
764 if (policy->priority > pol->priority)
765 continue;
766 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800767 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 continue;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (delpol)
771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700774 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700775 else
776 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800777 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800778 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800779
780 /* After previous checking, family can either be AF_INET or AF_INET6 */
781 if (policy->family == AF_INET)
782 rt_genid_bump_ipv4(net);
783 else
784 rt_genid_bump_ipv6(net);
785
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100786 if (delpol) {
787 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800788 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100789 }
Fan Due682adf2013-11-07 17:47:48 +0800790 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800791 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800792 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 policy->curlft.use_time = 0;
794 if (!mod_timer(&policy->timer, jiffies + HZ))
795 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800796 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
David S. Miller9b78a822005-12-22 07:39:48 -0800798 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800800 else if (xfrm_bydst_should_resize(net, dir, NULL))
801 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 0;
804}
805EXPORT_SYMBOL(xfrm_policy_insert);
806
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000807struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
808 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800809 struct xfrm_sec_ctx *ctx, int delete,
810 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
David S. Miller2518c7c2006-08-24 04:45:07 -0700812 struct xfrm_policy *pol, *ret;
813 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Eric Parisef41aaa2007-03-07 15:37:58 -0800815 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800816 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800817 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700818 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800819 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700820 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000821 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700822 !selector_cmp(sel, &pol->selector) &&
823 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700825 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700826 *err = security_xfrm_policy_delete(
827 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800828 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800829 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800830 return pol;
831 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800832 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 }
834 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
836 }
837 }
Fan Du283bc9f2013-11-07 17:47:50 +0800838 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000840 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
Trent Jaegerdf718372005-12-13 23:12:27 -0800844EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000846struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
847 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 struct xfrm_policy *pol, *ret;
850 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Herbert Xub5505c62007-05-14 02:15:47 -0700852 *err = -ENOENT;
853 if (xfrm_policy_id2dir(id) != dir)
854 return NULL;
855
Eric Parisef41aaa2007-03-07 15:37:58 -0800856 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800857 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800858 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700859 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800860 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000861 if (pol->type == type && pol->index == id &&
862 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700864 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700865 *err = security_xfrm_policy_delete(
866 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800867 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800868 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800869 return pol;
870 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800871 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700872 }
873 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 break;
875 }
876 }
Fan Du283bc9f2013-11-07 17:47:50 +0800877 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000879 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700880 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883EXPORT_SYMBOL(xfrm_policy_byid);
884
Joy Latten4aa2e622007-06-04 19:05:57 -0400885#ifdef CONFIG_SECURITY_NETWORK_XFRM
886static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900887xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Joy Latten4aa2e622007-06-04 19:05:57 -0400889 int dir, err = 0;
890
891 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
892 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400893 int i;
894
Sasha Levinb67bfe02013-02-27 17:06:00 -0800895 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800896 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400897 if (pol->type != type)
898 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700899 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400900 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900901 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400902 return err;
903 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900904 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800905 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800906 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800907 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400908 bydst) {
909 if (pol->type != type)
910 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700911 err = security_xfrm_policy_delete(
912 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400913 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700914 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900915 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400916 return err;
917 }
918 }
919 }
920 }
921 return err;
922}
923#else
924static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900925xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400926{
927 return 0;
928}
929#endif
930
Tetsuo Handa2e710292014-04-22 21:48:30 +0900931int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400932{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000933 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Fan Du283bc9f2013-11-07 17:47:50 +0800935 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400936
Tetsuo Handa2e710292014-04-22 21:48:30 +0900937 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400938 if (err)
939 goto out;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700942 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800943 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700944
945 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800946 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800947 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700948 if (pol->type != type)
949 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000950 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800951 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000952 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Tetsuo Handa2e710292014-04-22 21:48:30 +0900954 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600955
David S. Miller2518c7c2006-08-24 04:45:07 -0700956 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Fan Du283bc9f2013-11-07 17:47:50 +0800958 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700959 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700961
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800962 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700963 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800964 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800965 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700966 bydst) {
967 if (pol->type != type)
968 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000969 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800970 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000971 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700972
Tetsuo Handa2e710292014-04-22 21:48:30 +0900973 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700974 xfrm_policy_kill(pol);
975
Fan Du283bc9f2013-11-07 17:47:50 +0800976 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700977 goto again2;
978 }
979 }
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000982 if (!cnt)
983 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400984out:
Fan Du283bc9f2013-11-07 17:47:50 +0800985 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400986 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988EXPORT_SYMBOL(xfrm_policy_flush);
989
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800990int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800991 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 void *data)
993{
Herbert Xu12a169e2008-10-01 07:03:24 -0700994 struct xfrm_policy *pol;
995 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800996 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Timo Teras4c563f72008-02-28 21:31:08 -0800998 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
999 walk->type != XFRM_POLICY_TYPE_ANY)
1000 return -EINVAL;
1001
Herbert Xu12a169e2008-10-01 07:03:24 -07001002 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001003 return 0;
1004
Fan Du283bc9f2013-11-07 17:47:50 +08001005 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001006 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001007 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001008 else
Li RongQing80077702015-04-22 17:09:54 +08001009 x = list_first_entry(&walk->walk.all,
1010 struct xfrm_policy_walk_entry, all);
1011
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001012 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001013 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001014 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001015 pol = container_of(x, struct xfrm_policy, walk);
1016 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1017 walk->type != pol->type)
1018 continue;
1019 error = func(pol, xfrm_policy_id2dir(pol->index),
1020 walk->seq, data);
1021 if (error) {
1022 list_move_tail(&walk->walk.all, &x->all);
1023 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001024 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001025 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001027 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001028 error = -ENOENT;
1029 goto out;
1030 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001031 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032out:
Fan Du283bc9f2013-11-07 17:47:50 +08001033 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return error;
1035}
1036EXPORT_SYMBOL(xfrm_policy_walk);
1037
Herbert Xu12a169e2008-10-01 07:03:24 -07001038void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1039{
1040 INIT_LIST_HEAD(&walk->walk.all);
1041 walk->walk.dead = 1;
1042 walk->type = type;
1043 walk->seq = 0;
1044}
1045EXPORT_SYMBOL(xfrm_policy_walk_init);
1046
Fan Du283bc9f2013-11-07 17:47:50 +08001047void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001048{
1049 if (list_empty(&walk->walk.all))
1050 return;
1051
Fan Du283bc9f2013-11-07 17:47:50 +08001052 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001053 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001054 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001055}
1056EXPORT_SYMBOL(xfrm_policy_walk_done);
1057
James Morris134b0fc2006-10-05 15:42:27 -05001058/*
1059 * Find policy to apply to this flow.
1060 *
1061 * Returns 0 if policy found, else an -errno.
1062 */
David S. Millerf299d552011-02-24 01:23:30 -05001063static int xfrm_policy_match(const struct xfrm_policy *pol,
1064 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001065 u8 type, u16 family, int dir)
1066{
David S. Millerf299d552011-02-24 01:23:30 -05001067 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001068 int ret = -ESRCH;
1069 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001070
1071 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001072 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001073 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001074 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001075
1076 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001077 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001078 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001079 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001080
James Morris134b0fc2006-10-05 15:42:27 -05001081 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001082}
1083
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001084static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001085 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001086 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
James Morris134b0fc2006-10-05 15:42:27 -05001088 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001089 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001090 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001091 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001092 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001093
1094 daddr = xfrm_flowi_daddr(fl, family);
1095 saddr = xfrm_flowi_saddr(fl, family);
1096 if (unlikely(!daddr || !saddr))
1097 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Fan Du283bc9f2013-11-07 17:47:50 +08001099 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001100 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001101 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001102 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001103 err = xfrm_policy_match(pol, fl, type, family, dir);
1104 if (err) {
1105 if (err == -ESRCH)
1106 continue;
1107 else {
1108 ret = ERR_PTR(err);
1109 goto fail;
1110 }
1111 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001112 ret = pol;
1113 priority = ret->priority;
1114 break;
1115 }
1116 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001117 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001118 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001119 if ((pol->priority >= priority) && ret)
1120 break;
1121
James Morris134b0fc2006-10-05 15:42:27 -05001122 err = xfrm_policy_match(pol, fl, type, family, dir);
1123 if (err) {
1124 if (err == -ESRCH)
1125 continue;
1126 else {
1127 ret = ERR_PTR(err);
1128 goto fail;
1129 }
Li RongQing8faf4912015-05-14 11:16:59 +08001130 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001131 ret = pol;
1132 break;
1133 }
1134 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001135
1136 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001137fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001138 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001139
David S. Miller2518c7c2006-08-24 04:45:07 -07001140 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001141}
1142
Timo Teräs80c802f2010-04-07 00:30:05 +00001143static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001144__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001145{
1146#ifdef CONFIG_XFRM_SUB_POLICY
1147 struct xfrm_policy *pol;
1148
1149 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1150 if (pol != NULL)
1151 return pol;
1152#endif
1153 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1154}
1155
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001156static int flow_to_policy_dir(int dir)
1157{
1158 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1159 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1160 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1161 return dir;
1162
1163 switch (dir) {
1164 default:
1165 case FLOW_DIR_IN:
1166 return XFRM_POLICY_IN;
1167 case FLOW_DIR_OUT:
1168 return XFRM_POLICY_OUT;
1169 case FLOW_DIR_FWD:
1170 return XFRM_POLICY_FWD;
1171 }
1172}
1173
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001174static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001175xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001176 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001177{
1178 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001179
1180 if (old_obj)
1181 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001182
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001183 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001184 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001185 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001186
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001187 /* Resolver returns two references:
1188 * one for cache and one for caller of flow_cache_lookup() */
1189 xfrm_pol_hold(pol);
1190
1191 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Trent Jaegerdf718372005-12-13 23:12:27 -08001194static inline int policy_to_flow_dir(int dir)
1195{
1196 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001197 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1198 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1199 return dir;
1200 switch (dir) {
1201 default:
1202 case XFRM_POLICY_IN:
1203 return FLOW_DIR_IN;
1204 case XFRM_POLICY_OUT:
1205 return FLOW_DIR_OUT;
1206 case XFRM_POLICY_FWD:
1207 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001208 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001209}
1210
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001211static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001212 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001215 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Fan Du283bc9f2013-11-07 17:47:50 +08001217 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001219 bool match = xfrm_selector_match(&pol->selector, fl,
1220 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001221 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001222
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001223 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001224 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1225 pol = NULL;
1226 goto out;
1227 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001228 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001229 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001230 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001231 if (!err)
1232 xfrm_pol_hold(pol);
1233 else if (err == -ESRCH)
1234 pol = NULL;
1235 else
1236 pol = ERR_PTR(err);
1237 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 pol = NULL;
1239 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001240out:
Fan Du283bc9f2013-11-07 17:47:50 +08001241 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return pol;
1243}
1244
1245static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1246{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001247 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001248
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001249 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001250 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 xfrm_pol_hold(pol);
1252}
1253
1254static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1255 int dir)
1256{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001257 struct net *net = xp_net(pol);
1258
Herbert Xu53c2e282014-11-13 17:09:49 +08001259 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001260 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Herbert Xu53c2e282014-11-13 17:09:49 +08001262 /* Socket policies are not hashed. */
1263 if (!hlist_unhashed(&pol->bydst)) {
1264 hlist_del(&pol->bydst);
1265 hlist_del(&pol->byidx);
1266 }
1267
1268 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001269 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001270
1271 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Herbert Xu53c2e282014-11-13 17:09:49 +08001274static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1275{
1276 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1277}
1278
1279static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1280{
1281 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1282}
1283
Herbert Xu4666faa2005-06-18 22:43:22 -07001284int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Fan Du283bc9f2013-11-07 17:47:50 +08001286 struct net *net = xp_net(pol);
1287
1288 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001290 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001295 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
David S. Millera70fcb02006-03-20 19:18:52 -08001297EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1300{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001301 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 struct xfrm_policy *old_pol;
1303
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001304#ifdef CONFIG_XFRM_SUB_POLICY
1305 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1306 return -EINVAL;
1307#endif
1308
Fan Du283bc9f2013-11-07 17:47:50 +08001309 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 old_pol = sk->sk_policy[dir];
1311 sk->sk_policy[dir] = pol;
1312 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001313 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001314 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001315 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001317 if (old_pol) {
1318 if (pol)
1319 xfrm_policy_requeue(old_pol, pol);
1320
Timo Teräsea2dea92010-03-31 00:17:05 +00001321 /* Unlinking succeeds always. This is the only function
1322 * allowed to delete or replace socket policy.
1323 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001324 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001325 }
Fan Du283bc9f2013-11-07 17:47:50 +08001326 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (old_pol) {
1329 xfrm_policy_kill(old_pol);
1330 }
1331 return 0;
1332}
1333
David S. Millerd3e40a92011-02-24 01:25:41 -05001334static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001336 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001337 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (newp) {
1340 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001341 if (security_xfrm_policy_clone(old->security,
1342 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001343 kfree(newp);
1344 return NULL; /* ENOMEM */
1345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 newp->lft = old->lft;
1347 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001348 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 newp->action = old->action;
1350 newp->flags = old->flags;
1351 newp->xfrm_nr = old->xfrm_nr;
1352 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001353 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 memcpy(newp->xfrm_vec, old->xfrm_vec,
1355 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001356 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001357 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001358 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 xfrm_pol_put(newp);
1360 }
1361 return newp;
1362}
1363
1364int __xfrm_sk_clone_policy(struct sock *sk)
1365{
1366 struct xfrm_policy *p0 = sk->sk_policy[0],
1367 *p1 = sk->sk_policy[1];
1368
1369 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1370 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1371 return -ENOMEM;
1372 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1373 return -ENOMEM;
1374 return 0;
1375}
1376
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001377static int
David Ahern42a7b322015-08-10 16:58:11 -06001378xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1379 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001380{
1381 int err;
1382 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1383
1384 if (unlikely(afinfo == NULL))
1385 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001386 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001387 xfrm_policy_put_afinfo(afinfo);
1388 return err;
1389}
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391/* Resolve list of templates for the flow, given policy. */
1392
1393static int
David S. Millera6c2e612011-02-22 18:35:39 -08001394xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1395 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001397 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int nx;
1399 int i, error;
1400 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1401 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001402 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Weilong Chen9b7a7872013-12-24 09:43:46 +08001404 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct xfrm_state *x;
1406 xfrm_address_t *remote = daddr;
1407 xfrm_address_t *local = saddr;
1408 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1409
Joakim Koskela48b8d782007-07-26 00:08:42 -07001410 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1411 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 remote = &tmpl->id.daddr;
1413 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001414 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001415 error = xfrm_get_saddr(net, fl->flowi_oif,
1416 &tmp, remote,
1417 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;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001586 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001587 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001588
Madalin Bucurd4cae562011-09-26 07:04:36 +00001589 xfrm_policy_put_afinfo(afinfo);
1590
Herbert Xu25ee3282007-12-11 09:32:34 -08001591 return xdst;
1592}
1593
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001594static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1595 int nfheader_len)
1596{
1597 struct xfrm_policy_afinfo *afinfo =
1598 xfrm_policy_get_afinfo(dst->ops->family);
1599 int err;
1600
1601 if (!afinfo)
1602 return -EINVAL;
1603
1604 err = afinfo->init_path(path, dst, nfheader_len);
1605
1606 xfrm_policy_put_afinfo(afinfo);
1607
1608 return err;
1609}
1610
Herbert Xu87c1e122010-03-02 02:51:56 +00001611static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001612 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001613{
1614 struct xfrm_policy_afinfo *afinfo =
1615 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1616 int err;
1617
1618 if (!afinfo)
1619 return -EINVAL;
1620
Herbert Xu87c1e122010-03-02 02:51:56 +00001621 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001622
1623 xfrm_policy_put_afinfo(afinfo);
1624
1625 return err;
1626}
1627
Timo Teräs80c802f2010-04-07 00:30:05 +00001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1630 * all the metrics... Shortly, bundle a bundle.
1631 */
1632
Herbert Xu25ee3282007-12-11 09:32:34 -08001633static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1634 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001635 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001636 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001638 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001639 unsigned long now = jiffies;
1640 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001641 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001642 struct dst_entry *dst_prev = NULL;
1643 struct dst_entry *dst0 = NULL;
1644 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001646 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001647 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001648 int trailer_len = 0;
1649 int tos;
1650 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001651 xfrm_address_t saddr, daddr;
1652
1653 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001654
1655 tos = xfrm_get_tos(fl, family);
1656 err = tos;
1657 if (tos < 0)
1658 goto put_states;
1659
1660 dst_hold(dst);
1661
1662 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001663 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001664 struct dst_entry *dst1 = &xdst->u.dst;
1665
1666 err = PTR_ERR(xdst);
1667 if (IS_ERR(xdst)) {
1668 dst_release(dst);
1669 goto put_states;
1670 }
1671
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001672 if (xfrm[i]->sel.family == AF_UNSPEC) {
1673 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1674 xfrm_af2proto(family));
1675 if (!inner_mode) {
1676 err = -EAFNOSUPPORT;
1677 dst_release(dst);
1678 goto put_states;
1679 }
1680 } else
1681 inner_mode = xfrm[i]->inner_mode;
1682
Herbert Xu25ee3282007-12-11 09:32:34 -08001683 if (!dst_prev)
1684 dst0 = dst1;
1685 else {
1686 dst_prev->child = dst_clone(dst1);
1687 dst1->flags |= DST_NOHASH;
1688 }
1689
1690 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001691 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001692
1693 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1694 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001695 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1696 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001697 err = PTR_ERR(dst);
1698 if (IS_ERR(dst))
1699 goto put_states;
1700 } else
1701 dst_hold(dst);
1702
1703 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001704 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001705
David S. Millerf5b0a872012-07-19 12:31:33 -07001706 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001707 dst1->flags |= DST_HOST;
1708 dst1->lastuse = now;
1709
1710 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001711 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001712
1713 dst1->next = dst_prev;
1714 dst_prev = dst1;
1715
1716 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001717 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1718 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001719 trailer_len += xfrm[i]->props.trailer_len;
1720 }
1721
1722 dst_prev->child = dst;
1723 dst0->path = dst;
1724
1725 err = -ENODEV;
1726 dev = dst->dev;
1727 if (!dev)
1728 goto free_dst;
1729
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001730 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001731 xfrm_init_pmtu(dst_prev);
1732
1733 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1734 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1735
Herbert Xu87c1e122010-03-02 02:51:56 +00001736 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001737 if (err)
1738 goto free_dst;
1739
1740 dst_prev->header_len = header_len;
1741 dst_prev->trailer_len = trailer_len;
1742 header_len -= xdst->u.dst.xfrm->props.header_len;
1743 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1744 }
1745
1746out:
1747 return dst0;
1748
1749put_states:
1750 for (; i < nx; i++)
1751 xfrm_state_put(xfrm[i]);
1752free_dst:
1753 if (dst0)
1754 dst_free(dst0);
1755 dst0 = ERR_PTR(err);
1756 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Ying Xueda7c2242014-01-08 10:26:39 +08001759#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001760static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001761{
1762 if (!*target) {
1763 *target = kmalloc(size, GFP_ATOMIC);
1764 if (!*target)
1765 return -ENOMEM;
1766 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001767
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001768 memcpy(*target, src, size);
1769 return 0;
1770}
Ying Xueda7c2242014-01-08 10:26:39 +08001771#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001772
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001773static int xfrm_dst_update_parent(struct dst_entry *dst,
1774 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001775{
1776#ifdef CONFIG_XFRM_SUB_POLICY
1777 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1778 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1779 sel, sizeof(*sel));
1780#else
1781 return 0;
1782#endif
1783}
1784
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001785static int xfrm_dst_update_origin(struct dst_entry *dst,
1786 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001787{
1788#ifdef CONFIG_XFRM_SUB_POLICY
1789 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1790 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1791#else
1792 return 0;
1793#endif
1794}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
David S. Miller73ff93c2011-02-22 18:33:42 -08001796static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001797 struct xfrm_policy **pols,
1798 int *num_pols, int *num_xfrms)
1799{
1800 int i;
1801
1802 if (*num_pols == 0 || !pols[0]) {
1803 *num_pols = 0;
1804 *num_xfrms = 0;
1805 return 0;
1806 }
1807 if (IS_ERR(pols[0]))
1808 return PTR_ERR(pols[0]);
1809
1810 *num_xfrms = pols[0]->xfrm_nr;
1811
1812#ifdef CONFIG_XFRM_SUB_POLICY
1813 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1814 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1815 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1816 XFRM_POLICY_TYPE_MAIN,
1817 fl, family,
1818 XFRM_POLICY_OUT);
1819 if (pols[1]) {
1820 if (IS_ERR(pols[1])) {
1821 xfrm_pols_put(pols, *num_pols);
1822 return PTR_ERR(pols[1]);
1823 }
Weilong Chen02d08922013-12-24 09:43:48 +08001824 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001825 (*num_xfrms) += pols[1]->xfrm_nr;
1826 }
1827 }
1828#endif
1829 for (i = 0; i < *num_pols; i++) {
1830 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1831 *num_xfrms = -1;
1832 break;
1833 }
1834 }
1835
1836 return 0;
1837
1838}
1839
1840static struct xfrm_dst *
1841xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001842 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001843 struct dst_entry *dst_orig)
1844{
1845 struct net *net = xp_net(pols[0]);
1846 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1847 struct dst_entry *dst;
1848 struct xfrm_dst *xdst;
1849 int err;
1850
1851 /* Try to instantiate a bundle */
1852 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001853 if (err <= 0) {
1854 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001855 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1856 return ERR_PTR(err);
1857 }
1858
1859 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1860 if (IS_ERR(dst)) {
1861 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1862 return ERR_CAST(dst);
1863 }
1864
1865 xdst = (struct xfrm_dst *)dst;
1866 xdst->num_xfrms = err;
1867 if (num_pols > 1)
1868 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1869 else
1870 err = xfrm_dst_update_origin(dst, fl);
1871 if (unlikely(err)) {
1872 dst_free(dst);
1873 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1874 return ERR_PTR(err);
1875 }
1876
1877 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001878 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001879 xdst->policy_genid = atomic_read(&pols[0]->genid);
1880
1881 return xdst;
1882}
1883
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001884static void xfrm_policy_queue_process(unsigned long arg)
1885{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001886 struct sk_buff *skb;
1887 struct sock *sk;
1888 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001889 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1890 struct xfrm_policy_queue *pq = &pol->polq;
1891 struct flowi fl;
1892 struct sk_buff_head list;
1893
1894 spin_lock(&pq->hold_queue.lock);
1895 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001896 if (!skb) {
1897 spin_unlock(&pq->hold_queue.lock);
1898 goto out;
1899 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001900 dst = skb_dst(skb);
1901 sk = skb->sk;
1902 xfrm_decode_session(skb, &fl, dst->ops->family);
1903 spin_unlock(&pq->hold_queue.lock);
1904
1905 dst_hold(dst->path);
1906 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1907 sk, 0);
1908 if (IS_ERR(dst))
1909 goto purge_queue;
1910
1911 if (dst->flags & DST_XFRM_QUEUE) {
1912 dst_release(dst);
1913
1914 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1915 goto purge_queue;
1916
1917 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001918 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1919 xfrm_pol_hold(pol);
1920 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001921 }
1922
1923 dst_release(dst);
1924
1925 __skb_queue_head_init(&list);
1926
1927 spin_lock(&pq->hold_queue.lock);
1928 pq->timeout = 0;
1929 skb_queue_splice_init(&pq->hold_queue, &list);
1930 spin_unlock(&pq->hold_queue.lock);
1931
1932 while (!skb_queue_empty(&list)) {
1933 skb = __skb_dequeue(&list);
1934
1935 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1936 dst_hold(skb_dst(skb)->path);
1937 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1938 &fl, skb->sk, 0);
1939 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001940 kfree_skb(skb);
1941 continue;
1942 }
1943
1944 nf_reset(skb);
1945 skb_dst_drop(skb);
1946 skb_dst_set(skb, dst);
1947
Eric W. Biederman5a706492015-09-15 20:03:53 -05001948 dst_output(skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001949 }
1950
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001951out:
1952 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001953 return;
1954
1955purge_queue:
1956 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001957 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001958 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001959}
1960
Eric Dumazetaad88722014-04-15 13:47:15 -04001961static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001962{
1963 unsigned long sched_next;
1964 struct dst_entry *dst = skb_dst(skb);
1965 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001966 struct xfrm_policy *pol = xdst->pols[0];
1967 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001968
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001969 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001970 kfree_skb(skb);
1971 return 0;
1972 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001973
1974 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1975 kfree_skb(skb);
1976 return -EAGAIN;
1977 }
1978
1979 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001980
1981 spin_lock_bh(&pq->hold_queue.lock);
1982
1983 if (!pq->timeout)
1984 pq->timeout = XFRM_QUEUE_TMO_MIN;
1985
1986 sched_next = jiffies + pq->timeout;
1987
1988 if (del_timer(&pq->hold_timer)) {
1989 if (time_before(pq->hold_timer.expires, sched_next))
1990 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001991 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001992 }
1993
1994 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001995 if (!mod_timer(&pq->hold_timer, sched_next))
1996 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001997
1998 spin_unlock_bh(&pq->hold_queue.lock);
1999
2000 return 0;
2001}
2002
2003static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002004 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002005 const struct flowi *fl,
2006 int num_xfrms,
2007 u16 family)
2008{
2009 int err;
2010 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002011 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002012 struct dst_entry *dst1;
2013 struct xfrm_dst *xdst;
2014
2015 xdst = xfrm_alloc_dst(net, family);
2016 if (IS_ERR(xdst))
2017 return xdst;
2018
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002019 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2020 net->xfrm.sysctl_larval_drop ||
2021 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002022 return xdst;
2023
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002024 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002025 dst1 = &xdst->u.dst;
2026 dst_hold(dst);
2027 xdst->route = dst;
2028
2029 dst_copy_metrics(dst1, dst);
2030
2031 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2032 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2033 dst1->lastuse = jiffies;
2034
2035 dst1->input = dst_discard;
2036 dst1->output = xdst_queue_output;
2037
2038 dst_hold(dst);
2039 dst1->child = dst;
2040 dst1->path = dst;
2041
2042 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2043
2044 err = -ENODEV;
2045 dev = dst->dev;
2046 if (!dev)
2047 goto free_dst;
2048
2049 err = xfrm_fill_dst(xdst, dev, fl);
2050 if (err)
2051 goto free_dst;
2052
2053out:
2054 return xdst;
2055
2056free_dst:
2057 dst_release(dst1);
2058 xdst = ERR_PTR(err);
2059 goto out;
2060}
2061
Timo Teräs80c802f2010-04-07 00:30:05 +00002062static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002063xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002064 struct flow_cache_object *oldflo, void *ctx)
2065{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002066 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002067 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2068 struct xfrm_dst *xdst, *new_xdst;
2069 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2070
2071 /* Check if the policies from old bundle are usable */
2072 xdst = NULL;
2073 if (oldflo) {
2074 xdst = container_of(oldflo, struct xfrm_dst, flo);
2075 num_pols = xdst->num_pols;
2076 num_xfrms = xdst->num_xfrms;
2077 pol_dead = 0;
2078 for (i = 0; i < num_pols; i++) {
2079 pols[i] = xdst->pols[i];
2080 pol_dead |= pols[i]->walk.dead;
2081 }
2082 if (pol_dead) {
2083 dst_free(&xdst->u.dst);
2084 xdst = NULL;
2085 num_pols = 0;
2086 num_xfrms = 0;
2087 oldflo = NULL;
2088 }
2089 }
2090
2091 /* Resolve policies to use if we couldn't get them from
2092 * previous cache entry */
2093 if (xdst == NULL) {
2094 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002095 pols[0] = __xfrm_policy_lookup(net, fl, family,
2096 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002097 err = xfrm_expand_policies(fl, family, pols,
2098 &num_pols, &num_xfrms);
2099 if (err < 0)
2100 goto inc_error;
2101 if (num_pols == 0)
2102 return NULL;
2103 if (num_xfrms <= 0)
2104 goto make_dummy_bundle;
2105 }
2106
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002107 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2108 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002109 if (IS_ERR(new_xdst)) {
2110 err = PTR_ERR(new_xdst);
2111 if (err != -EAGAIN)
2112 goto error;
2113 if (oldflo == NULL)
2114 goto make_dummy_bundle;
2115 dst_hold(&xdst->u.dst);
2116 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002117 } else if (new_xdst == NULL) {
2118 num_xfrms = 0;
2119 if (oldflo == NULL)
2120 goto make_dummy_bundle;
2121 xdst->num_xfrms = 0;
2122 dst_hold(&xdst->u.dst);
2123 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002124 }
2125
2126 /* Kill the previous bundle */
2127 if (xdst) {
2128 /* The policies were stolen for newly generated bundle */
2129 xdst->num_pols = 0;
2130 dst_free(&xdst->u.dst);
2131 }
2132
2133 /* Flow cache does not have reference, it dst_free()'s,
2134 * but we do need to return one reference for original caller */
2135 dst_hold(&new_xdst->u.dst);
2136 return &new_xdst->flo;
2137
2138make_dummy_bundle:
2139 /* We found policies, but there's no bundles to instantiate:
2140 * either because the policy blocks, has no transformations or
2141 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002142 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002143 if (IS_ERR(xdst)) {
2144 xfrm_pols_put(pols, num_pols);
2145 return ERR_CAST(xdst);
2146 }
2147 xdst->num_pols = num_pols;
2148 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002149 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002150
2151 dst_hold(&xdst->u.dst);
2152 return &xdst->flo;
2153
2154inc_error:
2155 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2156error:
2157 if (xdst != NULL)
2158 dst_free(&xdst->u.dst);
2159 else
2160 xfrm_pols_put(pols, num_pols);
2161 return ERR_PTR(err);
2162}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
David S. Miller2774c132011-03-01 14:59:04 -08002164static struct dst_entry *make_blackhole(struct net *net, u16 family,
2165 struct dst_entry *dst_orig)
2166{
2167 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2168 struct dst_entry *ret;
2169
2170 if (!afinfo) {
2171 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002172 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002173 } else {
2174 ret = afinfo->blackhole_route(net, dst_orig);
2175 }
2176 xfrm_policy_put_afinfo(afinfo);
2177
2178 return ret;
2179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181/* Main function: finds/creates a bundle for given flow.
2182 *
2183 * At the moment we eat a raw IP route. Mostly to speed up lookups
2184 * on interfaces with disabled IPsec.
2185 */
David S. Miller452edd52011-03-02 13:27:41 -08002186struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2187 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002188 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002190 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002191 struct flow_cache_object *flo;
2192 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002193 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002194 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002195 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002196 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002197
Timo Teräs80c802f2010-04-07 00:30:05 +00002198 dst = NULL;
2199 xdst = NULL;
2200 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002201
Thomas Graff7944fb2007-08-25 13:46:55 -07002202 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002203 num_pols = 1;
2204 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2205 err = xfrm_expand_policies(fl, family, pols,
2206 &num_pols, &num_xfrms);
2207 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002208 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002209
2210 if (num_pols) {
2211 if (num_xfrms <= 0) {
2212 drop_pols = num_pols;
2213 goto no_transform;
2214 }
2215
2216 xdst = xfrm_resolve_and_create_bundle(
2217 pols, num_pols, fl,
2218 family, dst_orig);
2219 if (IS_ERR(xdst)) {
2220 xfrm_pols_put(pols, num_pols);
2221 err = PTR_ERR(xdst);
2222 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002223 } else if (xdst == NULL) {
2224 num_xfrms = 0;
2225 drop_pols = num_pols;
2226 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002227 }
2228
Steffen Klassertb7eea452014-06-18 12:34:21 +02002229 dst_hold(&xdst->u.dst);
2230 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002231 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002232 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Timo Teräs80c802f2010-04-07 00:30:05 +00002235 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002236 struct xfrm_flo xflo;
2237
2238 xflo.dst_orig = dst_orig;
2239 xflo.flags = flags;
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002242 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002243 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002244 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Timo Teräs80c802f2010-04-07 00:30:05 +00002246 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002247 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002248 if (flo == NULL)
2249 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002250 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002251 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002252 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002253 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002254 xdst = container_of(flo, struct xfrm_dst, flo);
2255
2256 num_pols = xdst->num_pols;
2257 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002258 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002259 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
Timo Teräs80c802f2010-04-07 00:30:05 +00002262 dst = &xdst->u.dst;
2263 if (route == NULL && num_xfrms > 0) {
2264 /* The only case when xfrm_bundle_lookup() returns a
2265 * bundle with null route, is when the template could
2266 * not be resolved. It means policies are there, but
2267 * bundle could not be created, since we don't yet
2268 * have the xfrm_state's. We need to wait for KM to
2269 * negotiate new SA's or bail out with error.*/
2270 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002271 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002272 err = -EREMOTE;
2273 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002274 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002275
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002276 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002277
2278 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2279 goto error;
2280 }
2281
2282no_transform:
2283 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002284 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Timo Teräs80c802f2010-04-07 00:30:05 +00002286 if ((flags & XFRM_LOOKUP_ICMP) &&
2287 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2288 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002289 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002290 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002291
Timo Teräs80c802f2010-04-07 00:30:05 +00002292 for (i = 0; i < num_pols; i++)
2293 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002294
Timo Teräs80c802f2010-04-07 00:30:05 +00002295 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002297 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002298 err = -EPERM;
2299 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002300 } else if (num_xfrms > 0) {
2301 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002302 dst_release(dst_orig);
2303 } else {
2304 /* Flow passes untransformed */
2305 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002306 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002308ok:
2309 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002310 if (dst && dst->xfrm &&
2311 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2312 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002313 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Timo Teräs80c802f2010-04-07 00:30:05 +00002315nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002316 if (!(flags & XFRM_LOOKUP_ICMP)) {
2317 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002318 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002319 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002320 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002322 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002323dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002324 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2325 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002326 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002327 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329EXPORT_SYMBOL(xfrm_lookup);
2330
Steffen Klassertf92ee612014-09-16 10:08:40 +02002331/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2332 * Otherwise we may send out blackholed packets.
2333 */
2334struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2335 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002336 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002337{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002338 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002339 flags | XFRM_LOOKUP_QUEUE |
2340 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002341
2342 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2343 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2344
2345 return dst;
2346}
2347EXPORT_SYMBOL(xfrm_lookup_route);
2348
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002349static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002350xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002351{
2352 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002353
2354 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2355 return 0;
2356 x = skb->sp->xvec[idx];
2357 if (!x->type->reject)
2358 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002359 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002360}
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362/* When skb is transformed back to its "native" form, we have to
2363 * check policy restrictions. At the moment we make this in maximally
2364 * stupid way. Shame on me. :-) Of course, connected sockets must
2365 * have policy cached at them.
2366 */
2367
2368static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002369xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 unsigned short family)
2371{
2372 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002373 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return x->id.proto == tmpl->id.proto &&
2375 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2376 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2377 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002378 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002379 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002380 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2381 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002384/*
2385 * 0 or more than 0 is returned when validation is succeeded (either bypass
2386 * because of optional transport mode, or next index of the mathced secpath
2387 * state with the template.
2388 * -1 is returned when no matching template is found.
2389 * Otherwise "-2 - errored_index" is returned.
2390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002392xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 unsigned short family)
2394{
2395 int idx = start;
2396
2397 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002398 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 return start;
2400 } else
2401 start = -1;
2402 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002403 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002405 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2406 if (start == -1)
2407 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411 return start;
2412}
2413
Herbert Xud5422ef2007-12-12 10:44:16 -08002414int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2415 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002418 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 if (unlikely(afinfo == NULL))
2421 return -EAFNOSUPPORT;
2422
Herbert Xud5422ef2007-12-12 10:44:16 -08002423 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002424 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
Herbert Xud5422ef2007-12-12 10:44:16 -08002428EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
David S. Miller9a7386e2011-02-24 01:44:12 -05002430static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002433 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002434 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438
2439 return 0;
2440}
2441
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002442int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 unsigned short family)
2444{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002445 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002447 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2448 int npols = 0;
2449 int xfrm_nr;
2450 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002451 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002453 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002454 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Herbert Xud5422ef2007-12-12 10:44:16 -08002456 reverse = dir & ~XFRM_POLICY_MASK;
2457 dir &= XFRM_POLICY_MASK;
2458 fl_dir = policy_to_flow_dir(dir);
2459
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002460 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002461 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002463 }
2464
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002465 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 /* First, check used SA against their selectors. */
2468 if (skb->sp) {
2469 int i;
2470
Weilong Chen9b7a7872013-12-24 09:43:46 +08002471 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002472 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002473 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002474 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478 }
2479
2480 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002481 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002482 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002483 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002484 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002485 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002486 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002489 if (!pol) {
2490 struct flow_cache_object *flo;
2491
2492 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2493 xfrm_policy_lookup, NULL);
2494 if (IS_ERR_OR_NULL(flo))
2495 pol = ERR_CAST(flo);
2496 else
2497 pol = container_of(flo, struct xfrm_policy, flo);
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002500 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002501 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002502 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002503 }
James Morris134b0fc2006-10-05 15:42:27 -05002504
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002505 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002506 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002507 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002508 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002509 return 0;
2510 }
2511 return 1;
2512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
James Morris9d729f72007-03-04 16:12:44 -08002514 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002516 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002517 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002518#ifdef CONFIG_XFRM_SUB_POLICY
2519 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002520 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002521 &fl, family,
2522 XFRM_POLICY_IN);
2523 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002524 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002525 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002526 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002527 }
James Morris9d729f72007-03-04 16:12:44 -08002528 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002529 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002530 }
2531 }
2532#endif
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (pol->action == XFRM_POLICY_ALLOW) {
2535 struct sec_path *sp;
2536 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002537 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002538 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002539 struct xfrm_tmpl **tpp = tp;
2540 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 int i, k;
2542
2543 if ((sp = skb->sp) == NULL)
2544 sp = &dummy;
2545
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002546 for (pi = 0; pi < npols; pi++) {
2547 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002548 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002549 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002550 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002551 }
2552 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002553 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002554 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002555 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002556 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2557 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2558 }
2559 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002560 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002561 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002562 tpp = stp;
2563 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /* For each tunnel xfrm, find the first matching tmpl.
2566 * For each tmpl before that, find corresponding xfrm.
2567 * Order is _important_. Later we will implement
2568 * some barriers, but at the moment barriers
2569 * are implied between each two transformations.
2570 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002571 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2572 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002573 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002574 if (k < -1)
2575 /* "-2 - errored_index" returned */
2576 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002577 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002582 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002583 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002587 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return 1;
2589 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002590 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002593 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002594reject_error:
2595 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597}
2598EXPORT_SYMBOL(__xfrm_policy_check);
2599
2600int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2601{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002602 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002604 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002605 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002607 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002608 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002612 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002613
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002614 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002615 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002616 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002617 dst = NULL;
2618 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002619 skb_dst_set(skb, dst);
2620 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622EXPORT_SYMBOL(__xfrm_route_forward);
2623
David S. Millerd49c73c2006-08-13 18:55:53 -07002624/* Optimize later using cookies and generation ids. */
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2627{
David S. Millerd49c73c2006-08-13 18:55:53 -07002628 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002629 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2630 * get validated by dst_ops->check on every use. We do this
2631 * because when a normal route referenced by an XFRM dst is
2632 * obsoleted we do not go looking around for all parent
2633 * referencing XFRM dsts so that we can invalidate them. It
2634 * is just too much work. Instead we make the checks here on
2635 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002636 *
2637 * XFRM dst A --> IPv4 dst X
2638 *
2639 * X is the "xdst->route" of A (X is also the "dst->path" of A
2640 * in this example). If X is marked obsolete, "A" will not
2641 * notice. That's what we are validating here via the
2642 * stale_bundle() check.
2643 *
2644 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002645 * dst which causes it's ->obsolete field to be set to
2646 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2647 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002648 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002649 if (dst->obsolete < 0 && !stale_bundle(dst))
2650 return dst;
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return NULL;
2653}
2654
2655static int stale_bundle(struct dst_entry *dst)
2656{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002657 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Herbert Xuaabc9762005-05-03 16:27:10 -07002660void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002663 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002664 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 dev_put(dev);
2666 }
2667}
Herbert Xuaabc9762005-05-03 16:27:10 -07002668EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670static void xfrm_link_failure(struct sk_buff *skb)
2671{
2672 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
2675static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2676{
2677 if (dst) {
2678 if (dst->obsolete) {
2679 dst_release(dst);
2680 dst = NULL;
2681 }
2682 }
2683 return dst;
2684}
2685
Paul Mooree4c17212013-05-29 07:36:25 +00002686void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002687{
Fan Duca925cf2014-01-18 09:55:27 +08002688 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002689}
Paul Mooree4c17212013-05-29 07:36:25 +00002690EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002691
2692static void xfrm_garbage_collect_deferred(struct net *net)
2693{
Fan Duca925cf2014-01-18 09:55:27 +08002694 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002695}
2696
Herbert Xu25ee3282007-12-11 09:32:34 -08002697static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 do {
2700 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2701 u32 pmtu, route_mtu_cached;
2702
2703 pmtu = dst_mtu(dst->child);
2704 xdst->child_mtu_cached = pmtu;
2705
2706 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2707
2708 route_mtu_cached = dst_mtu(xdst->route);
2709 xdst->route_mtu_cached = route_mtu_cached;
2710
2711 if (pmtu > route_mtu_cached)
2712 pmtu = route_mtu_cached;
2713
David S. Millerdefb3512010-12-08 21:16:57 -08002714 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 } while ((dst = dst->next));
2716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718/* Check that the bundle accepts the flow and its components are
2719 * still valid.
2720 */
2721
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002722static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
2724 struct dst_entry *dst = &first->u.dst;
2725 struct xfrm_dst *last;
2726 u32 mtu;
2727
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002728 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 (dst->dev && !netif_running(dst->dev)))
2730 return 0;
2731
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002732 if (dst->flags & DST_XFRM_QUEUE)
2733 return 1;
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 last = NULL;
2736
2737 do {
2738 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2741 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002742 if (xdst->xfrm_genid != dst->xfrm->genid)
2743 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002744 if (xdst->num_pols > 0 &&
2745 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 mtu = dst_mtu(dst->child);
2749 if (xdst->child_mtu_cached != mtu) {
2750 last = xdst;
2751 xdst->child_mtu_cached = mtu;
2752 }
2753
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002754 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return 0;
2756 mtu = dst_mtu(xdst->route);
2757 if (xdst->route_mtu_cached != mtu) {
2758 last = xdst;
2759 xdst->route_mtu_cached = mtu;
2760 }
2761
2762 dst = dst->child;
2763 } while (dst->xfrm);
2764
2765 if (likely(!last))
2766 return 1;
2767
2768 mtu = last->child_mtu_cached;
2769 for (;;) {
2770 dst = &last->u.dst;
2771
2772 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2773 if (mtu > last->route_mtu_cached)
2774 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002775 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 if (last == first)
2778 break;
2779
Patrick McHardybd0bf072007-07-18 01:55:52 -07002780 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 last->child_mtu_cached = mtu;
2782 }
2783
2784 return 1;
2785}
2786
David S. Miller0dbaee32010-12-13 12:52:14 -08002787static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2788{
2789 return dst_metric_advmss(dst->path);
2790}
2791
Steffen Klassertebb762f2011-11-23 02:12:51 +00002792static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002793{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002794 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2795
2796 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002797}
2798
David S. Millerf894cbf2012-07-02 21:52:24 -07002799static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2800 struct sk_buff *skb,
2801 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002802{
David S. Millerf894cbf2012-07-02 21:52:24 -07002803 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002804}
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2807{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002808 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 int err = 0;
2810 if (unlikely(afinfo == NULL))
2811 return -EINVAL;
2812 if (unlikely(afinfo->family >= NPROTO))
2813 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002814 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002816 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 else {
2818 struct dst_ops *dst_ops = afinfo->dst_ops;
2819 if (likely(dst_ops->kmem_cachep == NULL))
2820 dst_ops->kmem_cachep = xfrm_dst_cache;
2821 if (likely(dst_ops->check == NULL))
2822 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002823 if (likely(dst_ops->default_advmss == NULL))
2824 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002825 if (likely(dst_ops->mtu == NULL))
2826 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (likely(dst_ops->negative_advice == NULL))
2828 dst_ops->negative_advice = xfrm_negative_advice;
2829 if (likely(dst_ops->link_failure == NULL))
2830 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002831 if (likely(dst_ops->neigh_lookup == NULL))
2832 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002834 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002835 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002837 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002838
2839 rtnl_lock();
2840 for_each_net(net) {
2841 struct dst_ops *xfrm_dst_ops;
2842
2843 switch (afinfo->family) {
2844 case AF_INET:
2845 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2846 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002847#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002848 case AF_INET6:
2849 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2850 break;
2851#endif
2852 default:
2853 BUG();
2854 }
2855 *xfrm_dst_ops = *afinfo->dst_ops;
2856 }
2857 rtnl_unlock();
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return err;
2860}
2861EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2862
2863int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2864{
2865 int err = 0;
2866 if (unlikely(afinfo == NULL))
2867 return -EINVAL;
2868 if (unlikely(afinfo->family >= NPROTO))
2869 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002870 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2872 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2873 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002874 else
2875 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2876 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002878 spin_unlock(&xfrm_policy_afinfo_lock);
2879 if (!err) {
2880 struct dst_ops *dst_ops = afinfo->dst_ops;
2881
2882 synchronize_rcu();
2883
2884 dst_ops->kmem_cachep = NULL;
2885 dst_ops->check = NULL;
2886 dst_ops->negative_advice = NULL;
2887 dst_ops->link_failure = NULL;
2888 afinfo->garbage_collect = NULL;
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return err;
2891}
2892EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2893
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002894static void __net_init xfrm_dst_ops_init(struct net *net)
2895{
2896 struct xfrm_policy_afinfo *afinfo;
2897
Eric Dumazetef8531b2012-08-19 12:31:48 +02002898 rcu_read_lock();
2899 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002900 if (afinfo)
2901 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002902#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002903 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002904 if (afinfo)
2905 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2906#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002907 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002908}
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2911{
Jiri Pirko351638e2013-05-28 01:30:21 +00002912 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 switch (event) {
2915 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002916 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918 return NOTIFY_DONE;
2919}
2920
2921static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002922 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923};
2924
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002925#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002926static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002927{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002928 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002929 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2930 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002931 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002932 rv = xfrm_proc_init(net);
2933 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002934 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002935 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002936}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002937
2938static void xfrm_statistics_fini(struct net *net)
2939{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002940 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002941 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002942}
2943#else
2944static int __net_init xfrm_statistics_init(struct net *net)
2945{
2946 return 0;
2947}
2948
2949static void xfrm_statistics_fini(struct net *net)
2950{
2951}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002952#endif
2953
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002954static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
David S. Miller2518c7c2006-08-24 04:45:07 -07002956 unsigned int hmask, sz;
2957 int dir;
2958
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002959 if (net_eq(net, &init_net))
2960 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002962 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002963 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
David S. Miller2518c7c2006-08-24 04:45:07 -07002965 hmask = 8 - 1;
2966 sz = (hmask+1) * sizeof(struct hlist_head);
2967
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002968 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2969 if (!net->xfrm.policy_byidx)
2970 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002971 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002972
Herbert Xu53c2e282014-11-13 17:09:49 +08002973 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002974 struct xfrm_policy_hash *htab;
2975
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002976 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002977 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002978 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002979
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002980 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002981 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002982 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002983 goto out_bydst;
2984 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002985 htab->dbits4 = 32;
2986 htab->sbits4 = 32;
2987 htab->dbits6 = 128;
2988 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002989 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002990 net->xfrm.policy_hthresh.lbits4 = 32;
2991 net->xfrm.policy_hthresh.rbits4 = 32;
2992 net->xfrm.policy_hthresh.lbits6 = 128;
2993 net->xfrm.policy_hthresh.rbits6 = 128;
2994
2995 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002996
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002997 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002998 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002999 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003000 if (net_eq(net, &init_net))
3001 register_netdevice_notifier(&xfrm_dev_notifier);
3002 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003003
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003004out_bydst:
3005 for (dir--; dir >= 0; dir--) {
3006 struct xfrm_policy_hash *htab;
3007
3008 htab = &net->xfrm.policy_bydst[dir];
3009 xfrm_hash_free(htab->table, sz);
3010 }
3011 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003012out_byidx:
3013 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003016static void xfrm_policy_fini(struct net *net)
3017{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003018 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003019 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003020
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003021 flush_work(&net->xfrm.policy_hash_work);
3022#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003023 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003024#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003025 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003026
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003027 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003028
Herbert Xu53c2e282014-11-13 17:09:49 +08003029 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003030 struct xfrm_policy_hash *htab;
3031
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003032 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003033
3034 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003035 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003036 WARN_ON(!hlist_empty(htab->table));
3037 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003038 }
3039
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003040 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003041 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3042 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003043}
3044
3045static int __net_init xfrm_net_init(struct net *net)
3046{
3047 int rv;
3048
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003049 rv = xfrm_statistics_init(net);
3050 if (rv < 0)
3051 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003052 rv = xfrm_state_init(net);
3053 if (rv < 0)
3054 goto out_state;
3055 rv = xfrm_policy_init(net);
3056 if (rv < 0)
3057 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003058 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003059 rv = xfrm_sysctl_init(net);
3060 if (rv < 0)
3061 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003062 rv = flow_cache_init(net);
3063 if (rv < 0)
3064 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003065
3066 /* Initialize the per-net locks here */
3067 spin_lock_init(&net->xfrm.xfrm_state_lock);
3068 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003069 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3070
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003071 return 0;
3072
Steffen Klassert4a93f502014-03-12 09:43:17 +01003073out:
3074 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003075out_sysctl:
3076 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003077out_policy:
3078 xfrm_state_fini(net);
3079out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003080 xfrm_statistics_fini(net);
3081out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003082 return rv;
3083}
3084
3085static void __net_exit xfrm_net_exit(struct net *net)
3086{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003087 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003088 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003089 xfrm_policy_fini(net);
3090 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003091 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003092}
3093
3094static struct pernet_operations __net_initdata xfrm_net_ops = {
3095 .init = xfrm_net_init,
3096 .exit = xfrm_net_exit,
3097};
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099void __init xfrm_init(void)
3100{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003101 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 xfrm_input_init();
3103}
3104
Joy Lattenab5f5e82007-09-17 11:51:22 -07003105#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003106static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3107 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003108{
Paul Moore875179f2007-12-01 23:27:18 +11003109 struct xfrm_sec_ctx *ctx = xp->security;
3110 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003111
Paul Moore875179f2007-12-01 23:27:18 +11003112 if (ctx)
3113 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3114 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3115
Weilong Chen9b7a7872013-12-24 09:43:46 +08003116 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003117 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003118 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003119 if (sel->prefixlen_s != 32)
3120 audit_log_format(audit_buf, " src_prefixlen=%d",
3121 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003122 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003123 if (sel->prefixlen_d != 32)
3124 audit_log_format(audit_buf, " dst_prefixlen=%d",
3125 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003126 break;
3127 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003128 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003129 if (sel->prefixlen_s != 128)
3130 audit_log_format(audit_buf, " src_prefixlen=%d",
3131 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003132 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003133 if (sel->prefixlen_d != 128)
3134 audit_log_format(audit_buf, " dst_prefixlen=%d",
3135 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003136 break;
3137 }
3138}
3139
Tetsuo Handa2e710292014-04-22 21:48:30 +09003140void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003141{
3142 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003143
Paul Mooreafeb14b2007-12-21 14:58:11 -08003144 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003145 if (audit_buf == NULL)
3146 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003147 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003148 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003149 xfrm_audit_common_policyinfo(xp, audit_buf);
3150 audit_log_end(audit_buf);
3151}
3152EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3153
Paul Moore68277ac2007-12-20 20:49:33 -08003154void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003155 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003156{
3157 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003158
Paul Mooreafeb14b2007-12-21 14:58:11 -08003159 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003160 if (audit_buf == NULL)
3161 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003162 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003163 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003164 xfrm_audit_common_policyinfo(xp, audit_buf);
3165 audit_log_end(audit_buf);
3166}
3167EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3168#endif
3169
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003170#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003171static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3172 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003173{
3174 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3175 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003176 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3177 sel_cmp->family) &&
3178 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3179 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003180 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3181 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003182 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003183 }
3184 } else {
3185 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003186 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003187 }
3188 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003189 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003190}
3191
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003192static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3193 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003194{
3195 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003196 struct hlist_head *chain;
3197 u32 priority = ~0U;
3198
Fan Du283bc9f2013-11-07 17:47:50 +08003199 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003200 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003201 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003202 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3203 pol->type == type) {
3204 ret = pol;
3205 priority = ret->priority;
3206 break;
3207 }
3208 }
Fan Du8d549c42013-11-07 17:47:49 +08003209 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003210 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003211 if ((pol->priority >= priority) && ret)
3212 break;
3213
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003214 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003215 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003216 ret = pol;
3217 break;
3218 }
3219 }
3220
Li RongQing586f2eb2015-04-30 17:13:41 +08003221 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003222
Fan Du283bc9f2013-11-07 17:47:50 +08003223 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003224
3225 return ret;
3226}
3227
David S. Millerdd701752011-02-24 00:21:08 -05003228static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003229{
3230 int match = 0;
3231
3232 if (t->mode == m->mode && t->id.proto == m->proto &&
3233 (m->reqid == 0 || t->reqid == m->reqid)) {
3234 switch (t->mode) {
3235 case XFRM_MODE_TUNNEL:
3236 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003237 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3238 m->old_family) &&
3239 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3240 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003241 match = 1;
3242 }
3243 break;
3244 case XFRM_MODE_TRANSPORT:
3245 /* in case of transport mode, template does not store
3246 any IP addresses, hence we just compare mode and
3247 protocol */
3248 match = 1;
3249 break;
3250 default:
3251 break;
3252 }
3253 }
3254 return match;
3255}
3256
3257/* update endpoint address(es) of template(s) */
3258static int xfrm_policy_migrate(struct xfrm_policy *pol,
3259 struct xfrm_migrate *m, int num_migrate)
3260{
3261 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003262 int i, j, n = 0;
3263
3264 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003265 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003266 /* target policy has been deleted */
3267 write_unlock_bh(&pol->lock);
3268 return -ENOENT;
3269 }
3270
3271 for (i = 0; i < pol->xfrm_nr; i++) {
3272 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3273 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3274 continue;
3275 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003276 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3277 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003278 continue;
3279 /* update endpoints */
3280 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3281 sizeof(pol->xfrm_vec[i].id.daddr));
3282 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3283 sizeof(pol->xfrm_vec[i].saddr));
3284 pol->xfrm_vec[i].encap_family = mp->new_family;
3285 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003286 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003287 }
3288 }
3289
3290 write_unlock_bh(&pol->lock);
3291
3292 if (!n)
3293 return -ENODATA;
3294
3295 return 0;
3296}
3297
David S. Millerdd701752011-02-24 00:21:08 -05003298static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003299{
3300 int i, j;
3301
3302 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3303 return -EINVAL;
3304
3305 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003306 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3307 m[i].old_family) &&
3308 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3309 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003310 return -EINVAL;
3311 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3312 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3313 return -EINVAL;
3314
3315 /* check if there is any duplicated entry */
3316 for (j = i + 1; j < num_migrate; j++) {
3317 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3318 sizeof(m[i].old_daddr)) &&
3319 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3320 sizeof(m[i].old_saddr)) &&
3321 m[i].proto == m[j].proto &&
3322 m[i].mode == m[j].mode &&
3323 m[i].reqid == m[j].reqid &&
3324 m[i].old_family == m[j].old_family)
3325 return -EINVAL;
3326 }
3327 }
3328
3329 return 0;
3330}
3331
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003332int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003333 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003334 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003335{
3336 int i, err, nx_cur = 0, nx_new = 0;
3337 struct xfrm_policy *pol = NULL;
3338 struct xfrm_state *x, *xc;
3339 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3340 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3341 struct xfrm_migrate *mp;
3342
3343 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3344 goto out;
3345
3346 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003347 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003348 err = -ENOENT;
3349 goto out;
3350 }
3351
3352 /* Stage 2 - find and update state(s) */
3353 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003354 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003355 x_cur[nx_cur] = x;
3356 nx_cur++;
3357 if ((xc = xfrm_state_migrate(x, mp))) {
3358 x_new[nx_new] = xc;
3359 nx_new++;
3360 } else {
3361 err = -ENODATA;
3362 goto restore_state;
3363 }
3364 }
3365 }
3366
3367 /* Stage 3 - update policy */
3368 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3369 goto restore_state;
3370
3371 /* Stage 4 - delete old state(s) */
3372 if (nx_cur) {
3373 xfrm_states_put(x_cur, nx_cur);
3374 xfrm_states_delete(x_cur, nx_cur);
3375 }
3376
3377 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003378 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003379
3380 xfrm_pol_put(pol);
3381
3382 return 0;
3383out:
3384 return err;
3385
3386restore_state:
3387 if (pol)
3388 xfrm_pol_put(pol);
3389 if (nx_cur)
3390 xfrm_states_put(x_cur, nx_cur);
3391 if (nx_new)
3392 xfrm_states_delete(x_new, nx_new);
3393
3394 return err;
3395}
David S. Millere610e672007-02-08 13:29:15 -08003396EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003397#endif