blob: b9c3f9e943a9159d1617feec49c751055ea4dd55 [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
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080042static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050043 const xfrm_address_t *daddr,
44 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070045 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070046 unsigned short family)
47{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080048 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070049}
50
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080051static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050052 const xfrm_address_t *daddr,
53 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070054 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070055{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080056 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070057}
58
David S. Miller2575b652006-08-24 03:26:44 -070059static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050060xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
61 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070062{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080063 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070064}
65
David S. Millerf034b5d2006-08-24 03:08:07 -070066static void xfrm_hash_transfer(struct hlist_head *list,
67 struct hlist_head *ndsttable,
68 struct hlist_head *nsrctable,
69 struct hlist_head *nspitable,
70 unsigned int nhashmask)
71{
Sasha Levinb67bfe02013-02-27 17:06:00 -080072 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070073 struct xfrm_state *x;
74
Sasha Levinb67bfe02013-02-27 17:06:00 -080075 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070076 unsigned int h;
77
David S. Millerc1969f22006-08-24 04:00:03 -070078 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
79 x->props.reqid, x->props.family,
80 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070081 hlist_add_head(&x->bydst, ndsttable+h);
82
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070083 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
84 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070085 nhashmask);
86 hlist_add_head(&x->bysrc, nsrctable+h);
87
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070088 if (x->id.spi) {
89 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
90 x->id.proto, x->props.family,
91 nhashmask);
92 hlist_add_head(&x->byspi, nspitable+h);
93 }
David S. Millerf034b5d2006-08-24 03:08:07 -070094 }
95}
96
Alexey Dobriyan63082732008-11-25 17:19:07 -080097static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -070098{
Alexey Dobriyan63082732008-11-25 17:19:07 -080099 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700100}
101
102static DEFINE_MUTEX(hash_resize_mutex);
103
Alexey Dobriyan63082732008-11-25 17:19:07 -0800104static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700105{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800106 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700107 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
108 unsigned long nsize, osize;
109 unsigned int nhashmask, ohashmask;
110 int i;
111
112 mutex_lock(&hash_resize_mutex);
113
Alexey Dobriyan63082732008-11-25 17:19:07 -0800114 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700115 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700116 if (!ndst)
117 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700118 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700120 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700121 goto out_unlock;
122 }
David S. Miller44e36b42006-08-24 04:50:50 -0700123 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700124 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700125 xfrm_hash_free(ndst, nsize);
126 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 goto out_unlock;
128 }
129
130 spin_lock_bh(&xfrm_state_lock);
131
132 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800133 for (i = net->xfrm.state_hmask; i >= 0; i--)
134 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700135 nhashmask);
136
Alexey Dobriyan63082732008-11-25 17:19:07 -0800137 odst = net->xfrm.state_bydst;
138 osrc = net->xfrm.state_bysrc;
139 ospi = net->xfrm.state_byspi;
140 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700141
Alexey Dobriyan63082732008-11-25 17:19:07 -0800142 net->xfrm.state_bydst = ndst;
143 net->xfrm.state_bysrc = nsrc;
144 net->xfrm.state_byspi = nspi;
145 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700146
147 spin_unlock_bh(&xfrm_state_lock);
148
149 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700150 xfrm_hash_free(odst, osize);
151 xfrm_hash_free(osrc, osize);
152 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700153
154out_unlock:
155 mutex_unlock(&hash_resize_mutex);
156}
157
Cong Wang44abdc32013-01-16 16:05:05 +0800158static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
159static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static DEFINE_SPINLOCK(xfrm_state_gc_lock);
162
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800163int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800165int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000166void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Cong Wang7a9885b2013-01-17 16:34:11 +0800168static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800169int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700170{
Cong Wang7a9885b2013-01-17 16:34:11 +0800171 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800172 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700173 int err = 0;
174
175 if (unlikely(afinfo == NULL))
176 return -EAFNOSUPPORT;
177 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800178 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700179
180 if (likely(typemap[type->proto] == NULL))
181 typemap[type->proto] = type;
182 else
183 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800184 spin_unlock_bh(&xfrm_type_lock);
185 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700186 return err;
187}
188EXPORT_SYMBOL(xfrm_register_type);
189
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800190int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700191{
Cong Wang7a9885b2013-01-17 16:34:11 +0800192 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800193 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700194 int err = 0;
195
196 if (unlikely(afinfo == NULL))
197 return -EAFNOSUPPORT;
198 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800199 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700200
201 if (unlikely(typemap[type->proto] != type))
202 err = -ENOENT;
203 else
204 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800205 spin_unlock_bh(&xfrm_type_lock);
206 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700207 return err;
208}
209EXPORT_SYMBOL(xfrm_unregister_type);
210
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800211static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700212{
213 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800214 const struct xfrm_type **typemap;
215 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700216 int modload_attempted = 0;
217
218retry:
219 afinfo = xfrm_state_get_afinfo(family);
220 if (unlikely(afinfo == NULL))
221 return NULL;
222 typemap = afinfo->type_map;
223
224 type = typemap[proto];
225 if (unlikely(type && !try_module_get(type->owner)))
226 type = NULL;
227 if (!type && !modload_attempted) {
228 xfrm_state_put_afinfo(afinfo);
229 request_module("xfrm-type-%d-%d", family, proto);
230 modload_attempted = 1;
231 goto retry;
232 }
233
234 xfrm_state_put_afinfo(afinfo);
235 return type;
236}
237
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800238static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700239{
240 module_put(type->owner);
241}
242
Cong Wang7a9885b2013-01-17 16:34:11 +0800243static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700244int xfrm_register_mode(struct xfrm_mode *mode, int family)
245{
246 struct xfrm_state_afinfo *afinfo;
247 struct xfrm_mode **modemap;
248 int err;
249
250 if (unlikely(mode->encap >= XFRM_MODE_MAX))
251 return -EINVAL;
252
Cong Wang7a9885b2013-01-17 16:34:11 +0800253 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700254 if (unlikely(afinfo == NULL))
255 return -EAFNOSUPPORT;
256
257 err = -EEXIST;
258 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800259 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700260 if (modemap[mode->encap])
261 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700262
Herbert Xu17c2a422007-10-17 21:33:12 -0700263 err = -ENOENT;
264 if (!try_module_get(afinfo->owner))
265 goto out;
266
267 mode->afinfo = afinfo;
268 modemap[mode->encap] = mode;
269 err = 0;
270
271out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800272 spin_unlock_bh(&xfrm_mode_lock);
273 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700274 return err;
275}
276EXPORT_SYMBOL(xfrm_register_mode);
277
278int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
279{
280 struct xfrm_state_afinfo *afinfo;
281 struct xfrm_mode **modemap;
282 int err;
283
284 if (unlikely(mode->encap >= XFRM_MODE_MAX))
285 return -EINVAL;
286
Cong Wang7a9885b2013-01-17 16:34:11 +0800287 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700288 if (unlikely(afinfo == NULL))
289 return -EAFNOSUPPORT;
290
291 err = -ENOENT;
292 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800293 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700294 if (likely(modemap[mode->encap] == mode)) {
295 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700296 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700297 err = 0;
298 }
299
Cong Wang7a9885b2013-01-17 16:34:11 +0800300 spin_unlock_bh(&xfrm_mode_lock);
301 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700302 return err;
303}
304EXPORT_SYMBOL(xfrm_unregister_mode);
305
306static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
307{
308 struct xfrm_state_afinfo *afinfo;
309 struct xfrm_mode *mode;
310 int modload_attempted = 0;
311
312 if (unlikely(encap >= XFRM_MODE_MAX))
313 return NULL;
314
315retry:
316 afinfo = xfrm_state_get_afinfo(family);
317 if (unlikely(afinfo == NULL))
318 return NULL;
319
320 mode = afinfo->mode_map[encap];
321 if (unlikely(mode && !try_module_get(mode->owner)))
322 mode = NULL;
323 if (!mode && !modload_attempted) {
324 xfrm_state_put_afinfo(afinfo);
325 request_module("xfrm-mode-%d-%d", family, encap);
326 modload_attempted = 1;
327 goto retry;
328 }
329
330 xfrm_state_put_afinfo(afinfo);
331 return mode;
332}
333
334static void xfrm_put_mode(struct xfrm_mode *mode)
335{
336 module_put(mode->owner);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static void xfrm_state_gc_destroy(struct xfrm_state *x)
340{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800341 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700342 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800343 kfree(x->aalg);
344 kfree(x->ealg);
345 kfree(x->calg);
346 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700347 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000348 kfree(x->replay_esn);
349 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700350 if (x->inner_mode)
351 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700352 if (x->inner_mode_iaf)
353 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700354 if (x->outer_mode)
355 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (x->type) {
357 x->type->destructor(x);
358 xfrm_put_type(x->type);
359 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800360 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 kfree(x);
362}
363
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800364static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800366 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700367 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800368 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700369 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800372 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_unlock_bh(&xfrm_state_gc_lock);
374
Sasha Levinb67bfe02013-02-27 17:06:00 -0800375 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700377
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800378 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381static inline unsigned long make_jiffies(long secs)
382{
383 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
384 return MAX_SCHEDULE_TIMEOUT-1;
385 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900386 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800389static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800391 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
392 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800393 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800394 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 long next = LONG_MAX;
396 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600397 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 spin_lock(&x->lock);
400 if (x->km.state == XFRM_STATE_DEAD)
401 goto out;
402 if (x->km.state == XFRM_STATE_EXPIRED)
403 goto expired;
404 if (x->lft.hard_add_expires_seconds) {
405 long tmo = x->lft.hard_add_expires_seconds +
406 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000407 if (tmo <= 0) {
408 if (x->xflags & XFRM_SOFT_EXPIRE) {
409 /* enter hard expire without soft expire first?!
410 * setting a new date could trigger this.
411 * workarbound: fix x->curflt.add_time by below:
412 */
413 x->curlft.add_time = now - x->saved_tmo - 1;
414 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
415 } else
416 goto expired;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (tmo < next)
419 next = tmo;
420 }
421 if (x->lft.hard_use_expires_seconds) {
422 long tmo = x->lft.hard_use_expires_seconds +
423 (x->curlft.use_time ? : now) - now;
424 if (tmo <= 0)
425 goto expired;
426 if (tmo < next)
427 next = tmo;
428 }
429 if (x->km.dying)
430 goto resched;
431 if (x->lft.soft_add_expires_seconds) {
432 long tmo = x->lft.soft_add_expires_seconds +
433 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000434 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000436 x->xflags &= ~XFRM_SOFT_EXPIRE;
437 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000439 x->xflags |= XFRM_SOFT_EXPIRE;
440 x->saved_tmo = tmo;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 if (x->lft.soft_use_expires_seconds) {
444 long tmo = x->lft.soft_use_expires_seconds +
445 (x->curlft.use_time ? : now) - now;
446 if (tmo <= 0)
447 warn = 1;
448 else if (tmo < next)
449 next = tmo;
450 }
451
Herbert Xu4666faa2005-06-18 22:43:22 -0700452 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800454 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800456 if (next != LONG_MAX){
457 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
458 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 goto out;
461
462expired:
463 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
464 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800465 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 next = 2;
467 goto resched;
468 }
Joy Latten161a09e2006-11-27 13:11:54 -0600469
470 err = __xfrm_state_delete(x);
471 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800472 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Joy Lattenab5f5e82007-09-17 11:51:22 -0700474 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400475 audit_get_loginuid(current),
476 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478out:
479 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800480 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
David S. Miller0ac84752006-03-20 19:18:23 -0800483static void xfrm_replay_timer_handler(unsigned long data);
484
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800485struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct xfrm_state *x;
488
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700489 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800492 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 atomic_set(&x->refcnt, 1);
494 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700495 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700496 INIT_HLIST_NODE(&x->bydst);
497 INIT_HLIST_NODE(&x->bysrc);
498 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800499 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
500 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800501 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
502 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800503 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 x->lft.soft_byte_limit = XFRM_INF;
505 x->lft.soft_packet_limit = XFRM_INF;
506 x->lft.hard_byte_limit = XFRM_INF;
507 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800508 x->replay_maxage = 0;
509 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700510 x->inner_mode = NULL;
511 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 spin_lock_init(&x->lock);
513 }
514 return x;
515}
516EXPORT_SYMBOL(xfrm_state_alloc);
517
518void __xfrm_state_destroy(struct xfrm_state *x)
519{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800520 struct net *net = xs_net(x);
521
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700522 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800525 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800527 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529EXPORT_SYMBOL(__xfrm_state_destroy);
530
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800531int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800533 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700534 int err = -ESRCH;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (x->km.state != XFRM_STATE_DEAD) {
537 x->km.state = XFRM_STATE_DEAD;
538 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700539 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700540 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700541 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700542 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700543 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800544 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* All xfrm_state objects are created by xfrm_state_alloc.
548 * The xfrm_state_alloc call gives a reference, and that
549 * is what we are dropping here.
550 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800551 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700552 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554
555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800557EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700559int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700561 int err;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700564 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700566
567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569EXPORT_SYMBOL(xfrm_state_delete);
570
Joy Latten4aa2e622007-06-04 19:05:57 -0400571#ifdef CONFIG_SECURITY_NETWORK_XFRM
572static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800573xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Joy Latten4aa2e622007-06-04 19:05:57 -0400575 int i, err = 0;
576
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800577 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400578 struct xfrm_state *x;
579
Sasha Levinb67bfe02013-02-27 17:06:00 -0800580 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400581 if (xfrm_id_proto_match(x->id.proto, proto) &&
582 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700583 xfrm_audit_state_delete(x, 0,
584 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400585 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700586 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400587 return err;
588 }
589 }
590 }
591
592 return err;
593}
594#else
595static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800596xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400597{
598 return 0;
599}
600#endif
601
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800602int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400603{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000604 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800607 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400608 if (err)
609 goto out;
610
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000611 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800612 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700613 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800615 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700617 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 xfrm_state_hold(x);
619 spin_unlock_bh(&xfrm_state_lock);
620
Joy Latten161a09e2006-11-27 13:11:54 -0600621 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700622 xfrm_audit_state_delete(x, err ? 0 : 1,
623 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400624 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700625 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000627 if (!err)
628 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 spin_lock_bh(&xfrm_state_lock);
631 goto restart;
632 }
633 }
634 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000635 if (cnt)
636 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400637
638out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800640 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643EXPORT_SYMBOL(xfrm_state_flush);
644
Alexey Dobriyane0710412010-01-23 13:37:10 +0000645void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700646{
647 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000648 si->sadcnt = net->xfrm.state_num;
649 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700650 si->sadhmcnt = xfrm_state_hashmax;
651 spin_unlock_bh(&xfrm_state_lock);
652}
653EXPORT_SYMBOL(xfrm_sad_getinfo);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static int
David S. Miller1a898592011-02-22 18:22:34 -0800656xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500657 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500658 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700659 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
662 if (!afinfo)
663 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700664 afinfo->init_tempsel(&x->sel, fl);
665
666 if (family != tmpl->encap_family) {
667 xfrm_state_put_afinfo(afinfo);
668 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
669 if (!afinfo)
670 return -1;
671 }
672 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 xfrm_state_put_afinfo(afinfo);
674 return 0;
675}
676
David S. Miller9aa60082011-02-24 01:51:36 -0500677static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
678 const xfrm_address_t *daddr,
679 __be32 spi, u8 proto,
680 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700681{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800682 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700683 struct xfrm_state *x;
684
Sasha Levinb67bfe02013-02-27 17:06:00 -0800685 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700686 if (x->props.family != family ||
687 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000688 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000689 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700690 continue;
691
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000692 if ((mark & x->mark.m) != x->mark.v)
693 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700694 xfrm_state_hold(x);
695 return x;
696 }
697
698 return NULL;
699}
700
David S. Miller9aa60082011-02-24 01:51:36 -0500701static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
702 const xfrm_address_t *daddr,
703 const xfrm_address_t *saddr,
704 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700705{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800706 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700707 struct xfrm_state *x;
708
Sasha Levinb67bfe02013-02-27 17:06:00 -0800709 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700710 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000711 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000712 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
713 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700714 continue;
715
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000716 if ((mark & x->mark.m) != x->mark.v)
717 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700718 xfrm_state_hold(x);
719 return x;
720 }
721
722 return NULL;
723}
724
725static inline struct xfrm_state *
726__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
727{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800728 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800729 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800730
David S. Milleredcd5822006-08-24 00:42:45 -0700731 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800732 return __xfrm_state_lookup(net, mark, &x->id.daddr,
733 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700734 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800735 return __xfrm_state_lookup_byaddr(net, mark,
736 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700737 &x->props.saddr,
738 x->id.proto, family);
739}
740
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800741static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700742{
743 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800744 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
745 net->xfrm.state_num > net->xfrm.state_hmask)
746 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700747}
748
David S. Miller08ec9af2009-03-13 14:22:40 -0700749static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800750 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700751 struct xfrm_state **best, int *acq_in_progress,
752 int *error)
753{
754 /* Resolution logic:
755 * 1. There is a valid state with matching selector. Done.
756 * 2. Valid state with inappropriate selector. Skip.
757 *
758 * Entering area of "sysdeps".
759 *
760 * 3. If state is not valid, selector is temporary, it selects
761 * only session which triggered previous resolution. Key
762 * manager will do something to install a state with proper
763 * selector.
764 */
765 if (x->km.state == XFRM_STATE_VALID) {
766 if ((x->sel.family &&
767 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
768 !security_xfrm_state_pol_flow_match(x, pol, fl))
769 return;
770
771 if (!*best ||
772 (*best)->km.dying > x->km.dying ||
773 ((*best)->km.dying == x->km.dying &&
774 (*best)->curlft.add_time < x->curlft.add_time))
775 *best = x;
776 } else if (x->km.state == XFRM_STATE_ACQ) {
777 *acq_in_progress = 1;
778 } else if (x->km.state == XFRM_STATE_ERROR ||
779 x->km.state == XFRM_STATE_EXPIRED) {
780 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
781 security_xfrm_state_pol_flow_match(x, pol, fl))
782 *error = -ESRCH;
783 }
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500787xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800788 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct xfrm_policy *pol, int *err,
790 unsigned short family)
791{
David S. Miller08ec9af2009-03-13 14:22:40 -0700792 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800793 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700794 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700795 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int acquire_in_progress = 0;
797 int error = 0;
798 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800799 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700800 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900801
David S. Miller37b08e32008-09-02 20:14:15 -0700802 to_put = NULL;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700805 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800806 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700807 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000809 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700810 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700811 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 tmpl->mode == x->props.mode &&
813 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700814 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500815 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700816 &best, &acquire_in_progress, &error);
817 }
818 if (best)
819 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Thomas Egerer8444cf72010-09-20 11:11:38 -0700821 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800822 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700823 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700824 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000825 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700826 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700827 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700828 tmpl->mode == x->props.mode &&
829 tmpl->id.proto == x->id.proto &&
830 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500831 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700832 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
David S. Miller08ec9af2009-03-13 14:22:40 -0700835found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 x = best;
837 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700838 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800839 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700840 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700841 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 error = -EEXIST;
843 goto out;
844 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800845 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (x == NULL) {
847 error = -ENOMEM;
848 goto out;
849 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700850 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700852 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800853 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
David S. Miller1d28f422011-03-12 00:29:39 -0500855 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700856 if (error) {
857 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700858 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700859 x = NULL;
860 goto out;
861 }
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (km_query(x, tmpl, pol) == 0) {
864 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800865 list_add(&x->km.all, &net->xfrm.state_all);
866 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700867 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800868 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700870 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800871 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800873 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800874 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800875 net->xfrm.state_num++;
876 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 } else {
878 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700879 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 x = NULL;
881 error = -ESRCH;
882 }
883 }
884out:
885 if (x)
886 xfrm_state_hold(x);
887 else
888 *err = acquire_in_progress ? -EAGAIN : error;
889 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700890 if (to_put)
891 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return x;
893}
894
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700895struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800896xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800897 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700898 unsigned short family, u8 mode, u8 proto, u32 reqid)
899{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800900 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700901 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700902
903 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800904 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800905 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700906 if (x->props.family == family &&
907 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000908 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909 !(x->props.flags & XFRM_STATE_WILDRECV) &&
910 xfrm_state_addr_check(x, daddr, saddr, family) &&
911 mode == x->props.mode &&
912 proto == x->id.proto &&
913 x->km.state == XFRM_STATE_VALID) {
914 rx = x;
915 break;
916 }
917 }
918
919 if (rx)
920 xfrm_state_hold(rx);
921 spin_unlock(&xfrm_state_lock);
922
923
924 return rx;
925}
926EXPORT_SYMBOL(xfrm_stateonly_find);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928static void __xfrm_state_insert(struct xfrm_state *x)
929{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800930 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700931 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800933 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800934
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800935 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700936 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800937 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800939 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
940 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700942 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800943 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700944 x->props.family);
945
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800946 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700947 }
948
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800949 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700950 if (x->replay_maxage)
951 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800952
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800953 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700954
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700956
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800957 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
David S. Millerc7f5ea32006-08-24 03:29:04 -0700960/* xfrm_state_lock is held */
961static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
962{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800963 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700964 unsigned short family = xnew->props.family;
965 u32 reqid = xnew->props.reqid;
966 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700967 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000968 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700969
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800970 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800971 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700972 if (x->props.family == family &&
973 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000974 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000975 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
976 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000977 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700978 }
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981void xfrm_state_insert(struct xfrm_state *x)
982{
983 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700984 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 __xfrm_state_insert(x);
986 spin_unlock_bh(&xfrm_state_lock);
987}
988EXPORT_SYMBOL(xfrm_state_insert);
989
David S. Miller27708342006-08-24 00:13:10 -0700990/* xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +0200991static struct xfrm_state *__find_acq_core(struct net *net,
992 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -0800993 unsigned short family, u8 mode,
994 u32 reqid, u8 proto,
995 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +0200996 const xfrm_address_t *saddr,
997 int create)
David S. Miller27708342006-08-24 00:13:10 -0700998{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800999 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001000 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001001 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001002
Sasha Levinb67bfe02013-02-27 17:06:00 -08001003 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001004 if (x->props.reqid != reqid ||
1005 x->props.mode != mode ||
1006 x->props.family != family ||
1007 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001008 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001009 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001010 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001011 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1012 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001013 continue;
1014
David S. Miller27708342006-08-24 00:13:10 -07001015 xfrm_state_hold(x);
1016 return x;
1017 }
1018
1019 if (!create)
1020 return NULL;
1021
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001022 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001023 if (likely(x)) {
1024 switch (family) {
1025 case AF_INET:
1026 x->sel.daddr.a4 = daddr->a4;
1027 x->sel.saddr.a4 = saddr->a4;
1028 x->sel.prefixlen_d = 32;
1029 x->sel.prefixlen_s = 32;
1030 x->props.saddr.a4 = saddr->a4;
1031 x->id.daddr.a4 = daddr->a4;
1032 break;
1033
1034 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001035 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1036 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001037 x->sel.prefixlen_d = 128;
1038 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001039 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1040 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001041 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001042 }
David S. Miller27708342006-08-24 00:13:10 -07001043
1044 x->km.state = XFRM_STATE_ACQ;
1045 x->id.proto = proto;
1046 x->props.family = family;
1047 x->props.mode = mode;
1048 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001049 x->mark.v = m->v;
1050 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001051 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001052 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001053 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001054 list_add(&x->km.all, &net->xfrm.state_all);
1055 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1056 h = xfrm_src_hash(net, daddr, saddr, family);
1057 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001058
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001059 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001060
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001061 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001062 }
1063
1064 return x;
1065}
1066
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001067static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069int xfrm_state_add(struct xfrm_state *x)
1070{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001071 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001072 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 int family;
1074 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001075 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001076 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David S. Miller37b08e32008-09-02 20:14:15 -07001080 to_put = NULL;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 spin_lock_bh(&xfrm_state_lock);
1083
David S. Milleredcd5822006-08-24 00:42:45 -07001084 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001086 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 x1 = NULL;
1088 err = -EEXIST;
1089 goto out;
1090 }
1091
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001092 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001093 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001094 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001095 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001096 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 x1 = NULL;
1098 }
1099 }
1100
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001101 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001102 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1103 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001104 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
David S. Millerc7f5ea32006-08-24 03:29:04 -07001106 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 __xfrm_state_insert(x);
1108 err = 0;
1109
1110out:
1111 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 if (x1) {
1114 xfrm_state_delete(x1);
1115 xfrm_state_put(x1);
1116 }
1117
David S. Miller37b08e32008-09-02 20:14:15 -07001118 if (to_put)
1119 xfrm_state_put(to_put);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return err;
1122}
1123EXPORT_SYMBOL(xfrm_state_add);
1124
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001125#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001126static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001127{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001128 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001129 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001130 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001131 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001132 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001133
1134 memcpy(&x->id, &orig->id, sizeof(x->id));
1135 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1136 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1137 x->props.mode = orig->props.mode;
1138 x->props.replay_window = orig->props.replay_window;
1139 x->props.reqid = orig->props.reqid;
1140 x->props.family = orig->props.family;
1141 x->props.saddr = orig->props.saddr;
1142
1143 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001144 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001145 if (!x->aalg)
1146 goto error;
1147 }
1148 x->props.aalgo = orig->props.aalgo;
1149
1150 if (orig->ealg) {
1151 x->ealg = xfrm_algo_clone(orig->ealg);
1152 if (!x->ealg)
1153 goto error;
1154 }
1155 x->props.ealgo = orig->props.ealgo;
1156
1157 if (orig->calg) {
1158 x->calg = xfrm_algo_clone(orig->calg);
1159 if (!x->calg)
1160 goto error;
1161 }
1162 x->props.calgo = orig->props.calgo;
1163
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001164 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001165 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1166 if (!x->encap)
1167 goto error;
1168 }
1169
1170 if (orig->coaddr) {
1171 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1172 GFP_KERNEL);
1173 if (!x->coaddr)
1174 goto error;
1175 }
1176
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001177 if (orig->replay_esn) {
1178 err = xfrm_replay_clone(x, orig);
1179 if (err)
1180 goto error;
1181 }
1182
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001183 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1184
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001185 err = xfrm_init_state(x);
1186 if (err)
1187 goto error;
1188
1189 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001190 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001191
1192 x->curlft.add_time = orig->curlft.add_time;
1193 x->km.state = orig->km.state;
1194 x->km.seq = orig->km.seq;
1195
1196 return x;
1197
1198 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001199 xfrm_state_put(x);
1200out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001201 if (errp)
1202 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001203 return NULL;
1204}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001205
1206/* xfrm_state_lock is held */
1207struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1208{
1209 unsigned int h;
1210 struct xfrm_state *x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001211
1212 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001213 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001214 m->reqid, m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001215 hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001216 if (x->props.mode != m->mode ||
1217 x->id.proto != m->proto)
1218 continue;
1219 if (m->reqid && x->props.reqid != m->reqid)
1220 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001221 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1222 m->old_family) ||
1223 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1224 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001225 continue;
1226 xfrm_state_hold(x);
1227 return x;
1228 }
1229 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001230 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001231 m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001232 hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001233 if (x->props.mode != m->mode ||
1234 x->id.proto != m->proto)
1235 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001236 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1237 m->old_family) ||
1238 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1239 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001240 continue;
1241 xfrm_state_hold(x);
1242 return x;
1243 }
1244 }
1245
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001246 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001247}
1248EXPORT_SYMBOL(xfrm_migrate_state_find);
1249
1250struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1251 struct xfrm_migrate *m)
1252{
1253 struct xfrm_state *xc;
1254 int err;
1255
1256 xc = xfrm_state_clone(x, &err);
1257 if (!xc)
1258 return NULL;
1259
1260 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1261 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1262
1263 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001264 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001265 /* a care is needed when the destination address of the
1266 state is to be updated as it is a part of triplet */
1267 xfrm_state_insert(xc);
1268 } else {
1269 if ((err = xfrm_state_add(xc)) < 0)
1270 goto error;
1271 }
1272
1273 return xc;
1274error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001275 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001276 return NULL;
1277}
1278EXPORT_SYMBOL(xfrm_state_migrate);
1279#endif
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281int xfrm_state_update(struct xfrm_state *x)
1282{
David S. Miller37b08e32008-09-02 20:14:15 -07001283 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001285 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
David S. Miller37b08e32008-09-02 20:14:15 -07001287 to_put = NULL;
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001290 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 err = -ESRCH;
1293 if (!x1)
1294 goto out;
1295
1296 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001297 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 err = -EEXIST;
1299 goto out;
1300 }
1301
1302 if (x1->km.state == XFRM_STATE_ACQ) {
1303 __xfrm_state_insert(x);
1304 x = NULL;
1305 }
1306 err = 0;
1307
1308out:
1309 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
David S. Miller37b08e32008-09-02 20:14:15 -07001311 if (to_put)
1312 xfrm_state_put(to_put);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (err)
1315 return err;
1316
1317 if (!x) {
1318 xfrm_state_delete(x1);
1319 xfrm_state_put(x1);
1320 return 0;
1321 }
1322
1323 err = -EINVAL;
1324 spin_lock_bh(&x1->lock);
1325 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1326 if (x->encap && x1->encap)
1327 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001328 if (x->coaddr && x1->coaddr) {
1329 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1330 }
1331 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1332 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1334 x1->km.dying = 0;
1335
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001336 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (x1->curlft.use_time)
1338 xfrm_state_check_expire(x1);
1339
1340 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001341 x->km.state = XFRM_STATE_DEAD;
1342 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 spin_unlock_bh(&x1->lock);
1345
1346 xfrm_state_put(x1);
1347
1348 return err;
1349}
1350EXPORT_SYMBOL(xfrm_state_update);
1351
1352int xfrm_state_check_expire(struct xfrm_state *x)
1353{
1354 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001355 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1358 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001359 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001360 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EINVAL;
1362 }
1363
1364 if (!x->km.dying &&
1365 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001366 x->curlft.packets >= x->lft.soft_packet_limit)) {
1367 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001368 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return 0;
1371}
1372EXPORT_SYMBOL(xfrm_state_check_expire);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001375xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001376 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001381 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return x;
1384}
1385EXPORT_SYMBOL(xfrm_state_lookup);
1386
1387struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001388xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001389 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001390 u8 proto, unsigned short family)
1391{
1392 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001393
1394 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001395 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001396 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001397 return x;
1398}
1399EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1400
1401struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001402xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1403 u8 proto, const xfrm_address_t *daddr,
1404 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001409 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return x;
1413}
1414EXPORT_SYMBOL(xfrm_find_acq);
1415
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001416#ifdef CONFIG_XFRM_SUB_POLICY
1417int
1418xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1419 unsigned short family)
1420{
1421 int err = 0;
1422 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1423 if (!afinfo)
1424 return -EAFNOSUPPORT;
1425
1426 spin_lock_bh(&xfrm_state_lock);
1427 if (afinfo->tmpl_sort)
1428 err = afinfo->tmpl_sort(dst, src, n);
1429 spin_unlock_bh(&xfrm_state_lock);
1430 xfrm_state_put_afinfo(afinfo);
1431 return err;
1432}
1433EXPORT_SYMBOL(xfrm_tmpl_sort);
1434
1435int
1436xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1437 unsigned short family)
1438{
1439 int err = 0;
1440 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1441 if (!afinfo)
1442 return -EAFNOSUPPORT;
1443
1444 spin_lock_bh(&xfrm_state_lock);
1445 if (afinfo->state_sort)
1446 err = afinfo->state_sort(dst, src, n);
1447 spin_unlock_bh(&xfrm_state_lock);
1448 xfrm_state_put_afinfo(afinfo);
1449 return err;
1450}
1451EXPORT_SYMBOL(xfrm_state_sort);
1452#endif
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/* Silly enough, but I'm lazy to build resolution list */
1455
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001456static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001460 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001461 struct xfrm_state *x;
1462
Sasha Levinb67bfe02013-02-27 17:06:00 -08001463 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001464 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001465 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001466 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 xfrm_state_hold(x);
1468 return x;
1469 }
1470 }
1471 }
1472 return NULL;
1473}
1474
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001475struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 struct xfrm_state *x;
1478
1479 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001480 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 spin_unlock_bh(&xfrm_state_lock);
1482 return x;
1483}
1484EXPORT_SYMBOL(xfrm_find_acq_byseq);
1485
1486u32 xfrm_get_acqseq(void)
1487{
1488 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001489 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
jamal6836b9b2010-02-16 02:01:22 +00001491 do {
1492 res = atomic_inc_return(&acqseq);
1493 } while (!res);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return res;
1496}
1497EXPORT_SYMBOL(xfrm_get_acqseq);
1498
Herbert Xu658b2192007-10-09 13:29:52 -07001499int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001501 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001502 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001504 int err = -ENOENT;
1505 __be32 minspi = htonl(low);
1506 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001507 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Herbert Xu658b2192007-10-09 13:29:52 -07001509 spin_lock_bh(&x->lock);
1510 if (x->km.state == XFRM_STATE_DEAD)
1511 goto unlock;
1512
1513 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001515 goto unlock;
1516
1517 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001520 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (x0) {
1522 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001523 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525 x->id.spi = minspi;
1526 } else {
1527 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001528 for (h=0; h<high-low+1; h++) {
1529 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001530 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (x0 == NULL) {
1532 x->id.spi = htonl(spi);
1533 break;
1534 }
1535 xfrm_state_put(x0);
1536 }
1537 }
1538 if (x->id.spi) {
1539 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001540 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1541 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001543
1544 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Herbert Xu658b2192007-10-09 13:29:52 -07001546
1547unlock:
1548 spin_unlock_bh(&x->lock);
1549
1550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552EXPORT_SYMBOL(xfrm_alloc_spi);
1553
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001554int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001555 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 void *data)
1557{
Herbert Xu12a169e2008-10-01 07:03:24 -07001558 struct xfrm_state *state;
1559 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int err = 0;
1561
Herbert Xu12a169e2008-10-01 07:03:24 -07001562 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001563 return 0;
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001566 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001567 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001568 else
1569 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001570 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001571 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001572 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001573 state = container_of(x, struct xfrm_state, km);
1574 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001575 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001576 err = func(state, walk->seq, data);
1577 if (err) {
1578 list_move_tail(&walk->all, &x->all);
1579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001581 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001583 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 err = -ENOENT;
1585 goto out;
1586 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001587 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588out:
1589 spin_unlock_bh(&xfrm_state_lock);
1590 return err;
1591}
1592EXPORT_SYMBOL(xfrm_state_walk);
1593
Herbert Xu5c182452008-09-22 19:48:19 -07001594void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1595{
Herbert Xu12a169e2008-10-01 07:03:24 -07001596 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001597 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001598 walk->state = XFRM_STATE_DEAD;
1599 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001600}
1601EXPORT_SYMBOL(xfrm_state_walk_init);
1602
Herbert Xuabb81c42008-09-09 19:58:29 -07001603void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1604{
Herbert Xu12a169e2008-10-01 07:03:24 -07001605 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001606 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001607
Herbert Xu12a169e2008-10-01 07:03:24 -07001608 spin_lock_bh(&xfrm_state_lock);
1609 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001610 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001611}
1612EXPORT_SYMBOL(xfrm_state_walk_done);
1613
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001614static void xfrm_replay_timer_handler(unsigned long data)
1615{
1616 struct xfrm_state *x = (struct xfrm_state*)data;
1617
1618 spin_lock(&x->lock);
1619
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001620 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001621 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001622 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001623 else
1624 x->xflags |= XFRM_TIME_DEFER;
1625 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001626
1627 spin_unlock(&x->lock);
1628}
1629
Denis Chengdf018122007-12-07 00:51:11 -08001630static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
David S. Miller214e0052011-02-24 00:02:38 -05001632void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct xfrm_mgr *km;
1635
Cong Wang85168c02013-01-16 16:05:06 +08001636 rcu_read_lock();
1637 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001638 if (km->notify_policy)
1639 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001640 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001641}
1642
David S. Miller214e0052011-02-24 00:02:38 -05001643void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001644{
1645 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001646 rcu_read_lock();
1647 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001648 if (km->notify)
1649 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001650 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001651}
1652
1653EXPORT_SYMBOL(km_policy_notify);
1654EXPORT_SYMBOL(km_state_notify);
1655
Eric W. Biederman15e47302012-09-07 20:12:54 +00001656void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001657{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001658 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001659 struct km_event c;
1660
Herbert Xubf088672005-06-18 22:44:00 -07001661 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001662 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001663 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001664 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001667 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001670EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001671/*
1672 * We send to all registered managers regardless of failure
1673 * We are happy with one success
1674*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001675int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001677 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct xfrm_mgr *km;
1679
Cong Wang85168c02013-01-16 16:05:06 +08001680 rcu_read_lock();
1681 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001682 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001683 if (!acqret)
1684 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Cong Wang85168c02013-01-16 16:05:06 +08001686 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return err;
1688}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001689EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Al Viro5d36b182006-11-08 00:24:06 -08001691int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 int err = -EINVAL;
1694 struct xfrm_mgr *km;
1695
Cong Wang85168c02013-01-16 16:05:06 +08001696 rcu_read_lock();
1697 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (km->new_mapping)
1699 err = km->new_mapping(x, ipaddr, sport);
1700 if (!err)
1701 break;
1702 }
Cong Wang85168c02013-01-16 16:05:06 +08001703 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return err;
1705}
1706EXPORT_SYMBOL(km_new_mapping);
1707
Eric W. Biederman15e47302012-09-07 20:12:54 +00001708void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001710 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001711 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Herbert Xubf088672005-06-18 22:44:00 -07001713 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001714 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001715 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001716 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001719 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
David S. Millera70fcb02006-03-20 19:18:52 -08001721EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001723#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001724int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1725 const struct xfrm_migrate *m, int num_migrate,
1726 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001727{
1728 int err = -EINVAL;
1729 int ret;
1730 struct xfrm_mgr *km;
1731
Cong Wang85168c02013-01-16 16:05:06 +08001732 rcu_read_lock();
1733 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001734 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001735 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001736 if (!ret)
1737 err = ret;
1738 }
1739 }
Cong Wang85168c02013-01-16 16:05:06 +08001740 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001741 return err;
1742}
1743EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001744#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001745
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001746int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001747{
1748 int err = -EINVAL;
1749 int ret;
1750 struct xfrm_mgr *km;
1751
Cong Wang85168c02013-01-16 16:05:06 +08001752 rcu_read_lock();
1753 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001754 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001755 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001756 if (!ret)
1757 err = ret;
1758 }
1759 }
Cong Wang85168c02013-01-16 16:05:06 +08001760 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001761 return err;
1762}
1763EXPORT_SYMBOL(km_report);
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1766{
1767 int err;
1768 u8 *data;
1769 struct xfrm_mgr *km;
1770 struct xfrm_policy *pol = NULL;
1771
1772 if (optlen <= 0 || optlen > PAGE_SIZE)
1773 return -EMSGSIZE;
1774
1775 data = kmalloc(optlen, GFP_KERNEL);
1776 if (!data)
1777 return -ENOMEM;
1778
1779 err = -EFAULT;
1780 if (copy_from_user(data, optval, optlen))
1781 goto out;
1782
1783 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001784 rcu_read_lock();
1785 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001786 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 optlen, &err);
1788 if (err >= 0)
1789 break;
1790 }
Cong Wang85168c02013-01-16 16:05:06 +08001791 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (err >= 0) {
1794 xfrm_sk_policy_insert(sk, err, pol);
1795 xfrm_pol_put(pol);
1796 err = 0;
1797 }
1798
1799out:
1800 kfree(data);
1801 return err;
1802}
1803EXPORT_SYMBOL(xfrm_user_policy);
1804
Cong Wang85168c02013-01-16 16:05:06 +08001805static DEFINE_SPINLOCK(xfrm_km_lock);
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807int xfrm_register_km(struct xfrm_mgr *km)
1808{
Cong Wang85168c02013-01-16 16:05:06 +08001809 spin_lock_bh(&xfrm_km_lock);
1810 list_add_tail_rcu(&km->list, &xfrm_km_list);
1811 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813}
1814EXPORT_SYMBOL(xfrm_register_km);
1815
1816int xfrm_unregister_km(struct xfrm_mgr *km)
1817{
Cong Wang85168c02013-01-16 16:05:06 +08001818 spin_lock_bh(&xfrm_km_lock);
1819 list_del_rcu(&km->list);
1820 spin_unlock_bh(&xfrm_km_lock);
1821 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return 0;
1823}
1824EXPORT_SYMBOL(xfrm_unregister_km);
1825
1826int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1827{
1828 int err = 0;
1829 if (unlikely(afinfo == NULL))
1830 return -EINVAL;
1831 if (unlikely(afinfo->family >= NPROTO))
1832 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001833 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1835 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001836 else
Cong Wang44abdc32013-01-16 16:05:05 +08001837 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1838 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return err;
1840}
1841EXPORT_SYMBOL(xfrm_state_register_afinfo);
1842
1843int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1844{
1845 int err = 0;
1846 if (unlikely(afinfo == NULL))
1847 return -EINVAL;
1848 if (unlikely(afinfo->family >= NPROTO))
1849 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001850 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1852 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1853 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001854 else
Cong Wang44abdc32013-01-16 16:05:05 +08001855 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
Cong Wang44abdc32013-01-16 16:05:05 +08001857 spin_unlock_bh(&xfrm_state_afinfo_lock);
1858 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return err;
1860}
1861EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1862
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001863struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 struct xfrm_state_afinfo *afinfo;
1866 if (unlikely(family >= NPROTO))
1867 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001868 rcu_read_lock();
1869 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001870 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001871 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return afinfo;
1873}
1874
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001875void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Cong Wang44abdc32013-01-16 16:05:05 +08001877 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
1880/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1881void xfrm_state_delete_tunnel(struct xfrm_state *x)
1882{
1883 if (x->tunnel) {
1884 struct xfrm_state *t = x->tunnel;
1885
1886 if (atomic_read(&t->tunnel_users) == 2)
1887 xfrm_state_delete(t);
1888 atomic_dec(&t->tunnel_users);
1889 xfrm_state_put(t);
1890 x->tunnel = NULL;
1891 }
1892}
1893EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1894
1895int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1896{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001897 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Patrick McHardyc5c25232007-04-09 11:47:18 -07001899 spin_lock_bh(&x->lock);
1900 if (x->km.state == XFRM_STATE_VALID &&
1901 x->type && x->type->get_mtu)
1902 res = x->type->get_mtu(x, mtu);
1903 else
Patrick McHardy28121612007-06-18 22:30:15 -07001904 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001905 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return res;
1907}
1908
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001909int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001910{
Herbert Xud094cd82005-06-20 13:19:41 -07001911 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001912 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001913 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001914 int err;
1915
Herbert Xud094cd82005-06-20 13:19:41 -07001916 err = -EAFNOSUPPORT;
1917 afinfo = xfrm_state_get_afinfo(family);
1918 if (!afinfo)
1919 goto error;
1920
1921 err = 0;
1922 if (afinfo->init_flags)
1923 err = afinfo->init_flags(x);
1924
1925 xfrm_state_put_afinfo(afinfo);
1926
1927 if (err)
1928 goto error;
1929
1930 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001931
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001932 if (x->sel.family != AF_UNSPEC) {
1933 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1934 if (inner_mode == NULL)
1935 goto error;
1936
1937 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1938 family != x->sel.family) {
1939 xfrm_put_mode(inner_mode);
1940 goto error;
1941 }
1942
1943 x->inner_mode = inner_mode;
1944 } else {
1945 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001946 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001947
Martin Willid81d2282008-12-03 15:38:07 -08001948 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001949 if (inner_mode == NULL)
1950 goto error;
1951
1952 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1953 xfrm_put_mode(inner_mode);
1954 goto error;
1955 }
Martin Willid81d2282008-12-03 15:38:07 -08001956 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001957
Martin Willid81d2282008-12-03 15:38:07 -08001958 if (x->props.family == AF_INET)
1959 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001960
Martin Willid81d2282008-12-03 15:38:07 -08001961 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1962 if (inner_mode_iaf) {
1963 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1964 x->inner_mode_iaf = inner_mode_iaf;
1965 else
1966 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001967 }
1968 }
Herbert Xu13996372007-10-17 21:35:51 -07001969
Herbert Xud094cd82005-06-20 13:19:41 -07001970 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001971 if (x->type == NULL)
1972 goto error;
1973
1974 err = x->type->init_state(x);
1975 if (err)
1976 goto error;
1977
Herbert Xu13996372007-10-17 21:35:51 -07001978 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00001979 if (x->outer_mode == NULL) {
1980 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07001981 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00001982 }
Herbert Xub59f45d2006-05-27 23:05:54 -07001983
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001984 if (init_replay) {
1985 err = xfrm_init_replay(x);
1986 if (err)
1987 goto error;
1988 }
1989
Herbert Xu72cb6962005-06-20 13:18:08 -07001990 x->km.state = XFRM_STATE_VALID;
1991
1992error:
1993 return err;
1994}
1995
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001996EXPORT_SYMBOL(__xfrm_init_state);
1997
1998int xfrm_init_state(struct xfrm_state *x)
1999{
2000 return __xfrm_init_state(x, true);
2001}
2002
Herbert Xu72cb6962005-06-20 13:18:08 -07002003EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002004
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002005int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
David S. Millerf034b5d2006-08-24 03:08:07 -07002007 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002009 INIT_LIST_HEAD(&net->xfrm.state_all);
2010
David S. Millerf034b5d2006-08-24 03:08:07 -07002011 sz = sizeof(struct hlist_head) * 8;
2012
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002013 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2014 if (!net->xfrm.state_bydst)
2015 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002016 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2017 if (!net->xfrm.state_bysrc)
2018 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002019 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2020 if (!net->xfrm.state_byspi)
2021 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002022 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002023
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002024 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002025 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002026 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002027 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002028 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002029 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002030
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002031out_byspi:
2032 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002033out_bysrc:
2034 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002035out_bydst:
2036 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002037}
2038
2039void xfrm_state_fini(struct net *net)
2040{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002041 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002042 unsigned int sz;
2043
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002044 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002045 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002046 audit_info.sessionid = -1;
2047 audit_info.secid = 0;
2048 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2049 flush_work(&net->xfrm.state_gc_work);
2050
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002051 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002052
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002053 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002054 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2055 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002056 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2057 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002058 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2059 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Joy Lattenab5f5e82007-09-17 11:51:22 -07002062#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002063static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2064 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002065{
Paul Moore68277ac2007-12-20 20:49:33 -08002066 struct xfrm_sec_ctx *ctx = x->security;
2067 u32 spi = ntohl(x->id.spi);
2068
2069 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002070 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002071 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002072
2073 switch(x->props.family) {
2074 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002075 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2076 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002077 break;
2078 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002079 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002080 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002081 break;
2082 }
Paul Moore68277ac2007-12-20 20:49:33 -08002083
2084 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002085}
2086
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002087static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2088 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002089{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002090 const struct iphdr *iph4;
2091 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002092
2093 switch (family) {
2094 case AF_INET:
2095 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002096 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2097 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002098 break;
2099 case AF_INET6:
2100 iph6 = ipv6_hdr(skb);
2101 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002102 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002103 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002104 iph6->flow_lbl[0] & 0x0f,
2105 iph6->flow_lbl[1],
2106 iph6->flow_lbl[2]);
2107 break;
2108 }
2109}
2110
Paul Moore68277ac2007-12-20 20:49:33 -08002111void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002112 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002113{
2114 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002115
Paul Mooreafeb14b2007-12-21 14:58:11 -08002116 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002117 if (audit_buf == NULL)
2118 return;
Eric Paris25323862008-04-18 10:09:25 -04002119 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002120 xfrm_audit_helper_sainfo(x, audit_buf);
2121 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002122 audit_log_end(audit_buf);
2123}
2124EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2125
Paul Moore68277ac2007-12-20 20:49:33 -08002126void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002127 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002128{
2129 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002130
Paul Mooreafeb14b2007-12-21 14:58:11 -08002131 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002132 if (audit_buf == NULL)
2133 return;
Eric Paris25323862008-04-18 10:09:25 -04002134 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002135 xfrm_audit_helper_sainfo(x, audit_buf);
2136 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002137 audit_log_end(audit_buf);
2138}
2139EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002140
2141void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2142 struct sk_buff *skb)
2143{
2144 struct audit_buffer *audit_buf;
2145 u32 spi;
2146
2147 audit_buf = xfrm_audit_start("SA-replay-overflow");
2148 if (audit_buf == NULL)
2149 return;
2150 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2151 /* don't record the sequence number because it's inherent in this kind
2152 * of audit message */
2153 spi = ntohl(x->id.spi);
2154 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2155 audit_log_end(audit_buf);
2156}
2157EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2158
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002159void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002160 struct sk_buff *skb, __be32 net_seq)
2161{
2162 struct audit_buffer *audit_buf;
2163 u32 spi;
2164
2165 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2166 if (audit_buf == NULL)
2167 return;
2168 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2169 spi = ntohl(x->id.spi);
2170 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2171 spi, spi, ntohl(net_seq));
2172 audit_log_end(audit_buf);
2173}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002174EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002175
2176void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2177{
2178 struct audit_buffer *audit_buf;
2179
2180 audit_buf = xfrm_audit_start("SA-notfound");
2181 if (audit_buf == NULL)
2182 return;
2183 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2184 audit_log_end(audit_buf);
2185}
2186EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2187
2188void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2189 __be32 net_spi, __be32 net_seq)
2190{
2191 struct audit_buffer *audit_buf;
2192 u32 spi;
2193
2194 audit_buf = xfrm_audit_start("SA-notfound");
2195 if (audit_buf == NULL)
2196 return;
2197 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2198 spi = ntohl(net_spi);
2199 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2200 spi, spi, ntohl(net_seq));
2201 audit_log_end(audit_buf);
2202}
2203EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2204
2205void xfrm_audit_state_icvfail(struct xfrm_state *x,
2206 struct sk_buff *skb, u8 proto)
2207{
2208 struct audit_buffer *audit_buf;
2209 __be32 net_spi;
2210 __be32 net_seq;
2211
2212 audit_buf = xfrm_audit_start("SA-icv-failure");
2213 if (audit_buf == NULL)
2214 return;
2215 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2216 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2217 u32 spi = ntohl(net_spi);
2218 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2219 spi, spi, ntohl(net_seq));
2220 }
2221 audit_log_end(audit_buf);
2222}
2223EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002224#endif /* CONFIG_AUDITSYSCALL */