blob: 4f8ace8558649bacdc2f0c426d89c058bd5559d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_state.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16#include <linux/workqueue.h>
17#include <net/xfrm.h>
18#include <linux/pfkeyv2.h>
19#include <linux/ipsec.h>
20#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070021#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080022#include <linux/audit.h>
Jesper Juhlb5890d82007-08-10 15:20:21 -070023#include <asm/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080024#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080026#include <linux/interrupt.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* Each xfrm_state may be linked to two tables:
32
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070034 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 destination/tunnel endpoint. (output)
36 */
37
38static DEFINE_SPINLOCK(xfrm_state_lock);
39
David S. Millerf034b5d2006-08-24 03:08:07 -070040static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Herbert Xu17c2a422007-10-17 21:33:12 -070042static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
44
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080045static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050046 const xfrm_address_t *daddr,
47 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070048 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070049 unsigned short family)
50{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080051 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070052}
53
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050055 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070057 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070058{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080059 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070060}
61
David S. Miller2575b652006-08-24 03:26:44 -070062static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050063xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
64 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070065{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080066 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070067}
68
David S. Millerf034b5d2006-08-24 03:08:07 -070069static void xfrm_hash_transfer(struct hlist_head *list,
70 struct hlist_head *ndsttable,
71 struct hlist_head *nsrctable,
72 struct hlist_head *nspitable,
73 unsigned int nhashmask)
74{
Sasha Levinb67bfe02013-02-27 17:06:00 -080075 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070076 struct xfrm_state *x;
77
Sasha Levinb67bfe02013-02-27 17:06:00 -080078 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070079 unsigned int h;
80
David S. Millerc1969f22006-08-24 04:00:03 -070081 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
82 x->props.reqid, x->props.family,
83 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070084 hlist_add_head(&x->bydst, ndsttable+h);
85
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070086 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
87 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070088 nhashmask);
89 hlist_add_head(&x->bysrc, nsrctable+h);
90
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070091 if (x->id.spi) {
92 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
93 x->id.proto, x->props.family,
94 nhashmask);
95 hlist_add_head(&x->byspi, nspitable+h);
96 }
David S. Millerf034b5d2006-08-24 03:08:07 -070097 }
98}
99
Alexey Dobriyan63082732008-11-25 17:19:07 -0800100static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700101{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800102 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700103}
104
105static DEFINE_MUTEX(hash_resize_mutex);
106
Alexey Dobriyan63082732008-11-25 17:19:07 -0800107static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700108{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800109 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700110 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
111 unsigned long nsize, osize;
112 unsigned int nhashmask, ohashmask;
113 int i;
114
115 mutex_lock(&hash_resize_mutex);
116
Alexey Dobriyan63082732008-11-25 17:19:07 -0800117 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700118 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 if (!ndst)
120 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700121 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700123 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700124 goto out_unlock;
125 }
David S. Miller44e36b42006-08-24 04:50:50 -0700126 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700128 xfrm_hash_free(ndst, nsize);
129 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 goto out_unlock;
131 }
132
133 spin_lock_bh(&xfrm_state_lock);
134
135 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800136 for (i = net->xfrm.state_hmask; i >= 0; i--)
137 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700138 nhashmask);
139
Alexey Dobriyan63082732008-11-25 17:19:07 -0800140 odst = net->xfrm.state_bydst;
141 osrc = net->xfrm.state_bysrc;
142 ospi = net->xfrm.state_byspi;
143 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700144
Alexey Dobriyan63082732008-11-25 17:19:07 -0800145 net->xfrm.state_bydst = ndst;
146 net->xfrm.state_bysrc = nsrc;
147 net->xfrm.state_byspi = nspi;
148 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700149
150 spin_unlock_bh(&xfrm_state_lock);
151
152 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700153 xfrm_hash_free(odst, osize);
154 xfrm_hash_free(osrc, osize);
155 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700156
157out_unlock:
158 mutex_unlock(&hash_resize_mutex);
159}
160
Cong Wang44abdc32013-01-16 16:05:05 +0800161static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
162static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_SPINLOCK(xfrm_state_gc_lock);
165
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800166int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800168int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000169void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Cong Wang7a9885b2013-01-17 16:34:11 +0800171static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800172int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700173{
Cong Wang7a9885b2013-01-17 16:34:11 +0800174 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800175 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700176 int err = 0;
177
178 if (unlikely(afinfo == NULL))
179 return -EAFNOSUPPORT;
180 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800181 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700182
183 if (likely(typemap[type->proto] == NULL))
184 typemap[type->proto] = type;
185 else
186 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800187 spin_unlock_bh(&xfrm_type_lock);
188 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700189 return err;
190}
191EXPORT_SYMBOL(xfrm_register_type);
192
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800193int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700194{
Cong Wang7a9885b2013-01-17 16:34:11 +0800195 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800196 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700197 int err = 0;
198
199 if (unlikely(afinfo == NULL))
200 return -EAFNOSUPPORT;
201 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800202 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700203
204 if (unlikely(typemap[type->proto] != type))
205 err = -ENOENT;
206 else
207 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800208 spin_unlock_bh(&xfrm_type_lock);
209 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700210 return err;
211}
212EXPORT_SYMBOL(xfrm_unregister_type);
213
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800214static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700215{
216 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800217 const struct xfrm_type **typemap;
218 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700219 int modload_attempted = 0;
220
221retry:
222 afinfo = xfrm_state_get_afinfo(family);
223 if (unlikely(afinfo == NULL))
224 return NULL;
225 typemap = afinfo->type_map;
226
227 type = typemap[proto];
228 if (unlikely(type && !try_module_get(type->owner)))
229 type = NULL;
230 if (!type && !modload_attempted) {
231 xfrm_state_put_afinfo(afinfo);
232 request_module("xfrm-type-%d-%d", family, proto);
233 modload_attempted = 1;
234 goto retry;
235 }
236
237 xfrm_state_put_afinfo(afinfo);
238 return type;
239}
240
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800241static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700242{
243 module_put(type->owner);
244}
245
Cong Wang7a9885b2013-01-17 16:34:11 +0800246static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700247int xfrm_register_mode(struct xfrm_mode *mode, int family)
248{
249 struct xfrm_state_afinfo *afinfo;
250 struct xfrm_mode **modemap;
251 int err;
252
253 if (unlikely(mode->encap >= XFRM_MODE_MAX))
254 return -EINVAL;
255
Cong Wang7a9885b2013-01-17 16:34:11 +0800256 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700257 if (unlikely(afinfo == NULL))
258 return -EAFNOSUPPORT;
259
260 err = -EEXIST;
261 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800262 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700263 if (modemap[mode->encap])
264 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700265
Herbert Xu17c2a422007-10-17 21:33:12 -0700266 err = -ENOENT;
267 if (!try_module_get(afinfo->owner))
268 goto out;
269
270 mode->afinfo = afinfo;
271 modemap[mode->encap] = mode;
272 err = 0;
273
274out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800275 spin_unlock_bh(&xfrm_mode_lock);
276 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700277 return err;
278}
279EXPORT_SYMBOL(xfrm_register_mode);
280
281int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
282{
283 struct xfrm_state_afinfo *afinfo;
284 struct xfrm_mode **modemap;
285 int err;
286
287 if (unlikely(mode->encap >= XFRM_MODE_MAX))
288 return -EINVAL;
289
Cong Wang7a9885b2013-01-17 16:34:11 +0800290 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700291 if (unlikely(afinfo == NULL))
292 return -EAFNOSUPPORT;
293
294 err = -ENOENT;
295 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800296 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700297 if (likely(modemap[mode->encap] == mode)) {
298 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700299 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700300 err = 0;
301 }
302
Cong Wang7a9885b2013-01-17 16:34:11 +0800303 spin_unlock_bh(&xfrm_mode_lock);
304 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700305 return err;
306}
307EXPORT_SYMBOL(xfrm_unregister_mode);
308
309static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
310{
311 struct xfrm_state_afinfo *afinfo;
312 struct xfrm_mode *mode;
313 int modload_attempted = 0;
314
315 if (unlikely(encap >= XFRM_MODE_MAX))
316 return NULL;
317
318retry:
319 afinfo = xfrm_state_get_afinfo(family);
320 if (unlikely(afinfo == NULL))
321 return NULL;
322
323 mode = afinfo->mode_map[encap];
324 if (unlikely(mode && !try_module_get(mode->owner)))
325 mode = NULL;
326 if (!mode && !modload_attempted) {
327 xfrm_state_put_afinfo(afinfo);
328 request_module("xfrm-mode-%d-%d", family, encap);
329 modload_attempted = 1;
330 goto retry;
331 }
332
333 xfrm_state_put_afinfo(afinfo);
334 return mode;
335}
336
337static void xfrm_put_mode(struct xfrm_mode *mode)
338{
339 module_put(mode->owner);
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static void xfrm_state_gc_destroy(struct xfrm_state *x)
343{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800344 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700345 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800346 kfree(x->aalg);
347 kfree(x->ealg);
348 kfree(x->calg);
349 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700350 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000351 kfree(x->replay_esn);
352 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700353 if (x->inner_mode)
354 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700355 if (x->inner_mode_iaf)
356 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700357 if (x->outer_mode)
358 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (x->type) {
360 x->type->destructor(x);
361 xfrm_put_type(x->type);
362 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800363 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 kfree(x);
365}
366
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800367static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800369 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700370 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800371 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700372 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800375 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 spin_unlock_bh(&xfrm_state_gc_lock);
377
Sasha Levinb67bfe02013-02-27 17:06:00 -0800378 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700380
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800381 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static inline unsigned long make_jiffies(long secs)
385{
386 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
387 return MAX_SCHEDULE_TIMEOUT-1;
388 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900389 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800392static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800394 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
395 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800396 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800397 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 long next = LONG_MAX;
399 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600400 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 spin_lock(&x->lock);
403 if (x->km.state == XFRM_STATE_DEAD)
404 goto out;
405 if (x->km.state == XFRM_STATE_EXPIRED)
406 goto expired;
407 if (x->lft.hard_add_expires_seconds) {
408 long tmo = x->lft.hard_add_expires_seconds +
409 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000410 if (tmo <= 0) {
411 if (x->xflags & XFRM_SOFT_EXPIRE) {
412 /* enter hard expire without soft expire first?!
413 * setting a new date could trigger this.
414 * workarbound: fix x->curflt.add_time by below:
415 */
416 x->curlft.add_time = now - x->saved_tmo - 1;
417 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
418 } else
419 goto expired;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (tmo < next)
422 next = tmo;
423 }
424 if (x->lft.hard_use_expires_seconds) {
425 long tmo = x->lft.hard_use_expires_seconds +
426 (x->curlft.use_time ? : now) - now;
427 if (tmo <= 0)
428 goto expired;
429 if (tmo < next)
430 next = tmo;
431 }
432 if (x->km.dying)
433 goto resched;
434 if (x->lft.soft_add_expires_seconds) {
435 long tmo = x->lft.soft_add_expires_seconds +
436 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000437 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000439 x->xflags &= ~XFRM_SOFT_EXPIRE;
440 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000442 x->xflags |= XFRM_SOFT_EXPIRE;
443 x->saved_tmo = tmo;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 if (x->lft.soft_use_expires_seconds) {
447 long tmo = x->lft.soft_use_expires_seconds +
448 (x->curlft.use_time ? : now) - now;
449 if (tmo <= 0)
450 warn = 1;
451 else if (tmo < next)
452 next = tmo;
453 }
454
Herbert Xu4666faa2005-06-18 22:43:22 -0700455 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800457 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800459 if (next != LONG_MAX){
460 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
461 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto out;
464
465expired:
466 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
467 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800468 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 next = 2;
470 goto resched;
471 }
Joy Latten161a09e2006-11-27 13:11:54 -0600472
473 err = __xfrm_state_delete(x);
474 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800475 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Joy Lattenab5f5e82007-09-17 11:51:22 -0700477 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400478 audit_get_loginuid(current),
479 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481out:
482 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800483 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
David S. Miller0ac84752006-03-20 19:18:23 -0800486static void xfrm_replay_timer_handler(unsigned long data);
487
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800488struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct xfrm_state *x;
491
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700492 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800495 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 atomic_set(&x->refcnt, 1);
497 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700498 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700499 INIT_HLIST_NODE(&x->bydst);
500 INIT_HLIST_NODE(&x->bysrc);
501 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800502 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
503 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800504 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
505 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800506 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 x->lft.soft_byte_limit = XFRM_INF;
508 x->lft.soft_packet_limit = XFRM_INF;
509 x->lft.hard_byte_limit = XFRM_INF;
510 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800511 x->replay_maxage = 0;
512 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700513 x->inner_mode = NULL;
514 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 spin_lock_init(&x->lock);
516 }
517 return x;
518}
519EXPORT_SYMBOL(xfrm_state_alloc);
520
521void __xfrm_state_destroy(struct xfrm_state *x)
522{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800523 struct net *net = xs_net(x);
524
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700525 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800528 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800530 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532EXPORT_SYMBOL(__xfrm_state_destroy);
533
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800534int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800536 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700537 int err = -ESRCH;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (x->km.state != XFRM_STATE_DEAD) {
540 x->km.state = XFRM_STATE_DEAD;
541 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700542 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700543 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700544 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700545 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700546 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800547 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* All xfrm_state objects are created by xfrm_state_alloc.
551 * The xfrm_state_alloc call gives a reference, and that
552 * is what we are dropping here.
553 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800554 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700555 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700557
558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800560EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700562int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700564 int err;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700567 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700569
570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572EXPORT_SYMBOL(xfrm_state_delete);
573
Joy Latten4aa2e622007-06-04 19:05:57 -0400574#ifdef CONFIG_SECURITY_NETWORK_XFRM
575static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800576xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Joy Latten4aa2e622007-06-04 19:05:57 -0400578 int i, err = 0;
579
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800580 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400581 struct xfrm_state *x;
582
Sasha Levinb67bfe02013-02-27 17:06:00 -0800583 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400584 if (xfrm_id_proto_match(x->id.proto, proto) &&
585 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700586 xfrm_audit_state_delete(x, 0,
587 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400588 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700589 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400590 return err;
591 }
592 }
593 }
594
595 return err;
596}
597#else
598static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800599xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400600{
601 return 0;
602}
603#endif
604
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800605int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400606{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000607 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800610 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400611 if (err)
612 goto out;
613
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000614 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800615 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700616 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800618 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700620 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xfrm_state_hold(x);
622 spin_unlock_bh(&xfrm_state_lock);
623
Joy Latten161a09e2006-11-27 13:11:54 -0600624 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700625 xfrm_audit_state_delete(x, err ? 0 : 1,
626 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400627 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700628 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000630 if (!err)
631 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 spin_lock_bh(&xfrm_state_lock);
634 goto restart;
635 }
636 }
637 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000638 if (cnt)
639 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400640
641out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800643 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646EXPORT_SYMBOL(xfrm_state_flush);
647
Alexey Dobriyane0710412010-01-23 13:37:10 +0000648void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700649{
650 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000651 si->sadcnt = net->xfrm.state_num;
652 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700653 si->sadhmcnt = xfrm_state_hashmax;
654 spin_unlock_bh(&xfrm_state_lock);
655}
656EXPORT_SYMBOL(xfrm_sad_getinfo);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static int
David S. Miller1a898592011-02-22 18:22:34 -0800659xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500660 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500661 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700662 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
665 if (!afinfo)
666 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700667 afinfo->init_tempsel(&x->sel, fl);
668
669 if (family != tmpl->encap_family) {
670 xfrm_state_put_afinfo(afinfo);
671 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
672 if (!afinfo)
673 return -1;
674 }
675 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 xfrm_state_put_afinfo(afinfo);
677 return 0;
678}
679
David S. Miller9aa60082011-02-24 01:51:36 -0500680static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
681 const xfrm_address_t *daddr,
682 __be32 spi, u8 proto,
683 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700684{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800685 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700686 struct xfrm_state *x;
687
Sasha Levinb67bfe02013-02-27 17:06:00 -0800688 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700689 if (x->props.family != family ||
690 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000691 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000692 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700693 continue;
694
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000695 if ((mark & x->mark.m) != x->mark.v)
696 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700697 xfrm_state_hold(x);
698 return x;
699 }
700
701 return NULL;
702}
703
David S. Miller9aa60082011-02-24 01:51:36 -0500704static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
705 const xfrm_address_t *daddr,
706 const xfrm_address_t *saddr,
707 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700708{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800709 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700710 struct xfrm_state *x;
711
Sasha Levinb67bfe02013-02-27 17:06:00 -0800712 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700713 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000714 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000715 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
716 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700717 continue;
718
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000719 if ((mark & x->mark.m) != x->mark.v)
720 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700721 xfrm_state_hold(x);
722 return x;
723 }
724
725 return NULL;
726}
727
728static inline struct xfrm_state *
729__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
730{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800731 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800732 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800733
David S. Milleredcd5822006-08-24 00:42:45 -0700734 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800735 return __xfrm_state_lookup(net, mark, &x->id.daddr,
736 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700737 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800738 return __xfrm_state_lookup_byaddr(net, mark,
739 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700740 &x->props.saddr,
741 x->id.proto, family);
742}
743
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800744static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700745{
746 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800747 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
748 net->xfrm.state_num > net->xfrm.state_hmask)
749 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700750}
751
David S. Miller08ec9af2009-03-13 14:22:40 -0700752static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800753 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700754 struct xfrm_state **best, int *acq_in_progress,
755 int *error)
756{
757 /* Resolution logic:
758 * 1. There is a valid state with matching selector. Done.
759 * 2. Valid state with inappropriate selector. Skip.
760 *
761 * Entering area of "sysdeps".
762 *
763 * 3. If state is not valid, selector is temporary, it selects
764 * only session which triggered previous resolution. Key
765 * manager will do something to install a state with proper
766 * selector.
767 */
768 if (x->km.state == XFRM_STATE_VALID) {
769 if ((x->sel.family &&
770 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
771 !security_xfrm_state_pol_flow_match(x, pol, fl))
772 return;
773
774 if (!*best ||
775 (*best)->km.dying > x->km.dying ||
776 ((*best)->km.dying == x->km.dying &&
777 (*best)->curlft.add_time < x->curlft.add_time))
778 *best = x;
779 } else if (x->km.state == XFRM_STATE_ACQ) {
780 *acq_in_progress = 1;
781 } else if (x->km.state == XFRM_STATE_ERROR ||
782 x->km.state == XFRM_STATE_EXPIRED) {
783 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
784 security_xfrm_state_pol_flow_match(x, pol, fl))
785 *error = -ESRCH;
786 }
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500790xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800791 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct xfrm_policy *pol, int *err,
793 unsigned short family)
794{
David S. Miller08ec9af2009-03-13 14:22:40 -0700795 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800796 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700797 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700798 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int acquire_in_progress = 0;
800 int error = 0;
801 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800802 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700803 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900804
David S. Miller37b08e32008-09-02 20:14:15 -0700805 to_put = NULL;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800809 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700810 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000812 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700813 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700814 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 tmpl->mode == x->props.mode &&
816 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700817 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500818 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700819 &best, &acquire_in_progress, &error);
820 }
821 if (best)
822 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Thomas Egerer8444cf72010-09-20 11:11:38 -0700824 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800825 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700826 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700827 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000828 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700829 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700830 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700831 tmpl->mode == x->props.mode &&
832 tmpl->id.proto == x->id.proto &&
833 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500834 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700835 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
David S. Miller08ec9af2009-03-13 14:22:40 -0700838found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 x = best;
840 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700841 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800842 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700843 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700844 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 error = -EEXIST;
846 goto out;
847 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800848 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (x == NULL) {
850 error = -ENOMEM;
851 goto out;
852 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700853 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700855 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800856 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David S. Miller1d28f422011-03-12 00:29:39 -0500858 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700859 if (error) {
860 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700861 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700862 x = NULL;
863 goto out;
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (km_query(x, tmpl, pol) == 0) {
867 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800868 list_add(&x->km.all, &net->xfrm.state_all);
869 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700870 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800871 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700873 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800874 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800876 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800877 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800878 net->xfrm.state_num++;
879 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } else {
881 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700882 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 x = NULL;
884 error = -ESRCH;
885 }
886 }
887out:
888 if (x)
889 xfrm_state_hold(x);
890 else
891 *err = acquire_in_progress ? -EAGAIN : error;
892 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700893 if (to_put)
894 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return x;
896}
897
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700898struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800899xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800900 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700901 unsigned short family, u8 mode, u8 proto, u32 reqid)
902{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800903 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700904 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700905
906 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800907 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800908 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909 if (x->props.family == family &&
910 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000911 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700912 !(x->props.flags & XFRM_STATE_WILDRECV) &&
913 xfrm_state_addr_check(x, daddr, saddr, family) &&
914 mode == x->props.mode &&
915 proto == x->id.proto &&
916 x->km.state == XFRM_STATE_VALID) {
917 rx = x;
918 break;
919 }
920 }
921
922 if (rx)
923 xfrm_state_hold(rx);
924 spin_unlock(&xfrm_state_lock);
925
926
927 return rx;
928}
929EXPORT_SYMBOL(xfrm_stateonly_find);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static void __xfrm_state_insert(struct xfrm_state *x)
932{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800933 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700934 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800936 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800937
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800938 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700939 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800940 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800942 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
943 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700945 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800946 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700947 x->props.family);
948
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800949 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700950 }
951
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800952 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700953 if (x->replay_maxage)
954 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800955
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800956 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700957
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800958 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700959
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800960 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
David S. Millerc7f5ea32006-08-24 03:29:04 -0700963/* xfrm_state_lock is held */
964static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
965{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800966 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700967 unsigned short family = xnew->props.family;
968 u32 reqid = xnew->props.reqid;
969 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700970 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000971 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700972
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800973 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800974 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700975 if (x->props.family == family &&
976 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000977 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000978 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
979 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000980 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700981 }
982}
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984void xfrm_state_insert(struct xfrm_state *x)
985{
986 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700987 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 __xfrm_state_insert(x);
989 spin_unlock_bh(&xfrm_state_lock);
990}
991EXPORT_SYMBOL(xfrm_state_insert);
992
David S. Miller27708342006-08-24 00:13:10 -0700993/* xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +0200994static struct xfrm_state *__find_acq_core(struct net *net,
995 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -0800996 unsigned short family, u8 mode,
997 u32 reqid, u8 proto,
998 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +0200999 const xfrm_address_t *saddr,
1000 int create)
David S. Miller27708342006-08-24 00:13:10 -07001001{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001002 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001003 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001004 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001005
Sasha Levinb67bfe02013-02-27 17:06:00 -08001006 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001007 if (x->props.reqid != reqid ||
1008 x->props.mode != mode ||
1009 x->props.family != family ||
1010 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001011 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001012 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001013 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001014 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1015 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001016 continue;
1017
David S. Miller27708342006-08-24 00:13:10 -07001018 xfrm_state_hold(x);
1019 return x;
1020 }
1021
1022 if (!create)
1023 return NULL;
1024
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001025 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001026 if (likely(x)) {
1027 switch (family) {
1028 case AF_INET:
1029 x->sel.daddr.a4 = daddr->a4;
1030 x->sel.saddr.a4 = saddr->a4;
1031 x->sel.prefixlen_d = 32;
1032 x->sel.prefixlen_s = 32;
1033 x->props.saddr.a4 = saddr->a4;
1034 x->id.daddr.a4 = daddr->a4;
1035 break;
1036
1037 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001038 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1039 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001040 x->sel.prefixlen_d = 128;
1041 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001042 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1043 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001044 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001045 }
David S. Miller27708342006-08-24 00:13:10 -07001046
1047 x->km.state = XFRM_STATE_ACQ;
1048 x->id.proto = proto;
1049 x->props.family = family;
1050 x->props.mode = mode;
1051 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001052 x->mark.v = m->v;
1053 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001054 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001055 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001056 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001057 list_add(&x->km.all, &net->xfrm.state_all);
1058 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1059 h = xfrm_src_hash(net, daddr, saddr, family);
1060 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001061
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001062 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001063
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001064 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001065 }
1066
1067 return x;
1068}
1069
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001070static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072int xfrm_state_add(struct xfrm_state *x)
1073{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001074 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001075 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int family;
1077 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001078 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001079 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
David S. Miller37b08e32008-09-02 20:14:15 -07001083 to_put = NULL;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 spin_lock_bh(&xfrm_state_lock);
1086
David S. Milleredcd5822006-08-24 00:42:45 -07001087 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001089 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 x1 = NULL;
1091 err = -EEXIST;
1092 goto out;
1093 }
1094
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001095 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001096 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001097 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001098 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001099 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 x1 = NULL;
1101 }
1102 }
1103
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001104 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001105 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1106 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001107 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
David S. Millerc7f5ea32006-08-24 03:29:04 -07001109 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 __xfrm_state_insert(x);
1111 err = 0;
1112
1113out:
1114 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (x1) {
1117 xfrm_state_delete(x1);
1118 xfrm_state_put(x1);
1119 }
1120
David S. Miller37b08e32008-09-02 20:14:15 -07001121 if (to_put)
1122 xfrm_state_put(to_put);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return err;
1125}
1126EXPORT_SYMBOL(xfrm_state_add);
1127
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001128#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001129static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001130{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001131 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001132 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001133 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001134 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001135 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001136
1137 memcpy(&x->id, &orig->id, sizeof(x->id));
1138 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1139 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1140 x->props.mode = orig->props.mode;
1141 x->props.replay_window = orig->props.replay_window;
1142 x->props.reqid = orig->props.reqid;
1143 x->props.family = orig->props.family;
1144 x->props.saddr = orig->props.saddr;
1145
1146 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001147 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001148 if (!x->aalg)
1149 goto error;
1150 }
1151 x->props.aalgo = orig->props.aalgo;
1152
1153 if (orig->ealg) {
1154 x->ealg = xfrm_algo_clone(orig->ealg);
1155 if (!x->ealg)
1156 goto error;
1157 }
1158 x->props.ealgo = orig->props.ealgo;
1159
1160 if (orig->calg) {
1161 x->calg = xfrm_algo_clone(orig->calg);
1162 if (!x->calg)
1163 goto error;
1164 }
1165 x->props.calgo = orig->props.calgo;
1166
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001167 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001168 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1169 if (!x->encap)
1170 goto error;
1171 }
1172
1173 if (orig->coaddr) {
1174 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1175 GFP_KERNEL);
1176 if (!x->coaddr)
1177 goto error;
1178 }
1179
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001180 if (orig->replay_esn) {
1181 err = xfrm_replay_clone(x, orig);
1182 if (err)
1183 goto error;
1184 }
1185
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001186 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1187
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001188 err = xfrm_init_state(x);
1189 if (err)
1190 goto error;
1191
1192 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001193 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001194
1195 x->curlft.add_time = orig->curlft.add_time;
1196 x->km.state = orig->km.state;
1197 x->km.seq = orig->km.seq;
1198
1199 return x;
1200
1201 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001202 xfrm_state_put(x);
1203out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001204 if (errp)
1205 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001206 return NULL;
1207}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001208
1209/* xfrm_state_lock is held */
1210struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1211{
1212 unsigned int h;
1213 struct xfrm_state *x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001214
1215 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001216 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001217 m->reqid, m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001218 hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001219 if (x->props.mode != m->mode ||
1220 x->id.proto != m->proto)
1221 continue;
1222 if (m->reqid && x->props.reqid != m->reqid)
1223 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001224 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1225 m->old_family) ||
1226 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1227 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001228 continue;
1229 xfrm_state_hold(x);
1230 return x;
1231 }
1232 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001233 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001234 m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001235 hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001236 if (x->props.mode != m->mode ||
1237 x->id.proto != m->proto)
1238 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001239 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1240 m->old_family) ||
1241 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1242 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001243 continue;
1244 xfrm_state_hold(x);
1245 return x;
1246 }
1247 }
1248
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001249 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001250}
1251EXPORT_SYMBOL(xfrm_migrate_state_find);
1252
1253struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1254 struct xfrm_migrate *m)
1255{
1256 struct xfrm_state *xc;
1257 int err;
1258
1259 xc = xfrm_state_clone(x, &err);
1260 if (!xc)
1261 return NULL;
1262
1263 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1264 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1265
1266 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001267 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001268 /* a care is needed when the destination address of the
1269 state is to be updated as it is a part of triplet */
1270 xfrm_state_insert(xc);
1271 } else {
1272 if ((err = xfrm_state_add(xc)) < 0)
1273 goto error;
1274 }
1275
1276 return xc;
1277error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001278 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001279 return NULL;
1280}
1281EXPORT_SYMBOL(xfrm_state_migrate);
1282#endif
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284int xfrm_state_update(struct xfrm_state *x)
1285{
David S. Miller37b08e32008-09-02 20:14:15 -07001286 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001288 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
David S. Miller37b08e32008-09-02 20:14:15 -07001290 to_put = NULL;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001293 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 err = -ESRCH;
1296 if (!x1)
1297 goto out;
1298
1299 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001300 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 err = -EEXIST;
1302 goto out;
1303 }
1304
1305 if (x1->km.state == XFRM_STATE_ACQ) {
1306 __xfrm_state_insert(x);
1307 x = NULL;
1308 }
1309 err = 0;
1310
1311out:
1312 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
David S. Miller37b08e32008-09-02 20:14:15 -07001314 if (to_put)
1315 xfrm_state_put(to_put);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (err)
1318 return err;
1319
1320 if (!x) {
1321 xfrm_state_delete(x1);
1322 xfrm_state_put(x1);
1323 return 0;
1324 }
1325
1326 err = -EINVAL;
1327 spin_lock_bh(&x1->lock);
1328 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1329 if (x->encap && x1->encap)
1330 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001331 if (x->coaddr && x1->coaddr) {
1332 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1333 }
1334 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1335 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1337 x1->km.dying = 0;
1338
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001339 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (x1->curlft.use_time)
1341 xfrm_state_check_expire(x1);
1342
1343 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001344 x->km.state = XFRM_STATE_DEAD;
1345 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 spin_unlock_bh(&x1->lock);
1348
1349 xfrm_state_put(x1);
1350
1351 return err;
1352}
1353EXPORT_SYMBOL(xfrm_state_update);
1354
1355int xfrm_state_check_expire(struct xfrm_state *x)
1356{
1357 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001358 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1361 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001362 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001363 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EINVAL;
1365 }
1366
1367 if (!x->km.dying &&
1368 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001369 x->curlft.packets >= x->lft.soft_packet_limit)) {
1370 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001371 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375EXPORT_SYMBOL(xfrm_state_check_expire);
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001378xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001379 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001384 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return x;
1387}
1388EXPORT_SYMBOL(xfrm_state_lookup);
1389
1390struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001391xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001392 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001393 u8 proto, unsigned short family)
1394{
1395 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001396
1397 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001398 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001399 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001400 return x;
1401}
1402EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1403
1404struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001405xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1406 u8 proto, const xfrm_address_t *daddr,
1407 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001412 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return x;
1416}
1417EXPORT_SYMBOL(xfrm_find_acq);
1418
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001419#ifdef CONFIG_XFRM_SUB_POLICY
1420int
1421xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1422 unsigned short family)
1423{
1424 int err = 0;
1425 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1426 if (!afinfo)
1427 return -EAFNOSUPPORT;
1428
1429 spin_lock_bh(&xfrm_state_lock);
1430 if (afinfo->tmpl_sort)
1431 err = afinfo->tmpl_sort(dst, src, n);
1432 spin_unlock_bh(&xfrm_state_lock);
1433 xfrm_state_put_afinfo(afinfo);
1434 return err;
1435}
1436EXPORT_SYMBOL(xfrm_tmpl_sort);
1437
1438int
1439xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1440 unsigned short family)
1441{
1442 int err = 0;
1443 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1444 if (!afinfo)
1445 return -EAFNOSUPPORT;
1446
1447 spin_lock_bh(&xfrm_state_lock);
1448 if (afinfo->state_sort)
1449 err = afinfo->state_sort(dst, src, n);
1450 spin_unlock_bh(&xfrm_state_lock);
1451 xfrm_state_put_afinfo(afinfo);
1452 return err;
1453}
1454EXPORT_SYMBOL(xfrm_state_sort);
1455#endif
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457/* Silly enough, but I'm lazy to build resolution list */
1458
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001459static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001463 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001464 struct xfrm_state *x;
1465
Sasha Levinb67bfe02013-02-27 17:06:00 -08001466 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001467 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001468 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001469 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 xfrm_state_hold(x);
1471 return x;
1472 }
1473 }
1474 }
1475 return NULL;
1476}
1477
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001478struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct xfrm_state *x;
1481
1482 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001483 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 spin_unlock_bh(&xfrm_state_lock);
1485 return x;
1486}
1487EXPORT_SYMBOL(xfrm_find_acq_byseq);
1488
1489u32 xfrm_get_acqseq(void)
1490{
1491 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001492 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
jamal6836b9b2010-02-16 02:01:22 +00001494 do {
1495 res = atomic_inc_return(&acqseq);
1496 } while (!res);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return res;
1499}
1500EXPORT_SYMBOL(xfrm_get_acqseq);
1501
Herbert Xu658b2192007-10-09 13:29:52 -07001502int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001504 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001505 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001507 int err = -ENOENT;
1508 __be32 minspi = htonl(low);
1509 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001510 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Herbert Xu658b2192007-10-09 13:29:52 -07001512 spin_lock_bh(&x->lock);
1513 if (x->km.state == XFRM_STATE_DEAD)
1514 goto unlock;
1515
1516 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001518 goto unlock;
1519
1520 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001523 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (x0) {
1525 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001526 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 x->id.spi = minspi;
1529 } else {
1530 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001531 for (h=0; h<high-low+1; h++) {
1532 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001533 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (x0 == NULL) {
1535 x->id.spi = htonl(spi);
1536 break;
1537 }
1538 xfrm_state_put(x0);
1539 }
1540 }
1541 if (x->id.spi) {
1542 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001543 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1544 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001546
1547 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
Herbert Xu658b2192007-10-09 13:29:52 -07001549
1550unlock:
1551 spin_unlock_bh(&x->lock);
1552
1553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555EXPORT_SYMBOL(xfrm_alloc_spi);
1556
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001557int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001558 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 void *data)
1560{
Herbert Xu12a169e2008-10-01 07:03:24 -07001561 struct xfrm_state *state;
1562 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int err = 0;
1564
Herbert Xu12a169e2008-10-01 07:03:24 -07001565 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001566 return 0;
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001569 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001570 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001571 else
1572 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001573 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001574 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001575 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001576 state = container_of(x, struct xfrm_state, km);
1577 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001578 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001579 err = func(state, walk->seq, data);
1580 if (err) {
1581 list_move_tail(&walk->all, &x->all);
1582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001584 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001586 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 err = -ENOENT;
1588 goto out;
1589 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001590 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591out:
1592 spin_unlock_bh(&xfrm_state_lock);
1593 return err;
1594}
1595EXPORT_SYMBOL(xfrm_state_walk);
1596
Herbert Xu5c182452008-09-22 19:48:19 -07001597void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1598{
Herbert Xu12a169e2008-10-01 07:03:24 -07001599 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001600 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001601 walk->state = XFRM_STATE_DEAD;
1602 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001603}
1604EXPORT_SYMBOL(xfrm_state_walk_init);
1605
Herbert Xuabb81c42008-09-09 19:58:29 -07001606void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1607{
Herbert Xu12a169e2008-10-01 07:03:24 -07001608 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001609 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001610
Herbert Xu12a169e2008-10-01 07:03:24 -07001611 spin_lock_bh(&xfrm_state_lock);
1612 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001613 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001614}
1615EXPORT_SYMBOL(xfrm_state_walk_done);
1616
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001617static void xfrm_replay_timer_handler(unsigned long data)
1618{
1619 struct xfrm_state *x = (struct xfrm_state*)data;
1620
1621 spin_lock(&x->lock);
1622
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001623 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001624 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001625 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001626 else
1627 x->xflags |= XFRM_TIME_DEFER;
1628 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001629
1630 spin_unlock(&x->lock);
1631}
1632
Denis Chengdf018122007-12-07 00:51:11 -08001633static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
David S. Miller214e0052011-02-24 00:02:38 -05001635void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct xfrm_mgr *km;
1638
Cong Wang85168c02013-01-16 16:05:06 +08001639 rcu_read_lock();
1640 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001641 if (km->notify_policy)
1642 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001643 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001644}
1645
David S. Miller214e0052011-02-24 00:02:38 -05001646void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001647{
1648 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001649 rcu_read_lock();
1650 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001651 if (km->notify)
1652 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001653 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001654}
1655
1656EXPORT_SYMBOL(km_policy_notify);
1657EXPORT_SYMBOL(km_state_notify);
1658
Eric W. Biederman15e47302012-09-07 20:12:54 +00001659void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001660{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001661 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001662 struct km_event c;
1663
Herbert Xubf088672005-06-18 22:44:00 -07001664 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001665 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001666 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001667 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001670 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001673EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001674/*
1675 * We send to all registered managers regardless of failure
1676 * We are happy with one success
1677*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001678int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 struct xfrm_mgr *km;
1682
Cong Wang85168c02013-01-16 16:05:06 +08001683 rcu_read_lock();
1684 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001685 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001686 if (!acqret)
1687 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Cong Wang85168c02013-01-16 16:05:06 +08001689 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return err;
1691}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001692EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Al Viro5d36b182006-11-08 00:24:06 -08001694int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 int err = -EINVAL;
1697 struct xfrm_mgr *km;
1698
Cong Wang85168c02013-01-16 16:05:06 +08001699 rcu_read_lock();
1700 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (km->new_mapping)
1702 err = km->new_mapping(x, ipaddr, sport);
1703 if (!err)
1704 break;
1705 }
Cong Wang85168c02013-01-16 16:05:06 +08001706 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return err;
1708}
1709EXPORT_SYMBOL(km_new_mapping);
1710
Eric W. Biederman15e47302012-09-07 20:12:54 +00001711void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001713 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001714 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Herbert Xubf088672005-06-18 22:44:00 -07001716 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001717 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001718 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001719 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001722 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
David S. Millera70fcb02006-03-20 19:18:52 -08001724EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001726#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001727int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1728 const struct xfrm_migrate *m, int num_migrate,
1729 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001730{
1731 int err = -EINVAL;
1732 int ret;
1733 struct xfrm_mgr *km;
1734
Cong Wang85168c02013-01-16 16:05:06 +08001735 rcu_read_lock();
1736 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001737 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001738 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001739 if (!ret)
1740 err = ret;
1741 }
1742 }
Cong Wang85168c02013-01-16 16:05:06 +08001743 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001744 return err;
1745}
1746EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001747#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001748
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001749int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001750{
1751 int err = -EINVAL;
1752 int ret;
1753 struct xfrm_mgr *km;
1754
Cong Wang85168c02013-01-16 16:05:06 +08001755 rcu_read_lock();
1756 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001757 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001758 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001759 if (!ret)
1760 err = ret;
1761 }
1762 }
Cong Wang85168c02013-01-16 16:05:06 +08001763 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001764 return err;
1765}
1766EXPORT_SYMBOL(km_report);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1769{
1770 int err;
1771 u8 *data;
1772 struct xfrm_mgr *km;
1773 struct xfrm_policy *pol = NULL;
1774
1775 if (optlen <= 0 || optlen > PAGE_SIZE)
1776 return -EMSGSIZE;
1777
1778 data = kmalloc(optlen, GFP_KERNEL);
1779 if (!data)
1780 return -ENOMEM;
1781
1782 err = -EFAULT;
1783 if (copy_from_user(data, optval, optlen))
1784 goto out;
1785
1786 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001787 rcu_read_lock();
1788 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001789 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 optlen, &err);
1791 if (err >= 0)
1792 break;
1793 }
Cong Wang85168c02013-01-16 16:05:06 +08001794 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (err >= 0) {
1797 xfrm_sk_policy_insert(sk, err, pol);
1798 xfrm_pol_put(pol);
1799 err = 0;
1800 }
1801
1802out:
1803 kfree(data);
1804 return err;
1805}
1806EXPORT_SYMBOL(xfrm_user_policy);
1807
Cong Wang85168c02013-01-16 16:05:06 +08001808static DEFINE_SPINLOCK(xfrm_km_lock);
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810int xfrm_register_km(struct xfrm_mgr *km)
1811{
Cong Wang85168c02013-01-16 16:05:06 +08001812 spin_lock_bh(&xfrm_km_lock);
1813 list_add_tail_rcu(&km->list, &xfrm_km_list);
1814 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
1816}
1817EXPORT_SYMBOL(xfrm_register_km);
1818
1819int xfrm_unregister_km(struct xfrm_mgr *km)
1820{
Cong Wang85168c02013-01-16 16:05:06 +08001821 spin_lock_bh(&xfrm_km_lock);
1822 list_del_rcu(&km->list);
1823 spin_unlock_bh(&xfrm_km_lock);
1824 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return 0;
1826}
1827EXPORT_SYMBOL(xfrm_unregister_km);
1828
1829int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1830{
1831 int err = 0;
1832 if (unlikely(afinfo == NULL))
1833 return -EINVAL;
1834 if (unlikely(afinfo->family >= NPROTO))
1835 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001836 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1838 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001839 else
Cong Wang44abdc32013-01-16 16:05:05 +08001840 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1841 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return err;
1843}
1844EXPORT_SYMBOL(xfrm_state_register_afinfo);
1845
1846int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1847{
1848 int err = 0;
1849 if (unlikely(afinfo == NULL))
1850 return -EINVAL;
1851 if (unlikely(afinfo->family >= NPROTO))
1852 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001853 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1855 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1856 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001857 else
Cong Wang44abdc32013-01-16 16:05:05 +08001858 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Cong Wang44abdc32013-01-16 16:05:05 +08001860 spin_unlock_bh(&xfrm_state_afinfo_lock);
1861 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return err;
1863}
1864EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1865
Herbert Xu17c2a422007-10-17 21:33:12 -07001866static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
1868 struct xfrm_state_afinfo *afinfo;
1869 if (unlikely(family >= NPROTO))
1870 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001871 rcu_read_lock();
1872 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001873 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001874 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return afinfo;
1876}
1877
Herbert Xu17c2a422007-10-17 21:33:12 -07001878static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Cong Wang44abdc32013-01-16 16:05:05 +08001880 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
1883/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1884void xfrm_state_delete_tunnel(struct xfrm_state *x)
1885{
1886 if (x->tunnel) {
1887 struct xfrm_state *t = x->tunnel;
1888
1889 if (atomic_read(&t->tunnel_users) == 2)
1890 xfrm_state_delete(t);
1891 atomic_dec(&t->tunnel_users);
1892 xfrm_state_put(t);
1893 x->tunnel = NULL;
1894 }
1895}
1896EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1897
1898int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1899{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001900 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Patrick McHardyc5c25232007-04-09 11:47:18 -07001902 spin_lock_bh(&x->lock);
1903 if (x->km.state == XFRM_STATE_VALID &&
1904 x->type && x->type->get_mtu)
1905 res = x->type->get_mtu(x, mtu);
1906 else
Patrick McHardy28121612007-06-18 22:30:15 -07001907 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001908 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return res;
1910}
1911
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001912int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001913{
Herbert Xud094cd82005-06-20 13:19:41 -07001914 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001915 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001916 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001917 int err;
1918
Herbert Xud094cd82005-06-20 13:19:41 -07001919 err = -EAFNOSUPPORT;
1920 afinfo = xfrm_state_get_afinfo(family);
1921 if (!afinfo)
1922 goto error;
1923
1924 err = 0;
1925 if (afinfo->init_flags)
1926 err = afinfo->init_flags(x);
1927
1928 xfrm_state_put_afinfo(afinfo);
1929
1930 if (err)
1931 goto error;
1932
1933 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001934
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001935 if (x->sel.family != AF_UNSPEC) {
1936 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1937 if (inner_mode == NULL)
1938 goto error;
1939
1940 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1941 family != x->sel.family) {
1942 xfrm_put_mode(inner_mode);
1943 goto error;
1944 }
1945
1946 x->inner_mode = inner_mode;
1947 } else {
1948 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001949 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001950
Martin Willid81d2282008-12-03 15:38:07 -08001951 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001952 if (inner_mode == NULL)
1953 goto error;
1954
1955 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1956 xfrm_put_mode(inner_mode);
1957 goto error;
1958 }
Martin Willid81d2282008-12-03 15:38:07 -08001959 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001960
Martin Willid81d2282008-12-03 15:38:07 -08001961 if (x->props.family == AF_INET)
1962 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001963
Martin Willid81d2282008-12-03 15:38:07 -08001964 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1965 if (inner_mode_iaf) {
1966 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1967 x->inner_mode_iaf = inner_mode_iaf;
1968 else
1969 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001970 }
1971 }
Herbert Xu13996372007-10-17 21:35:51 -07001972
Herbert Xud094cd82005-06-20 13:19:41 -07001973 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001974 if (x->type == NULL)
1975 goto error;
1976
1977 err = x->type->init_state(x);
1978 if (err)
1979 goto error;
1980
Herbert Xu13996372007-10-17 21:35:51 -07001981 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00001982 if (x->outer_mode == NULL) {
1983 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07001984 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00001985 }
Herbert Xub59f45d2006-05-27 23:05:54 -07001986
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001987 if (init_replay) {
1988 err = xfrm_init_replay(x);
1989 if (err)
1990 goto error;
1991 }
1992
Herbert Xu72cb6962005-06-20 13:18:08 -07001993 x->km.state = XFRM_STATE_VALID;
1994
1995error:
1996 return err;
1997}
1998
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001999EXPORT_SYMBOL(__xfrm_init_state);
2000
2001int xfrm_init_state(struct xfrm_state *x)
2002{
2003 return __xfrm_init_state(x, true);
2004}
2005
Herbert Xu72cb6962005-06-20 13:18:08 -07002006EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002007
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002008int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
David S. Millerf034b5d2006-08-24 03:08:07 -07002010 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002012 INIT_LIST_HEAD(&net->xfrm.state_all);
2013
David S. Millerf034b5d2006-08-24 03:08:07 -07002014 sz = sizeof(struct hlist_head) * 8;
2015
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002016 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2017 if (!net->xfrm.state_bydst)
2018 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002019 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2020 if (!net->xfrm.state_bysrc)
2021 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002022 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2023 if (!net->xfrm.state_byspi)
2024 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002025 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002026
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002027 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002028 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002029 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002030 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002031 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002032 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002033
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002034out_byspi:
2035 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002036out_bysrc:
2037 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002038out_bydst:
2039 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002040}
2041
2042void xfrm_state_fini(struct net *net)
2043{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002044 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002045 unsigned int sz;
2046
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002047 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002048 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002049 audit_info.sessionid = -1;
2050 audit_info.secid = 0;
2051 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2052 flush_work(&net->xfrm.state_gc_work);
2053
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002054 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002055
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002056 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002057 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2058 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002059 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2060 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002061 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2062 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Joy Lattenab5f5e82007-09-17 11:51:22 -07002065#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002066static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2067 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002068{
Paul Moore68277ac2007-12-20 20:49:33 -08002069 struct xfrm_sec_ctx *ctx = x->security;
2070 u32 spi = ntohl(x->id.spi);
2071
2072 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002073 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002074 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002075
2076 switch(x->props.family) {
2077 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002078 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2079 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002080 break;
2081 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002082 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002083 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002084 break;
2085 }
Paul Moore68277ac2007-12-20 20:49:33 -08002086
2087 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002088}
2089
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002090static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2091 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002092{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002093 const struct iphdr *iph4;
2094 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002095
2096 switch (family) {
2097 case AF_INET:
2098 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002099 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2100 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002101 break;
2102 case AF_INET6:
2103 iph6 = ipv6_hdr(skb);
2104 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002105 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002106 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002107 iph6->flow_lbl[0] & 0x0f,
2108 iph6->flow_lbl[1],
2109 iph6->flow_lbl[2]);
2110 break;
2111 }
2112}
2113
Paul Moore68277ac2007-12-20 20:49:33 -08002114void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002115 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002116{
2117 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002118
Paul Mooreafeb14b2007-12-21 14:58:11 -08002119 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002120 if (audit_buf == NULL)
2121 return;
Eric Paris25323862008-04-18 10:09:25 -04002122 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002123 xfrm_audit_helper_sainfo(x, audit_buf);
2124 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002125 audit_log_end(audit_buf);
2126}
2127EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2128
Paul Moore68277ac2007-12-20 20:49:33 -08002129void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002130 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002131{
2132 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002133
Paul Mooreafeb14b2007-12-21 14:58:11 -08002134 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002135 if (audit_buf == NULL)
2136 return;
Eric Paris25323862008-04-18 10:09:25 -04002137 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002138 xfrm_audit_helper_sainfo(x, audit_buf);
2139 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002140 audit_log_end(audit_buf);
2141}
2142EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002143
2144void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2145 struct sk_buff *skb)
2146{
2147 struct audit_buffer *audit_buf;
2148 u32 spi;
2149
2150 audit_buf = xfrm_audit_start("SA-replay-overflow");
2151 if (audit_buf == NULL)
2152 return;
2153 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2154 /* don't record the sequence number because it's inherent in this kind
2155 * of audit message */
2156 spi = ntohl(x->id.spi);
2157 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2158 audit_log_end(audit_buf);
2159}
2160EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2161
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002162void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002163 struct sk_buff *skb, __be32 net_seq)
2164{
2165 struct audit_buffer *audit_buf;
2166 u32 spi;
2167
2168 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2169 if (audit_buf == NULL)
2170 return;
2171 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2172 spi = ntohl(x->id.spi);
2173 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2174 spi, spi, ntohl(net_seq));
2175 audit_log_end(audit_buf);
2176}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002177EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002178
2179void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2180{
2181 struct audit_buffer *audit_buf;
2182
2183 audit_buf = xfrm_audit_start("SA-notfound");
2184 if (audit_buf == NULL)
2185 return;
2186 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2187 audit_log_end(audit_buf);
2188}
2189EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2190
2191void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2192 __be32 net_spi, __be32 net_seq)
2193{
2194 struct audit_buffer *audit_buf;
2195 u32 spi;
2196
2197 audit_buf = xfrm_audit_start("SA-notfound");
2198 if (audit_buf == NULL)
2199 return;
2200 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2201 spi = ntohl(net_spi);
2202 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2203 spi, spi, ntohl(net_seq));
2204 audit_log_end(audit_buf);
2205}
2206EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2207
2208void xfrm_audit_state_icvfail(struct xfrm_state *x,
2209 struct sk_buff *skb, u8 proto)
2210{
2211 struct audit_buffer *audit_buf;
2212 __be32 net_spi;
2213 __be32 net_seq;
2214
2215 audit_buf = xfrm_audit_start("SA-icv-failure");
2216 if (audit_buf == NULL)
2217 return;
2218 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2219 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2220 u32 spi = ntohl(net_spi);
2221 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2222 spi, spi, ntohl(net_seq));
2223 }
2224 audit_log_end(audit_buf);
2225}
2226EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002227#endif /* CONFIG_AUDITSYSCALL */