blob: f57a5712cedd1db8c4f53fca06fa4ae274e5892a [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Steffen Klassertb8c203b2014-09-16 10:08:49 +020042struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
Priyanka Jain418a99a2012-08-12 21:22:29 +000047static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Herbert Xu25ee3282007-12-11 09:32:34 -080053static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000054static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000055static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010056static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Herbert Xu12bfa8b2014-11-13 17:09:50 +080058static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080059static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
60 int dir);
61
David S. Millerbc9b35a2012-05-15 15:04:57 -040062static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050063__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080064{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050065 const struct flowi4 *fl4 = &fl->u.ip4;
66
Alexey Dobriyan26bff942011-11-22 06:46:02 +000067 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
68 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050069 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
70 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
71 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
72 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080073}
74
David S. Millerbc9b35a2012-05-15 15:04:57 -040075static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050076__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080077{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050078 const struct flowi6 *fl6 = &fl->u.ip6;
79
80 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
81 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
82 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
83 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
84 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
85 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080086}
87
David S. Millerbc9b35a2012-05-15 15:04:57 -040088bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
89 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080090{
91 switch (family) {
92 case AF_INET:
93 return __xfrm4_selector_match(sel, fl);
94 case AF_INET6:
95 return __xfrm6_selector_match(sel, fl);
96 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040097 return false;
Andrew Morton77681022006-11-08 22:46:26 -080098}
99
Eric Dumazetef8531b2012-08-19 12:31:48 +0200100static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
101{
102 struct xfrm_policy_afinfo *afinfo;
103
104 if (unlikely(family >= NPROTO))
105 return NULL;
106 rcu_read_lock();
107 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
108 if (unlikely(!afinfo))
109 rcu_read_unlock();
110 return afinfo;
111}
112
113static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
114{
115 rcu_read_unlock();
116}
117
David Ahern42a7b322015-08-10 16:58:11 -0600118static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
119 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David Ahern42a7b322015-08-10 16:58:11 -0600131 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
137
David Ahern42a7b322015-08-10 16:58:11 -0600138static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
139 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140 xfrm_address_t *prev_saddr,
141 xfrm_address_t *prev_daddr,
142 int family)
143{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800144 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900145 xfrm_address_t *saddr = &x->props.saddr;
146 xfrm_address_t *daddr = &x->id.daddr;
147 struct dst_entry *dst;
148
149 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
150 saddr = x->coaddr;
151 daddr = prev_daddr;
152 }
153 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
154 saddr = prev_saddr;
155 daddr = x->coaddr;
156 }
157
David Ahern42a7b322015-08-10 16:58:11 -0600158 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900159
160 if (!IS_ERR(dst)) {
161 if (prev_saddr != saddr)
162 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
163 if (prev_daddr != daddr)
164 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
165 }
166
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800167 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static inline unsigned long make_jiffies(long secs)
171{
172 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
173 return MAX_SCHEDULE_TIMEOUT-1;
174 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900175 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static void xfrm_policy_timer(unsigned long data)
179{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800180 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800181 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 long next = LONG_MAX;
183 int warn = 0;
184 int dir;
185
186 read_lock(&xp->lock);
187
Timo Teräsea2dea92010-03-31 00:17:05 +0000188 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out;
190
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700191 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (xp->lft.hard_add_expires_seconds) {
194 long tmo = xp->lft.hard_add_expires_seconds +
195 xp->curlft.add_time - now;
196 if (tmo <= 0)
197 goto expired;
198 if (tmo < next)
199 next = tmo;
200 }
201 if (xp->lft.hard_use_expires_seconds) {
202 long tmo = xp->lft.hard_use_expires_seconds +
203 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
204 if (tmo <= 0)
205 goto expired;
206 if (tmo < next)
207 next = tmo;
208 }
209 if (xp->lft.soft_add_expires_seconds) {
210 long tmo = xp->lft.soft_add_expires_seconds +
211 xp->curlft.add_time - now;
212 if (tmo <= 0) {
213 warn = 1;
214 tmo = XFRM_KM_TIMEOUT;
215 }
216 if (tmo < next)
217 next = tmo;
218 }
219 if (xp->lft.soft_use_expires_seconds) {
220 long tmo = xp->lft.soft_use_expires_seconds +
221 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
222 if (tmo <= 0) {
223 warn = 1;
224 tmo = XFRM_KM_TIMEOUT;
225 }
226 if (tmo < next)
227 next = tmo;
228 }
229
230 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800231 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (next != LONG_MAX &&
233 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
234 xfrm_pol_hold(xp);
235
236out:
237 read_unlock(&xp->lock);
238 xfrm_pol_put(xp);
239 return;
240
241expired:
242 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700243 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800244 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 xfrm_pol_put(xp);
246}
247
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000248static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
249{
250 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
251
252 if (unlikely(pol->walk.dead))
253 flo = NULL;
254 else
255 xfrm_pol_hold(pol);
256
257 return flo;
258}
259
260static int xfrm_policy_flo_check(struct flow_cache_object *flo)
261{
262 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
263
264 return !pol->walk.dead;
265}
266
267static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
268{
269 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
270}
271
272static const struct flow_cache_ops xfrm_policy_fc_ops = {
273 .get = xfrm_policy_flo_get,
274 .check = xfrm_policy_flo_check,
275 .delete = xfrm_policy_flo_delete,
276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
279 * SPD calls.
280 */
281
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800282struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct xfrm_policy *policy;
285
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700286 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800289 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700290 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700291 INIT_HLIST_NODE(&policy->bydst);
292 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700294 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100295 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800296 setup_timer(&policy->timer, xfrm_policy_timer,
297 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100298 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
299 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000300 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302 return policy;
303}
304EXPORT_SYMBOL(xfrm_policy_alloc);
305
Eric Dumazet56f04732015-12-08 07:22:01 -0800306static void xfrm_policy_destroy_rcu(struct rcu_head *head)
307{
308 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
309
310 security_xfrm_policy_free(policy->security);
311 kfree(policy);
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Destroy xfrm_policy: descendant resources must be released to this moment. */
315
WANG Cong64c31b32008-01-07 22:34:29 -0800316void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Herbert Xu12a169e2008-10-01 07:03:24 -0700318 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Fan Du0659eea2013-08-01 18:08:36 +0800320 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 BUG();
322
Eric Dumazet56f04732015-12-08 07:22:01 -0800323 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
WANG Cong64c31b32008-01-07 22:34:29 -0800325EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* Rule must be locked. Release descentant resources, announce
328 * entry dead. The rule must be unlinked from lists to the moment.
329 */
330
331static void xfrm_policy_kill(struct xfrm_policy *policy)
332{
Herbert Xu12a169e2008-10-01 07:03:24 -0700333 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Timo Teräs285ead12010-04-07 00:30:06 +0000335 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200337 if (del_timer(&policy->polq.hold_timer))
338 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800339 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100340
Timo Teräs285ead12010-04-07 00:30:06 +0000341 if (del_timer(&policy->timer))
342 xfrm_pol_put(policy);
343
344 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
David S. Miller2518c7c2006-08-24 04:45:07 -0700347static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
348
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800349static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700350{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800351 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700352}
353
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200354/* calculate policy hash thresholds */
355static void __get_hash_thresh(struct net *net,
356 unsigned short family, int dir,
357 u8 *dbits, u8 *sbits)
358{
359 switch (family) {
360 case AF_INET:
361 *dbits = net->xfrm.policy_bydst[dir].dbits4;
362 *sbits = net->xfrm.policy_bydst[dir].sbits4;
363 break;
364
365 case AF_INET6:
366 *dbits = net->xfrm.policy_bydst[dir].dbits6;
367 *sbits = net->xfrm.policy_bydst[dir].sbits6;
368 break;
369
370 default:
371 *dbits = 0;
372 *sbits = 0;
373 }
374}
375
David S. Miller5f803b52011-02-24 00:33:19 -0500376static struct hlist_head *policy_hash_bysel(struct net *net,
377 const struct xfrm_selector *sel,
378 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700379{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800380 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200381 unsigned int hash;
382 u8 dbits;
383 u8 sbits;
384
385 __get_hash_thresh(net, family, dir, &dbits, &sbits);
386 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700387
388 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800389 &net->xfrm.policy_inexact[dir] :
390 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700391}
392
David S. Miller5f803b52011-02-24 00:33:19 -0500393static struct hlist_head *policy_hash_direct(struct net *net,
394 const xfrm_address_t *daddr,
395 const xfrm_address_t *saddr,
396 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700397{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800398 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200399 unsigned int hash;
400 u8 dbits;
401 u8 sbits;
402
403 __get_hash_thresh(net, family, dir, &dbits, &sbits);
404 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700405
Alexey Dobriyan11219942008-11-25 17:33:06 -0800406 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407}
408
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200409static void xfrm_dst_hash_transfer(struct net *net,
410 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700411 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200412 unsigned int nhashmask,
413 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700414{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800415 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800417 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418 u8 dbits;
419 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700420
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800421redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800422 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700423 unsigned int h;
424
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200425 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700426 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200427 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800428 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800429 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430 hlist_add_head(&pol->bydst, ndsttable+h);
431 h0 = h;
432 } else {
433 if (h != h0)
434 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800435 hlist_del(&pol->bydst);
Ken Helias1d023282014-08-06 16:09:16 -0700436 hlist_add_behind(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800438 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800439 }
440 if (!hlist_empty(list)) {
441 entry0 = NULL;
442 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700443 }
444}
445
446static void xfrm_idx_hash_transfer(struct hlist_head *list,
447 struct hlist_head *nidxtable,
448 unsigned int nhashmask)
449{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800450 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700451 struct xfrm_policy *pol;
452
Sasha Levinb67bfe02013-02-27 17:06:00 -0800453 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700454 unsigned int h;
455
456 h = __idx_hash(pol->index, nhashmask);
457 hlist_add_head(&pol->byidx, nidxtable+h);
458 }
459}
460
461static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
462{
463 return ((old_hmask + 1) << 1) - 1;
464}
465
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800466static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700467{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800468 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700469 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
470 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800471 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700472 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700473 int i;
474
475 if (!ndst)
476 return;
477
Fan Du283bc9f2013-11-07 17:47:50 +0800478 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700479
480 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200481 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700482
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800483 net->xfrm.policy_bydst[dir].table = ndst;
484 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700485
Fan Du283bc9f2013-11-07 17:47:50 +0800486 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700487
David S. Miller44e36b42006-08-24 04:50:50 -0700488 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700489}
490
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800491static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700492{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800493 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700494 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
495 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800496 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700497 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498 int i;
499
500 if (!nidx)
501 return;
502
Fan Du283bc9f2013-11-07 17:47:50 +0800503 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504
505 for (i = hmask; i >= 0; i--)
506 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
507
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800508 net->xfrm.policy_byidx = nidx;
509 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700510
Fan Du283bc9f2013-11-07 17:47:50 +0800511 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
David S. Miller44e36b42006-08-24 04:50:50 -0700513 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700514}
515
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700517{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800518 unsigned int cnt = net->xfrm.policy_count[dir];
519 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700520
521 if (total)
522 *total += cnt;
523
524 if ((hmask + 1) < xfrm_policy_hashmax &&
525 cnt > hmask)
526 return 1;
527
528 return 0;
529}
530
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800531static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700532{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800533 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700534
535 if ((hmask + 1) < xfrm_policy_hashmax &&
536 total > hmask)
537 return 1;
538
539 return 0;
540}
541
Alexey Dobriyane0710412010-01-23 13:37:10 +0000542void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700543{
Fan Du283bc9f2013-11-07 17:47:50 +0800544 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000545 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
546 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
547 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
548 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
549 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
550 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
551 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700552 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800553 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700554}
555EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700556
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700557static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800558static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700559{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800560 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700561 int dir, total;
562
563 mutex_lock(&hash_resize_mutex);
564
565 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800566 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800567 if (xfrm_bydst_should_resize(net, dir, &total))
568 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700569 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800570 if (xfrm_byidx_should_resize(net, total))
571 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700572
573 mutex_unlock(&hash_resize_mutex);
574}
575
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200576static void xfrm_hash_rebuild(struct work_struct *work)
577{
578 struct net *net = container_of(work, struct net,
579 xfrm.policy_hthresh.work);
580 unsigned int hmask;
581 struct xfrm_policy *pol;
582 struct xfrm_policy *policy;
583 struct hlist_head *chain;
584 struct hlist_head *odst;
585 struct hlist_node *newpos;
586 int i;
587 int dir;
588 unsigned seq;
589 u8 lbits4, rbits4, lbits6, rbits6;
590
591 mutex_lock(&hash_resize_mutex);
592
593 /* read selector prefixlen thresholds */
594 do {
595 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
596
597 lbits4 = net->xfrm.policy_hthresh.lbits4;
598 rbits4 = net->xfrm.policy_hthresh.rbits4;
599 lbits6 = net->xfrm.policy_hthresh.lbits6;
600 rbits6 = net->xfrm.policy_hthresh.rbits6;
601 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
602
603 write_lock_bh(&net->xfrm.xfrm_policy_lock);
604
605 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800606 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200607 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
608 hmask = net->xfrm.policy_bydst[dir].hmask;
609 odst = net->xfrm.policy_bydst[dir].table;
610 for (i = hmask; i >= 0; i--)
611 INIT_HLIST_HEAD(odst + i);
612 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
613 /* dir out => dst = remote, src = local */
614 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
615 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
616 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
617 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
618 } else {
619 /* dir in/fwd => dst = local, src = remote */
620 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
621 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
622 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
623 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
624 }
625 }
626
627 /* re-insert all policies by order of creation */
628 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
629 newpos = NULL;
630 chain = policy_hash_bysel(net, &policy->selector,
631 policy->family,
632 xfrm_policy_id2dir(policy->index));
633 hlist_for_each_entry(pol, chain, bydst) {
634 if (policy->priority >= pol->priority)
635 newpos = &pol->bydst;
636 else
637 break;
638 }
639 if (newpos)
640 hlist_add_behind(&policy->bydst, newpos);
641 else
642 hlist_add_head(&policy->bydst, chain);
643 }
644
645 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
646
647 mutex_unlock(&hash_resize_mutex);
648}
649
650void xfrm_policy_hash_rebuild(struct net *net)
651{
652 schedule_work(&net->xfrm.policy_hthresh.work);
653}
654EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/* Generate new index... KAME seems to generate them ordered by cost
657 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800658static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 static u32 idx_generator;
661
662 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700663 struct hlist_head *list;
664 struct xfrm_policy *p;
665 u32 idx;
666 int found;
667
Fan Due682adf2013-11-07 17:47:48 +0800668 if (!index) {
669 idx = (idx_generator | dir);
670 idx_generator += 8;
671 } else {
672 idx = index;
673 index = 0;
674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (idx == 0)
677 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800678 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800680 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700681 if (p->index == idx) {
682 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700686 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return idx;
688 }
689}
690
David S. Miller2518c7c2006-08-24 04:45:07 -0700691static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
692{
693 u32 *p1 = (u32 *) s1;
694 u32 *p2 = (u32 *) s2;
695 int len = sizeof(struct xfrm_selector) / sizeof(u32);
696 int i;
697
698 for (i = 0; i < len; i++) {
699 if (p1[i] != p2[i])
700 return 1;
701 }
702
703 return 0;
704}
705
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100706static void xfrm_policy_requeue(struct xfrm_policy *old,
707 struct xfrm_policy *new)
708{
709 struct xfrm_policy_queue *pq = &old->polq;
710 struct sk_buff_head list;
711
Li RongQingde2ad482015-04-30 17:25:19 +0800712 if (skb_queue_empty(&pq->hold_queue))
713 return;
714
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100715 __skb_queue_head_init(&list);
716
717 spin_lock_bh(&pq->hold_queue.lock);
718 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200719 if (del_timer(&pq->hold_timer))
720 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100721 spin_unlock_bh(&pq->hold_queue.lock);
722
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100723 pq = &new->polq;
724
725 spin_lock_bh(&pq->hold_queue.lock);
726 skb_queue_splice(&list, &pq->hold_queue);
727 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200728 if (!mod_timer(&pq->hold_timer, jiffies))
729 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100730 spin_unlock_bh(&pq->hold_queue.lock);
731}
732
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100733static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
734 struct xfrm_policy *pol)
735{
736 u32 mark = policy->mark.v & policy->mark.m;
737
738 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
739 return true;
740
741 if ((mark & pol->mark.m) == pol->mark.v &&
742 policy->priority == pol->priority)
743 return true;
744
745 return false;
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
749{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800750 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700751 struct xfrm_policy *pol;
752 struct xfrm_policy *delpol;
753 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800754 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Fan Du283bc9f2013-11-07 17:47:50 +0800756 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800757 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700758 delpol = NULL;
759 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800760 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800761 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700762 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100763 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800764 xfrm_sec_ctx_match(pol->security, policy->security) &&
765 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800767 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EEXIST;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 delpol = pol;
771 if (policy->priority > pol->priority)
772 continue;
773 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800774 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 continue;
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (delpol)
778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700781 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700782 else
783 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800784 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800785 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800786
787 /* After previous checking, family can either be AF_INET or AF_INET6 */
788 if (policy->family == AF_INET)
789 rt_genid_bump_ipv4(net);
790 else
791 rt_genid_bump_ipv6(net);
792
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100793 if (delpol) {
794 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800795 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100796 }
Fan Due682adf2013-11-07 17:47:48 +0800797 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800798 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800799 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 policy->curlft.use_time = 0;
801 if (!mod_timer(&policy->timer, jiffies + HZ))
802 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800803 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
David S. Miller9b78a822005-12-22 07:39:48 -0800805 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800807 else if (xfrm_bydst_should_resize(net, dir, NULL))
808 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0;
811}
812EXPORT_SYMBOL(xfrm_policy_insert);
813
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000814struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
815 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800816 struct xfrm_sec_ctx *ctx, int delete,
817 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
David S. Miller2518c7c2006-08-24 04:45:07 -0700819 struct xfrm_policy *pol, *ret;
820 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Eric Parisef41aaa2007-03-07 15:37:58 -0800822 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800823 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800824 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700825 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800826 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000828 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700829 !selector_cmp(sel, &pol->selector) &&
830 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700832 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700833 *err = security_xfrm_policy_delete(
834 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800835 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800836 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800837 return pol;
838 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800839 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 }
841 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 break;
843 }
844 }
Fan Du283bc9f2013-11-07 17:47:50 +0800845 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000847 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
Trent Jaegerdf718372005-12-13 23:12:27 -0800851EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000853struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
854 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 struct xfrm_policy *pol, *ret;
857 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Herbert Xub5505c62007-05-14 02:15:47 -0700859 *err = -ENOENT;
860 if (xfrm_policy_id2dir(id) != dir)
861 return NULL;
862
Eric Parisef41aaa2007-03-07 15:37:58 -0800863 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800864 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800865 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800867 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000868 if (pol->type == type && pol->index == id &&
869 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700871 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700872 *err = security_xfrm_policy_delete(
873 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800874 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800875 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800876 return pol;
877 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800878 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700879 }
880 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 }
883 }
Fan Du283bc9f2013-11-07 17:47:50 +0800884 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000886 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700887 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700888 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890EXPORT_SYMBOL(xfrm_policy_byid);
891
Joy Latten4aa2e622007-06-04 19:05:57 -0400892#ifdef CONFIG_SECURITY_NETWORK_XFRM
893static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900894xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Joy Latten4aa2e622007-06-04 19:05:57 -0400896 int dir, err = 0;
897
898 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
899 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400900 int i;
901
Sasha Levinb67bfe02013-02-27 17:06:00 -0800902 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800903 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400904 if (pol->type != type)
905 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700906 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400907 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900908 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400909 return err;
910 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900911 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800912 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800913 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800914 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400915 bydst) {
916 if (pol->type != type)
917 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700918 err = security_xfrm_policy_delete(
919 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400920 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700921 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900922 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400923 return err;
924 }
925 }
926 }
927 }
928 return err;
929}
930#else
931static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900932xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400933{
934 return 0;
935}
936#endif
937
Tetsuo Handa2e710292014-04-22 21:48:30 +0900938int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400939{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000940 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Fan Du283bc9f2013-11-07 17:47:50 +0800942 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400943
Tetsuo Handa2e710292014-04-22 21:48:30 +0900944 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400945 if (err)
946 goto out;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700949 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800950 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700951
952 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800953 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800954 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700955 if (pol->type != type)
956 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000957 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800958 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000959 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Tetsuo Handa2e710292014-04-22 21:48:30 +0900961 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600962
David S. Miller2518c7c2006-08-24 04:45:07 -0700963 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Fan Du283bc9f2013-11-07 17:47:50 +0800965 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700966 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700968
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800969 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700970 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800971 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800972 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700973 bydst) {
974 if (pol->type != type)
975 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000976 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800977 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000978 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700979
Tetsuo Handa2e710292014-04-22 21:48:30 +0900980 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700981 xfrm_policy_kill(pol);
982
Fan Du283bc9f2013-11-07 17:47:50 +0800983 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 goto again2;
985 }
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000989 if (!cnt)
990 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400991out:
Fan Du283bc9f2013-11-07 17:47:50 +0800992 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995EXPORT_SYMBOL(xfrm_policy_flush);
996
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800997int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800998 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 void *data)
1000{
Herbert Xu12a169e2008-10-01 07:03:24 -07001001 struct xfrm_policy *pol;
1002 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001003 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Timo Teras4c563f72008-02-28 21:31:08 -08001005 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1006 walk->type != XFRM_POLICY_TYPE_ANY)
1007 return -EINVAL;
1008
Herbert Xu12a169e2008-10-01 07:03:24 -07001009 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001010 return 0;
1011
Fan Du283bc9f2013-11-07 17:47:50 +08001012 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001013 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001014 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001015 else
Li RongQing80077702015-04-22 17:09:54 +08001016 x = list_first_entry(&walk->walk.all,
1017 struct xfrm_policy_walk_entry, all);
1018
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001019 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001020 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001021 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001022 pol = container_of(x, struct xfrm_policy, walk);
1023 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1024 walk->type != pol->type)
1025 continue;
1026 error = func(pol, xfrm_policy_id2dir(pol->index),
1027 walk->seq, data);
1028 if (error) {
1029 list_move_tail(&walk->walk.all, &x->all);
1030 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001031 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001032 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001034 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001035 error = -ENOENT;
1036 goto out;
1037 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001038 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039out:
Fan Du283bc9f2013-11-07 17:47:50 +08001040 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return error;
1042}
1043EXPORT_SYMBOL(xfrm_policy_walk);
1044
Herbert Xu12a169e2008-10-01 07:03:24 -07001045void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1046{
1047 INIT_LIST_HEAD(&walk->walk.all);
1048 walk->walk.dead = 1;
1049 walk->type = type;
1050 walk->seq = 0;
1051}
1052EXPORT_SYMBOL(xfrm_policy_walk_init);
1053
Fan Du283bc9f2013-11-07 17:47:50 +08001054void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001055{
1056 if (list_empty(&walk->walk.all))
1057 return;
1058
Fan Du283bc9f2013-11-07 17:47:50 +08001059 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001060 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001061 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001062}
1063EXPORT_SYMBOL(xfrm_policy_walk_done);
1064
James Morris134b0fc2006-10-05 15:42:27 -05001065/*
1066 * Find policy to apply to this flow.
1067 *
1068 * Returns 0 if policy found, else an -errno.
1069 */
David S. Millerf299d552011-02-24 01:23:30 -05001070static int xfrm_policy_match(const struct xfrm_policy *pol,
1071 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001072 u8 type, u16 family, int dir)
1073{
David S. Millerf299d552011-02-24 01:23:30 -05001074 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001075 int ret = -ESRCH;
1076 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001077
1078 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001079 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001080 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001081 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001082
1083 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001084 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001085 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001086 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001087
James Morris134b0fc2006-10-05 15:42:27 -05001088 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001089}
1090
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001091static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001092 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001093 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
James Morris134b0fc2006-10-05 15:42:27 -05001095 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001096 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001097 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001098 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -07001099 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001100
1101 daddr = xfrm_flowi_daddr(fl, family);
1102 saddr = xfrm_flowi_saddr(fl, family);
1103 if (unlikely(!daddr || !saddr))
1104 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Fan Du283bc9f2013-11-07 17:47:50 +08001106 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001107 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001108 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001109 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001110 err = xfrm_policy_match(pol, fl, type, family, dir);
1111 if (err) {
1112 if (err == -ESRCH)
1113 continue;
1114 else {
1115 ret = ERR_PTR(err);
1116 goto fail;
1117 }
1118 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001119 ret = pol;
1120 priority = ret->priority;
1121 break;
1122 }
1123 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001124 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001125 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001126 if ((pol->priority >= priority) && ret)
1127 break;
1128
James Morris134b0fc2006-10-05 15:42:27 -05001129 err = xfrm_policy_match(pol, fl, type, family, dir);
1130 if (err) {
1131 if (err == -ESRCH)
1132 continue;
1133 else {
1134 ret = ERR_PTR(err);
1135 goto fail;
1136 }
Li RongQing8faf4912015-05-14 11:16:59 +08001137 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001138 ret = pol;
1139 break;
1140 }
1141 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001142
1143 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001144fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001145 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001146
David S. Miller2518c7c2006-08-24 04:45:07 -07001147 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001148}
1149
Timo Teräs80c802f2010-04-07 00:30:05 +00001150static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001151__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001152{
1153#ifdef CONFIG_XFRM_SUB_POLICY
1154 struct xfrm_policy *pol;
1155
1156 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1157 if (pol != NULL)
1158 return pol;
1159#endif
1160 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1161}
1162
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001163static int flow_to_policy_dir(int dir)
1164{
1165 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1166 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1167 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1168 return dir;
1169
1170 switch (dir) {
1171 default:
1172 case FLOW_DIR_IN:
1173 return XFRM_POLICY_IN;
1174 case FLOW_DIR_OUT:
1175 return XFRM_POLICY_OUT;
1176 case FLOW_DIR_FWD:
1177 return XFRM_POLICY_FWD;
1178 }
1179}
1180
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001181static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001182xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001183 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001184{
1185 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001186
1187 if (old_obj)
1188 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001189
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001190 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001191 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001192 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001193
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001194 /* Resolver returns two references:
1195 * one for cache and one for caller of flow_cache_lookup() */
1196 xfrm_pol_hold(pol);
1197
1198 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Trent Jaegerdf718372005-12-13 23:12:27 -08001201static inline int policy_to_flow_dir(int dir)
1202{
1203 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001204 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1205 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1206 return dir;
1207 switch (dir) {
1208 default:
1209 case XFRM_POLICY_IN:
1210 return FLOW_DIR_IN;
1211 case XFRM_POLICY_OUT:
1212 return FLOW_DIR_OUT;
1213 case XFRM_POLICY_FWD:
1214 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001215 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001216}
1217
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001218static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001219 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001222 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Fan Du283bc9f2013-11-07 17:47:50 +08001224 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001226 bool match = xfrm_selector_match(&pol->selector, fl,
1227 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001228 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001229
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001230 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001231 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1232 pol = NULL;
1233 goto out;
1234 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001235 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001236 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001237 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001238 if (!err)
1239 xfrm_pol_hold(pol);
1240 else if (err == -ESRCH)
1241 pol = NULL;
1242 else
1243 pol = ERR_PTR(err);
1244 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 pol = NULL;
1246 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001247out:
Fan Du283bc9f2013-11-07 17:47:50 +08001248 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return pol;
1250}
1251
1252static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1253{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001254 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001255
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001256 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001257 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 xfrm_pol_hold(pol);
1259}
1260
1261static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1262 int dir)
1263{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001264 struct net *net = xp_net(pol);
1265
Herbert Xu53c2e282014-11-13 17:09:49 +08001266 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001267 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Herbert Xu53c2e282014-11-13 17:09:49 +08001269 /* Socket policies are not hashed. */
1270 if (!hlist_unhashed(&pol->bydst)) {
1271 hlist_del(&pol->bydst);
1272 hlist_del(&pol->byidx);
1273 }
1274
1275 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001276 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001277
1278 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Herbert Xu53c2e282014-11-13 17:09:49 +08001281static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1282{
1283 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1284}
1285
1286static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1287{
1288 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1289}
1290
Herbert Xu4666faa2005-06-18 22:43:22 -07001291int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Fan Du283bc9f2013-11-07 17:47:50 +08001293 struct net *net = xp_net(pol);
1294
1295 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001297 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001302 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
David S. Millera70fcb02006-03-20 19:18:52 -08001304EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1307{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001308 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 struct xfrm_policy *old_pol;
1310
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001311#ifdef CONFIG_XFRM_SUB_POLICY
1312 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1313 return -EINVAL;
1314#endif
1315
Fan Du283bc9f2013-11-07 17:47:50 +08001316 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 old_pol = sk->sk_policy[dir];
1318 sk->sk_policy[dir] = pol;
1319 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001320 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001321 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001322 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001324 if (old_pol) {
1325 if (pol)
1326 xfrm_policy_requeue(old_pol, pol);
1327
Timo Teräsea2dea92010-03-31 00:17:05 +00001328 /* Unlinking succeeds always. This is the only function
1329 * allowed to delete or replace socket policy.
1330 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001331 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001332 }
Fan Du283bc9f2013-11-07 17:47:50 +08001333 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 if (old_pol) {
1336 xfrm_policy_kill(old_pol);
1337 }
1338 return 0;
1339}
1340
David S. Millerd3e40a92011-02-24 01:25:41 -05001341static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001343 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001344 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 if (newp) {
1347 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001348 if (security_xfrm_policy_clone(old->security,
1349 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001350 kfree(newp);
1351 return NULL; /* ENOMEM */
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 newp->lft = old->lft;
1354 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001355 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 newp->action = old->action;
1357 newp->flags = old->flags;
1358 newp->xfrm_nr = old->xfrm_nr;
1359 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001360 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 memcpy(newp->xfrm_vec, old->xfrm_vec,
1362 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001363 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001364 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001365 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 xfrm_pol_put(newp);
1367 }
1368 return newp;
1369}
1370
1371int __xfrm_sk_clone_policy(struct sock *sk)
1372{
1373 struct xfrm_policy *p0 = sk->sk_policy[0],
1374 *p1 = sk->sk_policy[1];
1375
1376 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1377 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1378 return -ENOMEM;
1379 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1380 return -ENOMEM;
1381 return 0;
1382}
1383
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001384static int
David Ahern42a7b322015-08-10 16:58:11 -06001385xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1386 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001387{
1388 int err;
1389 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1390
1391 if (unlikely(afinfo == NULL))
1392 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001393 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001394 xfrm_policy_put_afinfo(afinfo);
1395 return err;
1396}
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398/* Resolve list of templates for the flow, given policy. */
1399
1400static int
David S. Millera6c2e612011-02-22 18:35:39 -08001401xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1402 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001404 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 int nx;
1406 int i, error;
1407 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1408 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001409 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Weilong Chen9b7a7872013-12-24 09:43:46 +08001411 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct xfrm_state *x;
1413 xfrm_address_t *remote = daddr;
1414 xfrm_address_t *local = saddr;
1415 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1416
Joakim Koskela48b8d782007-07-26 00:08:42 -07001417 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1418 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 remote = &tmpl->id.daddr;
1420 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001421 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001422 error = xfrm_get_saddr(net, fl->flowi_oif,
1423 &tmp, remote,
1424 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001425 if (error)
1426 goto fail;
1427 local = &tmp;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1432
1433 if (x && x->km.state == XFRM_STATE_VALID) {
1434 xfrm[nx++] = x;
1435 daddr = remote;
1436 saddr = local;
1437 continue;
1438 }
1439 if (x) {
1440 error = (x->km.state == XFRM_STATE_ERROR ?
1441 -EINVAL : -EAGAIN);
1442 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001443 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001444 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (!tmpl->optional)
1448 goto fail;
1449 }
1450 return nx;
1451
1452fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001453 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 xfrm_state_put(xfrm[nx]);
1455 return error;
1456}
1457
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001458static int
David S. Millera6c2e612011-02-22 18:35:39 -08001459xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1460 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001461{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001462 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1463 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001464 int cnx = 0;
1465 int error;
1466 int ret;
1467 int i;
1468
1469 for (i = 0; i < npols; i++) {
1470 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1471 error = -ENOBUFS;
1472 goto fail;
1473 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001474
1475 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001476 if (ret < 0) {
1477 error = ret;
1478 goto fail;
1479 } else
1480 cnx += ret;
1481 }
1482
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001483 /* found states are sorted for outbound processing */
1484 if (npols > 1)
1485 xfrm_state_sort(xfrm, tpp, cnx, family);
1486
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001487 return cnx;
1488
1489 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001490 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001491 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001492 return error;
1493
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/* Check that the bundle accepts the flow and its components are
1497 * still valid.
1498 */
1499
David S. Miller05d84022011-02-22 17:47:10 -08001500static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001501{
1502 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1503 int tos;
1504
1505 if (!afinfo)
1506 return -EINVAL;
1507
1508 tos = afinfo->get_tos(fl);
1509
1510 xfrm_policy_put_afinfo(afinfo);
1511
1512 return tos;
1513}
1514
Timo Teräs80c802f2010-04-07 00:30:05 +00001515static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1516{
1517 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1518 struct dst_entry *dst = &xdst->u.dst;
1519
1520 if (xdst->route == NULL) {
1521 /* Dummy bundle - if it has xfrms we were not
1522 * able to build bundle as template resolution failed.
1523 * It means we need to try again resolving. */
1524 if (xdst->num_xfrms > 0)
1525 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001526 } else if (dst->flags & DST_XFRM_QUEUE) {
1527 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001528 } else {
1529 /* Real bundle */
1530 if (stale_bundle(dst))
1531 return NULL;
1532 }
1533
1534 dst_hold(dst);
1535 return flo;
1536}
1537
1538static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1539{
1540 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1541 struct dst_entry *dst = &xdst->u.dst;
1542
1543 if (!xdst->route)
1544 return 0;
1545 if (stale_bundle(dst))
1546 return 0;
1547
1548 return 1;
1549}
1550
1551static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1552{
1553 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1554 struct dst_entry *dst = &xdst->u.dst;
1555
1556 dst_free(dst);
1557}
1558
1559static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1560 .get = xfrm_bundle_flo_get,
1561 .check = xfrm_bundle_flo_check,
1562 .delete = xfrm_bundle_flo_delete,
1563};
1564
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001565static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001566{
1567 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001568 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001569 struct xfrm_dst *xdst;
1570
1571 if (!afinfo)
1572 return ERR_PTR(-EINVAL);
1573
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001574 switch (family) {
1575 case AF_INET:
1576 dst_ops = &net->xfrm.xfrm4_dst_ops;
1577 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001578#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001579 case AF_INET6:
1580 dst_ops = &net->xfrm.xfrm6_dst_ops;
1581 break;
1582#endif
1583 default:
1584 BUG();
1585 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001586 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001587
Madalin Bucurd4cae562011-09-26 07:04:36 +00001588 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001589 struct dst_entry *dst = &xdst->u.dst;
1590
1591 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001592 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001593 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001594 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001595
Madalin Bucurd4cae562011-09-26 07:04:36 +00001596 xfrm_policy_put_afinfo(afinfo);
1597
Herbert Xu25ee3282007-12-11 09:32:34 -08001598 return xdst;
1599}
1600
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001601static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1602 int nfheader_len)
1603{
1604 struct xfrm_policy_afinfo *afinfo =
1605 xfrm_policy_get_afinfo(dst->ops->family);
1606 int err;
1607
1608 if (!afinfo)
1609 return -EINVAL;
1610
1611 err = afinfo->init_path(path, dst, nfheader_len);
1612
1613 xfrm_policy_put_afinfo(afinfo);
1614
1615 return err;
1616}
1617
Herbert Xu87c1e122010-03-02 02:51:56 +00001618static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001619 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001620{
1621 struct xfrm_policy_afinfo *afinfo =
1622 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1623 int err;
1624
1625 if (!afinfo)
1626 return -EINVAL;
1627
Herbert Xu87c1e122010-03-02 02:51:56 +00001628 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001629
1630 xfrm_policy_put_afinfo(afinfo);
1631
1632 return err;
1633}
1634
Timo Teräs80c802f2010-04-07 00:30:05 +00001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1637 * all the metrics... Shortly, bundle a bundle.
1638 */
1639
Herbert Xu25ee3282007-12-11 09:32:34 -08001640static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1641 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001642 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001643 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001645 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001646 unsigned long now = jiffies;
1647 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001648 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001649 struct dst_entry *dst_prev = NULL;
1650 struct dst_entry *dst0 = NULL;
1651 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001653 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001654 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001655 int trailer_len = 0;
1656 int tos;
1657 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001658 xfrm_address_t saddr, daddr;
1659
1660 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001661
1662 tos = xfrm_get_tos(fl, family);
1663 err = tos;
1664 if (tos < 0)
1665 goto put_states;
1666
1667 dst_hold(dst);
1668
1669 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001670 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001671 struct dst_entry *dst1 = &xdst->u.dst;
1672
1673 err = PTR_ERR(xdst);
1674 if (IS_ERR(xdst)) {
1675 dst_release(dst);
1676 goto put_states;
1677 }
1678
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001679 if (xfrm[i]->sel.family == AF_UNSPEC) {
1680 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1681 xfrm_af2proto(family));
1682 if (!inner_mode) {
1683 err = -EAFNOSUPPORT;
1684 dst_release(dst);
1685 goto put_states;
1686 }
1687 } else
1688 inner_mode = xfrm[i]->inner_mode;
1689
Herbert Xu25ee3282007-12-11 09:32:34 -08001690 if (!dst_prev)
1691 dst0 = dst1;
1692 else {
1693 dst_prev->child = dst_clone(dst1);
1694 dst1->flags |= DST_NOHASH;
1695 }
1696
1697 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001698 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001699
1700 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1701 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001702 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1703 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001704 err = PTR_ERR(dst);
1705 if (IS_ERR(dst))
1706 goto put_states;
1707 } else
1708 dst_hold(dst);
1709
1710 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001711 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001712
David S. Millerf5b0a872012-07-19 12:31:33 -07001713 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001714 dst1->flags |= DST_HOST;
1715 dst1->lastuse = now;
1716
1717 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001718 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001719
1720 dst1->next = dst_prev;
1721 dst_prev = dst1;
1722
1723 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001724 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1725 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001726 trailer_len += xfrm[i]->props.trailer_len;
1727 }
1728
1729 dst_prev->child = dst;
1730 dst0->path = dst;
1731
1732 err = -ENODEV;
1733 dev = dst->dev;
1734 if (!dev)
1735 goto free_dst;
1736
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001737 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001738 xfrm_init_pmtu(dst_prev);
1739
1740 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1741 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1742
Herbert Xu87c1e122010-03-02 02:51:56 +00001743 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001744 if (err)
1745 goto free_dst;
1746
1747 dst_prev->header_len = header_len;
1748 dst_prev->trailer_len = trailer_len;
1749 header_len -= xdst->u.dst.xfrm->props.header_len;
1750 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1751 }
1752
1753out:
1754 return dst0;
1755
1756put_states:
1757 for (; i < nx; i++)
1758 xfrm_state_put(xfrm[i]);
1759free_dst:
1760 if (dst0)
1761 dst_free(dst0);
1762 dst0 = ERR_PTR(err);
1763 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Ying Xueda7c2242014-01-08 10:26:39 +08001766#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001767static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001768{
1769 if (!*target) {
1770 *target = kmalloc(size, GFP_ATOMIC);
1771 if (!*target)
1772 return -ENOMEM;
1773 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001774
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001775 memcpy(*target, src, size);
1776 return 0;
1777}
Ying Xueda7c2242014-01-08 10:26:39 +08001778#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001779
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001780static int xfrm_dst_update_parent(struct dst_entry *dst,
1781 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001782{
1783#ifdef CONFIG_XFRM_SUB_POLICY
1784 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1785 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1786 sel, sizeof(*sel));
1787#else
1788 return 0;
1789#endif
1790}
1791
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001792static int xfrm_dst_update_origin(struct dst_entry *dst,
1793 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001794{
1795#ifdef CONFIG_XFRM_SUB_POLICY
1796 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1797 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1798#else
1799 return 0;
1800#endif
1801}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
David S. Miller73ff93c2011-02-22 18:33:42 -08001803static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001804 struct xfrm_policy **pols,
1805 int *num_pols, int *num_xfrms)
1806{
1807 int i;
1808
1809 if (*num_pols == 0 || !pols[0]) {
1810 *num_pols = 0;
1811 *num_xfrms = 0;
1812 return 0;
1813 }
1814 if (IS_ERR(pols[0]))
1815 return PTR_ERR(pols[0]);
1816
1817 *num_xfrms = pols[0]->xfrm_nr;
1818
1819#ifdef CONFIG_XFRM_SUB_POLICY
1820 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1821 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1822 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1823 XFRM_POLICY_TYPE_MAIN,
1824 fl, family,
1825 XFRM_POLICY_OUT);
1826 if (pols[1]) {
1827 if (IS_ERR(pols[1])) {
1828 xfrm_pols_put(pols, *num_pols);
1829 return PTR_ERR(pols[1]);
1830 }
Weilong Chen02d08922013-12-24 09:43:48 +08001831 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001832 (*num_xfrms) += pols[1]->xfrm_nr;
1833 }
1834 }
1835#endif
1836 for (i = 0; i < *num_pols; i++) {
1837 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1838 *num_xfrms = -1;
1839 break;
1840 }
1841 }
1842
1843 return 0;
1844
1845}
1846
1847static struct xfrm_dst *
1848xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001849 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001850 struct dst_entry *dst_orig)
1851{
1852 struct net *net = xp_net(pols[0]);
1853 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1854 struct dst_entry *dst;
1855 struct xfrm_dst *xdst;
1856 int err;
1857
1858 /* Try to instantiate a bundle */
1859 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001860 if (err <= 0) {
1861 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001862 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1863 return ERR_PTR(err);
1864 }
1865
1866 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1867 if (IS_ERR(dst)) {
1868 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1869 return ERR_CAST(dst);
1870 }
1871
1872 xdst = (struct xfrm_dst *)dst;
1873 xdst->num_xfrms = err;
1874 if (num_pols > 1)
1875 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1876 else
1877 err = xfrm_dst_update_origin(dst, fl);
1878 if (unlikely(err)) {
1879 dst_free(dst);
1880 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1881 return ERR_PTR(err);
1882 }
1883
1884 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001885 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001886 xdst->policy_genid = atomic_read(&pols[0]->genid);
1887
1888 return xdst;
1889}
1890
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001891static void xfrm_policy_queue_process(unsigned long arg)
1892{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001893 struct sk_buff *skb;
1894 struct sock *sk;
1895 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001896 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001897 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001898 struct xfrm_policy_queue *pq = &pol->polq;
1899 struct flowi fl;
1900 struct sk_buff_head list;
1901
1902 spin_lock(&pq->hold_queue.lock);
1903 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001904 if (!skb) {
1905 spin_unlock(&pq->hold_queue.lock);
1906 goto out;
1907 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001908 dst = skb_dst(skb);
1909 sk = skb->sk;
1910 xfrm_decode_session(skb, &fl, dst->ops->family);
1911 spin_unlock(&pq->hold_queue.lock);
1912
1913 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001914 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001915 if (IS_ERR(dst))
1916 goto purge_queue;
1917
1918 if (dst->flags & DST_XFRM_QUEUE) {
1919 dst_release(dst);
1920
1921 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1922 goto purge_queue;
1923
1924 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001925 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1926 xfrm_pol_hold(pol);
1927 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001928 }
1929
1930 dst_release(dst);
1931
1932 __skb_queue_head_init(&list);
1933
1934 spin_lock(&pq->hold_queue.lock);
1935 pq->timeout = 0;
1936 skb_queue_splice_init(&pq->hold_queue, &list);
1937 spin_unlock(&pq->hold_queue.lock);
1938
1939 while (!skb_queue_empty(&list)) {
1940 skb = __skb_dequeue(&list);
1941
1942 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1943 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001944 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001945 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001946 kfree_skb(skb);
1947 continue;
1948 }
1949
1950 nf_reset(skb);
1951 skb_dst_drop(skb);
1952 skb_dst_set(skb, dst);
1953
Eric W. Biederman13206b62015-10-07 16:48:35 -05001954 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001955 }
1956
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001957out:
1958 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001959 return;
1960
1961purge_queue:
1962 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001963 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001964 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001965}
1966
Eric W. Biedermanede20592015-10-07 16:48:47 -05001967static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001968{
1969 unsigned long sched_next;
1970 struct dst_entry *dst = skb_dst(skb);
1971 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001972 struct xfrm_policy *pol = xdst->pols[0];
1973 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001974
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001975 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001976 kfree_skb(skb);
1977 return 0;
1978 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001979
1980 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1981 kfree_skb(skb);
1982 return -EAGAIN;
1983 }
1984
1985 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001986
1987 spin_lock_bh(&pq->hold_queue.lock);
1988
1989 if (!pq->timeout)
1990 pq->timeout = XFRM_QUEUE_TMO_MIN;
1991
1992 sched_next = jiffies + pq->timeout;
1993
1994 if (del_timer(&pq->hold_timer)) {
1995 if (time_before(pq->hold_timer.expires, sched_next))
1996 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001997 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001998 }
1999
2000 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002001 if (!mod_timer(&pq->hold_timer, sched_next))
2002 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002003
2004 spin_unlock_bh(&pq->hold_queue.lock);
2005
2006 return 0;
2007}
2008
2009static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002010 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002011 const struct flowi *fl,
2012 int num_xfrms,
2013 u16 family)
2014{
2015 int err;
2016 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002017 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002018 struct dst_entry *dst1;
2019 struct xfrm_dst *xdst;
2020
2021 xdst = xfrm_alloc_dst(net, family);
2022 if (IS_ERR(xdst))
2023 return xdst;
2024
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002025 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2026 net->xfrm.sysctl_larval_drop ||
2027 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002028 return xdst;
2029
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002030 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002031 dst1 = &xdst->u.dst;
2032 dst_hold(dst);
2033 xdst->route = dst;
2034
2035 dst_copy_metrics(dst1, dst);
2036
2037 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2038 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2039 dst1->lastuse = jiffies;
2040
2041 dst1->input = dst_discard;
2042 dst1->output = xdst_queue_output;
2043
2044 dst_hold(dst);
2045 dst1->child = dst;
2046 dst1->path = dst;
2047
2048 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2049
2050 err = -ENODEV;
2051 dev = dst->dev;
2052 if (!dev)
2053 goto free_dst;
2054
2055 err = xfrm_fill_dst(xdst, dev, fl);
2056 if (err)
2057 goto free_dst;
2058
2059out:
2060 return xdst;
2061
2062free_dst:
2063 dst_release(dst1);
2064 xdst = ERR_PTR(err);
2065 goto out;
2066}
2067
Timo Teräs80c802f2010-04-07 00:30:05 +00002068static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002069xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002070 struct flow_cache_object *oldflo, void *ctx)
2071{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002072 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002073 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2074 struct xfrm_dst *xdst, *new_xdst;
2075 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2076
2077 /* Check if the policies from old bundle are usable */
2078 xdst = NULL;
2079 if (oldflo) {
2080 xdst = container_of(oldflo, struct xfrm_dst, flo);
2081 num_pols = xdst->num_pols;
2082 num_xfrms = xdst->num_xfrms;
2083 pol_dead = 0;
2084 for (i = 0; i < num_pols; i++) {
2085 pols[i] = xdst->pols[i];
2086 pol_dead |= pols[i]->walk.dead;
2087 }
2088 if (pol_dead) {
2089 dst_free(&xdst->u.dst);
2090 xdst = NULL;
2091 num_pols = 0;
2092 num_xfrms = 0;
2093 oldflo = NULL;
2094 }
2095 }
2096
2097 /* Resolve policies to use if we couldn't get them from
2098 * previous cache entry */
2099 if (xdst == NULL) {
2100 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002101 pols[0] = __xfrm_policy_lookup(net, fl, family,
2102 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002103 err = xfrm_expand_policies(fl, family, pols,
2104 &num_pols, &num_xfrms);
2105 if (err < 0)
2106 goto inc_error;
2107 if (num_pols == 0)
2108 return NULL;
2109 if (num_xfrms <= 0)
2110 goto make_dummy_bundle;
2111 }
2112
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002113 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2114 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002115 if (IS_ERR(new_xdst)) {
2116 err = PTR_ERR(new_xdst);
2117 if (err != -EAGAIN)
2118 goto error;
2119 if (oldflo == NULL)
2120 goto make_dummy_bundle;
2121 dst_hold(&xdst->u.dst);
2122 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002123 } else if (new_xdst == NULL) {
2124 num_xfrms = 0;
2125 if (oldflo == NULL)
2126 goto make_dummy_bundle;
2127 xdst->num_xfrms = 0;
2128 dst_hold(&xdst->u.dst);
2129 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002130 }
2131
2132 /* Kill the previous bundle */
2133 if (xdst) {
2134 /* The policies were stolen for newly generated bundle */
2135 xdst->num_pols = 0;
2136 dst_free(&xdst->u.dst);
2137 }
2138
2139 /* Flow cache does not have reference, it dst_free()'s,
2140 * but we do need to return one reference for original caller */
2141 dst_hold(&new_xdst->u.dst);
2142 return &new_xdst->flo;
2143
2144make_dummy_bundle:
2145 /* We found policies, but there's no bundles to instantiate:
2146 * either because the policy blocks, has no transformations or
2147 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002148 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002149 if (IS_ERR(xdst)) {
2150 xfrm_pols_put(pols, num_pols);
2151 return ERR_CAST(xdst);
2152 }
2153 xdst->num_pols = num_pols;
2154 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002155 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002156
2157 dst_hold(&xdst->u.dst);
2158 return &xdst->flo;
2159
2160inc_error:
2161 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2162error:
2163 if (xdst != NULL)
2164 dst_free(&xdst->u.dst);
2165 else
2166 xfrm_pols_put(pols, num_pols);
2167 return ERR_PTR(err);
2168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
David S. Miller2774c132011-03-01 14:59:04 -08002170static struct dst_entry *make_blackhole(struct net *net, u16 family,
2171 struct dst_entry *dst_orig)
2172{
2173 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2174 struct dst_entry *ret;
2175
2176 if (!afinfo) {
2177 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002178 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002179 } else {
2180 ret = afinfo->blackhole_route(net, dst_orig);
2181 }
2182 xfrm_policy_put_afinfo(afinfo);
2183
2184 return ret;
2185}
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/* Main function: finds/creates a bundle for given flow.
2188 *
2189 * At the moment we eat a raw IP route. Mostly to speed up lookups
2190 * on interfaces with disabled IPsec.
2191 */
David S. Miller452edd52011-03-02 13:27:41 -08002192struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2193 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002194 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002196 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002197 struct flow_cache_object *flo;
2198 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002199 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002200 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002201 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002202 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002203
Timo Teräs80c802f2010-04-07 00:30:05 +00002204 dst = NULL;
2205 xdst = NULL;
2206 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002207
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002208 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002209 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002210 num_pols = 1;
2211 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2212 err = xfrm_expand_policies(fl, family, pols,
2213 &num_pols, &num_xfrms);
2214 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002215 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002216
2217 if (num_pols) {
2218 if (num_xfrms <= 0) {
2219 drop_pols = num_pols;
2220 goto no_transform;
2221 }
2222
2223 xdst = xfrm_resolve_and_create_bundle(
2224 pols, num_pols, fl,
2225 family, dst_orig);
2226 if (IS_ERR(xdst)) {
2227 xfrm_pols_put(pols, num_pols);
2228 err = PTR_ERR(xdst);
2229 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002230 } else if (xdst == NULL) {
2231 num_xfrms = 0;
2232 drop_pols = num_pols;
2233 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002234 }
2235
Steffen Klassertb7eea452014-06-18 12:34:21 +02002236 dst_hold(&xdst->u.dst);
2237 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002238 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002239 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Timo Teräs80c802f2010-04-07 00:30:05 +00002242 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002243 struct xfrm_flo xflo;
2244
2245 xflo.dst_orig = dst_orig;
2246 xflo.flags = flags;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002249 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002250 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002251 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Timo Teräs80c802f2010-04-07 00:30:05 +00002253 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002254 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002255 if (flo == NULL)
2256 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002257 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002258 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002259 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002260 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002261 xdst = container_of(flo, struct xfrm_dst, flo);
2262
2263 num_pols = xdst->num_pols;
2264 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002265 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002266 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
2268
Timo Teräs80c802f2010-04-07 00:30:05 +00002269 dst = &xdst->u.dst;
2270 if (route == NULL && num_xfrms > 0) {
2271 /* The only case when xfrm_bundle_lookup() returns a
2272 * bundle with null route, is when the template could
2273 * not be resolved. It means policies are there, but
2274 * bundle could not be created, since we don't yet
2275 * have the xfrm_state's. We need to wait for KM to
2276 * negotiate new SA's or bail out with error.*/
2277 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002278 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002279 err = -EREMOTE;
2280 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002281 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002282
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002283 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002284
2285 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2286 goto error;
2287 }
2288
2289no_transform:
2290 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002291 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Timo Teräs80c802f2010-04-07 00:30:05 +00002293 if ((flags & XFRM_LOOKUP_ICMP) &&
2294 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2295 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002296 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002297 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002298
Timo Teräs80c802f2010-04-07 00:30:05 +00002299 for (i = 0; i < num_pols; i++)
2300 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002301
Timo Teräs80c802f2010-04-07 00:30:05 +00002302 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002304 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002305 err = -EPERM;
2306 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002307 } else if (num_xfrms > 0) {
2308 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002309 dst_release(dst_orig);
2310 } else {
2311 /* Flow passes untransformed */
2312 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002313 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002315ok:
2316 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002317 if (dst && dst->xfrm &&
2318 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2319 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002320 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Timo Teräs80c802f2010-04-07 00:30:05 +00002322nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002323 if (!(flags & XFRM_LOOKUP_ICMP)) {
2324 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002325 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002326 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002327 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002329 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002330dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002331 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2332 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002333 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002334 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336EXPORT_SYMBOL(xfrm_lookup);
2337
Steffen Klassertf92ee612014-09-16 10:08:40 +02002338/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2339 * Otherwise we may send out blackholed packets.
2340 */
2341struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2342 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002343 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002344{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002345 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002346 flags | XFRM_LOOKUP_QUEUE |
2347 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002348
2349 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2350 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2351
2352 return dst;
2353}
2354EXPORT_SYMBOL(xfrm_lookup_route);
2355
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002356static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002357xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002358{
2359 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002360
2361 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2362 return 0;
2363 x = skb->sp->xvec[idx];
2364 if (!x->type->reject)
2365 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002366 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002367}
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/* When skb is transformed back to its "native" form, we have to
2370 * check policy restrictions. At the moment we make this in maximally
2371 * stupid way. Shame on me. :-) Of course, connected sockets must
2372 * have policy cached at them.
2373 */
2374
2375static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002376xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 unsigned short family)
2378{
2379 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002380 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return x->id.proto == tmpl->id.proto &&
2382 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2383 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2384 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002385 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002386 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002387 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2388 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002391/*
2392 * 0 or more than 0 is returned when validation is succeeded (either bypass
2393 * because of optional transport mode, or next index of the mathced secpath
2394 * state with the template.
2395 * -1 is returned when no matching template is found.
2396 * Otherwise "-2 - errored_index" is returned.
2397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002399xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 unsigned short family)
2401{
2402 int idx = start;
2403
2404 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002405 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return start;
2407 } else
2408 start = -1;
2409 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002410 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002412 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2413 if (start == -1)
2414 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 return start;
2419}
2420
Herbert Xud5422ef2007-12-12 10:44:16 -08002421int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2422 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002425 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 if (unlikely(afinfo == NULL))
2428 return -EAFNOSUPPORT;
2429
Herbert Xud5422ef2007-12-12 10:44:16 -08002430 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002431 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002433 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434}
Herbert Xud5422ef2007-12-12 10:44:16 -08002435EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
David S. Miller9a7386e2011-02-24 01:44:12 -05002437static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002440 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002441 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 return 0;
2447}
2448
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002449int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned short family)
2451{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002452 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002454 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2455 int npols = 0;
2456 int xfrm_nr;
2457 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002458 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002460 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002461 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Herbert Xud5422ef2007-12-12 10:44:16 -08002463 reverse = dir & ~XFRM_POLICY_MASK;
2464 dir &= XFRM_POLICY_MASK;
2465 fl_dir = policy_to_flow_dir(dir);
2466
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002467 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002468 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002470 }
2471
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002472 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 /* First, check used SA against their selectors. */
2475 if (skb->sp) {
2476 int i;
2477
Weilong Chen9b7a7872013-12-24 09:43:46 +08002478 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002479 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002480 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002481 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485 }
2486
2487 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002488 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002489 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002490 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002491 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002492 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002493 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002494 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002497 if (!pol) {
2498 struct flow_cache_object *flo;
2499
2500 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2501 xfrm_policy_lookup, NULL);
2502 if (IS_ERR_OR_NULL(flo))
2503 pol = ERR_CAST(flo);
2504 else
2505 pol = container_of(flo, struct xfrm_policy, flo);
2506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002508 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002509 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002510 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002511 }
James Morris134b0fc2006-10-05 15:42:27 -05002512
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002513 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002514 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002515 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002516 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002517 return 0;
2518 }
2519 return 1;
2520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
James Morris9d729f72007-03-04 16:12:44 -08002522 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002524 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002525 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002526#ifdef CONFIG_XFRM_SUB_POLICY
2527 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002528 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002529 &fl, family,
2530 XFRM_POLICY_IN);
2531 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002532 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002533 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002534 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002535 }
James Morris9d729f72007-03-04 16:12:44 -08002536 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002537 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002538 }
2539 }
2540#endif
2541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (pol->action == XFRM_POLICY_ALLOW) {
2543 struct sec_path *sp;
2544 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002545 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002546 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002547 struct xfrm_tmpl **tpp = tp;
2548 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 int i, k;
2550
2551 if ((sp = skb->sp) == NULL)
2552 sp = &dummy;
2553
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002554 for (pi = 0; pi < npols; pi++) {
2555 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002556 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002557 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002558 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002559 }
2560 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002561 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002562 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002563 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002564 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2565 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2566 }
2567 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002568 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002569 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002570 tpp = stp;
2571 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 /* For each tunnel xfrm, find the first matching tmpl.
2574 * For each tmpl before that, find corresponding xfrm.
2575 * Order is _important_. Later we will implement
2576 * some barriers, but at the moment barriers
2577 * are implied between each two transformations.
2578 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002579 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2580 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002581 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002582 if (k < -1)
2583 /* "-2 - errored_index" returned */
2584 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002585 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002590 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002591 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002595 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 1;
2597 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002598 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002601 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002602reject_error:
2603 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return 0;
2605}
2606EXPORT_SYMBOL(__xfrm_policy_check);
2607
2608int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2609{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002610 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002612 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002613 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002615 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002616 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002620 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002621
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002622 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002623 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002624 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002625 dst = NULL;
2626 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002627 skb_dst_set(skb, dst);
2628 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630EXPORT_SYMBOL(__xfrm_route_forward);
2631
David S. Millerd49c73c2006-08-13 18:55:53 -07002632/* Optimize later using cookies and generation ids. */
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2635{
David S. Millerd49c73c2006-08-13 18:55:53 -07002636 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002637 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2638 * get validated by dst_ops->check on every use. We do this
2639 * because when a normal route referenced by an XFRM dst is
2640 * obsoleted we do not go looking around for all parent
2641 * referencing XFRM dsts so that we can invalidate them. It
2642 * is just too much work. Instead we make the checks here on
2643 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002644 *
2645 * XFRM dst A --> IPv4 dst X
2646 *
2647 * X is the "xdst->route" of A (X is also the "dst->path" of A
2648 * in this example). If X is marked obsolete, "A" will not
2649 * notice. That's what we are validating here via the
2650 * stale_bundle() check.
2651 *
2652 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002653 * dst which causes it's ->obsolete field to be set to
2654 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2655 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002656 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002657 if (dst->obsolete < 0 && !stale_bundle(dst))
2658 return dst;
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 return NULL;
2661}
2662
2663static int stale_bundle(struct dst_entry *dst)
2664{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002665 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Herbert Xuaabc9762005-05-03 16:27:10 -07002668void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002671 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002672 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 dev_put(dev);
2674 }
2675}
Herbert Xuaabc9762005-05-03 16:27:10 -07002676EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678static void xfrm_link_failure(struct sk_buff *skb)
2679{
2680 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
2683static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2684{
2685 if (dst) {
2686 if (dst->obsolete) {
2687 dst_release(dst);
2688 dst = NULL;
2689 }
2690 }
2691 return dst;
2692}
2693
Paul Mooree4c17212013-05-29 07:36:25 +00002694void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002695{
Fan Duca925cf2014-01-18 09:55:27 +08002696 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002697}
Paul Mooree4c17212013-05-29 07:36:25 +00002698EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002699
2700static void xfrm_garbage_collect_deferred(struct net *net)
2701{
Fan Duca925cf2014-01-18 09:55:27 +08002702 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002703}
2704
Herbert Xu25ee3282007-12-11 09:32:34 -08002705static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 do {
2708 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2709 u32 pmtu, route_mtu_cached;
2710
2711 pmtu = dst_mtu(dst->child);
2712 xdst->child_mtu_cached = pmtu;
2713
2714 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2715
2716 route_mtu_cached = dst_mtu(xdst->route);
2717 xdst->route_mtu_cached = route_mtu_cached;
2718
2719 if (pmtu > route_mtu_cached)
2720 pmtu = route_mtu_cached;
2721
David S. Millerdefb3512010-12-08 21:16:57 -08002722 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 } while ((dst = dst->next));
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726/* Check that the bundle accepts the flow and its components are
2727 * still valid.
2728 */
2729
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002730static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 struct dst_entry *dst = &first->u.dst;
2733 struct xfrm_dst *last;
2734 u32 mtu;
2735
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002736 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 (dst->dev && !netif_running(dst->dev)))
2738 return 0;
2739
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002740 if (dst->flags & DST_XFRM_QUEUE)
2741 return 1;
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 last = NULL;
2744
2745 do {
2746 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2749 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002750 if (xdst->xfrm_genid != dst->xfrm->genid)
2751 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002752 if (xdst->num_pols > 0 &&
2753 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 mtu = dst_mtu(dst->child);
2757 if (xdst->child_mtu_cached != mtu) {
2758 last = xdst;
2759 xdst->child_mtu_cached = mtu;
2760 }
2761
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002762 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return 0;
2764 mtu = dst_mtu(xdst->route);
2765 if (xdst->route_mtu_cached != mtu) {
2766 last = xdst;
2767 xdst->route_mtu_cached = mtu;
2768 }
2769
2770 dst = dst->child;
2771 } while (dst->xfrm);
2772
2773 if (likely(!last))
2774 return 1;
2775
2776 mtu = last->child_mtu_cached;
2777 for (;;) {
2778 dst = &last->u.dst;
2779
2780 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2781 if (mtu > last->route_mtu_cached)
2782 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002783 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 if (last == first)
2786 break;
2787
Patrick McHardybd0bf072007-07-18 01:55:52 -07002788 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 last->child_mtu_cached = mtu;
2790 }
2791
2792 return 1;
2793}
2794
David S. Miller0dbaee32010-12-13 12:52:14 -08002795static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2796{
2797 return dst_metric_advmss(dst->path);
2798}
2799
Steffen Klassertebb762f2011-11-23 02:12:51 +00002800static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002801{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002802 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2803
2804 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002805}
2806
David S. Millerf894cbf2012-07-02 21:52:24 -07002807static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2808 struct sk_buff *skb,
2809 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002810{
David S. Millerf894cbf2012-07-02 21:52:24 -07002811 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002812}
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2815{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002816 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 int err = 0;
2818 if (unlikely(afinfo == NULL))
2819 return -EINVAL;
2820 if (unlikely(afinfo->family >= NPROTO))
2821 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002822 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002824 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 else {
2826 struct dst_ops *dst_ops = afinfo->dst_ops;
2827 if (likely(dst_ops->kmem_cachep == NULL))
2828 dst_ops->kmem_cachep = xfrm_dst_cache;
2829 if (likely(dst_ops->check == NULL))
2830 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002831 if (likely(dst_ops->default_advmss == NULL))
2832 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002833 if (likely(dst_ops->mtu == NULL))
2834 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (likely(dst_ops->negative_advice == NULL))
2836 dst_ops->negative_advice = xfrm_negative_advice;
2837 if (likely(dst_ops->link_failure == NULL))
2838 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002839 if (likely(dst_ops->neigh_lookup == NULL))
2840 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002842 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002843 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002845 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002846
2847 rtnl_lock();
2848 for_each_net(net) {
2849 struct dst_ops *xfrm_dst_ops;
2850
2851 switch (afinfo->family) {
2852 case AF_INET:
2853 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2854 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002855#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002856 case AF_INET6:
2857 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2858 break;
2859#endif
2860 default:
2861 BUG();
2862 }
2863 *xfrm_dst_ops = *afinfo->dst_ops;
2864 }
2865 rtnl_unlock();
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return err;
2868}
2869EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2870
2871int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2872{
2873 int err = 0;
2874 if (unlikely(afinfo == NULL))
2875 return -EINVAL;
2876 if (unlikely(afinfo->family >= NPROTO))
2877 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002878 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2880 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2881 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002882 else
2883 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2884 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002886 spin_unlock(&xfrm_policy_afinfo_lock);
2887 if (!err) {
2888 struct dst_ops *dst_ops = afinfo->dst_ops;
2889
2890 synchronize_rcu();
2891
2892 dst_ops->kmem_cachep = NULL;
2893 dst_ops->check = NULL;
2894 dst_ops->negative_advice = NULL;
2895 dst_ops->link_failure = NULL;
2896 afinfo->garbage_collect = NULL;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return err;
2899}
2900EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2901
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002902static void __net_init xfrm_dst_ops_init(struct net *net)
2903{
2904 struct xfrm_policy_afinfo *afinfo;
2905
Eric Dumazetef8531b2012-08-19 12:31:48 +02002906 rcu_read_lock();
2907 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002908 if (afinfo)
2909 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002910#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002911 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002912 if (afinfo)
2913 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2914#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002915 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002916}
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2919{
Jiri Pirko351638e2013-05-28 01:30:21 +00002920 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 switch (event) {
2923 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002924 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926 return NOTIFY_DONE;
2927}
2928
2929static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002930 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931};
2932
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002933#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002934static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002935{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002936 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002937 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2938 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002939 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002940 rv = xfrm_proc_init(net);
2941 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002942 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002943 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002944}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002945
2946static void xfrm_statistics_fini(struct net *net)
2947{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002948 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002949 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002950}
2951#else
2952static int __net_init xfrm_statistics_init(struct net *net)
2953{
2954 return 0;
2955}
2956
2957static void xfrm_statistics_fini(struct net *net)
2958{
2959}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002960#endif
2961
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002962static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
David S. Miller2518c7c2006-08-24 04:45:07 -07002964 unsigned int hmask, sz;
2965 int dir;
2966
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002967 if (net_eq(net, &init_net))
2968 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002970 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002971 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
David S. Miller2518c7c2006-08-24 04:45:07 -07002973 hmask = 8 - 1;
2974 sz = (hmask+1) * sizeof(struct hlist_head);
2975
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002976 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2977 if (!net->xfrm.policy_byidx)
2978 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002979 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002980
Herbert Xu53c2e282014-11-13 17:09:49 +08002981 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002982 struct xfrm_policy_hash *htab;
2983
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002984 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002985 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002986 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002987
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002988 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002989 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002990 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002991 goto out_bydst;
2992 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002993 htab->dbits4 = 32;
2994 htab->sbits4 = 32;
2995 htab->dbits6 = 128;
2996 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002997 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002998 net->xfrm.policy_hthresh.lbits4 = 32;
2999 net->xfrm.policy_hthresh.rbits4 = 32;
3000 net->xfrm.policy_hthresh.lbits6 = 128;
3001 net->xfrm.policy_hthresh.rbits6 = 128;
3002
3003 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003004
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003005 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003006 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003007 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003008 if (net_eq(net, &init_net))
3009 register_netdevice_notifier(&xfrm_dev_notifier);
3010 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003011
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003012out_bydst:
3013 for (dir--; dir >= 0; dir--) {
3014 struct xfrm_policy_hash *htab;
3015
3016 htab = &net->xfrm.policy_bydst[dir];
3017 xfrm_hash_free(htab->table, sz);
3018 }
3019 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003020out_byidx:
3021 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003024static void xfrm_policy_fini(struct net *net)
3025{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003026 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003027 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003028
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003029 flush_work(&net->xfrm.policy_hash_work);
3030#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003031 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003032#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003033 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003034
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003035 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003036
Herbert Xu53c2e282014-11-13 17:09:49 +08003037 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003038 struct xfrm_policy_hash *htab;
3039
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003040 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003041
3042 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003043 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003044 WARN_ON(!hlist_empty(htab->table));
3045 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003046 }
3047
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003048 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003049 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3050 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003051}
3052
3053static int __net_init xfrm_net_init(struct net *net)
3054{
3055 int rv;
3056
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003057 rv = xfrm_statistics_init(net);
3058 if (rv < 0)
3059 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003060 rv = xfrm_state_init(net);
3061 if (rv < 0)
3062 goto out_state;
3063 rv = xfrm_policy_init(net);
3064 if (rv < 0)
3065 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08003066 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003067 rv = xfrm_sysctl_init(net);
3068 if (rv < 0)
3069 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003070 rv = flow_cache_init(net);
3071 if (rv < 0)
3072 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003073
3074 /* Initialize the per-net locks here */
3075 spin_lock_init(&net->xfrm.xfrm_state_lock);
3076 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003077 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3078
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003079 return 0;
3080
Steffen Klassert4a93f502014-03-12 09:43:17 +01003081out:
3082 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003083out_sysctl:
3084 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003085out_policy:
3086 xfrm_state_fini(net);
3087out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003088 xfrm_statistics_fini(net);
3089out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003090 return rv;
3091}
3092
3093static void __net_exit xfrm_net_exit(struct net *net)
3094{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003095 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003096 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003097 xfrm_policy_fini(net);
3098 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003099 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003100}
3101
3102static struct pernet_operations __net_initdata xfrm_net_ops = {
3103 .init = xfrm_net_init,
3104 .exit = xfrm_net_exit,
3105};
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107void __init xfrm_init(void)
3108{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003109 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 xfrm_input_init();
3111}
3112
Joy Lattenab5f5e82007-09-17 11:51:22 -07003113#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003114static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3115 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003116{
Paul Moore875179f2007-12-01 23:27:18 +11003117 struct xfrm_sec_ctx *ctx = xp->security;
3118 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003119
Paul Moore875179f2007-12-01 23:27:18 +11003120 if (ctx)
3121 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3122 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3123
Weilong Chen9b7a7872013-12-24 09:43:46 +08003124 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003125 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003126 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003127 if (sel->prefixlen_s != 32)
3128 audit_log_format(audit_buf, " src_prefixlen=%d",
3129 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003130 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003131 if (sel->prefixlen_d != 32)
3132 audit_log_format(audit_buf, " dst_prefixlen=%d",
3133 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003134 break;
3135 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003136 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003137 if (sel->prefixlen_s != 128)
3138 audit_log_format(audit_buf, " src_prefixlen=%d",
3139 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003140 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003141 if (sel->prefixlen_d != 128)
3142 audit_log_format(audit_buf, " dst_prefixlen=%d",
3143 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003144 break;
3145 }
3146}
3147
Tetsuo Handa2e710292014-04-22 21:48:30 +09003148void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003149{
3150 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151
Paul Mooreafeb14b2007-12-21 14:58:11 -08003152 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153 if (audit_buf == NULL)
3154 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003155 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003156 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003157 xfrm_audit_common_policyinfo(xp, audit_buf);
3158 audit_log_end(audit_buf);
3159}
3160EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3161
Paul Moore68277ac2007-12-20 20:49:33 -08003162void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003163 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003164{
3165 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003166
Paul Mooreafeb14b2007-12-21 14:58:11 -08003167 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003168 if (audit_buf == NULL)
3169 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003170 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003171 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003172 xfrm_audit_common_policyinfo(xp, audit_buf);
3173 audit_log_end(audit_buf);
3174}
3175EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3176#endif
3177
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003178#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003179static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3180 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003181{
3182 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3183 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003184 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3185 sel_cmp->family) &&
3186 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3187 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003188 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3189 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003190 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003191 }
3192 } else {
3193 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003194 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003195 }
3196 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003197 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003198}
3199
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003200static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3201 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003202{
3203 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204 struct hlist_head *chain;
3205 u32 priority = ~0U;
3206
Fan Du283bc9f2013-11-07 17:47:50 +08003207 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003208 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003209 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003210 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3211 pol->type == type) {
3212 ret = pol;
3213 priority = ret->priority;
3214 break;
3215 }
3216 }
Fan Du8d549c42013-11-07 17:47:49 +08003217 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003218 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003219 if ((pol->priority >= priority) && ret)
3220 break;
3221
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003222 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003223 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003224 ret = pol;
3225 break;
3226 }
3227 }
3228
Li RongQing586f2eb2015-04-30 17:13:41 +08003229 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003230
Fan Du283bc9f2013-11-07 17:47:50 +08003231 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232
3233 return ret;
3234}
3235
David S. Millerdd701752011-02-24 00:21:08 -05003236static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003237{
3238 int match = 0;
3239
3240 if (t->mode == m->mode && t->id.proto == m->proto &&
3241 (m->reqid == 0 || t->reqid == m->reqid)) {
3242 switch (t->mode) {
3243 case XFRM_MODE_TUNNEL:
3244 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003245 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3246 m->old_family) &&
3247 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3248 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003249 match = 1;
3250 }
3251 break;
3252 case XFRM_MODE_TRANSPORT:
3253 /* in case of transport mode, template does not store
3254 any IP addresses, hence we just compare mode and
3255 protocol */
3256 match = 1;
3257 break;
3258 default:
3259 break;
3260 }
3261 }
3262 return match;
3263}
3264
3265/* update endpoint address(es) of template(s) */
3266static int xfrm_policy_migrate(struct xfrm_policy *pol,
3267 struct xfrm_migrate *m, int num_migrate)
3268{
3269 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003270 int i, j, n = 0;
3271
3272 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003273 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003274 /* target policy has been deleted */
3275 write_unlock_bh(&pol->lock);
3276 return -ENOENT;
3277 }
3278
3279 for (i = 0; i < pol->xfrm_nr; i++) {
3280 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3281 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3282 continue;
3283 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003284 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3285 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003286 continue;
3287 /* update endpoints */
3288 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3289 sizeof(pol->xfrm_vec[i].id.daddr));
3290 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3291 sizeof(pol->xfrm_vec[i].saddr));
3292 pol->xfrm_vec[i].encap_family = mp->new_family;
3293 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003294 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003295 }
3296 }
3297
3298 write_unlock_bh(&pol->lock);
3299
3300 if (!n)
3301 return -ENODATA;
3302
3303 return 0;
3304}
3305
David S. Millerdd701752011-02-24 00:21:08 -05003306static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003307{
3308 int i, j;
3309
3310 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3311 return -EINVAL;
3312
3313 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003314 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3315 m[i].old_family) &&
3316 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3317 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003318 return -EINVAL;
3319 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3320 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3321 return -EINVAL;
3322
3323 /* check if there is any duplicated entry */
3324 for (j = i + 1; j < num_migrate; j++) {
3325 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3326 sizeof(m[i].old_daddr)) &&
3327 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3328 sizeof(m[i].old_saddr)) &&
3329 m[i].proto == m[j].proto &&
3330 m[i].mode == m[j].mode &&
3331 m[i].reqid == m[j].reqid &&
3332 m[i].old_family == m[j].old_family)
3333 return -EINVAL;
3334 }
3335 }
3336
3337 return 0;
3338}
3339
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003340int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003341 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003342 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003343{
3344 int i, err, nx_cur = 0, nx_new = 0;
3345 struct xfrm_policy *pol = NULL;
3346 struct xfrm_state *x, *xc;
3347 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3348 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3349 struct xfrm_migrate *mp;
3350
3351 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3352 goto out;
3353
3354 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003355 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003356 err = -ENOENT;
3357 goto out;
3358 }
3359
3360 /* Stage 2 - find and update state(s) */
3361 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003362 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003363 x_cur[nx_cur] = x;
3364 nx_cur++;
3365 if ((xc = xfrm_state_migrate(x, mp))) {
3366 x_new[nx_new] = xc;
3367 nx_new++;
3368 } else {
3369 err = -ENODATA;
3370 goto restore_state;
3371 }
3372 }
3373 }
3374
3375 /* Stage 3 - update policy */
3376 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3377 goto restore_state;
3378
3379 /* Stage 4 - delete old state(s) */
3380 if (nx_cur) {
3381 xfrm_states_put(x_cur, nx_cur);
3382 xfrm_states_delete(x_cur, nx_cur);
3383 }
3384
3385 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003386 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003387
3388 xfrm_pol_put(pol);
3389
3390 return 0;
3391out:
3392 return err;
3393
3394restore_state:
3395 if (pol)
3396 xfrm_pol_put(pol);
3397 if (nx_cur)
3398 xfrm_states_put(x_cur, nx_cur);
3399 if (nx_new)
3400 xfrm_states_delete(x_new, nx_new);
3401
3402 return err;
3403}
David S. Millere610e672007-02-08 13:29:15 -08003404EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003405#endif