blob: 54c0acd2946861babd611bf8ecf8ecb6751a71ad [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 Salim53bc6b4d2006-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 Salim53bc6b4d2006-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 Salim53bc6b4d2006-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);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800499 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800500 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
501 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800502 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 x->lft.soft_byte_limit = XFRM_INF;
504 x->lft.soft_packet_limit = XFRM_INF;
505 x->lft.hard_byte_limit = XFRM_INF;
506 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800507 x->replay_maxage = 0;
508 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700509 x->inner_mode = NULL;
510 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 spin_lock_init(&x->lock);
512 }
513 return x;
514}
515EXPORT_SYMBOL(xfrm_state_alloc);
516
517void __xfrm_state_destroy(struct xfrm_state *x)
518{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800519 struct net *net = xs_net(x);
520
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700521 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800524 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800526 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528EXPORT_SYMBOL(__xfrm_state_destroy);
529
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800530int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800532 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700533 int err = -ESRCH;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (x->km.state != XFRM_STATE_DEAD) {
536 x->km.state = XFRM_STATE_DEAD;
537 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700538 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700539 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700540 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700541 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700542 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800543 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* All xfrm_state objects are created by xfrm_state_alloc.
547 * The xfrm_state_alloc call gives a reference, and that
548 * is what we are dropping here.
549 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800550 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700551 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700553
554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800556EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700558int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700560 int err;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700563 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700565
566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568EXPORT_SYMBOL(xfrm_state_delete);
569
Joy Latten4aa2e622007-06-04 19:05:57 -0400570#ifdef CONFIG_SECURITY_NETWORK_XFRM
571static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800572xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Joy Latten4aa2e622007-06-04 19:05:57 -0400574 int i, err = 0;
575
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800576 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400577 struct xfrm_state *x;
578
Sasha Levinb67bfe02013-02-27 17:06:00 -0800579 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400580 if (xfrm_id_proto_match(x->id.proto, proto) &&
581 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700582 xfrm_audit_state_delete(x, 0,
583 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400584 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700585 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400586 return err;
587 }
588 }
589 }
590
591 return err;
592}
593#else
594static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800595xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400596{
597 return 0;
598}
599#endif
600
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800601int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400602{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000603 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800606 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400607 if (err)
608 goto out;
609
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000610 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800611 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700612 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800614 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700616 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfrm_state_hold(x);
618 spin_unlock_bh(&xfrm_state_lock);
619
Joy Latten161a09e2006-11-27 13:11:54 -0600620 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700621 xfrm_audit_state_delete(x, err ? 0 : 1,
622 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400623 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700624 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000626 if (!err)
627 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 spin_lock_bh(&xfrm_state_lock);
630 goto restart;
631 }
632 }
633 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000634 if (cnt)
635 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400636
637out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800639 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642EXPORT_SYMBOL(xfrm_state_flush);
643
Alexey Dobriyane0710412010-01-23 13:37:10 +0000644void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700645{
646 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000647 si->sadcnt = net->xfrm.state_num;
648 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700649 si->sadhmcnt = xfrm_state_hashmax;
650 spin_unlock_bh(&xfrm_state_lock);
651}
652EXPORT_SYMBOL(xfrm_sad_getinfo);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int
David S. Miller1a898592011-02-22 18:22:34 -0800655xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500656 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500657 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700658 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
661 if (!afinfo)
662 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700663 afinfo->init_tempsel(&x->sel, fl);
664
665 if (family != tmpl->encap_family) {
666 xfrm_state_put_afinfo(afinfo);
667 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
668 if (!afinfo)
669 return -1;
670 }
671 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 xfrm_state_put_afinfo(afinfo);
673 return 0;
674}
675
David S. Miller9aa60082011-02-24 01:51:36 -0500676static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
677 const xfrm_address_t *daddr,
678 __be32 spi, u8 proto,
679 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700680{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800681 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700682 struct xfrm_state *x;
683
Sasha Levinb67bfe02013-02-27 17:06:00 -0800684 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700685 if (x->props.family != family ||
686 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000687 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000688 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700689 continue;
690
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000691 if ((mark & x->mark.m) != x->mark.v)
692 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700693 xfrm_state_hold(x);
694 return x;
695 }
696
697 return NULL;
698}
699
David S. Miller9aa60082011-02-24 01:51:36 -0500700static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
701 const xfrm_address_t *daddr,
702 const xfrm_address_t *saddr,
703 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700704{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800705 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700706 struct xfrm_state *x;
707
Sasha Levinb67bfe02013-02-27 17:06:00 -0800708 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700709 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000710 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000711 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
712 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700713 continue;
714
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000715 if ((mark & x->mark.m) != x->mark.v)
716 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700717 xfrm_state_hold(x);
718 return x;
719 }
720
721 return NULL;
722}
723
724static inline struct xfrm_state *
725__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
726{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800727 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800728 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800729
David S. Milleredcd5822006-08-24 00:42:45 -0700730 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800731 return __xfrm_state_lookup(net, mark, &x->id.daddr,
732 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700733 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800734 return __xfrm_state_lookup_byaddr(net, mark,
735 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700736 &x->props.saddr,
737 x->id.proto, family);
738}
739
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800740static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700741{
742 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800743 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
744 net->xfrm.state_num > net->xfrm.state_hmask)
745 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700746}
747
David S. Miller08ec9af2009-03-13 14:22:40 -0700748static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800749 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700750 struct xfrm_state **best, int *acq_in_progress,
751 int *error)
752{
753 /* Resolution logic:
754 * 1. There is a valid state with matching selector. Done.
755 * 2. Valid state with inappropriate selector. Skip.
756 *
757 * Entering area of "sysdeps".
758 *
759 * 3. If state is not valid, selector is temporary, it selects
760 * only session which triggered previous resolution. Key
761 * manager will do something to install a state with proper
762 * selector.
763 */
764 if (x->km.state == XFRM_STATE_VALID) {
765 if ((x->sel.family &&
766 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
767 !security_xfrm_state_pol_flow_match(x, pol, fl))
768 return;
769
770 if (!*best ||
771 (*best)->km.dying > x->km.dying ||
772 ((*best)->km.dying == x->km.dying &&
773 (*best)->curlft.add_time < x->curlft.add_time))
774 *best = x;
775 } else if (x->km.state == XFRM_STATE_ACQ) {
776 *acq_in_progress = 1;
777 } else if (x->km.state == XFRM_STATE_ERROR ||
778 x->km.state == XFRM_STATE_EXPIRED) {
779 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
780 security_xfrm_state_pol_flow_match(x, pol, fl))
781 *error = -ESRCH;
782 }
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500786xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800787 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct xfrm_policy *pol, int *err,
789 unsigned short family)
790{
David S. Miller08ec9af2009-03-13 14:22:40 -0700791 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800792 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700793 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700794 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int acquire_in_progress = 0;
796 int error = 0;
797 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800798 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700799 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900800
David S. Miller37b08e32008-09-02 20:14:15 -0700801 to_put = NULL;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700804 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800805 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700806 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000808 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700809 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700810 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 tmpl->mode == x->props.mode &&
812 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700813 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500814 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700815 &best, &acquire_in_progress, &error);
816 }
817 if (best)
818 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Thomas Egerer8444cf72010-09-20 11:11:38 -0700820 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800821 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700822 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700823 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000824 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700825 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700826 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700827 tmpl->mode == x->props.mode &&
828 tmpl->id.proto == x->id.proto &&
829 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500830 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700831 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
David S. Miller08ec9af2009-03-13 14:22:40 -0700834found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 x = best;
836 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700837 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800838 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700839 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700840 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 error = -EEXIST;
842 goto out;
843 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800844 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (x == NULL) {
846 error = -ENOMEM;
847 goto out;
848 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700849 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700851 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800852 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
David S. Miller1d28f422011-03-12 00:29:39 -0500854 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700855 if (error) {
856 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700857 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700858 x = NULL;
859 goto out;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (km_query(x, tmpl, pol) == 0) {
863 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800864 list_add(&x->km.all, &net->xfrm.state_all);
865 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700866 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800867 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700869 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800870 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800872 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800873 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800874 net->xfrm.state_num++;
875 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 } else {
877 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700878 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 x = NULL;
880 error = -ESRCH;
881 }
882 }
883out:
884 if (x)
885 xfrm_state_hold(x);
886 else
887 *err = acquire_in_progress ? -EAGAIN : error;
888 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700889 if (to_put)
890 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return x;
892}
893
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700894struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800895xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800896 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700897 unsigned short family, u8 mode, u8 proto, u32 reqid)
898{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800899 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700900 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700901
902 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800903 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800904 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700905 if (x->props.family == family &&
906 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000907 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700908 !(x->props.flags & XFRM_STATE_WILDRECV) &&
909 xfrm_state_addr_check(x, daddr, saddr, family) &&
910 mode == x->props.mode &&
911 proto == x->id.proto &&
912 x->km.state == XFRM_STATE_VALID) {
913 rx = x;
914 break;
915 }
916 }
917
918 if (rx)
919 xfrm_state_hold(rx);
920 spin_unlock(&xfrm_state_lock);
921
922
923 return rx;
924}
925EXPORT_SYMBOL(xfrm_stateonly_find);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static void __xfrm_state_insert(struct xfrm_state *x)
928{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800929 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700930 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800932 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800933
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800934 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700935 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800936 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800938 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
939 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700941 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800942 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700943 x->props.family);
944
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800945 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700946 }
947
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800948 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700949 if (x->replay_maxage)
950 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800951
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800952 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700953
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800954 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700955
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800956 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
David S. Millerc7f5ea32006-08-24 03:29:04 -0700959/* xfrm_state_lock is held */
960static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
961{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800962 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700963 unsigned short family = xnew->props.family;
964 u32 reqid = xnew->props.reqid;
965 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700966 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000967 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700968
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800969 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800970 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700971 if (x->props.family == family &&
972 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000973 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000974 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
975 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000976 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700977 }
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980void xfrm_state_insert(struct xfrm_state *x)
981{
982 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700983 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 __xfrm_state_insert(x);
985 spin_unlock_bh(&xfrm_state_lock);
986}
987EXPORT_SYMBOL(xfrm_state_insert);
988
David S. Miller27708342006-08-24 00:13:10 -0700989/* xfrm_state_lock is held */
David S. Millera70486f2011-02-27 23:17:24 -0800990static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
991 unsigned short family, u8 mode,
992 u32 reqid, u8 proto,
993 const xfrm_address_t *daddr,
994 const xfrm_address_t *saddr, int create)
David S. Miller27708342006-08-24 00:13:10 -0700995{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800996 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -0700997 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000998 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -0700999
Sasha Levinb67bfe02013-02-27 17:06:00 -08001000 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001001 if (x->props.reqid != reqid ||
1002 x->props.mode != mode ||
1003 x->props.family != family ||
1004 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001005 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001006 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001007 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001008 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1009 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001010 continue;
1011
David S. Miller27708342006-08-24 00:13:10 -07001012 xfrm_state_hold(x);
1013 return x;
1014 }
1015
1016 if (!create)
1017 return NULL;
1018
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001019 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001020 if (likely(x)) {
1021 switch (family) {
1022 case AF_INET:
1023 x->sel.daddr.a4 = daddr->a4;
1024 x->sel.saddr.a4 = saddr->a4;
1025 x->sel.prefixlen_d = 32;
1026 x->sel.prefixlen_s = 32;
1027 x->props.saddr.a4 = saddr->a4;
1028 x->id.daddr.a4 = daddr->a4;
1029 break;
1030
1031 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001032 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1033 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001034 x->sel.prefixlen_d = 128;
1035 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001036 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1037 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001038 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001039 }
David S. Miller27708342006-08-24 00:13:10 -07001040
1041 x->km.state = XFRM_STATE_ACQ;
1042 x->id.proto = proto;
1043 x->props.family = family;
1044 x->props.mode = mode;
1045 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001046 x->mark.v = m->v;
1047 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001048 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001049 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001050 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001051 list_add(&x->km.all, &net->xfrm.state_all);
1052 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1053 h = xfrm_src_hash(net, daddr, saddr, family);
1054 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001055
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001056 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001057
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001058 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001059 }
1060
1061 return x;
1062}
1063
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001064static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066int xfrm_state_add(struct xfrm_state *x)
1067{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001068 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001069 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int family;
1071 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001072 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001073 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
David S. Miller37b08e32008-09-02 20:14:15 -07001077 to_put = NULL;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 spin_lock_bh(&xfrm_state_lock);
1080
David S. Milleredcd5822006-08-24 00:42:45 -07001081 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001083 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 x1 = NULL;
1085 err = -EEXIST;
1086 goto out;
1087 }
1088
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001089 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001090 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001091 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001092 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001093 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 x1 = NULL;
1095 }
1096 }
1097
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001098 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001099 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1100 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001101 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
David S. Millerc7f5ea32006-08-24 03:29:04 -07001103 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 __xfrm_state_insert(x);
1105 err = 0;
1106
1107out:
1108 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (x1) {
1111 xfrm_state_delete(x1);
1112 xfrm_state_put(x1);
1113 }
1114
David S. Miller37b08e32008-09-02 20:14:15 -07001115 if (to_put)
1116 xfrm_state_put(to_put);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return err;
1119}
1120EXPORT_SYMBOL(xfrm_state_add);
1121
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001122#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001123static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001124{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001125 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001126 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001127 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001128 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001129 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001130
1131 memcpy(&x->id, &orig->id, sizeof(x->id));
1132 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1133 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1134 x->props.mode = orig->props.mode;
1135 x->props.replay_window = orig->props.replay_window;
1136 x->props.reqid = orig->props.reqid;
1137 x->props.family = orig->props.family;
1138 x->props.saddr = orig->props.saddr;
1139
1140 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001141 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001142 if (!x->aalg)
1143 goto error;
1144 }
1145 x->props.aalgo = orig->props.aalgo;
1146
1147 if (orig->ealg) {
1148 x->ealg = xfrm_algo_clone(orig->ealg);
1149 if (!x->ealg)
1150 goto error;
1151 }
1152 x->props.ealgo = orig->props.ealgo;
1153
1154 if (orig->calg) {
1155 x->calg = xfrm_algo_clone(orig->calg);
1156 if (!x->calg)
1157 goto error;
1158 }
1159 x->props.calgo = orig->props.calgo;
1160
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001161 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001162 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1163 if (!x->encap)
1164 goto error;
1165 }
1166
1167 if (orig->coaddr) {
1168 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1169 GFP_KERNEL);
1170 if (!x->coaddr)
1171 goto error;
1172 }
1173
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001174 if (orig->replay_esn) {
1175 err = xfrm_replay_clone(x, orig);
1176 if (err)
1177 goto error;
1178 }
1179
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001180 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1181
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001182 err = xfrm_init_state(x);
1183 if (err)
1184 goto error;
1185
1186 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001187 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001188
1189 x->curlft.add_time = orig->curlft.add_time;
1190 x->km.state = orig->km.state;
1191 x->km.seq = orig->km.seq;
1192
1193 return x;
1194
1195 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001196 xfrm_state_put(x);
1197out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001198 if (errp)
1199 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001200 return NULL;
1201}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001202
1203/* xfrm_state_lock is held */
1204struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1205{
1206 unsigned int h;
1207 struct xfrm_state *x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001208
1209 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001210 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001211 m->reqid, m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001212 hlist_for_each_entry(x, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001213 if (x->props.mode != m->mode ||
1214 x->id.proto != m->proto)
1215 continue;
1216 if (m->reqid && x->props.reqid != m->reqid)
1217 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001218 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1219 m->old_family) ||
1220 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1221 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001222 continue;
1223 xfrm_state_hold(x);
1224 return x;
1225 }
1226 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001227 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001228 m->old_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 hlist_for_each_entry(x, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001230 if (x->props.mode != m->mode ||
1231 x->id.proto != m->proto)
1232 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001233 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1234 m->old_family) ||
1235 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1236 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001237 continue;
1238 xfrm_state_hold(x);
1239 return x;
1240 }
1241 }
1242
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001243 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001244}
1245EXPORT_SYMBOL(xfrm_migrate_state_find);
1246
1247struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1248 struct xfrm_migrate *m)
1249{
1250 struct xfrm_state *xc;
1251 int err;
1252
1253 xc = xfrm_state_clone(x, &err);
1254 if (!xc)
1255 return NULL;
1256
1257 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1258 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1259
1260 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001261 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001262 /* a care is needed when the destination address of the
1263 state is to be updated as it is a part of triplet */
1264 xfrm_state_insert(xc);
1265 } else {
1266 if ((err = xfrm_state_add(xc)) < 0)
1267 goto error;
1268 }
1269
1270 return xc;
1271error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001272 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001273 return NULL;
1274}
1275EXPORT_SYMBOL(xfrm_state_migrate);
1276#endif
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278int xfrm_state_update(struct xfrm_state *x)
1279{
David S. Miller37b08e32008-09-02 20:14:15 -07001280 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001282 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
David S. Miller37b08e32008-09-02 20:14:15 -07001284 to_put = NULL;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001287 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 err = -ESRCH;
1290 if (!x1)
1291 goto out;
1292
1293 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001294 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 err = -EEXIST;
1296 goto out;
1297 }
1298
1299 if (x1->km.state == XFRM_STATE_ACQ) {
1300 __xfrm_state_insert(x);
1301 x = NULL;
1302 }
1303 err = 0;
1304
1305out:
1306 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
David S. Miller37b08e32008-09-02 20:14:15 -07001308 if (to_put)
1309 xfrm_state_put(to_put);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (err)
1312 return err;
1313
1314 if (!x) {
1315 xfrm_state_delete(x1);
1316 xfrm_state_put(x1);
1317 return 0;
1318 }
1319
1320 err = -EINVAL;
1321 spin_lock_bh(&x1->lock);
1322 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1323 if (x->encap && x1->encap)
1324 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001325 if (x->coaddr && x1->coaddr) {
1326 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1327 }
1328 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1329 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1331 x1->km.dying = 0;
1332
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001333 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (x1->curlft.use_time)
1335 xfrm_state_check_expire(x1);
1336
1337 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001338 x->km.state = XFRM_STATE_DEAD;
1339 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 spin_unlock_bh(&x1->lock);
1342
1343 xfrm_state_put(x1);
1344
1345 return err;
1346}
1347EXPORT_SYMBOL(xfrm_state_update);
1348
1349int xfrm_state_check_expire(struct xfrm_state *x)
1350{
1351 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001352 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1355 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001356 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001357 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return -EINVAL;
1359 }
1360
1361 if (!x->km.dying &&
1362 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001363 x->curlft.packets >= x->lft.soft_packet_limit)) {
1364 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001365 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 0;
1368}
1369EXPORT_SYMBOL(xfrm_state_check_expire);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001372xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001373 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001378 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return x;
1381}
1382EXPORT_SYMBOL(xfrm_state_lookup);
1383
1384struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001385xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001386 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001387 u8 proto, unsigned short family)
1388{
1389 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001390
1391 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001392 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001393 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001394 return x;
1395}
1396EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1397
1398struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001399xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001400 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int create, unsigned short family)
1402{
1403 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001406 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return x;
1410}
1411EXPORT_SYMBOL(xfrm_find_acq);
1412
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001413#ifdef CONFIG_XFRM_SUB_POLICY
1414int
1415xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1416 unsigned short family)
1417{
1418 int err = 0;
1419 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1420 if (!afinfo)
1421 return -EAFNOSUPPORT;
1422
1423 spin_lock_bh(&xfrm_state_lock);
1424 if (afinfo->tmpl_sort)
1425 err = afinfo->tmpl_sort(dst, src, n);
1426 spin_unlock_bh(&xfrm_state_lock);
1427 xfrm_state_put_afinfo(afinfo);
1428 return err;
1429}
1430EXPORT_SYMBOL(xfrm_tmpl_sort);
1431
1432int
1433xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1434 unsigned short family)
1435{
1436 int err = 0;
1437 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1438 if (!afinfo)
1439 return -EAFNOSUPPORT;
1440
1441 spin_lock_bh(&xfrm_state_lock);
1442 if (afinfo->state_sort)
1443 err = afinfo->state_sort(dst, src, n);
1444 spin_unlock_bh(&xfrm_state_lock);
1445 xfrm_state_put_afinfo(afinfo);
1446 return err;
1447}
1448EXPORT_SYMBOL(xfrm_state_sort);
1449#endif
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/* Silly enough, but I'm lazy to build resolution list */
1452
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001453static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001457 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001458 struct xfrm_state *x;
1459
Sasha Levinb67bfe02013-02-27 17:06:00 -08001460 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001461 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001462 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001463 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 xfrm_state_hold(x);
1465 return x;
1466 }
1467 }
1468 }
1469 return NULL;
1470}
1471
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001472struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct xfrm_state *x;
1475
1476 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001477 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 spin_unlock_bh(&xfrm_state_lock);
1479 return x;
1480}
1481EXPORT_SYMBOL(xfrm_find_acq_byseq);
1482
1483u32 xfrm_get_acqseq(void)
1484{
1485 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001486 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
jamal6836b9b2010-02-16 02:01:22 +00001488 do {
1489 res = atomic_inc_return(&acqseq);
1490 } while (!res);
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return res;
1493}
1494EXPORT_SYMBOL(xfrm_get_acqseq);
1495
Herbert Xu658b2192007-10-09 13:29:52 -07001496int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001498 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001499 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001501 int err = -ENOENT;
1502 __be32 minspi = htonl(low);
1503 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001504 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Herbert Xu658b2192007-10-09 13:29:52 -07001506 spin_lock_bh(&x->lock);
1507 if (x->km.state == XFRM_STATE_DEAD)
1508 goto unlock;
1509
1510 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001512 goto unlock;
1513
1514 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001517 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (x0) {
1519 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001520 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 x->id.spi = minspi;
1523 } else {
1524 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001525 for (h=0; h<high-low+1; h++) {
1526 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001527 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (x0 == NULL) {
1529 x->id.spi = htonl(spi);
1530 break;
1531 }
1532 xfrm_state_put(x0);
1533 }
1534 }
1535 if (x->id.spi) {
1536 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001537 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1538 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001540
1541 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Herbert Xu658b2192007-10-09 13:29:52 -07001543
1544unlock:
1545 spin_unlock_bh(&x->lock);
1546
1547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549EXPORT_SYMBOL(xfrm_alloc_spi);
1550
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001551int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001552 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 void *data)
1554{
Herbert Xu12a169e2008-10-01 07:03:24 -07001555 struct xfrm_state *state;
1556 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 int err = 0;
1558
Herbert Xu12a169e2008-10-01 07:03:24 -07001559 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001560 return 0;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001563 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001564 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001565 else
1566 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001567 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001568 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001569 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001570 state = container_of(x, struct xfrm_state, km);
1571 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001572 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001573 err = func(state, walk->seq, data);
1574 if (err) {
1575 list_move_tail(&walk->all, &x->all);
1576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001578 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001580 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 err = -ENOENT;
1582 goto out;
1583 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001584 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585out:
1586 spin_unlock_bh(&xfrm_state_lock);
1587 return err;
1588}
1589EXPORT_SYMBOL(xfrm_state_walk);
1590
Herbert Xu5c182452008-09-22 19:48:19 -07001591void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1592{
Herbert Xu12a169e2008-10-01 07:03:24 -07001593 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001594 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001595 walk->state = XFRM_STATE_DEAD;
1596 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001597}
1598EXPORT_SYMBOL(xfrm_state_walk_init);
1599
Herbert Xuabb81c42008-09-09 19:58:29 -07001600void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1601{
Herbert Xu12a169e2008-10-01 07:03:24 -07001602 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001603 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001604
Herbert Xu12a169e2008-10-01 07:03:24 -07001605 spin_lock_bh(&xfrm_state_lock);
1606 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001607 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001608}
1609EXPORT_SYMBOL(xfrm_state_walk_done);
1610
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001611static void xfrm_replay_timer_handler(unsigned long data)
1612{
1613 struct xfrm_state *x = (struct xfrm_state*)data;
1614
1615 spin_lock(&x->lock);
1616
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001617 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001618 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001619 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001620 else
1621 x->xflags |= XFRM_TIME_DEFER;
1622 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001623
1624 spin_unlock(&x->lock);
1625}
1626
Denis Chengdf018122007-12-07 00:51:11 -08001627static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
David S. Miller214e0052011-02-24 00:02:38 -05001629void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct xfrm_mgr *km;
1632
Cong Wang85168c02013-01-16 16:05:06 +08001633 rcu_read_lock();
1634 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001635 if (km->notify_policy)
1636 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001637 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001638}
1639
David S. Miller214e0052011-02-24 00:02:38 -05001640void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001641{
1642 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001643 rcu_read_lock();
1644 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001645 if (km->notify)
1646 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001647 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001648}
1649
1650EXPORT_SYMBOL(km_policy_notify);
1651EXPORT_SYMBOL(km_state_notify);
1652
Eric W. Biederman15e47302012-09-07 20:12:54 +00001653void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001654{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001655 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001656 struct km_event c;
1657
Herbert Xubf08867f92005-06-18 22:44:00 -07001658 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001659 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001660 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001661 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001664 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001667EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001668/*
1669 * We send to all registered managers regardless of failure
1670 * We are happy with one success
1671*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001672int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001674 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct xfrm_mgr *km;
1676
Cong Wang85168c02013-01-16 16:05:06 +08001677 rcu_read_lock();
1678 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001679 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 if (!acqret)
1681 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Cong Wang85168c02013-01-16 16:05:06 +08001683 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return err;
1685}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001686EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Al Viro5d36b182006-11-08 00:24:06 -08001688int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 int err = -EINVAL;
1691 struct xfrm_mgr *km;
1692
Cong Wang85168c02013-01-16 16:05:06 +08001693 rcu_read_lock();
1694 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (km->new_mapping)
1696 err = km->new_mapping(x, ipaddr, sport);
1697 if (!err)
1698 break;
1699 }
Cong Wang85168c02013-01-16 16:05:06 +08001700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return err;
1702}
1703EXPORT_SYMBOL(km_new_mapping);
1704
Eric W. Biederman15e47302012-09-07 20:12:54 +00001705void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001707 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001708 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Herbert Xubf08867f92005-06-18 22:44:00 -07001710 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001711 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001712 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001713 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001716 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
David S. Millera70fcb02006-03-20 19:18:52 -08001718EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001720#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001721int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1722 const struct xfrm_migrate *m, int num_migrate,
1723 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001724{
1725 int err = -EINVAL;
1726 int ret;
1727 struct xfrm_mgr *km;
1728
Cong Wang85168c02013-01-16 16:05:06 +08001729 rcu_read_lock();
1730 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001731 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001732 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001733 if (!ret)
1734 err = ret;
1735 }
1736 }
Cong Wang85168c02013-01-16 16:05:06 +08001737 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001738 return err;
1739}
1740EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001741#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001742
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001743int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001744{
1745 int err = -EINVAL;
1746 int ret;
1747 struct xfrm_mgr *km;
1748
Cong Wang85168c02013-01-16 16:05:06 +08001749 rcu_read_lock();
1750 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001751 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001752 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001753 if (!ret)
1754 err = ret;
1755 }
1756 }
Cong Wang85168c02013-01-16 16:05:06 +08001757 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001758 return err;
1759}
1760EXPORT_SYMBOL(km_report);
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1763{
1764 int err;
1765 u8 *data;
1766 struct xfrm_mgr *km;
1767 struct xfrm_policy *pol = NULL;
1768
1769 if (optlen <= 0 || optlen > PAGE_SIZE)
1770 return -EMSGSIZE;
1771
1772 data = kmalloc(optlen, GFP_KERNEL);
1773 if (!data)
1774 return -ENOMEM;
1775
1776 err = -EFAULT;
1777 if (copy_from_user(data, optval, optlen))
1778 goto out;
1779
1780 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001781 rcu_read_lock();
1782 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001783 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 optlen, &err);
1785 if (err >= 0)
1786 break;
1787 }
Cong Wang85168c02013-01-16 16:05:06 +08001788 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 if (err >= 0) {
1791 xfrm_sk_policy_insert(sk, err, pol);
1792 xfrm_pol_put(pol);
1793 err = 0;
1794 }
1795
1796out:
1797 kfree(data);
1798 return err;
1799}
1800EXPORT_SYMBOL(xfrm_user_policy);
1801
Cong Wang85168c02013-01-16 16:05:06 +08001802static DEFINE_SPINLOCK(xfrm_km_lock);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804int xfrm_register_km(struct xfrm_mgr *km)
1805{
Cong Wang85168c02013-01-16 16:05:06 +08001806 spin_lock_bh(&xfrm_km_lock);
1807 list_add_tail_rcu(&km->list, &xfrm_km_list);
1808 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return 0;
1810}
1811EXPORT_SYMBOL(xfrm_register_km);
1812
1813int xfrm_unregister_km(struct xfrm_mgr *km)
1814{
Cong Wang85168c02013-01-16 16:05:06 +08001815 spin_lock_bh(&xfrm_km_lock);
1816 list_del_rcu(&km->list);
1817 spin_unlock_bh(&xfrm_km_lock);
1818 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return 0;
1820}
1821EXPORT_SYMBOL(xfrm_unregister_km);
1822
1823int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1824{
1825 int err = 0;
1826 if (unlikely(afinfo == NULL))
1827 return -EINVAL;
1828 if (unlikely(afinfo->family >= NPROTO))
1829 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001830 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1832 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001833 else
Cong Wang44abdc32013-01-16 16:05:05 +08001834 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1835 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return err;
1837}
1838EXPORT_SYMBOL(xfrm_state_register_afinfo);
1839
1840int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1841{
1842 int err = 0;
1843 if (unlikely(afinfo == NULL))
1844 return -EINVAL;
1845 if (unlikely(afinfo->family >= NPROTO))
1846 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001847 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1849 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1850 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001851 else
Cong Wang44abdc32013-01-16 16:05:05 +08001852 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Cong Wang44abdc32013-01-16 16:05:05 +08001854 spin_unlock_bh(&xfrm_state_afinfo_lock);
1855 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return err;
1857}
1858EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1859
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001860struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 struct xfrm_state_afinfo *afinfo;
1863 if (unlikely(family >= NPROTO))
1864 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001865 rcu_read_lock();
1866 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001867 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001868 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return afinfo;
1870}
1871
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001872void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Cong Wang44abdc32013-01-16 16:05:05 +08001874 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1878void xfrm_state_delete_tunnel(struct xfrm_state *x)
1879{
1880 if (x->tunnel) {
1881 struct xfrm_state *t = x->tunnel;
1882
1883 if (atomic_read(&t->tunnel_users) == 2)
1884 xfrm_state_delete(t);
1885 atomic_dec(&t->tunnel_users);
1886 xfrm_state_put(t);
1887 x->tunnel = NULL;
1888 }
1889}
1890EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1891
1892int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1893{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001894 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Patrick McHardyc5c25232007-04-09 11:47:18 -07001896 spin_lock_bh(&x->lock);
1897 if (x->km.state == XFRM_STATE_VALID &&
1898 x->type && x->type->get_mtu)
1899 res = x->type->get_mtu(x, mtu);
1900 else
Patrick McHardy28121612007-06-18 22:30:15 -07001901 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001902 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return res;
1904}
1905
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001906int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001907{
Herbert Xud094cd82005-06-20 13:19:41 -07001908 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001909 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001910 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001911 int err;
1912
Herbert Xud094cd82005-06-20 13:19:41 -07001913 err = -EAFNOSUPPORT;
1914 afinfo = xfrm_state_get_afinfo(family);
1915 if (!afinfo)
1916 goto error;
1917
1918 err = 0;
1919 if (afinfo->init_flags)
1920 err = afinfo->init_flags(x);
1921
1922 xfrm_state_put_afinfo(afinfo);
1923
1924 if (err)
1925 goto error;
1926
1927 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001928
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001929 if (x->sel.family != AF_UNSPEC) {
1930 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1931 if (inner_mode == NULL)
1932 goto error;
1933
1934 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1935 family != x->sel.family) {
1936 xfrm_put_mode(inner_mode);
1937 goto error;
1938 }
1939
1940 x->inner_mode = inner_mode;
1941 } else {
1942 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001943 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001944
Martin Willid81d2282008-12-03 15:38:07 -08001945 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001946 if (inner_mode == NULL)
1947 goto error;
1948
1949 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1950 xfrm_put_mode(inner_mode);
1951 goto error;
1952 }
Martin Willid81d2282008-12-03 15:38:07 -08001953 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001954
Martin Willid81d2282008-12-03 15:38:07 -08001955 if (x->props.family == AF_INET)
1956 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001957
Martin Willid81d2282008-12-03 15:38:07 -08001958 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1959 if (inner_mode_iaf) {
1960 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1961 x->inner_mode_iaf = inner_mode_iaf;
1962 else
1963 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001964 }
1965 }
Herbert Xu13996372007-10-17 21:35:51 -07001966
Herbert Xud094cd82005-06-20 13:19:41 -07001967 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001968 if (x->type == NULL)
1969 goto error;
1970
1971 err = x->type->init_state(x);
1972 if (err)
1973 goto error;
1974
Herbert Xu13996372007-10-17 21:35:51 -07001975 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00001976 if (x->outer_mode == NULL) {
1977 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07001978 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00001979 }
Herbert Xub59f45d2006-05-27 23:05:54 -07001980
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001981 if (init_replay) {
1982 err = xfrm_init_replay(x);
1983 if (err)
1984 goto error;
1985 }
1986
Herbert Xu72cb6962005-06-20 13:18:08 -07001987 x->km.state = XFRM_STATE_VALID;
1988
1989error:
1990 return err;
1991}
1992
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001993EXPORT_SYMBOL(__xfrm_init_state);
1994
1995int xfrm_init_state(struct xfrm_state *x)
1996{
1997 return __xfrm_init_state(x, true);
1998}
1999
Herbert Xu72cb6962005-06-20 13:18:08 -07002000EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002001
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002002int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
David S. Millerf034b5d2006-08-24 03:08:07 -07002004 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002006 INIT_LIST_HEAD(&net->xfrm.state_all);
2007
David S. Millerf034b5d2006-08-24 03:08:07 -07002008 sz = sizeof(struct hlist_head) * 8;
2009
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002010 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2011 if (!net->xfrm.state_bydst)
2012 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002013 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2014 if (!net->xfrm.state_bysrc)
2015 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002016 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2017 if (!net->xfrm.state_byspi)
2018 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002019 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002020
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002021 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002022 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002023 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002024 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002025 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002026 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002027
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002028out_byspi:
2029 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002030out_bysrc:
2031 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002032out_bydst:
2033 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002034}
2035
2036void xfrm_state_fini(struct net *net)
2037{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002038 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002039 unsigned int sz;
2040
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002041 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002042 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002043 audit_info.sessionid = -1;
2044 audit_info.secid = 0;
2045 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2046 flush_work(&net->xfrm.state_gc_work);
2047
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002048 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002049
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002050 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002051 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2052 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002053 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2054 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002055 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2056 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Joy Lattenab5f5e82007-09-17 11:51:22 -07002059#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002060static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2061 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002062{
Paul Moore68277ac2007-12-20 20:49:33 -08002063 struct xfrm_sec_ctx *ctx = x->security;
2064 u32 spi = ntohl(x->id.spi);
2065
2066 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002067 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002068 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002069
2070 switch(x->props.family) {
2071 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002072 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2073 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002074 break;
2075 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002076 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002077 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002078 break;
2079 }
Paul Moore68277ac2007-12-20 20:49:33 -08002080
2081 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002082}
2083
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002084static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2085 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002086{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002087 const struct iphdr *iph4;
2088 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002089
2090 switch (family) {
2091 case AF_INET:
2092 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002093 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2094 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002095 break;
2096 case AF_INET6:
2097 iph6 = ipv6_hdr(skb);
2098 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002099 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002100 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002101 iph6->flow_lbl[0] & 0x0f,
2102 iph6->flow_lbl[1],
2103 iph6->flow_lbl[2]);
2104 break;
2105 }
2106}
2107
Paul Moore68277ac2007-12-20 20:49:33 -08002108void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002109 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002110{
2111 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002112
Paul Mooreafeb14b2007-12-21 14:58:11 -08002113 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002114 if (audit_buf == NULL)
2115 return;
Eric Paris25323862008-04-18 10:09:25 -04002116 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002117 xfrm_audit_helper_sainfo(x, audit_buf);
2118 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002119 audit_log_end(audit_buf);
2120}
2121EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2122
Paul Moore68277ac2007-12-20 20:49:33 -08002123void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002124 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002125{
2126 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002127
Paul Mooreafeb14b2007-12-21 14:58:11 -08002128 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002129 if (audit_buf == NULL)
2130 return;
Eric Paris25323862008-04-18 10:09:25 -04002131 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002132 xfrm_audit_helper_sainfo(x, audit_buf);
2133 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002134 audit_log_end(audit_buf);
2135}
2136EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002137
2138void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2139 struct sk_buff *skb)
2140{
2141 struct audit_buffer *audit_buf;
2142 u32 spi;
2143
2144 audit_buf = xfrm_audit_start("SA-replay-overflow");
2145 if (audit_buf == NULL)
2146 return;
2147 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2148 /* don't record the sequence number because it's inherent in this kind
2149 * of audit message */
2150 spi = ntohl(x->id.spi);
2151 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2152 audit_log_end(audit_buf);
2153}
2154EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2155
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002156void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002157 struct sk_buff *skb, __be32 net_seq)
2158{
2159 struct audit_buffer *audit_buf;
2160 u32 spi;
2161
2162 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2163 if (audit_buf == NULL)
2164 return;
2165 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2166 spi = ntohl(x->id.spi);
2167 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2168 spi, spi, ntohl(net_seq));
2169 audit_log_end(audit_buf);
2170}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002171EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002172
2173void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2174{
2175 struct audit_buffer *audit_buf;
2176
2177 audit_buf = xfrm_audit_start("SA-notfound");
2178 if (audit_buf == NULL)
2179 return;
2180 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2181 audit_log_end(audit_buf);
2182}
2183EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2184
2185void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2186 __be32 net_spi, __be32 net_seq)
2187{
2188 struct audit_buffer *audit_buf;
2189 u32 spi;
2190
2191 audit_buf = xfrm_audit_start("SA-notfound");
2192 if (audit_buf == NULL)
2193 return;
2194 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2195 spi = ntohl(net_spi);
2196 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2197 spi, spi, ntohl(net_seq));
2198 audit_log_end(audit_buf);
2199}
2200EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2201
2202void xfrm_audit_state_icvfail(struct xfrm_state *x,
2203 struct sk_buff *skb, u8 proto)
2204{
2205 struct audit_buffer *audit_buf;
2206 __be32 net_spi;
2207 __be32 net_seq;
2208
2209 audit_buf = xfrm_audit_start("SA-icv-failure");
2210 if (audit_buf == NULL)
2211 return;
2212 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2213 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2214 u32 spi = ntohl(net_spi);
2215 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2216 spi, spi, ntohl(net_seq));
2217 }
2218 audit_log_end(audit_buf);
2219}
2220EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002221#endif /* CONFIG_AUDITSYSCALL */