blob: b2cd806c0be344e0f94b55a64cdc172ae605b87c [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);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800502 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800503 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
504 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800505 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 x->lft.soft_byte_limit = XFRM_INF;
507 x->lft.soft_packet_limit = XFRM_INF;
508 x->lft.hard_byte_limit = XFRM_INF;
509 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800510 x->replay_maxage = 0;
511 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700512 x->inner_mode = NULL;
513 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_lock_init(&x->lock);
515 }
516 return x;
517}
518EXPORT_SYMBOL(xfrm_state_alloc);
519
520void __xfrm_state_destroy(struct xfrm_state *x)
521{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800522 struct net *net = xs_net(x);
523
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700524 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800527 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800529 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531EXPORT_SYMBOL(__xfrm_state_destroy);
532
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800533int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800535 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700536 int err = -ESRCH;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (x->km.state != XFRM_STATE_DEAD) {
539 x->km.state = XFRM_STATE_DEAD;
540 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700541 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700542 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700543 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700544 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700545 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800546 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* All xfrm_state objects are created by xfrm_state_alloc.
550 * The xfrm_state_alloc call gives a reference, and that
551 * is what we are dropping here.
552 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800553 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700556
557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800559EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700561int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700563 int err;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700566 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700568
569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571EXPORT_SYMBOL(xfrm_state_delete);
572
Joy Latten4aa2e622007-06-04 19:05:57 -0400573#ifdef CONFIG_SECURITY_NETWORK_XFRM
574static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800575xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Joy Latten4aa2e622007-06-04 19:05:57 -0400577 int i, err = 0;
578
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800579 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400580 struct xfrm_state *x;
581
Sasha Levinb67bfe02013-02-27 17:06:00 -0800582 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400583 if (xfrm_id_proto_match(x->id.proto, proto) &&
584 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700585 xfrm_audit_state_delete(x, 0,
586 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400587 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700588 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400589 return err;
590 }
591 }
592 }
593
594 return err;
595}
596#else
597static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800598xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400599{
600 return 0;
601}
602#endif
603
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800604int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400605{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000606 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800609 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400610 if (err)
611 goto out;
612
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000613 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800614 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700615 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800617 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700619 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 xfrm_state_hold(x);
621 spin_unlock_bh(&xfrm_state_lock);
622
Joy Latten161a09e2006-11-27 13:11:54 -0600623 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700624 xfrm_audit_state_delete(x, err ? 0 : 1,
625 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400626 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700627 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000629 if (!err)
630 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 spin_lock_bh(&xfrm_state_lock);
633 goto restart;
634 }
635 }
636 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000637 if (cnt)
638 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400639
640out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800642 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645EXPORT_SYMBOL(xfrm_state_flush);
646
Alexey Dobriyane0710412010-01-23 13:37:10 +0000647void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700648{
649 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000650 si->sadcnt = net->xfrm.state_num;
651 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700652 si->sadhmcnt = xfrm_state_hashmax;
653 spin_unlock_bh(&xfrm_state_lock);
654}
655EXPORT_SYMBOL(xfrm_sad_getinfo);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static int
David S. Miller1a898592011-02-22 18:22:34 -0800658xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500659 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500660 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700661 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
664 if (!afinfo)
665 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700666 afinfo->init_tempsel(&x->sel, fl);
667
668 if (family != tmpl->encap_family) {
669 xfrm_state_put_afinfo(afinfo);
670 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
671 if (!afinfo)
672 return -1;
673 }
674 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 xfrm_state_put_afinfo(afinfo);
676 return 0;
677}
678
David S. Miller9aa60082011-02-24 01:51:36 -0500679static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
680 const xfrm_address_t *daddr,
681 __be32 spi, u8 proto,
682 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700683{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800684 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700685 struct xfrm_state *x;
686
Sasha Levinb67bfe02013-02-27 17:06:00 -0800687 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700688 if (x->props.family != family ||
689 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000690 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000691 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700692 continue;
693
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000694 if ((mark & x->mark.m) != x->mark.v)
695 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700696 xfrm_state_hold(x);
697 return x;
698 }
699
700 return NULL;
701}
702
David S. Miller9aa60082011-02-24 01:51:36 -0500703static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
704 const xfrm_address_t *daddr,
705 const xfrm_address_t *saddr,
706 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700707{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800708 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700709 struct xfrm_state *x;
710
Sasha Levinb67bfe02013-02-27 17:06:00 -0800711 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700712 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000713 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000714 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
715 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700716 continue;
717
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000718 if ((mark & x->mark.m) != x->mark.v)
719 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700720 xfrm_state_hold(x);
721 return x;
722 }
723
724 return NULL;
725}
726
727static inline struct xfrm_state *
728__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
729{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800730 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800731 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800732
David S. Milleredcd5822006-08-24 00:42:45 -0700733 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800734 return __xfrm_state_lookup(net, mark, &x->id.daddr,
735 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700736 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800737 return __xfrm_state_lookup_byaddr(net, mark,
738 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700739 &x->props.saddr,
740 x->id.proto, family);
741}
742
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800743static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700744{
745 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800746 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
747 net->xfrm.state_num > net->xfrm.state_hmask)
748 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700749}
750
David S. Miller08ec9af2009-03-13 14:22:40 -0700751static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800752 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700753 struct xfrm_state **best, int *acq_in_progress,
754 int *error)
755{
756 /* Resolution logic:
757 * 1. There is a valid state with matching selector. Done.
758 * 2. Valid state with inappropriate selector. Skip.
759 *
760 * Entering area of "sysdeps".
761 *
762 * 3. If state is not valid, selector is temporary, it selects
763 * only session which triggered previous resolution. Key
764 * manager will do something to install a state with proper
765 * selector.
766 */
767 if (x->km.state == XFRM_STATE_VALID) {
768 if ((x->sel.family &&
769 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
770 !security_xfrm_state_pol_flow_match(x, pol, fl))
771 return;
772
773 if (!*best ||
774 (*best)->km.dying > x->km.dying ||
775 ((*best)->km.dying == x->km.dying &&
776 (*best)->curlft.add_time < x->curlft.add_time))
777 *best = x;
778 } else if (x->km.state == XFRM_STATE_ACQ) {
779 *acq_in_progress = 1;
780 } else if (x->km.state == XFRM_STATE_ERROR ||
781 x->km.state == XFRM_STATE_EXPIRED) {
782 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
783 security_xfrm_state_pol_flow_match(x, pol, fl))
784 *error = -ESRCH;
785 }
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500789xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800790 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct xfrm_policy *pol, int *err,
792 unsigned short family)
793{
David S. Miller08ec9af2009-03-13 14:22:40 -0700794 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800795 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700796 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700797 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int acquire_in_progress = 0;
799 int error = 0;
800 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800801 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700802 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900803
David S. Miller37b08e32008-09-02 20:14:15 -0700804 to_put = NULL;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700807 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800808 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700809 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000811 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700812 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700813 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 tmpl->mode == x->props.mode &&
815 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700816 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500817 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700818 &best, &acquire_in_progress, &error);
819 }
820 if (best)
821 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Thomas Egerer8444cf72010-09-20 11:11:38 -0700823 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800824 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700825 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700826 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000827 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700828 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700829 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700830 tmpl->mode == x->props.mode &&
831 tmpl->id.proto == x->id.proto &&
832 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500833 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700834 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
David S. Miller08ec9af2009-03-13 14:22:40 -0700837found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 x = best;
839 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700840 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800841 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700842 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700843 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 error = -EEXIST;
845 goto out;
846 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800847 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (x == NULL) {
849 error = -ENOMEM;
850 goto out;
851 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700852 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700854 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800855 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
David S. Miller1d28f422011-03-12 00:29:39 -0500857 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700858 if (error) {
859 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700860 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700861 x = NULL;
862 goto out;
863 }
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (km_query(x, tmpl, pol) == 0) {
866 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800867 list_add(&x->km.all, &net->xfrm.state_all);
868 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700869 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800870 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700872 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800873 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800875 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800876 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800877 net->xfrm.state_num++;
878 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else {
880 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700881 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 x = NULL;
883 error = -ESRCH;
884 }
885 }
886out:
887 if (x)
888 xfrm_state_hold(x);
889 else
890 *err = acquire_in_progress ? -EAGAIN : error;
891 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700892 if (to_put)
893 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return x;
895}
896
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700897struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800898xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800899 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700900 unsigned short family, u8 mode, u8 proto, u32 reqid)
901{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800902 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700903 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700904
905 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800906 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800907 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700908 if (x->props.family == family &&
909 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000910 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700911 !(x->props.flags & XFRM_STATE_WILDRECV) &&
912 xfrm_state_addr_check(x, daddr, saddr, family) &&
913 mode == x->props.mode &&
914 proto == x->id.proto &&
915 x->km.state == XFRM_STATE_VALID) {
916 rx = x;
917 break;
918 }
919 }
920
921 if (rx)
922 xfrm_state_hold(rx);
923 spin_unlock(&xfrm_state_lock);
924
925
926 return rx;
927}
928EXPORT_SYMBOL(xfrm_stateonly_find);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static void __xfrm_state_insert(struct xfrm_state *x)
931{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800932 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700933 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800935 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800936
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800937 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700938 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800939 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800941 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
942 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700944 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800945 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700946 x->props.family);
947
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800948 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700949 }
950
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800951 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700952 if (x->replay_maxage)
953 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800954
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700956
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800957 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700958
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800959 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
David S. Millerc7f5ea32006-08-24 03:29:04 -0700962/* xfrm_state_lock is held */
963static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
964{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800965 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700966 unsigned short family = xnew->props.family;
967 u32 reqid = xnew->props.reqid;
968 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700969 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000970 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700971
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800972 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800973 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700974 if (x->props.family == family &&
975 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000976 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000977 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
978 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000979 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700980 }
981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983void xfrm_state_insert(struct xfrm_state *x)
984{
985 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700986 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 __xfrm_state_insert(x);
988 spin_unlock_bh(&xfrm_state_lock);
989}
990EXPORT_SYMBOL(xfrm_state_insert);
991
David S. Miller27708342006-08-24 00:13:10 -0700992/* xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +0200993static struct xfrm_state *__find_acq_core(struct net *net,
994 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -0800995 unsigned short family, u8 mode,
996 u32 reqid, u8 proto,
997 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +0200998 const xfrm_address_t *saddr,
999 int create)
David S. Miller27708342006-08-24 00:13:10 -07001000{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001001 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001002 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001003 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001004
Sasha Levinb67bfe02013-02-27 17:06:00 -08001005 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001006 if (x->props.reqid != reqid ||
1007 x->props.mode != mode ||
1008 x->props.family != family ||
1009 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001010 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001011 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001012 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001013 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1014 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001015 continue;
1016
David S. Miller27708342006-08-24 00:13:10 -07001017 xfrm_state_hold(x);
1018 return x;
1019 }
1020
1021 if (!create)
1022 return NULL;
1023
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001024 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001025 if (likely(x)) {
1026 switch (family) {
1027 case AF_INET:
1028 x->sel.daddr.a4 = daddr->a4;
1029 x->sel.saddr.a4 = saddr->a4;
1030 x->sel.prefixlen_d = 32;
1031 x->sel.prefixlen_s = 32;
1032 x->props.saddr.a4 = saddr->a4;
1033 x->id.daddr.a4 = daddr->a4;
1034 break;
1035
1036 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001037 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1038 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001039 x->sel.prefixlen_d = 128;
1040 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001041 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1042 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001043 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001044 }
David S. Miller27708342006-08-24 00:13:10 -07001045
1046 x->km.state = XFRM_STATE_ACQ;
1047 x->id.proto = proto;
1048 x->props.family = family;
1049 x->props.mode = mode;
1050 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001051 x->mark.v = m->v;
1052 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001053 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001054 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001055 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001056 list_add(&x->km.all, &net->xfrm.state_all);
1057 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1058 h = xfrm_src_hash(net, daddr, saddr, family);
1059 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001060
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001061 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001062
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001063 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001064 }
1065
1066 return x;
1067}
1068
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001069static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071int xfrm_state_add(struct xfrm_state *x)
1072{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001073 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001074 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int family;
1076 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001077 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001078 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
David S. Miller37b08e32008-09-02 20:14:15 -07001082 to_put = NULL;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 spin_lock_bh(&xfrm_state_lock);
1085
David S. Milleredcd5822006-08-24 00:42:45 -07001086 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001088 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 x1 = NULL;
1090 err = -EEXIST;
1091 goto out;
1092 }
1093
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001094 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001095 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001096 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001097 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001098 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 x1 = NULL;
1100 }
1101 }
1102
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001103 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001104 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1105 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001106 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
David S. Millerc7f5ea32006-08-24 03:29:04 -07001108 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 __xfrm_state_insert(x);
1110 err = 0;
1111
1112out:
1113 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 if (x1) {
1116 xfrm_state_delete(x1);
1117 xfrm_state_put(x1);
1118 }
1119
David S. Miller37b08e32008-09-02 20:14:15 -07001120 if (to_put)
1121 xfrm_state_put(to_put);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return err;
1124}
1125EXPORT_SYMBOL(xfrm_state_add);
1126
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001127#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001128static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001129{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001130 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001131 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001132 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001133 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001134 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001135
1136 memcpy(&x->id, &orig->id, sizeof(x->id));
1137 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1138 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1139 x->props.mode = orig->props.mode;
1140 x->props.replay_window = orig->props.replay_window;
1141 x->props.reqid = orig->props.reqid;
1142 x->props.family = orig->props.family;
1143 x->props.saddr = orig->props.saddr;
1144
1145 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001146 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001147 if (!x->aalg)
1148 goto error;
1149 }
1150 x->props.aalgo = orig->props.aalgo;
1151
1152 if (orig->ealg) {
1153 x->ealg = xfrm_algo_clone(orig->ealg);
1154 if (!x->ealg)
1155 goto error;
1156 }
1157 x->props.ealgo = orig->props.ealgo;
1158
1159 if (orig->calg) {
1160 x->calg = xfrm_algo_clone(orig->calg);
1161 if (!x->calg)
1162 goto error;
1163 }
1164 x->props.calgo = orig->props.calgo;
1165
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001166 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001167 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1168 if (!x->encap)
1169 goto error;
1170 }
1171
1172 if (orig->coaddr) {
1173 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1174 GFP_KERNEL);
1175 if (!x->coaddr)
1176 goto error;
1177 }
1178
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001179 if (orig->replay_esn) {
1180 err = xfrm_replay_clone(x, orig);
1181 if (err)
1182 goto error;
1183 }
1184
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001185 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1186
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001187 err = xfrm_init_state(x);
1188 if (err)
1189 goto error;
1190
1191 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001192 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001193
1194 x->curlft.add_time = orig->curlft.add_time;
1195 x->km.state = orig->km.state;
1196 x->km.seq = orig->km.seq;
1197
1198 return x;
1199
1200 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001201 xfrm_state_put(x);
1202out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001203 if (errp)
1204 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001205 return NULL;
1206}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001207
1208/* xfrm_state_lock is held */
1209struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1210{
1211 unsigned int h;
1212 struct xfrm_state *x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001213
1214 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001215 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001216 m->reqid, m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001217 hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001218 if (x->props.mode != m->mode ||
1219 x->id.proto != m->proto)
1220 continue;
1221 if (m->reqid && x->props.reqid != m->reqid)
1222 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001223 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1224 m->old_family) ||
1225 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1226 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001227 continue;
1228 xfrm_state_hold(x);
1229 return x;
1230 }
1231 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001232 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001233 m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001234 hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001235 if (x->props.mode != m->mode ||
1236 x->id.proto != m->proto)
1237 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001238 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1239 m->old_family) ||
1240 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1241 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001242 continue;
1243 xfrm_state_hold(x);
1244 return x;
1245 }
1246 }
1247
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001248 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001249}
1250EXPORT_SYMBOL(xfrm_migrate_state_find);
1251
1252struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1253 struct xfrm_migrate *m)
1254{
1255 struct xfrm_state *xc;
1256 int err;
1257
1258 xc = xfrm_state_clone(x, &err);
1259 if (!xc)
1260 return NULL;
1261
1262 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1263 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1264
1265 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001266 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001267 /* a care is needed when the destination address of the
1268 state is to be updated as it is a part of triplet */
1269 xfrm_state_insert(xc);
1270 } else {
1271 if ((err = xfrm_state_add(xc)) < 0)
1272 goto error;
1273 }
1274
1275 return xc;
1276error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001277 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001278 return NULL;
1279}
1280EXPORT_SYMBOL(xfrm_state_migrate);
1281#endif
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283int xfrm_state_update(struct xfrm_state *x)
1284{
David S. Miller37b08e32008-09-02 20:14:15 -07001285 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001287 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
David S. Miller37b08e32008-09-02 20:14:15 -07001289 to_put = NULL;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001292 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 err = -ESRCH;
1295 if (!x1)
1296 goto out;
1297
1298 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001299 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 err = -EEXIST;
1301 goto out;
1302 }
1303
1304 if (x1->km.state == XFRM_STATE_ACQ) {
1305 __xfrm_state_insert(x);
1306 x = NULL;
1307 }
1308 err = 0;
1309
1310out:
1311 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
David S. Miller37b08e32008-09-02 20:14:15 -07001313 if (to_put)
1314 xfrm_state_put(to_put);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (err)
1317 return err;
1318
1319 if (!x) {
1320 xfrm_state_delete(x1);
1321 xfrm_state_put(x1);
1322 return 0;
1323 }
1324
1325 err = -EINVAL;
1326 spin_lock_bh(&x1->lock);
1327 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1328 if (x->encap && x1->encap)
1329 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001330 if (x->coaddr && x1->coaddr) {
1331 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1332 }
1333 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1334 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1336 x1->km.dying = 0;
1337
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001338 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (x1->curlft.use_time)
1340 xfrm_state_check_expire(x1);
1341
1342 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001343 x->km.state = XFRM_STATE_DEAD;
1344 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 spin_unlock_bh(&x1->lock);
1347
1348 xfrm_state_put(x1);
1349
1350 return err;
1351}
1352EXPORT_SYMBOL(xfrm_state_update);
1353
1354int xfrm_state_check_expire(struct xfrm_state *x)
1355{
1356 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001357 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1360 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001361 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001362 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EINVAL;
1364 }
1365
1366 if (!x->km.dying &&
1367 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001368 x->curlft.packets >= x->lft.soft_packet_limit)) {
1369 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001370 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return 0;
1373}
1374EXPORT_SYMBOL(xfrm_state_check_expire);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001377xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001378 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001383 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return x;
1386}
1387EXPORT_SYMBOL(xfrm_state_lookup);
1388
1389struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001390xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001391 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001392 u8 proto, unsigned short family)
1393{
1394 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001395
1396 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001397 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001398 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001399 return x;
1400}
1401EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1402
1403struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001404xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1405 u8 proto, const xfrm_address_t *daddr,
1406 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001411 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return x;
1415}
1416EXPORT_SYMBOL(xfrm_find_acq);
1417
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001418#ifdef CONFIG_XFRM_SUB_POLICY
1419int
1420xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1421 unsigned short family)
1422{
1423 int err = 0;
1424 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1425 if (!afinfo)
1426 return -EAFNOSUPPORT;
1427
1428 spin_lock_bh(&xfrm_state_lock);
1429 if (afinfo->tmpl_sort)
1430 err = afinfo->tmpl_sort(dst, src, n);
1431 spin_unlock_bh(&xfrm_state_lock);
1432 xfrm_state_put_afinfo(afinfo);
1433 return err;
1434}
1435EXPORT_SYMBOL(xfrm_tmpl_sort);
1436
1437int
1438xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1439 unsigned short family)
1440{
1441 int err = 0;
1442 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1443 if (!afinfo)
1444 return -EAFNOSUPPORT;
1445
1446 spin_lock_bh(&xfrm_state_lock);
1447 if (afinfo->state_sort)
1448 err = afinfo->state_sort(dst, src, n);
1449 spin_unlock_bh(&xfrm_state_lock);
1450 xfrm_state_put_afinfo(afinfo);
1451 return err;
1452}
1453EXPORT_SYMBOL(xfrm_state_sort);
1454#endif
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/* Silly enough, but I'm lazy to build resolution list */
1457
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001458static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001462 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001463 struct xfrm_state *x;
1464
Sasha Levinb67bfe02013-02-27 17:06:00 -08001465 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001466 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001467 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001468 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 xfrm_state_hold(x);
1470 return x;
1471 }
1472 }
1473 }
1474 return NULL;
1475}
1476
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001477struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct xfrm_state *x;
1480
1481 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001482 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 spin_unlock_bh(&xfrm_state_lock);
1484 return x;
1485}
1486EXPORT_SYMBOL(xfrm_find_acq_byseq);
1487
1488u32 xfrm_get_acqseq(void)
1489{
1490 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001491 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
jamal6836b9b2010-02-16 02:01:22 +00001493 do {
1494 res = atomic_inc_return(&acqseq);
1495 } while (!res);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return res;
1498}
1499EXPORT_SYMBOL(xfrm_get_acqseq);
1500
Herbert Xu658b2192007-10-09 13:29:52 -07001501int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001503 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001504 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001506 int err = -ENOENT;
1507 __be32 minspi = htonl(low);
1508 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001509 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Herbert Xu658b2192007-10-09 13:29:52 -07001511 spin_lock_bh(&x->lock);
1512 if (x->km.state == XFRM_STATE_DEAD)
1513 goto unlock;
1514
1515 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001517 goto unlock;
1518
1519 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001522 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (x0) {
1524 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001525 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 x->id.spi = minspi;
1528 } else {
1529 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001530 for (h=0; h<high-low+1; h++) {
1531 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001532 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (x0 == NULL) {
1534 x->id.spi = htonl(spi);
1535 break;
1536 }
1537 xfrm_state_put(x0);
1538 }
1539 }
1540 if (x->id.spi) {
1541 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001542 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1543 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001545
1546 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Herbert Xu658b2192007-10-09 13:29:52 -07001548
1549unlock:
1550 spin_unlock_bh(&x->lock);
1551
1552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554EXPORT_SYMBOL(xfrm_alloc_spi);
1555
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001556int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001557 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 void *data)
1559{
Herbert Xu12a169e2008-10-01 07:03:24 -07001560 struct xfrm_state *state;
1561 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int err = 0;
1563
Herbert Xu12a169e2008-10-01 07:03:24 -07001564 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001565 return 0;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001568 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001569 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001570 else
1571 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001572 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001573 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001574 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001575 state = container_of(x, struct xfrm_state, km);
1576 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001577 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001578 err = func(state, walk->seq, data);
1579 if (err) {
1580 list_move_tail(&walk->all, &x->all);
1581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001583 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001585 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 err = -ENOENT;
1587 goto out;
1588 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001589 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590out:
1591 spin_unlock_bh(&xfrm_state_lock);
1592 return err;
1593}
1594EXPORT_SYMBOL(xfrm_state_walk);
1595
Herbert Xu5c182452008-09-22 19:48:19 -07001596void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1597{
Herbert Xu12a169e2008-10-01 07:03:24 -07001598 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001599 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001600 walk->state = XFRM_STATE_DEAD;
1601 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001602}
1603EXPORT_SYMBOL(xfrm_state_walk_init);
1604
Herbert Xuabb81c42008-09-09 19:58:29 -07001605void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1606{
Herbert Xu12a169e2008-10-01 07:03:24 -07001607 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001608 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001609
Herbert Xu12a169e2008-10-01 07:03:24 -07001610 spin_lock_bh(&xfrm_state_lock);
1611 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001612 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001613}
1614EXPORT_SYMBOL(xfrm_state_walk_done);
1615
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001616static void xfrm_replay_timer_handler(unsigned long data)
1617{
1618 struct xfrm_state *x = (struct xfrm_state*)data;
1619
1620 spin_lock(&x->lock);
1621
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001622 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001623 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001624 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001625 else
1626 x->xflags |= XFRM_TIME_DEFER;
1627 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001628
1629 spin_unlock(&x->lock);
1630}
1631
Denis Chengdf018122007-12-07 00:51:11 -08001632static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
David S. Miller214e0052011-02-24 00:02:38 -05001634void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct xfrm_mgr *km;
1637
Cong Wang85168c02013-01-16 16:05:06 +08001638 rcu_read_lock();
1639 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001640 if (km->notify_policy)
1641 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001642 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001643}
1644
David S. Miller214e0052011-02-24 00:02:38 -05001645void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001646{
1647 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001648 rcu_read_lock();
1649 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001650 if (km->notify)
1651 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001652 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001653}
1654
1655EXPORT_SYMBOL(km_policy_notify);
1656EXPORT_SYMBOL(km_state_notify);
1657
Eric W. Biederman15e47302012-09-07 20:12:54 +00001658void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001659{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001660 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001661 struct km_event c;
1662
Herbert Xubf088672005-06-18 22:44:00 -07001663 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001664 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001665 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001666 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001669 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001672EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001673/*
1674 * We send to all registered managers regardless of failure
1675 * We are happy with one success
1676*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001677int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001679 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct xfrm_mgr *km;
1681
Cong Wang85168c02013-01-16 16:05:06 +08001682 rcu_read_lock();
1683 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001684 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001685 if (!acqret)
1686 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
Cong Wang85168c02013-01-16 16:05:06 +08001688 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return err;
1690}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001691EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Al Viro5d36b182006-11-08 00:24:06 -08001693int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 int err = -EINVAL;
1696 struct xfrm_mgr *km;
1697
Cong Wang85168c02013-01-16 16:05:06 +08001698 rcu_read_lock();
1699 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (km->new_mapping)
1701 err = km->new_mapping(x, ipaddr, sport);
1702 if (!err)
1703 break;
1704 }
Cong Wang85168c02013-01-16 16:05:06 +08001705 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return err;
1707}
1708EXPORT_SYMBOL(km_new_mapping);
1709
Eric W. Biederman15e47302012-09-07 20:12:54 +00001710void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001712 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001713 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Herbert Xubf088672005-06-18 22:44:00 -07001715 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001716 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001717 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001721 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
David S. Millera70fcb02006-03-20 19:18:52 -08001723EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001725#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001726int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1727 const struct xfrm_migrate *m, int num_migrate,
1728 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001729{
1730 int err = -EINVAL;
1731 int ret;
1732 struct xfrm_mgr *km;
1733
Cong Wang85168c02013-01-16 16:05:06 +08001734 rcu_read_lock();
1735 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001736 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001737 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001738 if (!ret)
1739 err = ret;
1740 }
1741 }
Cong Wang85168c02013-01-16 16:05:06 +08001742 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001743 return err;
1744}
1745EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001746#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001747
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001748int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001749{
1750 int err = -EINVAL;
1751 int ret;
1752 struct xfrm_mgr *km;
1753
Cong Wang85168c02013-01-16 16:05:06 +08001754 rcu_read_lock();
1755 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001756 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001757 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001758 if (!ret)
1759 err = ret;
1760 }
1761 }
Cong Wang85168c02013-01-16 16:05:06 +08001762 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001763 return err;
1764}
1765EXPORT_SYMBOL(km_report);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1768{
1769 int err;
1770 u8 *data;
1771 struct xfrm_mgr *km;
1772 struct xfrm_policy *pol = NULL;
1773
1774 if (optlen <= 0 || optlen > PAGE_SIZE)
1775 return -EMSGSIZE;
1776
1777 data = kmalloc(optlen, GFP_KERNEL);
1778 if (!data)
1779 return -ENOMEM;
1780
1781 err = -EFAULT;
1782 if (copy_from_user(data, optval, optlen))
1783 goto out;
1784
1785 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001786 rcu_read_lock();
1787 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001788 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 optlen, &err);
1790 if (err >= 0)
1791 break;
1792 }
Cong Wang85168c02013-01-16 16:05:06 +08001793 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 if (err >= 0) {
1796 xfrm_sk_policy_insert(sk, err, pol);
1797 xfrm_pol_put(pol);
1798 err = 0;
1799 }
1800
1801out:
1802 kfree(data);
1803 return err;
1804}
1805EXPORT_SYMBOL(xfrm_user_policy);
1806
Cong Wang85168c02013-01-16 16:05:06 +08001807static DEFINE_SPINLOCK(xfrm_km_lock);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809int xfrm_register_km(struct xfrm_mgr *km)
1810{
Cong Wang85168c02013-01-16 16:05:06 +08001811 spin_lock_bh(&xfrm_km_lock);
1812 list_add_tail_rcu(&km->list, &xfrm_km_list);
1813 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
1815}
1816EXPORT_SYMBOL(xfrm_register_km);
1817
1818int xfrm_unregister_km(struct xfrm_mgr *km)
1819{
Cong Wang85168c02013-01-16 16:05:06 +08001820 spin_lock_bh(&xfrm_km_lock);
1821 list_del_rcu(&km->list);
1822 spin_unlock_bh(&xfrm_km_lock);
1823 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return 0;
1825}
1826EXPORT_SYMBOL(xfrm_unregister_km);
1827
1828int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1829{
1830 int err = 0;
1831 if (unlikely(afinfo == NULL))
1832 return -EINVAL;
1833 if (unlikely(afinfo->family >= NPROTO))
1834 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001835 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1837 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001838 else
Cong Wang44abdc32013-01-16 16:05:05 +08001839 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1840 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return err;
1842}
1843EXPORT_SYMBOL(xfrm_state_register_afinfo);
1844
1845int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1846{
1847 int err = 0;
1848 if (unlikely(afinfo == NULL))
1849 return -EINVAL;
1850 if (unlikely(afinfo->family >= NPROTO))
1851 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001852 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1854 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1855 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001856 else
Cong Wang44abdc32013-01-16 16:05:05 +08001857 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Cong Wang44abdc32013-01-16 16:05:05 +08001859 spin_unlock_bh(&xfrm_state_afinfo_lock);
1860 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return err;
1862}
1863EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1864
Herbert Xu17c2a422007-10-17 21:33:12 -07001865static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
1867 struct xfrm_state_afinfo *afinfo;
1868 if (unlikely(family >= NPROTO))
1869 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001870 rcu_read_lock();
1871 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001872 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001873 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return afinfo;
1875}
1876
Herbert Xu17c2a422007-10-17 21:33:12 -07001877static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Cong Wang44abdc32013-01-16 16:05:05 +08001879 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1883void xfrm_state_delete_tunnel(struct xfrm_state *x)
1884{
1885 if (x->tunnel) {
1886 struct xfrm_state *t = x->tunnel;
1887
1888 if (atomic_read(&t->tunnel_users) == 2)
1889 xfrm_state_delete(t);
1890 atomic_dec(&t->tunnel_users);
1891 xfrm_state_put(t);
1892 x->tunnel = NULL;
1893 }
1894}
1895EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1896
1897int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1898{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001899 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Patrick McHardyc5c25232007-04-09 11:47:18 -07001901 spin_lock_bh(&x->lock);
1902 if (x->km.state == XFRM_STATE_VALID &&
1903 x->type && x->type->get_mtu)
1904 res = x->type->get_mtu(x, mtu);
1905 else
Patrick McHardy28121612007-06-18 22:30:15 -07001906 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001907 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return res;
1909}
1910
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001911int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001912{
Herbert Xud094cd82005-06-20 13:19:41 -07001913 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001914 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001915 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001916 int err;
1917
Herbert Xud094cd82005-06-20 13:19:41 -07001918 err = -EAFNOSUPPORT;
1919 afinfo = xfrm_state_get_afinfo(family);
1920 if (!afinfo)
1921 goto error;
1922
1923 err = 0;
1924 if (afinfo->init_flags)
1925 err = afinfo->init_flags(x);
1926
1927 xfrm_state_put_afinfo(afinfo);
1928
1929 if (err)
1930 goto error;
1931
1932 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001933
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001934 if (x->sel.family != AF_UNSPEC) {
1935 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1936 if (inner_mode == NULL)
1937 goto error;
1938
1939 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1940 family != x->sel.family) {
1941 xfrm_put_mode(inner_mode);
1942 goto error;
1943 }
1944
1945 x->inner_mode = inner_mode;
1946 } else {
1947 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001948 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001949
Martin Willid81d2282008-12-03 15:38:07 -08001950 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001951 if (inner_mode == NULL)
1952 goto error;
1953
1954 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1955 xfrm_put_mode(inner_mode);
1956 goto error;
1957 }
Martin Willid81d2282008-12-03 15:38:07 -08001958 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001959
Martin Willid81d2282008-12-03 15:38:07 -08001960 if (x->props.family == AF_INET)
1961 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001962
Martin Willid81d2282008-12-03 15:38:07 -08001963 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1964 if (inner_mode_iaf) {
1965 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1966 x->inner_mode_iaf = inner_mode_iaf;
1967 else
1968 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001969 }
1970 }
Herbert Xu13996372007-10-17 21:35:51 -07001971
Herbert Xud094cd82005-06-20 13:19:41 -07001972 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001973 if (x->type == NULL)
1974 goto error;
1975
1976 err = x->type->init_state(x);
1977 if (err)
1978 goto error;
1979
Herbert Xu13996372007-10-17 21:35:51 -07001980 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00001981 if (x->outer_mode == NULL) {
1982 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07001983 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00001984 }
Herbert Xub59f45d2006-05-27 23:05:54 -07001985
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001986 if (init_replay) {
1987 err = xfrm_init_replay(x);
1988 if (err)
1989 goto error;
1990 }
1991
Herbert Xu72cb6962005-06-20 13:18:08 -07001992 x->km.state = XFRM_STATE_VALID;
1993
1994error:
1995 return err;
1996}
1997
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001998EXPORT_SYMBOL(__xfrm_init_state);
1999
2000int xfrm_init_state(struct xfrm_state *x)
2001{
2002 return __xfrm_init_state(x, true);
2003}
2004
Herbert Xu72cb6962005-06-20 13:18:08 -07002005EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002006
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002007int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
David S. Millerf034b5d2006-08-24 03:08:07 -07002009 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002011 INIT_LIST_HEAD(&net->xfrm.state_all);
2012
David S. Millerf034b5d2006-08-24 03:08:07 -07002013 sz = sizeof(struct hlist_head) * 8;
2014
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002015 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2016 if (!net->xfrm.state_bydst)
2017 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002018 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2019 if (!net->xfrm.state_bysrc)
2020 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002021 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2022 if (!net->xfrm.state_byspi)
2023 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002024 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002025
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002026 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002027 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002028 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002029 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002030 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002031 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002032
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002033out_byspi:
2034 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002035out_bysrc:
2036 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002037out_bydst:
2038 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002039}
2040
2041void xfrm_state_fini(struct net *net)
2042{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002043 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002044 unsigned int sz;
2045
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002046 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002047 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002048 audit_info.sessionid = -1;
2049 audit_info.secid = 0;
2050 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2051 flush_work(&net->xfrm.state_gc_work);
2052
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002053 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002054
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002055 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002056 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2057 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002058 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2059 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002060 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2061 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Joy Lattenab5f5e82007-09-17 11:51:22 -07002064#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002065static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2066 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002067{
Paul Moore68277ac2007-12-20 20:49:33 -08002068 struct xfrm_sec_ctx *ctx = x->security;
2069 u32 spi = ntohl(x->id.spi);
2070
2071 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002072 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002073 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002074
2075 switch(x->props.family) {
2076 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002077 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2078 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002079 break;
2080 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002081 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002082 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002083 break;
2084 }
Paul Moore68277ac2007-12-20 20:49:33 -08002085
2086 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002087}
2088
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002089static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2090 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002091{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002092 const struct iphdr *iph4;
2093 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002094
2095 switch (family) {
2096 case AF_INET:
2097 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002098 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2099 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002100 break;
2101 case AF_INET6:
2102 iph6 = ipv6_hdr(skb);
2103 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002104 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002105 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002106 iph6->flow_lbl[0] & 0x0f,
2107 iph6->flow_lbl[1],
2108 iph6->flow_lbl[2]);
2109 break;
2110 }
2111}
2112
Paul Moore68277ac2007-12-20 20:49:33 -08002113void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002114 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002115{
2116 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002117
Paul Mooreafeb14b2007-12-21 14:58:11 -08002118 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002119 if (audit_buf == NULL)
2120 return;
Eric Paris25323862008-04-18 10:09:25 -04002121 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002122 xfrm_audit_helper_sainfo(x, audit_buf);
2123 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002124 audit_log_end(audit_buf);
2125}
2126EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2127
Paul Moore68277ac2007-12-20 20:49:33 -08002128void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002129 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002130{
2131 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002132
Paul Mooreafeb14b2007-12-21 14:58:11 -08002133 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002134 if (audit_buf == NULL)
2135 return;
Eric Paris25323862008-04-18 10:09:25 -04002136 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002137 xfrm_audit_helper_sainfo(x, audit_buf);
2138 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002139 audit_log_end(audit_buf);
2140}
2141EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002142
2143void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2144 struct sk_buff *skb)
2145{
2146 struct audit_buffer *audit_buf;
2147 u32 spi;
2148
2149 audit_buf = xfrm_audit_start("SA-replay-overflow");
2150 if (audit_buf == NULL)
2151 return;
2152 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2153 /* don't record the sequence number because it's inherent in this kind
2154 * of audit message */
2155 spi = ntohl(x->id.spi);
2156 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2157 audit_log_end(audit_buf);
2158}
2159EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2160
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002161void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002162 struct sk_buff *skb, __be32 net_seq)
2163{
2164 struct audit_buffer *audit_buf;
2165 u32 spi;
2166
2167 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2168 if (audit_buf == NULL)
2169 return;
2170 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2171 spi = ntohl(x->id.spi);
2172 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2173 spi, spi, ntohl(net_seq));
2174 audit_log_end(audit_buf);
2175}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002176EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002177
2178void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2179{
2180 struct audit_buffer *audit_buf;
2181
2182 audit_buf = xfrm_audit_start("SA-notfound");
2183 if (audit_buf == NULL)
2184 return;
2185 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2186 audit_log_end(audit_buf);
2187}
2188EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2189
2190void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2191 __be32 net_spi, __be32 net_seq)
2192{
2193 struct audit_buffer *audit_buf;
2194 u32 spi;
2195
2196 audit_buf = xfrm_audit_start("SA-notfound");
2197 if (audit_buf == NULL)
2198 return;
2199 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2200 spi = ntohl(net_spi);
2201 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2202 spi, spi, ntohl(net_seq));
2203 audit_log_end(audit_buf);
2204}
2205EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2206
2207void xfrm_audit_state_icvfail(struct xfrm_state *x,
2208 struct sk_buff *skb, u8 proto)
2209{
2210 struct audit_buffer *audit_buf;
2211 __be32 net_spi;
2212 __be32 net_seq;
2213
2214 audit_buf = xfrm_audit_start("SA-icv-failure");
2215 if (audit_buf == NULL)
2216 return;
2217 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2218 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2219 u32 spi = ntohl(net_spi);
2220 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2221 spi, spi, ntohl(net_seq));
2222 }
2223 audit_log_end(audit_buf);
2224}
2225EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002226#endif /* CONFIG_AUDITSYSCALL */