blob: b5e665b3cfb05f4d08970387802fee8aec8243dd [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
Eric Dumazetd188ba82015-12-08 07:22:02 -08001224 rcu_read_lock();
Fan Du283bc9f2013-11-07 17:47:50 +08001225 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001226 pol = rcu_dereference(sk->sk_policy[dir]);
1227 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001228 bool match = xfrm_selector_match(&pol->selector, fl,
1229 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001230 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001231
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001232 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001233 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1234 pol = NULL;
1235 goto out;
1236 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001237 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001238 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001239 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001240 if (!err)
1241 xfrm_pol_hold(pol);
1242 else if (err == -ESRCH)
1243 pol = NULL;
1244 else
1245 pol = ERR_PTR(err);
1246 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 pol = NULL;
1248 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001249out:
Fan Du283bc9f2013-11-07 17:47:50 +08001250 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001251 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return pol;
1253}
1254
1255static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1256{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001257 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001258
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001259 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001260 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 xfrm_pol_hold(pol);
1262}
1263
1264static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1265 int dir)
1266{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001267 struct net *net = xp_net(pol);
1268
Herbert Xu53c2e282014-11-13 17:09:49 +08001269 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Herbert Xu53c2e282014-11-13 17:09:49 +08001272 /* Socket policies are not hashed. */
1273 if (!hlist_unhashed(&pol->bydst)) {
1274 hlist_del(&pol->bydst);
1275 hlist_del(&pol->byidx);
1276 }
1277
1278 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001279 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001280
1281 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Herbert Xu53c2e282014-11-13 17:09:49 +08001284static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1285{
1286 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1287}
1288
1289static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1290{
1291 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1292}
1293
Herbert Xu4666faa2005-06-18 22:43:22 -07001294int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Fan Du283bc9f2013-11-07 17:47:50 +08001296 struct net *net = xp_net(pol);
1297
1298 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001300 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001305 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
David S. Millera70fcb02006-03-20 19:18:52 -08001307EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1310{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001311 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct xfrm_policy *old_pol;
1313
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001314#ifdef CONFIG_XFRM_SUB_POLICY
1315 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1316 return -EINVAL;
1317#endif
1318
Fan Du283bc9f2013-11-07 17:47:50 +08001319 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001320 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1321 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001323 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001324 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001325 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001327 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001328 if (old_pol) {
1329 if (pol)
1330 xfrm_policy_requeue(old_pol, pol);
1331
Timo Teräsea2dea92010-03-31 00:17:05 +00001332 /* Unlinking succeeds always. This is the only function
1333 * allowed to delete or replace socket policy.
1334 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001335 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001336 }
Fan Du283bc9f2013-11-07 17:47:50 +08001337 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (old_pol) {
1340 xfrm_policy_kill(old_pol);
1341 }
1342 return 0;
1343}
1344
David S. Millerd3e40a92011-02-24 01:25:41 -05001345static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001347 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001348 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 if (newp) {
1351 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001352 if (security_xfrm_policy_clone(old->security,
1353 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001354 kfree(newp);
1355 return NULL; /* ENOMEM */
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 newp->lft = old->lft;
1358 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001359 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 newp->action = old->action;
1361 newp->flags = old->flags;
1362 newp->xfrm_nr = old->xfrm_nr;
1363 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001364 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 memcpy(newp->xfrm_vec, old->xfrm_vec,
1366 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001367 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001368 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001369 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 xfrm_pol_put(newp);
1371 }
1372 return newp;
1373}
1374
Eric Dumazetd188ba82015-12-08 07:22:02 -08001375int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001377 const struct xfrm_policy *p;
1378 struct xfrm_policy *np;
1379 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Eric Dumazetd188ba82015-12-08 07:22:02 -08001381 rcu_read_lock();
1382 for (i = 0; i < 2; i++) {
1383 p = rcu_dereference(osk->sk_policy[i]);
1384 if (p) {
1385 np = clone_policy(p, i);
1386 if (unlikely(!np)) {
1387 ret = -ENOMEM;
1388 break;
1389 }
1390 rcu_assign_pointer(sk->sk_policy[i], np);
1391 }
1392 }
1393 rcu_read_unlock();
1394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001397static int
David Ahern42a7b322015-08-10 16:58:11 -06001398xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1399 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001400{
1401 int err;
1402 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1403
1404 if (unlikely(afinfo == NULL))
1405 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001406 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001407 xfrm_policy_put_afinfo(afinfo);
1408 return err;
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411/* Resolve list of templates for the flow, given policy. */
1412
1413static int
David S. Millera6c2e612011-02-22 18:35:39 -08001414xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1415 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001417 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int nx;
1419 int i, error;
1420 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1421 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001422 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Weilong Chen9b7a7872013-12-24 09:43:46 +08001424 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct xfrm_state *x;
1426 xfrm_address_t *remote = daddr;
1427 xfrm_address_t *local = saddr;
1428 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1429
Joakim Koskela48b8d782007-07-26 00:08:42 -07001430 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1431 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 remote = &tmpl->id.daddr;
1433 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001434 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001435 error = xfrm_get_saddr(net, fl->flowi_oif,
1436 &tmp, remote,
1437 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001438 if (error)
1439 goto fail;
1440 local = &tmp;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
1444 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1445
1446 if (x && x->km.state == XFRM_STATE_VALID) {
1447 xfrm[nx++] = x;
1448 daddr = remote;
1449 saddr = local;
1450 continue;
1451 }
1452 if (x) {
1453 error = (x->km.state == XFRM_STATE_ERROR ?
1454 -EINVAL : -EAGAIN);
1455 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001456 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001457 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (!tmpl->optional)
1461 goto fail;
1462 }
1463 return nx;
1464
1465fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001466 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 xfrm_state_put(xfrm[nx]);
1468 return error;
1469}
1470
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001471static int
David S. Millera6c2e612011-02-22 18:35:39 -08001472xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1473 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001474{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001475 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1476 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001477 int cnx = 0;
1478 int error;
1479 int ret;
1480 int i;
1481
1482 for (i = 0; i < npols; i++) {
1483 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1484 error = -ENOBUFS;
1485 goto fail;
1486 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001487
1488 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001489 if (ret < 0) {
1490 error = ret;
1491 goto fail;
1492 } else
1493 cnx += ret;
1494 }
1495
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001496 /* found states are sorted for outbound processing */
1497 if (npols > 1)
1498 xfrm_state_sort(xfrm, tpp, cnx, family);
1499
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001500 return cnx;
1501
1502 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001503 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001504 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001505 return error;
1506
1507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* Check that the bundle accepts the flow and its components are
1510 * still valid.
1511 */
1512
David S. Miller05d84022011-02-22 17:47:10 -08001513static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001514{
1515 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1516 int tos;
1517
1518 if (!afinfo)
1519 return -EINVAL;
1520
1521 tos = afinfo->get_tos(fl);
1522
1523 xfrm_policy_put_afinfo(afinfo);
1524
1525 return tos;
1526}
1527
Timo Teräs80c802f2010-04-07 00:30:05 +00001528static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1529{
1530 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1531 struct dst_entry *dst = &xdst->u.dst;
1532
1533 if (xdst->route == NULL) {
1534 /* Dummy bundle - if it has xfrms we were not
1535 * able to build bundle as template resolution failed.
1536 * It means we need to try again resolving. */
1537 if (xdst->num_xfrms > 0)
1538 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001539 } else if (dst->flags & DST_XFRM_QUEUE) {
1540 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001541 } else {
1542 /* Real bundle */
1543 if (stale_bundle(dst))
1544 return NULL;
1545 }
1546
1547 dst_hold(dst);
1548 return flo;
1549}
1550
1551static int xfrm_bundle_flo_check(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 if (!xdst->route)
1557 return 0;
1558 if (stale_bundle(dst))
1559 return 0;
1560
1561 return 1;
1562}
1563
1564static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1565{
1566 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1567 struct dst_entry *dst = &xdst->u.dst;
1568
1569 dst_free(dst);
1570}
1571
1572static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1573 .get = xfrm_bundle_flo_get,
1574 .check = xfrm_bundle_flo_check,
1575 .delete = xfrm_bundle_flo_delete,
1576};
1577
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001578static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001579{
1580 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001581 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001582 struct xfrm_dst *xdst;
1583
1584 if (!afinfo)
1585 return ERR_PTR(-EINVAL);
1586
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001587 switch (family) {
1588 case AF_INET:
1589 dst_ops = &net->xfrm.xfrm4_dst_ops;
1590 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001591#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001592 case AF_INET6:
1593 dst_ops = &net->xfrm.xfrm6_dst_ops;
1594 break;
1595#endif
1596 default:
1597 BUG();
1598 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001599 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001600
Madalin Bucurd4cae562011-09-26 07:04:36 +00001601 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001602 struct dst_entry *dst = &xdst->u.dst;
1603
1604 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001605 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001606 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001607 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001608
Madalin Bucurd4cae562011-09-26 07:04:36 +00001609 xfrm_policy_put_afinfo(afinfo);
1610
Herbert Xu25ee3282007-12-11 09:32:34 -08001611 return xdst;
1612}
1613
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001614static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1615 int nfheader_len)
1616{
1617 struct xfrm_policy_afinfo *afinfo =
1618 xfrm_policy_get_afinfo(dst->ops->family);
1619 int err;
1620
1621 if (!afinfo)
1622 return -EINVAL;
1623
1624 err = afinfo->init_path(path, dst, nfheader_len);
1625
1626 xfrm_policy_put_afinfo(afinfo);
1627
1628 return err;
1629}
1630
Herbert Xu87c1e122010-03-02 02:51:56 +00001631static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001632 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001633{
1634 struct xfrm_policy_afinfo *afinfo =
1635 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1636 int err;
1637
1638 if (!afinfo)
1639 return -EINVAL;
1640
Herbert Xu87c1e122010-03-02 02:51:56 +00001641 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001642
1643 xfrm_policy_put_afinfo(afinfo);
1644
1645 return err;
1646}
1647
Timo Teräs80c802f2010-04-07 00:30:05 +00001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1650 * all the metrics... Shortly, bundle a bundle.
1651 */
1652
Herbert Xu25ee3282007-12-11 09:32:34 -08001653static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1654 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001655 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001656 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001658 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001659 unsigned long now = jiffies;
1660 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001661 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001662 struct dst_entry *dst_prev = NULL;
1663 struct dst_entry *dst0 = NULL;
1664 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001666 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001667 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001668 int trailer_len = 0;
1669 int tos;
1670 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001671 xfrm_address_t saddr, daddr;
1672
1673 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001674
1675 tos = xfrm_get_tos(fl, family);
1676 err = tos;
1677 if (tos < 0)
1678 goto put_states;
1679
1680 dst_hold(dst);
1681
1682 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001683 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001684 struct dst_entry *dst1 = &xdst->u.dst;
1685
1686 err = PTR_ERR(xdst);
1687 if (IS_ERR(xdst)) {
1688 dst_release(dst);
1689 goto put_states;
1690 }
1691
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001692 if (xfrm[i]->sel.family == AF_UNSPEC) {
1693 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1694 xfrm_af2proto(family));
1695 if (!inner_mode) {
1696 err = -EAFNOSUPPORT;
1697 dst_release(dst);
1698 goto put_states;
1699 }
1700 } else
1701 inner_mode = xfrm[i]->inner_mode;
1702
Herbert Xu25ee3282007-12-11 09:32:34 -08001703 if (!dst_prev)
1704 dst0 = dst1;
1705 else {
1706 dst_prev->child = dst_clone(dst1);
1707 dst1->flags |= DST_NOHASH;
1708 }
1709
1710 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001711 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001712
1713 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1714 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001715 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1716 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001717 err = PTR_ERR(dst);
1718 if (IS_ERR(dst))
1719 goto put_states;
1720 } else
1721 dst_hold(dst);
1722
1723 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001724 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001725
David S. Millerf5b0a872012-07-19 12:31:33 -07001726 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001727 dst1->flags |= DST_HOST;
1728 dst1->lastuse = now;
1729
1730 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001731 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001732
1733 dst1->next = dst_prev;
1734 dst_prev = dst1;
1735
1736 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001737 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1738 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001739 trailer_len += xfrm[i]->props.trailer_len;
1740 }
1741
1742 dst_prev->child = dst;
1743 dst0->path = dst;
1744
1745 err = -ENODEV;
1746 dev = dst->dev;
1747 if (!dev)
1748 goto free_dst;
1749
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001750 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001751 xfrm_init_pmtu(dst_prev);
1752
1753 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1754 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1755
Herbert Xu87c1e122010-03-02 02:51:56 +00001756 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001757 if (err)
1758 goto free_dst;
1759
1760 dst_prev->header_len = header_len;
1761 dst_prev->trailer_len = trailer_len;
1762 header_len -= xdst->u.dst.xfrm->props.header_len;
1763 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1764 }
1765
1766out:
1767 return dst0;
1768
1769put_states:
1770 for (; i < nx; i++)
1771 xfrm_state_put(xfrm[i]);
1772free_dst:
1773 if (dst0)
1774 dst_free(dst0);
1775 dst0 = ERR_PTR(err);
1776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Ying Xueda7c2242014-01-08 10:26:39 +08001779#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001780static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001781{
1782 if (!*target) {
1783 *target = kmalloc(size, GFP_ATOMIC);
1784 if (!*target)
1785 return -ENOMEM;
1786 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001787
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001788 memcpy(*target, src, size);
1789 return 0;
1790}
Ying Xueda7c2242014-01-08 10:26:39 +08001791#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001792
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001793static int xfrm_dst_update_parent(struct dst_entry *dst,
1794 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001795{
1796#ifdef CONFIG_XFRM_SUB_POLICY
1797 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1798 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1799 sel, sizeof(*sel));
1800#else
1801 return 0;
1802#endif
1803}
1804
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001805static int xfrm_dst_update_origin(struct dst_entry *dst,
1806 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001807{
1808#ifdef CONFIG_XFRM_SUB_POLICY
1809 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1810 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1811#else
1812 return 0;
1813#endif
1814}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
David S. Miller73ff93c2011-02-22 18:33:42 -08001816static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001817 struct xfrm_policy **pols,
1818 int *num_pols, int *num_xfrms)
1819{
1820 int i;
1821
1822 if (*num_pols == 0 || !pols[0]) {
1823 *num_pols = 0;
1824 *num_xfrms = 0;
1825 return 0;
1826 }
1827 if (IS_ERR(pols[0]))
1828 return PTR_ERR(pols[0]);
1829
1830 *num_xfrms = pols[0]->xfrm_nr;
1831
1832#ifdef CONFIG_XFRM_SUB_POLICY
1833 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1834 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1835 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1836 XFRM_POLICY_TYPE_MAIN,
1837 fl, family,
1838 XFRM_POLICY_OUT);
1839 if (pols[1]) {
1840 if (IS_ERR(pols[1])) {
1841 xfrm_pols_put(pols, *num_pols);
1842 return PTR_ERR(pols[1]);
1843 }
Weilong Chen02d08922013-12-24 09:43:48 +08001844 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001845 (*num_xfrms) += pols[1]->xfrm_nr;
1846 }
1847 }
1848#endif
1849 for (i = 0; i < *num_pols; i++) {
1850 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1851 *num_xfrms = -1;
1852 break;
1853 }
1854 }
1855
1856 return 0;
1857
1858}
1859
1860static struct xfrm_dst *
1861xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001862 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001863 struct dst_entry *dst_orig)
1864{
1865 struct net *net = xp_net(pols[0]);
1866 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1867 struct dst_entry *dst;
1868 struct xfrm_dst *xdst;
1869 int err;
1870
1871 /* Try to instantiate a bundle */
1872 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001873 if (err <= 0) {
1874 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001875 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1876 return ERR_PTR(err);
1877 }
1878
1879 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1880 if (IS_ERR(dst)) {
1881 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1882 return ERR_CAST(dst);
1883 }
1884
1885 xdst = (struct xfrm_dst *)dst;
1886 xdst->num_xfrms = err;
1887 if (num_pols > 1)
1888 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1889 else
1890 err = xfrm_dst_update_origin(dst, fl);
1891 if (unlikely(err)) {
1892 dst_free(dst);
1893 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1894 return ERR_PTR(err);
1895 }
1896
1897 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001898 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001899 xdst->policy_genid = atomic_read(&pols[0]->genid);
1900
1901 return xdst;
1902}
1903
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001904static void xfrm_policy_queue_process(unsigned long arg)
1905{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001906 struct sk_buff *skb;
1907 struct sock *sk;
1908 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001909 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001910 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001911 struct xfrm_policy_queue *pq = &pol->polq;
1912 struct flowi fl;
1913 struct sk_buff_head list;
1914
1915 spin_lock(&pq->hold_queue.lock);
1916 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001917 if (!skb) {
1918 spin_unlock(&pq->hold_queue.lock);
1919 goto out;
1920 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001921 dst = skb_dst(skb);
1922 sk = skb->sk;
1923 xfrm_decode_session(skb, &fl, dst->ops->family);
1924 spin_unlock(&pq->hold_queue.lock);
1925
1926 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001927 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001928 if (IS_ERR(dst))
1929 goto purge_queue;
1930
1931 if (dst->flags & DST_XFRM_QUEUE) {
1932 dst_release(dst);
1933
1934 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1935 goto purge_queue;
1936
1937 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001938 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1939 xfrm_pol_hold(pol);
1940 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001941 }
1942
1943 dst_release(dst);
1944
1945 __skb_queue_head_init(&list);
1946
1947 spin_lock(&pq->hold_queue.lock);
1948 pq->timeout = 0;
1949 skb_queue_splice_init(&pq->hold_queue, &list);
1950 spin_unlock(&pq->hold_queue.lock);
1951
1952 while (!skb_queue_empty(&list)) {
1953 skb = __skb_dequeue(&list);
1954
1955 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1956 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001957 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001958 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001959 kfree_skb(skb);
1960 continue;
1961 }
1962
1963 nf_reset(skb);
1964 skb_dst_drop(skb);
1965 skb_dst_set(skb, dst);
1966
Eric W. Biederman13206b62015-10-07 16:48:35 -05001967 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001968 }
1969
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001970out:
1971 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001972 return;
1973
1974purge_queue:
1975 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08001976 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001977 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001978}
1979
Eric W. Biedermanede20592015-10-07 16:48:47 -05001980static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001981{
1982 unsigned long sched_next;
1983 struct dst_entry *dst = skb_dst(skb);
1984 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001985 struct xfrm_policy *pol = xdst->pols[0];
1986 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001987
Eric Dumazet39bb5e62014-10-30 10:32:34 -07001988 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001989 kfree_skb(skb);
1990 return 0;
1991 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001992
1993 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1994 kfree_skb(skb);
1995 return -EAGAIN;
1996 }
1997
1998 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001999
2000 spin_lock_bh(&pq->hold_queue.lock);
2001
2002 if (!pq->timeout)
2003 pq->timeout = XFRM_QUEUE_TMO_MIN;
2004
2005 sched_next = jiffies + pq->timeout;
2006
2007 if (del_timer(&pq->hold_timer)) {
2008 if (time_before(pq->hold_timer.expires, sched_next))
2009 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002010 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002011 }
2012
2013 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002014 if (!mod_timer(&pq->hold_timer, sched_next))
2015 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002016
2017 spin_unlock_bh(&pq->hold_queue.lock);
2018
2019 return 0;
2020}
2021
2022static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002023 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002024 const struct flowi *fl,
2025 int num_xfrms,
2026 u16 family)
2027{
2028 int err;
2029 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002030 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002031 struct dst_entry *dst1;
2032 struct xfrm_dst *xdst;
2033
2034 xdst = xfrm_alloc_dst(net, family);
2035 if (IS_ERR(xdst))
2036 return xdst;
2037
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002038 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2039 net->xfrm.sysctl_larval_drop ||
2040 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002041 return xdst;
2042
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002043 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002044 dst1 = &xdst->u.dst;
2045 dst_hold(dst);
2046 xdst->route = dst;
2047
2048 dst_copy_metrics(dst1, dst);
2049
2050 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2051 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2052 dst1->lastuse = jiffies;
2053
2054 dst1->input = dst_discard;
2055 dst1->output = xdst_queue_output;
2056
2057 dst_hold(dst);
2058 dst1->child = dst;
2059 dst1->path = dst;
2060
2061 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2062
2063 err = -ENODEV;
2064 dev = dst->dev;
2065 if (!dev)
2066 goto free_dst;
2067
2068 err = xfrm_fill_dst(xdst, dev, fl);
2069 if (err)
2070 goto free_dst;
2071
2072out:
2073 return xdst;
2074
2075free_dst:
2076 dst_release(dst1);
2077 xdst = ERR_PTR(err);
2078 goto out;
2079}
2080
Timo Teräs80c802f2010-04-07 00:30:05 +00002081static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002082xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002083 struct flow_cache_object *oldflo, void *ctx)
2084{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002085 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002086 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2087 struct xfrm_dst *xdst, *new_xdst;
2088 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2089
2090 /* Check if the policies from old bundle are usable */
2091 xdst = NULL;
2092 if (oldflo) {
2093 xdst = container_of(oldflo, struct xfrm_dst, flo);
2094 num_pols = xdst->num_pols;
2095 num_xfrms = xdst->num_xfrms;
2096 pol_dead = 0;
2097 for (i = 0; i < num_pols; i++) {
2098 pols[i] = xdst->pols[i];
2099 pol_dead |= pols[i]->walk.dead;
2100 }
2101 if (pol_dead) {
2102 dst_free(&xdst->u.dst);
2103 xdst = NULL;
2104 num_pols = 0;
2105 num_xfrms = 0;
2106 oldflo = NULL;
2107 }
2108 }
2109
2110 /* Resolve policies to use if we couldn't get them from
2111 * previous cache entry */
2112 if (xdst == NULL) {
2113 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002114 pols[0] = __xfrm_policy_lookup(net, fl, family,
2115 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002116 err = xfrm_expand_policies(fl, family, pols,
2117 &num_pols, &num_xfrms);
2118 if (err < 0)
2119 goto inc_error;
2120 if (num_pols == 0)
2121 return NULL;
2122 if (num_xfrms <= 0)
2123 goto make_dummy_bundle;
2124 }
2125
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002126 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2127 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002128 if (IS_ERR(new_xdst)) {
2129 err = PTR_ERR(new_xdst);
2130 if (err != -EAGAIN)
2131 goto error;
2132 if (oldflo == NULL)
2133 goto make_dummy_bundle;
2134 dst_hold(&xdst->u.dst);
2135 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002136 } else if (new_xdst == NULL) {
2137 num_xfrms = 0;
2138 if (oldflo == NULL)
2139 goto make_dummy_bundle;
2140 xdst->num_xfrms = 0;
2141 dst_hold(&xdst->u.dst);
2142 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002143 }
2144
2145 /* Kill the previous bundle */
2146 if (xdst) {
2147 /* The policies were stolen for newly generated bundle */
2148 xdst->num_pols = 0;
2149 dst_free(&xdst->u.dst);
2150 }
2151
2152 /* Flow cache does not have reference, it dst_free()'s,
2153 * but we do need to return one reference for original caller */
2154 dst_hold(&new_xdst->u.dst);
2155 return &new_xdst->flo;
2156
2157make_dummy_bundle:
2158 /* We found policies, but there's no bundles to instantiate:
2159 * either because the policy blocks, has no transformations or
2160 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002161 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002162 if (IS_ERR(xdst)) {
2163 xfrm_pols_put(pols, num_pols);
2164 return ERR_CAST(xdst);
2165 }
2166 xdst->num_pols = num_pols;
2167 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002168 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002169
2170 dst_hold(&xdst->u.dst);
2171 return &xdst->flo;
2172
2173inc_error:
2174 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2175error:
2176 if (xdst != NULL)
2177 dst_free(&xdst->u.dst);
2178 else
2179 xfrm_pols_put(pols, num_pols);
2180 return ERR_PTR(err);
2181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
David S. Miller2774c132011-03-01 14:59:04 -08002183static struct dst_entry *make_blackhole(struct net *net, u16 family,
2184 struct dst_entry *dst_orig)
2185{
2186 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2187 struct dst_entry *ret;
2188
2189 if (!afinfo) {
2190 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002191 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002192 } else {
2193 ret = afinfo->blackhole_route(net, dst_orig);
2194 }
2195 xfrm_policy_put_afinfo(afinfo);
2196
2197 return ret;
2198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200/* Main function: finds/creates a bundle for given flow.
2201 *
2202 * At the moment we eat a raw IP route. Mostly to speed up lookups
2203 * on interfaces with disabled IPsec.
2204 */
David S. Miller452edd52011-03-02 13:27:41 -08002205struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2206 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002207 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002209 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002210 struct flow_cache_object *flo;
2211 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002212 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002213 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002214 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002215 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002216
Timo Teräs80c802f2010-04-07 00:30:05 +00002217 dst = NULL;
2218 xdst = NULL;
2219 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002220
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002221 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002222 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002223 num_pols = 1;
2224 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2225 err = xfrm_expand_policies(fl, family, pols,
2226 &num_pols, &num_xfrms);
2227 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002228 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002229
2230 if (num_pols) {
2231 if (num_xfrms <= 0) {
2232 drop_pols = num_pols;
2233 goto no_transform;
2234 }
2235
2236 xdst = xfrm_resolve_and_create_bundle(
2237 pols, num_pols, fl,
2238 family, dst_orig);
2239 if (IS_ERR(xdst)) {
2240 xfrm_pols_put(pols, num_pols);
2241 err = PTR_ERR(xdst);
2242 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002243 } else if (xdst == NULL) {
2244 num_xfrms = 0;
2245 drop_pols = num_pols;
2246 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002247 }
2248
Steffen Klassertb7eea452014-06-18 12:34:21 +02002249 dst_hold(&xdst->u.dst);
2250 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002251 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002252 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Timo Teräs80c802f2010-04-07 00:30:05 +00002255 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002256 struct xfrm_flo xflo;
2257
2258 xflo.dst_orig = dst_orig;
2259 xflo.flags = flags;
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002262 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002263 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002264 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Timo Teräs80c802f2010-04-07 00:30:05 +00002266 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002267 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002268 if (flo == NULL)
2269 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002270 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002271 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002272 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002273 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002274 xdst = container_of(flo, struct xfrm_dst, flo);
2275
2276 num_pols = xdst->num_pols;
2277 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002278 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002279 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281
Timo Teräs80c802f2010-04-07 00:30:05 +00002282 dst = &xdst->u.dst;
2283 if (route == NULL && num_xfrms > 0) {
2284 /* The only case when xfrm_bundle_lookup() returns a
2285 * bundle with null route, is when the template could
2286 * not be resolved. It means policies are there, but
2287 * bundle could not be created, since we don't yet
2288 * have the xfrm_state's. We need to wait for KM to
2289 * negotiate new SA's or bail out with error.*/
2290 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002291 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002292 err = -EREMOTE;
2293 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002294 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002295
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002296 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002297
2298 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2299 goto error;
2300 }
2301
2302no_transform:
2303 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002304 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Timo Teräs80c802f2010-04-07 00:30:05 +00002306 if ((flags & XFRM_LOOKUP_ICMP) &&
2307 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2308 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002309 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002310 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002311
Timo Teräs80c802f2010-04-07 00:30:05 +00002312 for (i = 0; i < num_pols; i++)
2313 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002314
Timo Teräs80c802f2010-04-07 00:30:05 +00002315 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002317 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002318 err = -EPERM;
2319 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002320 } else if (num_xfrms > 0) {
2321 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002322 dst_release(dst_orig);
2323 } else {
2324 /* Flow passes untransformed */
2325 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002326 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002328ok:
2329 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002330 if (dst && dst->xfrm &&
2331 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2332 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002333 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Timo Teräs80c802f2010-04-07 00:30:05 +00002335nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002336 if (!(flags & XFRM_LOOKUP_ICMP)) {
2337 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002339 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002340 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002342 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002343dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002344 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2345 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002347 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349EXPORT_SYMBOL(xfrm_lookup);
2350
Steffen Klassertf92ee612014-09-16 10:08:40 +02002351/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2352 * Otherwise we may send out blackholed packets.
2353 */
2354struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2355 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002356 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002357{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002358 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002359 flags | XFRM_LOOKUP_QUEUE |
2360 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002361
2362 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2363 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2364
2365 return dst;
2366}
2367EXPORT_SYMBOL(xfrm_lookup_route);
2368
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002369static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002370xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002371{
2372 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002373
2374 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2375 return 0;
2376 x = skb->sp->xvec[idx];
2377 if (!x->type->reject)
2378 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002379 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002380}
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382/* When skb is transformed back to its "native" form, we have to
2383 * check policy restrictions. At the moment we make this in maximally
2384 * stupid way. Shame on me. :-) Of course, connected sockets must
2385 * have policy cached at them.
2386 */
2387
2388static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002389xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 unsigned short family)
2391{
2392 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002393 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return x->id.proto == tmpl->id.proto &&
2395 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2396 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2397 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002398 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002399 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002400 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2401 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002404/*
2405 * 0 or more than 0 is returned when validation is succeeded (either bypass
2406 * because of optional transport mode, or next index of the mathced secpath
2407 * state with the template.
2408 * -1 is returned when no matching template is found.
2409 * Otherwise "-2 - errored_index" is returned.
2410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002412xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 unsigned short family)
2414{
2415 int idx = start;
2416
2417 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002418 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return start;
2420 } else
2421 start = -1;
2422 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002423 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002425 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2426 if (start == -1)
2427 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431 return start;
2432}
2433
Herbert Xud5422ef2007-12-12 10:44:16 -08002434int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2435 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002438 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (unlikely(afinfo == NULL))
2441 return -EAFNOSUPPORT;
2442
Herbert Xud5422ef2007-12-12 10:44:16 -08002443 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002444 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
Herbert Xud5422ef2007-12-12 10:44:16 -08002448EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
David S. Miller9a7386e2011-02-24 01:44:12 -05002450static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
2452 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002453 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002454 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458
2459 return 0;
2460}
2461
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002462int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 unsigned short family)
2464{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002465 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002467 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2468 int npols = 0;
2469 int xfrm_nr;
2470 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002471 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002473 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002474 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Herbert Xud5422ef2007-12-12 10:44:16 -08002476 reverse = dir & ~XFRM_POLICY_MASK;
2477 dir &= XFRM_POLICY_MASK;
2478 fl_dir = policy_to_flow_dir(dir);
2479
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002480 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002481 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002483 }
2484
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002485 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 /* First, check used SA against their selectors. */
2488 if (skb->sp) {
2489 int i;
2490
Weilong Chen9b7a7872013-12-24 09:43:46 +08002491 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002492 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002493 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002494 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 }
2499
2500 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002501 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002502 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002503 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002504 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002505 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002506 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002507 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002510 if (!pol) {
2511 struct flow_cache_object *flo;
2512
2513 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2514 xfrm_policy_lookup, NULL);
2515 if (IS_ERR_OR_NULL(flo))
2516 pol = ERR_CAST(flo);
2517 else
2518 pol = container_of(flo, struct xfrm_policy, flo);
2519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002521 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002522 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002523 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002524 }
James Morris134b0fc2006-10-05 15:42:27 -05002525
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002526 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002527 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002528 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002529 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002530 return 0;
2531 }
2532 return 1;
2533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
James Morris9d729f72007-03-04 16:12:44 -08002535 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002537 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002538 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002539#ifdef CONFIG_XFRM_SUB_POLICY
2540 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002541 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002542 &fl, family,
2543 XFRM_POLICY_IN);
2544 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002545 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002546 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002547 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002548 }
James Morris9d729f72007-03-04 16:12:44 -08002549 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002550 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002551 }
2552 }
2553#endif
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (pol->action == XFRM_POLICY_ALLOW) {
2556 struct sec_path *sp;
2557 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002558 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002559 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002560 struct xfrm_tmpl **tpp = tp;
2561 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int i, k;
2563
2564 if ((sp = skb->sp) == NULL)
2565 sp = &dummy;
2566
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002567 for (pi = 0; pi < npols; pi++) {
2568 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002569 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002570 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002571 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002572 }
2573 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002574 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002575 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002576 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002577 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2578 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2579 }
2580 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002581 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002582 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002583 tpp = stp;
2584 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 /* For each tunnel xfrm, find the first matching tmpl.
2587 * For each tmpl before that, find corresponding xfrm.
2588 * Order is _important_. Later we will implement
2589 * some barriers, but at the moment barriers
2590 * are implied between each two transformations.
2591 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002592 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2593 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002594 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002595 if (k < -1)
2596 /* "-2 - errored_index" returned */
2597 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002598 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002603 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002604 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002608 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return 1;
2610 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002611 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002614 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002615reject_error:
2616 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return 0;
2618}
2619EXPORT_SYMBOL(__xfrm_policy_check);
2620
2621int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2622{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002623 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002625 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002626 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002628 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002629 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002633 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002634
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002635 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002636 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002637 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002638 dst = NULL;
2639 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002640 skb_dst_set(skb, dst);
2641 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643EXPORT_SYMBOL(__xfrm_route_forward);
2644
David S. Millerd49c73c2006-08-13 18:55:53 -07002645/* Optimize later using cookies and generation ids. */
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2648{
David S. Millerd49c73c2006-08-13 18:55:53 -07002649 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002650 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2651 * get validated by dst_ops->check on every use. We do this
2652 * because when a normal route referenced by an XFRM dst is
2653 * obsoleted we do not go looking around for all parent
2654 * referencing XFRM dsts so that we can invalidate them. It
2655 * is just too much work. Instead we make the checks here on
2656 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002657 *
2658 * XFRM dst A --> IPv4 dst X
2659 *
2660 * X is the "xdst->route" of A (X is also the "dst->path" of A
2661 * in this example). If X is marked obsolete, "A" will not
2662 * notice. That's what we are validating here via the
2663 * stale_bundle() check.
2664 *
2665 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002666 * dst which causes it's ->obsolete field to be set to
2667 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2668 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002669 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002670 if (dst->obsolete < 0 && !stale_bundle(dst))
2671 return dst;
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return NULL;
2674}
2675
2676static int stale_bundle(struct dst_entry *dst)
2677{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002678 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Herbert Xuaabc9762005-05-03 16:27:10 -07002681void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002684 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002685 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 dev_put(dev);
2687 }
2688}
Herbert Xuaabc9762005-05-03 16:27:10 -07002689EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691static void xfrm_link_failure(struct sk_buff *skb)
2692{
2693 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2697{
2698 if (dst) {
2699 if (dst->obsolete) {
2700 dst_release(dst);
2701 dst = NULL;
2702 }
2703 }
2704 return dst;
2705}
2706
Paul Mooree4c17212013-05-29 07:36:25 +00002707void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002708{
Fan Duca925cf2014-01-18 09:55:27 +08002709 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002710}
Paul Mooree4c17212013-05-29 07:36:25 +00002711EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002712
2713static void xfrm_garbage_collect_deferred(struct net *net)
2714{
Fan Duca925cf2014-01-18 09:55:27 +08002715 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002716}
2717
Herbert Xu25ee3282007-12-11 09:32:34 -08002718static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 do {
2721 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2722 u32 pmtu, route_mtu_cached;
2723
2724 pmtu = dst_mtu(dst->child);
2725 xdst->child_mtu_cached = pmtu;
2726
2727 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2728
2729 route_mtu_cached = dst_mtu(xdst->route);
2730 xdst->route_mtu_cached = route_mtu_cached;
2731
2732 if (pmtu > route_mtu_cached)
2733 pmtu = route_mtu_cached;
2734
David S. Millerdefb3512010-12-08 21:16:57 -08002735 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 } while ((dst = dst->next));
2737}
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739/* Check that the bundle accepts the flow and its components are
2740 * still valid.
2741 */
2742
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002743static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 struct dst_entry *dst = &first->u.dst;
2746 struct xfrm_dst *last;
2747 u32 mtu;
2748
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002749 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 (dst->dev && !netif_running(dst->dev)))
2751 return 0;
2752
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002753 if (dst->flags & DST_XFRM_QUEUE)
2754 return 1;
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 last = NULL;
2757
2758 do {
2759 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2762 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002763 if (xdst->xfrm_genid != dst->xfrm->genid)
2764 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002765 if (xdst->num_pols > 0 &&
2766 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 mtu = dst_mtu(dst->child);
2770 if (xdst->child_mtu_cached != mtu) {
2771 last = xdst;
2772 xdst->child_mtu_cached = mtu;
2773 }
2774
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002775 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return 0;
2777 mtu = dst_mtu(xdst->route);
2778 if (xdst->route_mtu_cached != mtu) {
2779 last = xdst;
2780 xdst->route_mtu_cached = mtu;
2781 }
2782
2783 dst = dst->child;
2784 } while (dst->xfrm);
2785
2786 if (likely(!last))
2787 return 1;
2788
2789 mtu = last->child_mtu_cached;
2790 for (;;) {
2791 dst = &last->u.dst;
2792
2793 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2794 if (mtu > last->route_mtu_cached)
2795 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002796 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 if (last == first)
2799 break;
2800
Patrick McHardybd0bf072007-07-18 01:55:52 -07002801 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 last->child_mtu_cached = mtu;
2803 }
2804
2805 return 1;
2806}
2807
David S. Miller0dbaee32010-12-13 12:52:14 -08002808static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2809{
2810 return dst_metric_advmss(dst->path);
2811}
2812
Steffen Klassertebb762f2011-11-23 02:12:51 +00002813static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002814{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002815 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2816
2817 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002818}
2819
David S. Millerf894cbf2012-07-02 21:52:24 -07002820static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2821 struct sk_buff *skb,
2822 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002823{
David S. Millerf894cbf2012-07-02 21:52:24 -07002824 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2828{
2829 int err = 0;
2830 if (unlikely(afinfo == NULL))
2831 return -EINVAL;
2832 if (unlikely(afinfo->family >= NPROTO))
2833 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002834 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002836 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 else {
2838 struct dst_ops *dst_ops = afinfo->dst_ops;
2839 if (likely(dst_ops->kmem_cachep == NULL))
2840 dst_ops->kmem_cachep = xfrm_dst_cache;
2841 if (likely(dst_ops->check == NULL))
2842 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002843 if (likely(dst_ops->default_advmss == NULL))
2844 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002845 if (likely(dst_ops->mtu == NULL))
2846 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (likely(dst_ops->negative_advice == NULL))
2848 dst_ops->negative_advice = xfrm_negative_advice;
2849 if (likely(dst_ops->link_failure == NULL))
2850 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002851 if (likely(dst_ops->neigh_lookup == NULL))
2852 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002854 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002855 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002857 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return err;
2860}
2861EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2862
2863int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2864{
2865 int err = 0;
2866 if (unlikely(afinfo == NULL))
2867 return -EINVAL;
2868 if (unlikely(afinfo->family >= NPROTO))
2869 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002870 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2872 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2873 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002874 else
2875 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2876 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002878 spin_unlock(&xfrm_policy_afinfo_lock);
2879 if (!err) {
2880 struct dst_ops *dst_ops = afinfo->dst_ops;
2881
2882 synchronize_rcu();
2883
2884 dst_ops->kmem_cachep = NULL;
2885 dst_ops->check = NULL;
2886 dst_ops->negative_advice = NULL;
2887 dst_ops->link_failure = NULL;
2888 afinfo->garbage_collect = NULL;
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return err;
2891}
2892EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2895{
Jiri Pirko351638e2013-05-28 01:30:21 +00002896 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 switch (event) {
2899 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002900 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 return NOTIFY_DONE;
2903}
2904
2905static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002906 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907};
2908
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002909#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002910static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002911{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002912 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002913 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2914 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002915 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002916 rv = xfrm_proc_init(net);
2917 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002918 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002919 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002920}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002921
2922static void xfrm_statistics_fini(struct net *net)
2923{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002924 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002925 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002926}
2927#else
2928static int __net_init xfrm_statistics_init(struct net *net)
2929{
2930 return 0;
2931}
2932
2933static void xfrm_statistics_fini(struct net *net)
2934{
2935}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002936#endif
2937
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002938static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
David S. Miller2518c7c2006-08-24 04:45:07 -07002940 unsigned int hmask, sz;
2941 int dir;
2942
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002943 if (net_eq(net, &init_net))
2944 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002946 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002947 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
David S. Miller2518c7c2006-08-24 04:45:07 -07002949 hmask = 8 - 1;
2950 sz = (hmask+1) * sizeof(struct hlist_head);
2951
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002952 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2953 if (!net->xfrm.policy_byidx)
2954 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002955 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002956
Herbert Xu53c2e282014-11-13 17:09:49 +08002957 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002958 struct xfrm_policy_hash *htab;
2959
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002960 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002961 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002962 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002963
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002964 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002965 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002966 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002967 goto out_bydst;
2968 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002969 htab->dbits4 = 32;
2970 htab->sbits4 = 32;
2971 htab->dbits6 = 128;
2972 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002973 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002974 net->xfrm.policy_hthresh.lbits4 = 32;
2975 net->xfrm.policy_hthresh.rbits4 = 32;
2976 net->xfrm.policy_hthresh.lbits6 = 128;
2977 net->xfrm.policy_hthresh.rbits6 = 128;
2978
2979 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002980
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002981 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002982 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02002983 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002984 if (net_eq(net, &init_net))
2985 register_netdevice_notifier(&xfrm_dev_notifier);
2986 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002987
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002988out_bydst:
2989 for (dir--; dir >= 0; dir--) {
2990 struct xfrm_policy_hash *htab;
2991
2992 htab = &net->xfrm.policy_bydst[dir];
2993 xfrm_hash_free(htab->table, sz);
2994 }
2995 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002996out_byidx:
2997 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003000static void xfrm_policy_fini(struct net *net)
3001{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003002 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003003 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003004
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003005 flush_work(&net->xfrm.policy_hash_work);
3006#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003007 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003008#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003009 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003010
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003011 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003012
Herbert Xu53c2e282014-11-13 17:09:49 +08003013 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003014 struct xfrm_policy_hash *htab;
3015
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003016 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003017
3018 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003019 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003020 WARN_ON(!hlist_empty(htab->table));
3021 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003022 }
3023
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003024 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003025 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3026 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003027}
3028
3029static int __net_init xfrm_net_init(struct net *net)
3030{
3031 int rv;
3032
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003033 rv = xfrm_statistics_init(net);
3034 if (rv < 0)
3035 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003036 rv = xfrm_state_init(net);
3037 if (rv < 0)
3038 goto out_state;
3039 rv = xfrm_policy_init(net);
3040 if (rv < 0)
3041 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003042 rv = xfrm_sysctl_init(net);
3043 if (rv < 0)
3044 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003045 rv = flow_cache_init(net);
3046 if (rv < 0)
3047 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003048
3049 /* Initialize the per-net locks here */
3050 spin_lock_init(&net->xfrm.xfrm_state_lock);
3051 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003052 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3053
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003054 return 0;
3055
Steffen Klassert4a93f502014-03-12 09:43:17 +01003056out:
3057 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003058out_sysctl:
3059 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003060out_policy:
3061 xfrm_state_fini(net);
3062out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003063 xfrm_statistics_fini(net);
3064out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003065 return rv;
3066}
3067
3068static void __net_exit xfrm_net_exit(struct net *net)
3069{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003070 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003071 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003072 xfrm_policy_fini(net);
3073 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003074 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003075}
3076
3077static struct pernet_operations __net_initdata xfrm_net_ops = {
3078 .init = xfrm_net_init,
3079 .exit = xfrm_net_exit,
3080};
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082void __init xfrm_init(void)
3083{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003084 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 xfrm_input_init();
3086}
3087
Joy Lattenab5f5e82007-09-17 11:51:22 -07003088#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003089static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3090 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003091{
Paul Moore875179f2007-12-01 23:27:18 +11003092 struct xfrm_sec_ctx *ctx = xp->security;
3093 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003094
Paul Moore875179f2007-12-01 23:27:18 +11003095 if (ctx)
3096 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3097 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3098
Weilong Chen9b7a7872013-12-24 09:43:46 +08003099 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003100 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003101 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003102 if (sel->prefixlen_s != 32)
3103 audit_log_format(audit_buf, " src_prefixlen=%d",
3104 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003105 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003106 if (sel->prefixlen_d != 32)
3107 audit_log_format(audit_buf, " dst_prefixlen=%d",
3108 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003109 break;
3110 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003111 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003112 if (sel->prefixlen_s != 128)
3113 audit_log_format(audit_buf, " src_prefixlen=%d",
3114 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003115 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003116 if (sel->prefixlen_d != 128)
3117 audit_log_format(audit_buf, " dst_prefixlen=%d",
3118 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003119 break;
3120 }
3121}
3122
Tetsuo Handa2e710292014-04-22 21:48:30 +09003123void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003124{
3125 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003126
Paul Mooreafeb14b2007-12-21 14:58:11 -08003127 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003128 if (audit_buf == NULL)
3129 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003130 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003131 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003132 xfrm_audit_common_policyinfo(xp, audit_buf);
3133 audit_log_end(audit_buf);
3134}
3135EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3136
Paul Moore68277ac2007-12-20 20:49:33 -08003137void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003138 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003139{
3140 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003141
Paul Mooreafeb14b2007-12-21 14:58:11 -08003142 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003143 if (audit_buf == NULL)
3144 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003145 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003146 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003147 xfrm_audit_common_policyinfo(xp, audit_buf);
3148 audit_log_end(audit_buf);
3149}
3150EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3151#endif
3152
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003153#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003154static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3155 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003156{
3157 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3158 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003159 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3160 sel_cmp->family) &&
3161 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3162 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003163 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3164 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003165 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003166 }
3167 } else {
3168 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003169 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003170 }
3171 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003172 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003173}
3174
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003175static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3176 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003177{
3178 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003179 struct hlist_head *chain;
3180 u32 priority = ~0U;
3181
Fan Du283bc9f2013-11-07 17:47:50 +08003182 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003183 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003184 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003185 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3186 pol->type == type) {
3187 ret = pol;
3188 priority = ret->priority;
3189 break;
3190 }
3191 }
Fan Du8d549c42013-11-07 17:47:49 +08003192 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003193 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003194 if ((pol->priority >= priority) && ret)
3195 break;
3196
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003197 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003198 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003199 ret = pol;
3200 break;
3201 }
3202 }
3203
Li RongQing586f2eb2015-04-30 17:13:41 +08003204 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003205
Fan Du283bc9f2013-11-07 17:47:50 +08003206 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003207
3208 return ret;
3209}
3210
David S. Millerdd701752011-02-24 00:21:08 -05003211static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003212{
3213 int match = 0;
3214
3215 if (t->mode == m->mode && t->id.proto == m->proto &&
3216 (m->reqid == 0 || t->reqid == m->reqid)) {
3217 switch (t->mode) {
3218 case XFRM_MODE_TUNNEL:
3219 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003220 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3221 m->old_family) &&
3222 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3223 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003224 match = 1;
3225 }
3226 break;
3227 case XFRM_MODE_TRANSPORT:
3228 /* in case of transport mode, template does not store
3229 any IP addresses, hence we just compare mode and
3230 protocol */
3231 match = 1;
3232 break;
3233 default:
3234 break;
3235 }
3236 }
3237 return match;
3238}
3239
3240/* update endpoint address(es) of template(s) */
3241static int xfrm_policy_migrate(struct xfrm_policy *pol,
3242 struct xfrm_migrate *m, int num_migrate)
3243{
3244 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003245 int i, j, n = 0;
3246
3247 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003248 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003249 /* target policy has been deleted */
3250 write_unlock_bh(&pol->lock);
3251 return -ENOENT;
3252 }
3253
3254 for (i = 0; i < pol->xfrm_nr; i++) {
3255 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3256 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3257 continue;
3258 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003259 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3260 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003261 continue;
3262 /* update endpoints */
3263 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3264 sizeof(pol->xfrm_vec[i].id.daddr));
3265 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3266 sizeof(pol->xfrm_vec[i].saddr));
3267 pol->xfrm_vec[i].encap_family = mp->new_family;
3268 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003269 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003270 }
3271 }
3272
3273 write_unlock_bh(&pol->lock);
3274
3275 if (!n)
3276 return -ENODATA;
3277
3278 return 0;
3279}
3280
David S. Millerdd701752011-02-24 00:21:08 -05003281static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003282{
3283 int i, j;
3284
3285 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3286 return -EINVAL;
3287
3288 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003289 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3290 m[i].old_family) &&
3291 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3292 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003293 return -EINVAL;
3294 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3295 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3296 return -EINVAL;
3297
3298 /* check if there is any duplicated entry */
3299 for (j = i + 1; j < num_migrate; j++) {
3300 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3301 sizeof(m[i].old_daddr)) &&
3302 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3303 sizeof(m[i].old_saddr)) &&
3304 m[i].proto == m[j].proto &&
3305 m[i].mode == m[j].mode &&
3306 m[i].reqid == m[j].reqid &&
3307 m[i].old_family == m[j].old_family)
3308 return -EINVAL;
3309 }
3310 }
3311
3312 return 0;
3313}
3314
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003315int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003316 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003317 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003318{
3319 int i, err, nx_cur = 0, nx_new = 0;
3320 struct xfrm_policy *pol = NULL;
3321 struct xfrm_state *x, *xc;
3322 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3323 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3324 struct xfrm_migrate *mp;
3325
3326 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3327 goto out;
3328
3329 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003330 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003331 err = -ENOENT;
3332 goto out;
3333 }
3334
3335 /* Stage 2 - find and update state(s) */
3336 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003337 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003338 x_cur[nx_cur] = x;
3339 nx_cur++;
3340 if ((xc = xfrm_state_migrate(x, mp))) {
3341 x_new[nx_new] = xc;
3342 nx_new++;
3343 } else {
3344 err = -ENODATA;
3345 goto restore_state;
3346 }
3347 }
3348 }
3349
3350 /* Stage 3 - update policy */
3351 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3352 goto restore_state;
3353
3354 /* Stage 4 - delete old state(s) */
3355 if (nx_cur) {
3356 xfrm_states_put(x_cur, nx_cur);
3357 xfrm_states_delete(x_cur, nx_cur);
3358 }
3359
3360 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003361 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003362
3363 xfrm_pol_put(pol);
3364
3365 return 0;
3366out:
3367 return err;
3368
3369restore_state:
3370 if (pol)
3371 xfrm_pol_put(pol);
3372 if (nx_cur)
3373 xfrm_states_put(x_cur, nx_cur);
3374 if (nx_new)
3375 xfrm_states_delete(x_new, nx_new);
3376
3377 return err;
3378}
David S. Millere610e672007-02-08 13:29:15 -08003379EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003380#endif