blob: 3459692092ec1f44220d02a4d73d82613896793d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_state.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16#include <linux/workqueue.h>
17#include <net/xfrm.h>
18#include <linux/pfkeyv2.h>
19#include <linux/ipsec.h>
20#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070021#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080022#include <linux/audit.h>
Jesper Juhlb5890d82007-08-10 15:20:21 -070023#include <asm/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080024#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080026#include <linux/interrupt.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* Each xfrm_state may be linked to two tables:
32
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070034 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 destination/tunnel endpoint. (output)
36 */
37
38static DEFINE_SPINLOCK(xfrm_state_lock);
39
David S. Millerf034b5d2006-08-24 03:08:07 -070040static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Herbert Xu17c2a422007-10-17 21:33:12 -070042static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
43static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
44
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080045static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050046 const xfrm_address_t *daddr,
47 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070048 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070049 unsigned short family)
50{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080051 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070052}
53
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050055 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070057 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070058{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080059 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070060}
61
David S. Miller2575b652006-08-24 03:26:44 -070062static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050063xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
64 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070065{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080066 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070067}
68
David S. Millerf034b5d2006-08-24 03:08:07 -070069static void xfrm_hash_transfer(struct hlist_head *list,
70 struct hlist_head *ndsttable,
71 struct hlist_head *nsrctable,
72 struct hlist_head *nspitable,
73 unsigned int nhashmask)
74{
75 struct hlist_node *entry, *tmp;
76 struct xfrm_state *x;
77
78 hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
79 unsigned int h;
80
David S. Millerc1969f22006-08-24 04:00:03 -070081 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
82 x->props.reqid, x->props.family,
83 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070084 hlist_add_head(&x->bydst, ndsttable+h);
85
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070086 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
87 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070088 nhashmask);
89 hlist_add_head(&x->bysrc, nsrctable+h);
90
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070091 if (x->id.spi) {
92 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
93 x->id.proto, x->props.family,
94 nhashmask);
95 hlist_add_head(&x->byspi, nspitable+h);
96 }
David S. Millerf034b5d2006-08-24 03:08:07 -070097 }
98}
99
Alexey Dobriyan63082732008-11-25 17:19:07 -0800100static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700101{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800102 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700103}
104
105static DEFINE_MUTEX(hash_resize_mutex);
106
Alexey Dobriyan63082732008-11-25 17:19:07 -0800107static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700108{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800109 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700110 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
111 unsigned long nsize, osize;
112 unsigned int nhashmask, ohashmask;
113 int i;
114
115 mutex_lock(&hash_resize_mutex);
116
Alexey Dobriyan63082732008-11-25 17:19:07 -0800117 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700118 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 if (!ndst)
120 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700121 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700123 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700124 goto out_unlock;
125 }
David S. Miller44e36b42006-08-24 04:50:50 -0700126 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700128 xfrm_hash_free(ndst, nsize);
129 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 goto out_unlock;
131 }
132
133 spin_lock_bh(&xfrm_state_lock);
134
135 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800136 for (i = net->xfrm.state_hmask; i >= 0; i--)
137 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700138 nhashmask);
139
Alexey Dobriyan63082732008-11-25 17:19:07 -0800140 odst = net->xfrm.state_bydst;
141 osrc = net->xfrm.state_bysrc;
142 ospi = net->xfrm.state_byspi;
143 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700144
Alexey Dobriyan63082732008-11-25 17:19:07 -0800145 net->xfrm.state_bydst = ndst;
146 net->xfrm.state_bysrc = nsrc;
147 net->xfrm.state_byspi = nspi;
148 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700149
150 spin_unlock_bh(&xfrm_state_lock);
151
152 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700153 xfrm_hash_free(odst, osize);
154 xfrm_hash_free(osrc, osize);
155 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700156
157out_unlock:
158 mutex_unlock(&hash_resize_mutex);
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
162static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_SPINLOCK(xfrm_state_gc_lock);
165
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800166int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800168int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000169void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700171static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
172{
173 struct xfrm_state_afinfo *afinfo;
174 if (unlikely(family >= NPROTO))
175 return NULL;
176 write_lock_bh(&xfrm_state_afinfo_lock);
177 afinfo = xfrm_state_afinfo[family];
178 if (unlikely(!afinfo))
179 write_unlock_bh(&xfrm_state_afinfo_lock);
180 return afinfo;
181}
182
183static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800184 __releases(xfrm_state_afinfo_lock)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700185{
186 write_unlock_bh(&xfrm_state_afinfo_lock);
187}
188
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800189int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700190{
191 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800192 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700193 int err = 0;
194
195 if (unlikely(afinfo == NULL))
196 return -EAFNOSUPPORT;
197 typemap = afinfo->type_map;
198
199 if (likely(typemap[type->proto] == NULL))
200 typemap[type->proto] = type;
201 else
202 err = -EEXIST;
203 xfrm_state_unlock_afinfo(afinfo);
204 return err;
205}
206EXPORT_SYMBOL(xfrm_register_type);
207
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800208int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700209{
210 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800211 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700212 int err = 0;
213
214 if (unlikely(afinfo == NULL))
215 return -EAFNOSUPPORT;
216 typemap = afinfo->type_map;
217
218 if (unlikely(typemap[type->proto] != type))
219 err = -ENOENT;
220 else
221 typemap[type->proto] = NULL;
222 xfrm_state_unlock_afinfo(afinfo);
223 return err;
224}
225EXPORT_SYMBOL(xfrm_unregister_type);
226
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800227static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700228{
229 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800230 const struct xfrm_type **typemap;
231 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700232 int modload_attempted = 0;
233
234retry:
235 afinfo = xfrm_state_get_afinfo(family);
236 if (unlikely(afinfo == NULL))
237 return NULL;
238 typemap = afinfo->type_map;
239
240 type = typemap[proto];
241 if (unlikely(type && !try_module_get(type->owner)))
242 type = NULL;
243 if (!type && !modload_attempted) {
244 xfrm_state_put_afinfo(afinfo);
245 request_module("xfrm-type-%d-%d", family, proto);
246 modload_attempted = 1;
247 goto retry;
248 }
249
250 xfrm_state_put_afinfo(afinfo);
251 return type;
252}
253
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800254static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700255{
256 module_put(type->owner);
257}
258
259int xfrm_register_mode(struct xfrm_mode *mode, int family)
260{
261 struct xfrm_state_afinfo *afinfo;
262 struct xfrm_mode **modemap;
263 int err;
264
265 if (unlikely(mode->encap >= XFRM_MODE_MAX))
266 return -EINVAL;
267
268 afinfo = xfrm_state_lock_afinfo(family);
269 if (unlikely(afinfo == NULL))
270 return -EAFNOSUPPORT;
271
272 err = -EEXIST;
273 modemap = afinfo->mode_map;
Herbert Xu17c2a422007-10-17 21:33:12 -0700274 if (modemap[mode->encap])
275 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700276
Herbert Xu17c2a422007-10-17 21:33:12 -0700277 err = -ENOENT;
278 if (!try_module_get(afinfo->owner))
279 goto out;
280
281 mode->afinfo = afinfo;
282 modemap[mode->encap] = mode;
283 err = 0;
284
285out:
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700286 xfrm_state_unlock_afinfo(afinfo);
287 return err;
288}
289EXPORT_SYMBOL(xfrm_register_mode);
290
291int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
292{
293 struct xfrm_state_afinfo *afinfo;
294 struct xfrm_mode **modemap;
295 int err;
296
297 if (unlikely(mode->encap >= XFRM_MODE_MAX))
298 return -EINVAL;
299
300 afinfo = xfrm_state_lock_afinfo(family);
301 if (unlikely(afinfo == NULL))
302 return -EAFNOSUPPORT;
303
304 err = -ENOENT;
305 modemap = afinfo->mode_map;
306 if (likely(modemap[mode->encap] == mode)) {
307 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700308 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700309 err = 0;
310 }
311
312 xfrm_state_unlock_afinfo(afinfo);
313 return err;
314}
315EXPORT_SYMBOL(xfrm_unregister_mode);
316
317static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
318{
319 struct xfrm_state_afinfo *afinfo;
320 struct xfrm_mode *mode;
321 int modload_attempted = 0;
322
323 if (unlikely(encap >= XFRM_MODE_MAX))
324 return NULL;
325
326retry:
327 afinfo = xfrm_state_get_afinfo(family);
328 if (unlikely(afinfo == NULL))
329 return NULL;
330
331 mode = afinfo->mode_map[encap];
332 if (unlikely(mode && !try_module_get(mode->owner)))
333 mode = NULL;
334 if (!mode && !modload_attempted) {
335 xfrm_state_put_afinfo(afinfo);
336 request_module("xfrm-mode-%d-%d", family, encap);
337 modload_attempted = 1;
338 goto retry;
339 }
340
341 xfrm_state_put_afinfo(afinfo);
342 return mode;
343}
344
345static void xfrm_put_mode(struct xfrm_mode *mode)
346{
347 module_put(mode->owner);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static void xfrm_state_gc_destroy(struct xfrm_state *x)
351{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800352 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700353 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800354 kfree(x->aalg);
355 kfree(x->ealg);
356 kfree(x->calg);
357 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700358 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000359 kfree(x->replay_esn);
360 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700361 if (x->inner_mode)
362 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700363 if (x->inner_mode_iaf)
364 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700365 if (x->outer_mode)
366 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (x->type) {
368 x->type->destructor(x);
369 xfrm_put_type(x->type);
370 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800371 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 kfree(x);
373}
374
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800375static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800377 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700378 struct xfrm_state *x;
379 struct hlist_node *entry, *tmp;
380 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800383 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_unlock_bh(&xfrm_state_gc_lock);
385
Herbert Xu12a169e2008-10-01 07:03:24 -0700386 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700388
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800389 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static inline unsigned long make_jiffies(long secs)
393{
394 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
395 return MAX_SCHEDULE_TIMEOUT-1;
396 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900397 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800400static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800402 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
403 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800404 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800405 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 long next = LONG_MAX;
407 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600408 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 spin_lock(&x->lock);
411 if (x->km.state == XFRM_STATE_DEAD)
412 goto out;
413 if (x->km.state == XFRM_STATE_EXPIRED)
414 goto expired;
415 if (x->lft.hard_add_expires_seconds) {
416 long tmo = x->lft.hard_add_expires_seconds +
417 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000418 if (tmo <= 0) {
419 if (x->xflags & XFRM_SOFT_EXPIRE) {
420 /* enter hard expire without soft expire first?!
421 * setting a new date could trigger this.
422 * workarbound: fix x->curflt.add_time by below:
423 */
424 x->curlft.add_time = now - x->saved_tmo - 1;
425 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
426 } else
427 goto expired;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (tmo < next)
430 next = tmo;
431 }
432 if (x->lft.hard_use_expires_seconds) {
433 long tmo = x->lft.hard_use_expires_seconds +
434 (x->curlft.use_time ? : now) - now;
435 if (tmo <= 0)
436 goto expired;
437 if (tmo < next)
438 next = tmo;
439 }
440 if (x->km.dying)
441 goto resched;
442 if (x->lft.soft_add_expires_seconds) {
443 long tmo = x->lft.soft_add_expires_seconds +
444 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000445 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000447 x->xflags &= ~XFRM_SOFT_EXPIRE;
448 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000450 x->xflags |= XFRM_SOFT_EXPIRE;
451 x->saved_tmo = tmo;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 if (x->lft.soft_use_expires_seconds) {
455 long tmo = x->lft.soft_use_expires_seconds +
456 (x->curlft.use_time ? : now) - now;
457 if (tmo <= 0)
458 warn = 1;
459 else if (tmo < next)
460 next = tmo;
461 }
462
Herbert Xu4666faa2005-06-18 22:43:22 -0700463 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800465 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800467 if (next != LONG_MAX){
468 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
469 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 goto out;
472
473expired:
474 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
475 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800476 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 next = 2;
478 goto resched;
479 }
Joy Latten161a09e2006-11-27 13:11:54 -0600480
481 err = __xfrm_state_delete(x);
482 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800483 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Joy Lattenab5f5e82007-09-17 11:51:22 -0700485 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400486 audit_get_loginuid(current),
487 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489out:
490 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800491 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
David S. Miller0ac84752006-03-20 19:18:23 -0800494static void xfrm_replay_timer_handler(unsigned long data);
495
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800496struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct xfrm_state *x;
499
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700500 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800503 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 atomic_set(&x->refcnt, 1);
505 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700506 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700507 INIT_HLIST_NODE(&x->bydst);
508 INIT_HLIST_NODE(&x->bysrc);
509 INIT_HLIST_NODE(&x->byspi);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800510 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800511 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
512 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800513 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 x->lft.soft_byte_limit = XFRM_INF;
515 x->lft.soft_packet_limit = XFRM_INF;
516 x->lft.hard_byte_limit = XFRM_INF;
517 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800518 x->replay_maxage = 0;
519 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700520 x->inner_mode = NULL;
521 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 spin_lock_init(&x->lock);
523 }
524 return x;
525}
526EXPORT_SYMBOL(xfrm_state_alloc);
527
528void __xfrm_state_destroy(struct xfrm_state *x)
529{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800530 struct net *net = xs_net(x);
531
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700532 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800535 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800537 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539EXPORT_SYMBOL(__xfrm_state_destroy);
540
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800541int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800543 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700544 int err = -ESRCH;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (x->km.state != XFRM_STATE_DEAD) {
547 x->km.state = XFRM_STATE_DEAD;
548 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700549 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700550 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700551 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700552 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700553 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800554 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* All xfrm_state objects are created by xfrm_state_alloc.
558 * The xfrm_state_alloc call gives a reference, and that
559 * is what we are dropping here.
560 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800561 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700562 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700564
565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800567EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700569int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700571 int err;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700574 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700576
577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579EXPORT_SYMBOL(xfrm_state_delete);
580
Joy Latten4aa2e622007-06-04 19:05:57 -0400581#ifdef CONFIG_SECURITY_NETWORK_XFRM
582static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800583xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Joy Latten4aa2e622007-06-04 19:05:57 -0400585 int i, err = 0;
586
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800587 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400588 struct hlist_node *entry;
589 struct xfrm_state *x;
590
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800591 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400592 if (xfrm_id_proto_match(x->id.proto, proto) &&
593 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700594 xfrm_audit_state_delete(x, 0,
595 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400596 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700597 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400598 return err;
599 }
600 }
601 }
602
603 return err;
604}
605#else
606static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800607xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400608{
609 return 0;
610}
611#endif
612
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800613int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400614{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000615 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800618 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400619 if (err)
620 goto out;
621
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000622 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800623 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700624 struct hlist_node *entry;
625 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626restart:
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800627 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700629 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 xfrm_state_hold(x);
631 spin_unlock_bh(&xfrm_state_lock);
632
Joy Latten161a09e2006-11-27 13:11:54 -0600633 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700634 xfrm_audit_state_delete(x, err ? 0 : 1,
635 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400636 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700637 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000639 if (!err)
640 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 spin_lock_bh(&xfrm_state_lock);
643 goto restart;
644 }
645 }
646 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000647 if (cnt)
648 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400649
650out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800652 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655EXPORT_SYMBOL(xfrm_state_flush);
656
Alexey Dobriyane0710412010-01-23 13:37:10 +0000657void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700658{
659 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000660 si->sadcnt = net->xfrm.state_num;
661 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700662 si->sadhmcnt = xfrm_state_hashmax;
663 spin_unlock_bh(&xfrm_state_lock);
664}
665EXPORT_SYMBOL(xfrm_sad_getinfo);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667static int
David S. Miller1a898592011-02-22 18:22:34 -0800668xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500669 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500670 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700671 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
674 if (!afinfo)
675 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700676 afinfo->init_tempsel(&x->sel, fl);
677
678 if (family != tmpl->encap_family) {
679 xfrm_state_put_afinfo(afinfo);
680 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
681 if (!afinfo)
682 return -1;
683 }
684 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 xfrm_state_put_afinfo(afinfo);
686 return 0;
687}
688
David S. Miller9aa60082011-02-24 01:51:36 -0500689static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
690 const xfrm_address_t *daddr,
691 __be32 spi, u8 proto,
692 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700693{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800694 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700695 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700696 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700697
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800698 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700699 if (x->props.family != family ||
700 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000701 x->id.proto != proto ||
702 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700703 continue;
704
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000705 if ((mark & x->mark.m) != x->mark.v)
706 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700707 xfrm_state_hold(x);
708 return x;
709 }
710
711 return NULL;
712}
713
David S. Miller9aa60082011-02-24 01:51:36 -0500714static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
715 const xfrm_address_t *daddr,
716 const xfrm_address_t *saddr,
717 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700718{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800719 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700720 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700721 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700722
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800723 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700724 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000725 x->id.proto != proto ||
726 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
727 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700728 continue;
729
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000730 if ((mark & x->mark.m) != x->mark.v)
731 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700732 xfrm_state_hold(x);
733 return x;
734 }
735
736 return NULL;
737}
738
739static inline struct xfrm_state *
740__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
741{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800742 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800743 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800744
David S. Milleredcd5822006-08-24 00:42:45 -0700745 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800746 return __xfrm_state_lookup(net, mark, &x->id.daddr,
747 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700748 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800749 return __xfrm_state_lookup_byaddr(net, mark,
750 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700751 &x->props.saddr,
752 x->id.proto, family);
753}
754
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800755static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700756{
757 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800758 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
759 net->xfrm.state_num > net->xfrm.state_hmask)
760 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700761}
762
David S. Miller08ec9af2009-03-13 14:22:40 -0700763static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800764 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700765 struct xfrm_state **best, int *acq_in_progress,
766 int *error)
767{
768 /* Resolution logic:
769 * 1. There is a valid state with matching selector. Done.
770 * 2. Valid state with inappropriate selector. Skip.
771 *
772 * Entering area of "sysdeps".
773 *
774 * 3. If state is not valid, selector is temporary, it selects
775 * only session which triggered previous resolution. Key
776 * manager will do something to install a state with proper
777 * selector.
778 */
779 if (x->km.state == XFRM_STATE_VALID) {
780 if ((x->sel.family &&
781 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
782 !security_xfrm_state_pol_flow_match(x, pol, fl))
783 return;
784
785 if (!*best ||
786 (*best)->km.dying > x->km.dying ||
787 ((*best)->km.dying == x->km.dying &&
788 (*best)->curlft.add_time < x->curlft.add_time))
789 *best = x;
790 } else if (x->km.state == XFRM_STATE_ACQ) {
791 *acq_in_progress = 1;
792 } else if (x->km.state == XFRM_STATE_ERROR ||
793 x->km.state == XFRM_STATE_EXPIRED) {
794 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
795 security_xfrm_state_pol_flow_match(x, pol, fl))
796 *error = -ESRCH;
797 }
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500801xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800802 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct xfrm_policy *pol, int *err,
804 unsigned short family)
805{
David S. Miller08ec9af2009-03-13 14:22:40 -0700806 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800807 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700808 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700809 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700810 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int acquire_in_progress = 0;
812 int error = 0;
813 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800814 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700815 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900816
David S. Miller37b08e32008-09-02 20:14:15 -0700817 to_put = NULL;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700820 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800821 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700822 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000824 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700825 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700826 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 tmpl->mode == x->props.mode &&
828 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700829 (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);
832 }
833 if (best)
834 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Thomas Egerer8444cf72010-09-20 11:11:38 -0700836 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700837 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700838 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700839 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000840 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700841 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700842 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700843 tmpl->mode == x->props.mode &&
844 tmpl->id.proto == x->id.proto &&
845 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500846 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700847 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
David S. Miller08ec9af2009-03-13 14:22:40 -0700850found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 x = best;
852 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700853 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800854 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700855 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700856 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 error = -EEXIST;
858 goto out;
859 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800860 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (x == NULL) {
862 error = -ENOMEM;
863 goto out;
864 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700865 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700867 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800868 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
David S. Miller1d28f422011-03-12 00:29:39 -0500870 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700871 if (error) {
872 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700873 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700874 x = NULL;
875 goto out;
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (km_query(x, tmpl, pol) == 0) {
879 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800880 list_add(&x->km.all, &net->xfrm.state_all);
881 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700882 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800883 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700885 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800886 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800888 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800889 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800890 net->xfrm.state_num++;
891 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 } else {
893 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700894 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 x = NULL;
896 error = -ESRCH;
897 }
898 }
899out:
900 if (x)
901 xfrm_state_hold(x);
902 else
903 *err = acquire_in_progress ? -EAGAIN : error;
904 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700905 if (to_put)
906 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return x;
908}
909
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700910struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800911xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800912 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700913 unsigned short family, u8 mode, u8 proto, u32 reqid)
914{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800915 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700916 struct xfrm_state *rx = NULL, *x = NULL;
917 struct hlist_node *entry;
918
919 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800920 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
921 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700922 if (x->props.family == family &&
923 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000924 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700925 !(x->props.flags & XFRM_STATE_WILDRECV) &&
926 xfrm_state_addr_check(x, daddr, saddr, family) &&
927 mode == x->props.mode &&
928 proto == x->id.proto &&
929 x->km.state == XFRM_STATE_VALID) {
930 rx = x;
931 break;
932 }
933 }
934
935 if (rx)
936 xfrm_state_hold(rx);
937 spin_unlock(&xfrm_state_lock);
938
939
940 return rx;
941}
942EXPORT_SYMBOL(xfrm_stateonly_find);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944static void __xfrm_state_insert(struct xfrm_state *x)
945{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800946 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700947 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800949 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800950
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800951 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700952 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800953 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
956 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700958 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800959 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700960 x->props.family);
961
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800962 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700963 }
964
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800965 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700966 if (x->replay_maxage)
967 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800968
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800969 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700970
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800971 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700972
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800973 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
David S. Millerc7f5ea32006-08-24 03:29:04 -0700976/* xfrm_state_lock is held */
977static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
978{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800979 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700980 unsigned short family = xnew->props.family;
981 u32 reqid = xnew->props.reqid;
982 struct xfrm_state *x;
983 struct hlist_node *entry;
984 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000985 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700986
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800987 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
988 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700989 if (x->props.family == family &&
990 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000991 (mark & x->mark.m) == x->mark.v &&
David S. Millerc1969f22006-08-24 04:00:03 -0700992 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
993 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000994 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700995 }
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998void xfrm_state_insert(struct xfrm_state *x)
999{
1000 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001001 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 __xfrm_state_insert(x);
1003 spin_unlock_bh(&xfrm_state_lock);
1004}
1005EXPORT_SYMBOL(xfrm_state_insert);
1006
David S. Miller27708342006-08-24 00:13:10 -07001007/* xfrm_state_lock is held */
David S. Millera70486f2011-02-27 23:17:24 -08001008static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
1009 unsigned short family, u8 mode,
1010 u32 reqid, u8 proto,
1011 const xfrm_address_t *daddr,
1012 const xfrm_address_t *saddr, int create)
David S. Miller27708342006-08-24 00:13:10 -07001013{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001014 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -07001015 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -07001016 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001017 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001018
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001019 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001020 if (x->props.reqid != reqid ||
1021 x->props.mode != mode ||
1022 x->props.family != family ||
1023 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001024 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001025 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001026 (mark & x->mark.m) != x->mark.v ||
Wei Yongjun18025712009-06-28 18:42:53 +00001027 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1028 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001029 continue;
1030
David S. Miller27708342006-08-24 00:13:10 -07001031 xfrm_state_hold(x);
1032 return x;
1033 }
1034
1035 if (!create)
1036 return NULL;
1037
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001038 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001039 if (likely(x)) {
1040 switch (family) {
1041 case AF_INET:
1042 x->sel.daddr.a4 = daddr->a4;
1043 x->sel.saddr.a4 = saddr->a4;
1044 x->sel.prefixlen_d = 32;
1045 x->sel.prefixlen_s = 32;
1046 x->props.saddr.a4 = saddr->a4;
1047 x->id.daddr.a4 = daddr->a4;
1048 break;
1049
1050 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001051 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1052 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001053 x->sel.prefixlen_d = 128;
1054 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001055 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1056 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001057 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001058 }
David S. Miller27708342006-08-24 00:13:10 -07001059
1060 x->km.state = XFRM_STATE_ACQ;
1061 x->id.proto = proto;
1062 x->props.family = family;
1063 x->props.mode = mode;
1064 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001065 x->mark.v = m->v;
1066 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001067 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001068 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001069 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001070 list_add(&x->km.all, &net->xfrm.state_all);
1071 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1072 h = xfrm_src_hash(net, daddr, saddr, family);
1073 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001074
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001075 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001076
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001077 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001078 }
1079
1080 return x;
1081}
1082
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001083static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085int xfrm_state_add(struct xfrm_state *x)
1086{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001087 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001088 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 int family;
1090 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001091 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001092 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
David S. Miller37b08e32008-09-02 20:14:15 -07001096 to_put = NULL;
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 spin_lock_bh(&xfrm_state_lock);
1099
David S. Milleredcd5822006-08-24 00:42:45 -07001100 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001102 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 x1 = NULL;
1104 err = -EEXIST;
1105 goto out;
1106 }
1107
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001108 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001109 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001110 if (x1 && ((x1->id.proto != x->id.proto) ||
1111 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001112 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 x1 = NULL;
1114 }
1115 }
1116
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001117 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001118 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1119 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001120 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
David S. Millerc7f5ea32006-08-24 03:29:04 -07001122 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 __xfrm_state_insert(x);
1124 err = 0;
1125
1126out:
1127 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (x1) {
1130 xfrm_state_delete(x1);
1131 xfrm_state_put(x1);
1132 }
1133
David S. Miller37b08e32008-09-02 20:14:15 -07001134 if (to_put)
1135 xfrm_state_put(to_put);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return err;
1138}
1139EXPORT_SYMBOL(xfrm_state_add);
1140
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001141#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001142static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001143{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001144 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001145 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001146 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001147 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001148 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001149
1150 memcpy(&x->id, &orig->id, sizeof(x->id));
1151 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1152 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1153 x->props.mode = orig->props.mode;
1154 x->props.replay_window = orig->props.replay_window;
1155 x->props.reqid = orig->props.reqid;
1156 x->props.family = orig->props.family;
1157 x->props.saddr = orig->props.saddr;
1158
1159 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001160 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001161 if (!x->aalg)
1162 goto error;
1163 }
1164 x->props.aalgo = orig->props.aalgo;
1165
1166 if (orig->ealg) {
1167 x->ealg = xfrm_algo_clone(orig->ealg);
1168 if (!x->ealg)
1169 goto error;
1170 }
1171 x->props.ealgo = orig->props.ealgo;
1172
1173 if (orig->calg) {
1174 x->calg = xfrm_algo_clone(orig->calg);
1175 if (!x->calg)
1176 goto error;
1177 }
1178 x->props.calgo = orig->props.calgo;
1179
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001180 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001181 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1182 if (!x->encap)
1183 goto error;
1184 }
1185
1186 if (orig->coaddr) {
1187 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1188 GFP_KERNEL);
1189 if (!x->coaddr)
1190 goto error;
1191 }
1192
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001193 if (orig->replay_esn) {
1194 err = xfrm_replay_clone(x, orig);
1195 if (err)
1196 goto error;
1197 }
1198
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001199 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1200
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001201 err = xfrm_init_state(x);
1202 if (err)
1203 goto error;
1204
1205 x->props.flags = orig->props.flags;
1206
1207 x->curlft.add_time = orig->curlft.add_time;
1208 x->km.state = orig->km.state;
1209 x->km.seq = orig->km.seq;
1210
1211 return x;
1212
1213 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001214 xfrm_state_put(x);
1215out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001216 if (errp)
1217 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001218 return NULL;
1219}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001220
1221/* xfrm_state_lock is held */
1222struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1223{
1224 unsigned int h;
1225 struct xfrm_state *x;
1226 struct hlist_node *entry;
1227
1228 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001229 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001230 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001231 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001232 if (x->props.mode != m->mode ||
1233 x->id.proto != m->proto)
1234 continue;
1235 if (m->reqid && x->props.reqid != m->reqid)
1236 continue;
1237 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1238 m->old_family) ||
1239 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1240 m->old_family))
1241 continue;
1242 xfrm_state_hold(x);
1243 return x;
1244 }
1245 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001246 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001247 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001248 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001249 if (x->props.mode != m->mode ||
1250 x->id.proto != m->proto)
1251 continue;
1252 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1253 m->old_family) ||
1254 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1255 m->old_family))
1256 continue;
1257 xfrm_state_hold(x);
1258 return x;
1259 }
1260 }
1261
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001262 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001263}
1264EXPORT_SYMBOL(xfrm_migrate_state_find);
1265
1266struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1267 struct xfrm_migrate *m)
1268{
1269 struct xfrm_state *xc;
1270 int err;
1271
1272 xc = xfrm_state_clone(x, &err);
1273 if (!xc)
1274 return NULL;
1275
1276 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1277 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1278
1279 /* add state */
1280 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1281 /* a care is needed when the destination address of the
1282 state is to be updated as it is a part of triplet */
1283 xfrm_state_insert(xc);
1284 } else {
1285 if ((err = xfrm_state_add(xc)) < 0)
1286 goto error;
1287 }
1288
1289 return xc;
1290error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001291 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001292 return NULL;
1293}
1294EXPORT_SYMBOL(xfrm_state_migrate);
1295#endif
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297int xfrm_state_update(struct xfrm_state *x)
1298{
David S. Miller37b08e32008-09-02 20:14:15 -07001299 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001301 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
David S. Miller37b08e32008-09-02 20:14:15 -07001303 to_put = NULL;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001306 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 err = -ESRCH;
1309 if (!x1)
1310 goto out;
1311
1312 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001313 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 err = -EEXIST;
1315 goto out;
1316 }
1317
1318 if (x1->km.state == XFRM_STATE_ACQ) {
1319 __xfrm_state_insert(x);
1320 x = NULL;
1321 }
1322 err = 0;
1323
1324out:
1325 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
David S. Miller37b08e32008-09-02 20:14:15 -07001327 if (to_put)
1328 xfrm_state_put(to_put);
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (err)
1331 return err;
1332
1333 if (!x) {
1334 xfrm_state_delete(x1);
1335 xfrm_state_put(x1);
1336 return 0;
1337 }
1338
1339 err = -EINVAL;
1340 spin_lock_bh(&x1->lock);
1341 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1342 if (x->encap && x1->encap)
1343 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001344 if (x->coaddr && x1->coaddr) {
1345 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1346 }
1347 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1348 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1350 x1->km.dying = 0;
1351
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001352 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (x1->curlft.use_time)
1354 xfrm_state_check_expire(x1);
1355
1356 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001357 x->km.state = XFRM_STATE_DEAD;
1358 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 spin_unlock_bh(&x1->lock);
1361
1362 xfrm_state_put(x1);
1363
1364 return err;
1365}
1366EXPORT_SYMBOL(xfrm_state_update);
1367
1368int xfrm_state_check_expire(struct xfrm_state *x)
1369{
1370 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001371 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (x->km.state != XFRM_STATE_VALID)
1374 return -EINVAL;
1375
1376 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1377 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001378 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001379 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return -EINVAL;
1381 }
1382
1383 if (!x->km.dying &&
1384 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001385 x->curlft.packets >= x->lft.soft_packet_limit)) {
1386 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001387 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 0;
1390}
1391EXPORT_SYMBOL(xfrm_state_check_expire);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001394xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001395 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001400 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return x;
1403}
1404EXPORT_SYMBOL(xfrm_state_lookup);
1405
1406struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001407xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001408 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001409 u8 proto, unsigned short family)
1410{
1411 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001412
1413 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001414 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001415 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001416 return x;
1417}
1418EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1419
1420struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001421xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001422 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int create, unsigned short family)
1424{
1425 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001428 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return x;
1432}
1433EXPORT_SYMBOL(xfrm_find_acq);
1434
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001435#ifdef CONFIG_XFRM_SUB_POLICY
1436int
1437xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1438 unsigned short family)
1439{
1440 int err = 0;
1441 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1442 if (!afinfo)
1443 return -EAFNOSUPPORT;
1444
1445 spin_lock_bh(&xfrm_state_lock);
1446 if (afinfo->tmpl_sort)
1447 err = afinfo->tmpl_sort(dst, src, n);
1448 spin_unlock_bh(&xfrm_state_lock);
1449 xfrm_state_put_afinfo(afinfo);
1450 return err;
1451}
1452EXPORT_SYMBOL(xfrm_tmpl_sort);
1453
1454int
1455xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1456 unsigned short family)
1457{
1458 int err = 0;
1459 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1460 if (!afinfo)
1461 return -EAFNOSUPPORT;
1462
1463 spin_lock_bh(&xfrm_state_lock);
1464 if (afinfo->state_sort)
1465 err = afinfo->state_sort(dst, src, n);
1466 spin_unlock_bh(&xfrm_state_lock);
1467 xfrm_state_put_afinfo(afinfo);
1468 return err;
1469}
1470EXPORT_SYMBOL(xfrm_state_sort);
1471#endif
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/* Silly enough, but I'm lazy to build resolution list */
1474
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001475static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001479 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001480 struct hlist_node *entry;
1481 struct xfrm_state *x;
1482
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001483 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001484 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001485 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001486 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 xfrm_state_hold(x);
1488 return x;
1489 }
1490 }
1491 }
1492 return NULL;
1493}
1494
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001495struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct xfrm_state *x;
1498
1499 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001500 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 spin_unlock_bh(&xfrm_state_lock);
1502 return x;
1503}
1504EXPORT_SYMBOL(xfrm_find_acq_byseq);
1505
1506u32 xfrm_get_acqseq(void)
1507{
1508 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001509 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
jamal6836b9b2010-02-16 02:01:22 +00001511 do {
1512 res = atomic_inc_return(&acqseq);
1513 } while (!res);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return res;
1516}
1517EXPORT_SYMBOL(xfrm_get_acqseq);
1518
Herbert Xu658b2192007-10-09 13:29:52 -07001519int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001521 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001522 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001524 int err = -ENOENT;
1525 __be32 minspi = htonl(low);
1526 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001527 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Herbert Xu658b2192007-10-09 13:29:52 -07001529 spin_lock_bh(&x->lock);
1530 if (x->km.state == XFRM_STATE_DEAD)
1531 goto unlock;
1532
1533 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001535 goto unlock;
1536
1537 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001540 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (x0) {
1542 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001543 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 x->id.spi = minspi;
1546 } else {
1547 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001548 for (h=0; h<high-low+1; h++) {
1549 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001550 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (x0 == NULL) {
1552 x->id.spi = htonl(spi);
1553 break;
1554 }
1555 xfrm_state_put(x0);
1556 }
1557 }
1558 if (x->id.spi) {
1559 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001560 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1561 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001563
1564 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
Herbert Xu658b2192007-10-09 13:29:52 -07001566
1567unlock:
1568 spin_unlock_bh(&x->lock);
1569
1570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572EXPORT_SYMBOL(xfrm_alloc_spi);
1573
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001574int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001575 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 void *data)
1577{
Herbert Xu12a169e2008-10-01 07:03:24 -07001578 struct xfrm_state *state;
1579 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int err = 0;
1581
Herbert Xu12a169e2008-10-01 07:03:24 -07001582 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001583 return 0;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001586 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001587 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001588 else
1589 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001590 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001591 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001592 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001593 state = container_of(x, struct xfrm_state, km);
1594 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001595 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001596 err = func(state, walk->seq, data);
1597 if (err) {
1598 list_move_tail(&walk->all, &x->all);
1599 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001601 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001603 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 err = -ENOENT;
1605 goto out;
1606 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001607 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608out:
1609 spin_unlock_bh(&xfrm_state_lock);
1610 return err;
1611}
1612EXPORT_SYMBOL(xfrm_state_walk);
1613
Herbert Xu5c182452008-09-22 19:48:19 -07001614void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1615{
Herbert Xu12a169e2008-10-01 07:03:24 -07001616 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001617 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001618 walk->state = XFRM_STATE_DEAD;
1619 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001620}
1621EXPORT_SYMBOL(xfrm_state_walk_init);
1622
Herbert Xuabb81c42008-09-09 19:58:29 -07001623void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1624{
Herbert Xu12a169e2008-10-01 07:03:24 -07001625 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001626 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001627
Herbert Xu12a169e2008-10-01 07:03:24 -07001628 spin_lock_bh(&xfrm_state_lock);
1629 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001630 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001631}
1632EXPORT_SYMBOL(xfrm_state_walk_done);
1633
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001634static void xfrm_replay_timer_handler(unsigned long data)
1635{
1636 struct xfrm_state *x = (struct xfrm_state*)data;
1637
1638 spin_lock(&x->lock);
1639
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001640 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001641 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001642 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001643 else
1644 x->xflags |= XFRM_TIME_DEFER;
1645 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001646
1647 spin_unlock(&x->lock);
1648}
1649
Denis Chengdf018122007-12-07 00:51:11 -08001650static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651static DEFINE_RWLOCK(xfrm_km_lock);
1652
David S. Miller214e0052011-02-24 00:02:38 -05001653void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
1655 struct xfrm_mgr *km;
1656
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001657 read_lock(&xfrm_km_lock);
1658 list_for_each_entry(km, &xfrm_km_list, list)
1659 if (km->notify_policy)
1660 km->notify_policy(xp, dir, c);
1661 read_unlock(&xfrm_km_lock);
1662}
1663
David S. Miller214e0052011-02-24 00:02:38 -05001664void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001665{
1666 struct xfrm_mgr *km;
1667 read_lock(&xfrm_km_lock);
1668 list_for_each_entry(km, &xfrm_km_list, list)
1669 if (km->notify)
1670 km->notify(x, c);
1671 read_unlock(&xfrm_km_lock);
1672}
1673
1674EXPORT_SYMBOL(km_policy_notify);
1675EXPORT_SYMBOL(km_state_notify);
1676
Eric W. Biederman15e47302012-09-07 20:12:54 +00001677void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001678{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001679 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 struct km_event c;
1681
Herbert Xubf088672005-06-18 22:44:00 -07001682 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001683 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001684 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001685 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001688 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001691EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001692/*
1693 * We send to all registered managers regardless of failure
1694 * We are happy with one success
1695*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001696int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001698 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct xfrm_mgr *km;
1700
1701 read_lock(&xfrm_km_lock);
1702 list_for_each_entry(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001703 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001704 if (!acqret)
1705 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 read_unlock(&xfrm_km_lock);
1708 return err;
1709}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001710EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Al Viro5d36b182006-11-08 00:24:06 -08001712int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int err = -EINVAL;
1715 struct xfrm_mgr *km;
1716
1717 read_lock(&xfrm_km_lock);
1718 list_for_each_entry(km, &xfrm_km_list, list) {
1719 if (km->new_mapping)
1720 err = km->new_mapping(x, ipaddr, sport);
1721 if (!err)
1722 break;
1723 }
1724 read_unlock(&xfrm_km_lock);
1725 return err;
1726}
1727EXPORT_SYMBOL(km_new_mapping);
1728
Eric W. Biederman15e47302012-09-07 20:12:54 +00001729void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001731 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001732 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Herbert Xubf088672005-06-18 22:44:00 -07001734 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001735 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001736 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001737 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001740 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
David S. Millera70fcb02006-03-20 19:18:52 -08001742EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001744#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001745int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1746 const struct xfrm_migrate *m, int num_migrate,
1747 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001748{
1749 int err = -EINVAL;
1750 int ret;
1751 struct xfrm_mgr *km;
1752
1753 read_lock(&xfrm_km_lock);
1754 list_for_each_entry(km, &xfrm_km_list, list) {
1755 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001756 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001757 if (!ret)
1758 err = ret;
1759 }
1760 }
1761 read_unlock(&xfrm_km_lock);
1762 return err;
1763}
1764EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001765#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001766
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001767int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001768{
1769 int err = -EINVAL;
1770 int ret;
1771 struct xfrm_mgr *km;
1772
1773 read_lock(&xfrm_km_lock);
1774 list_for_each_entry(km, &xfrm_km_list, list) {
1775 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001776 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001777 if (!ret)
1778 err = ret;
1779 }
1780 }
1781 read_unlock(&xfrm_km_lock);
1782 return err;
1783}
1784EXPORT_SYMBOL(km_report);
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1787{
1788 int err;
1789 u8 *data;
1790 struct xfrm_mgr *km;
1791 struct xfrm_policy *pol = NULL;
1792
1793 if (optlen <= 0 || optlen > PAGE_SIZE)
1794 return -EMSGSIZE;
1795
1796 data = kmalloc(optlen, GFP_KERNEL);
1797 if (!data)
1798 return -ENOMEM;
1799
1800 err = -EFAULT;
1801 if (copy_from_user(data, optval, optlen))
1802 goto out;
1803
1804 err = -EINVAL;
1805 read_lock(&xfrm_km_lock);
1806 list_for_each_entry(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001807 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 optlen, &err);
1809 if (err >= 0)
1810 break;
1811 }
1812 read_unlock(&xfrm_km_lock);
1813
1814 if (err >= 0) {
1815 xfrm_sk_policy_insert(sk, err, pol);
1816 xfrm_pol_put(pol);
1817 err = 0;
1818 }
1819
1820out:
1821 kfree(data);
1822 return err;
1823}
1824EXPORT_SYMBOL(xfrm_user_policy);
1825
1826int xfrm_register_km(struct xfrm_mgr *km)
1827{
1828 write_lock_bh(&xfrm_km_lock);
1829 list_add_tail(&km->list, &xfrm_km_list);
1830 write_unlock_bh(&xfrm_km_lock);
1831 return 0;
1832}
1833EXPORT_SYMBOL(xfrm_register_km);
1834
1835int xfrm_unregister_km(struct xfrm_mgr *km)
1836{
1837 write_lock_bh(&xfrm_km_lock);
1838 list_del(&km->list);
1839 write_unlock_bh(&xfrm_km_lock);
1840 return 0;
1841}
1842EXPORT_SYMBOL(xfrm_unregister_km);
1843
1844int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1845{
1846 int err = 0;
1847 if (unlikely(afinfo == NULL))
1848 return -EINVAL;
1849 if (unlikely(afinfo->family >= NPROTO))
1850 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001851 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1853 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001854 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 xfrm_state_afinfo[afinfo->family] = afinfo;
Ingo Molnarf3111502006-04-28 15:30:03 -07001856 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return err;
1858}
1859EXPORT_SYMBOL(xfrm_state_register_afinfo);
1860
1861int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1862{
1863 int err = 0;
1864 if (unlikely(afinfo == NULL))
1865 return -EINVAL;
1866 if (unlikely(afinfo->family >= NPROTO))
1867 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001868 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1870 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1871 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001872 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 xfrm_state_afinfo[afinfo->family] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Ingo Molnarf3111502006-04-28 15:30:03 -07001875 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return err;
1877}
1878EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1879
Herbert Xu17c2a422007-10-17 21:33:12 -07001880static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct xfrm_state_afinfo *afinfo;
1883 if (unlikely(family >= NPROTO))
1884 return NULL;
1885 read_lock(&xfrm_state_afinfo_lock);
1886 afinfo = xfrm_state_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001887 if (unlikely(!afinfo))
1888 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return afinfo;
1890}
1891
Herbert Xu17c2a422007-10-17 21:33:12 -07001892static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001893 __releases(xfrm_state_afinfo_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Herbert Xu546be242006-05-27 23:03:58 -07001895 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
1898/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1899void xfrm_state_delete_tunnel(struct xfrm_state *x)
1900{
1901 if (x->tunnel) {
1902 struct xfrm_state *t = x->tunnel;
1903
1904 if (atomic_read(&t->tunnel_users) == 2)
1905 xfrm_state_delete(t);
1906 atomic_dec(&t->tunnel_users);
1907 xfrm_state_put(t);
1908 x->tunnel = NULL;
1909 }
1910}
1911EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1912
1913int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1914{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001915 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Patrick McHardyc5c25232007-04-09 11:47:18 -07001917 spin_lock_bh(&x->lock);
1918 if (x->km.state == XFRM_STATE_VALID &&
1919 x->type && x->type->get_mtu)
1920 res = x->type->get_mtu(x, mtu);
1921 else
Patrick McHardy28121612007-06-18 22:30:15 -07001922 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001923 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return res;
1925}
1926
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001927int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001928{
Herbert Xud094cd82005-06-20 13:19:41 -07001929 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001930 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001931 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001932 int err;
1933
Herbert Xud094cd82005-06-20 13:19:41 -07001934 err = -EAFNOSUPPORT;
1935 afinfo = xfrm_state_get_afinfo(family);
1936 if (!afinfo)
1937 goto error;
1938
1939 err = 0;
1940 if (afinfo->init_flags)
1941 err = afinfo->init_flags(x);
1942
1943 xfrm_state_put_afinfo(afinfo);
1944
1945 if (err)
1946 goto error;
1947
1948 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001949
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001950 if (x->sel.family != AF_UNSPEC) {
1951 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1952 if (inner_mode == NULL)
1953 goto error;
1954
1955 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1956 family != x->sel.family) {
1957 xfrm_put_mode(inner_mode);
1958 goto error;
1959 }
1960
1961 x->inner_mode = inner_mode;
1962 } else {
1963 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001964 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001965
Martin Willid81d2282008-12-03 15:38:07 -08001966 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001967 if (inner_mode == NULL)
1968 goto error;
1969
1970 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1971 xfrm_put_mode(inner_mode);
1972 goto error;
1973 }
Martin Willid81d2282008-12-03 15:38:07 -08001974 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001975
Martin Willid81d2282008-12-03 15:38:07 -08001976 if (x->props.family == AF_INET)
1977 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001978
Martin Willid81d2282008-12-03 15:38:07 -08001979 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1980 if (inner_mode_iaf) {
1981 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1982 x->inner_mode_iaf = inner_mode_iaf;
1983 else
1984 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001985 }
1986 }
Herbert Xu13996372007-10-17 21:35:51 -07001987
Herbert Xud094cd82005-06-20 13:19:41 -07001988 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001989 if (x->type == NULL)
1990 goto error;
1991
1992 err = x->type->init_state(x);
1993 if (err)
1994 goto error;
1995
Herbert Xu13996372007-10-17 21:35:51 -07001996 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00001997 if (x->outer_mode == NULL) {
1998 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07001999 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002000 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002001
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002002 if (init_replay) {
2003 err = xfrm_init_replay(x);
2004 if (err)
2005 goto error;
2006 }
2007
Herbert Xu72cb6962005-06-20 13:18:08 -07002008 x->km.state = XFRM_STATE_VALID;
2009
2010error:
2011 return err;
2012}
2013
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002014EXPORT_SYMBOL(__xfrm_init_state);
2015
2016int xfrm_init_state(struct xfrm_state *x)
2017{
2018 return __xfrm_init_state(x, true);
2019}
2020
Herbert Xu72cb6962005-06-20 13:18:08 -07002021EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002022
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002023int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
David S. Millerf034b5d2006-08-24 03:08:07 -07002025 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002027 INIT_LIST_HEAD(&net->xfrm.state_all);
2028
David S. Millerf034b5d2006-08-24 03:08:07 -07002029 sz = sizeof(struct hlist_head) * 8;
2030
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002031 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2032 if (!net->xfrm.state_bydst)
2033 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002034 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2035 if (!net->xfrm.state_bysrc)
2036 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002037 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2038 if (!net->xfrm.state_byspi)
2039 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002040 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002041
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002042 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002043 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002044 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002045 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002046 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002047 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002048
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002049out_byspi:
2050 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002051out_bysrc:
2052 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002053out_bydst:
2054 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002055}
2056
2057void xfrm_state_fini(struct net *net)
2058{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002059 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002060 unsigned int sz;
2061
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002062 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002063 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002064 audit_info.sessionid = -1;
2065 audit_info.secid = 0;
2066 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2067 flush_work(&net->xfrm.state_gc_work);
2068
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002069 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002070
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002071 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002072 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2073 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002074 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2075 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002076 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2077 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Joy Lattenab5f5e82007-09-17 11:51:22 -07002080#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002081static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2082 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002083{
Paul Moore68277ac2007-12-20 20:49:33 -08002084 struct xfrm_sec_ctx *ctx = x->security;
2085 u32 spi = ntohl(x->id.spi);
2086
2087 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002088 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002089 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002090
2091 switch(x->props.family) {
2092 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002093 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2094 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002095 break;
2096 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002097 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002098 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002099 break;
2100 }
Paul Moore68277ac2007-12-20 20:49:33 -08002101
2102 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002103}
2104
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002105static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2106 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002107{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002108 const struct iphdr *iph4;
2109 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002110
2111 switch (family) {
2112 case AF_INET:
2113 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002114 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2115 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002116 break;
2117 case AF_INET6:
2118 iph6 = ipv6_hdr(skb);
2119 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002120 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002121 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002122 iph6->flow_lbl[0] & 0x0f,
2123 iph6->flow_lbl[1],
2124 iph6->flow_lbl[2]);
2125 break;
2126 }
2127}
2128
Paul Moore68277ac2007-12-20 20:49:33 -08002129void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002130 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002131{
2132 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002133
Paul Mooreafeb14b2007-12-21 14:58:11 -08002134 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002135 if (audit_buf == NULL)
2136 return;
Eric Paris25323862008-04-18 10:09:25 -04002137 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002138 xfrm_audit_helper_sainfo(x, audit_buf);
2139 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002140 audit_log_end(audit_buf);
2141}
2142EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2143
Paul Moore68277ac2007-12-20 20:49:33 -08002144void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002145 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002146{
2147 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002148
Paul Mooreafeb14b2007-12-21 14:58:11 -08002149 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002150 if (audit_buf == NULL)
2151 return;
Eric Paris25323862008-04-18 10:09:25 -04002152 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002153 xfrm_audit_helper_sainfo(x, audit_buf);
2154 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002155 audit_log_end(audit_buf);
2156}
2157EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002158
2159void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2160 struct sk_buff *skb)
2161{
2162 struct audit_buffer *audit_buf;
2163 u32 spi;
2164
2165 audit_buf = xfrm_audit_start("SA-replay-overflow");
2166 if (audit_buf == NULL)
2167 return;
2168 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2169 /* don't record the sequence number because it's inherent in this kind
2170 * of audit message */
2171 spi = ntohl(x->id.spi);
2172 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2173 audit_log_end(audit_buf);
2174}
2175EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2176
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002177void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002178 struct sk_buff *skb, __be32 net_seq)
2179{
2180 struct audit_buffer *audit_buf;
2181 u32 spi;
2182
2183 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2184 if (audit_buf == NULL)
2185 return;
2186 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2187 spi = ntohl(x->id.spi);
2188 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2189 spi, spi, ntohl(net_seq));
2190 audit_log_end(audit_buf);
2191}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002192EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002193
2194void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2195{
2196 struct audit_buffer *audit_buf;
2197
2198 audit_buf = xfrm_audit_start("SA-notfound");
2199 if (audit_buf == NULL)
2200 return;
2201 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2202 audit_log_end(audit_buf);
2203}
2204EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2205
2206void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2207 __be32 net_spi, __be32 net_seq)
2208{
2209 struct audit_buffer *audit_buf;
2210 u32 spi;
2211
2212 audit_buf = xfrm_audit_start("SA-notfound");
2213 if (audit_buf == NULL)
2214 return;
2215 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2216 spi = ntohl(net_spi);
2217 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2218 spi, spi, ntohl(net_seq));
2219 audit_log_end(audit_buf);
2220}
2221EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2222
2223void xfrm_audit_state_icvfail(struct xfrm_state *x,
2224 struct sk_buff *skb, u8 proto)
2225{
2226 struct audit_buffer *audit_buf;
2227 __be32 net_spi;
2228 __be32 net_seq;
2229
2230 audit_buf = xfrm_audit_start("SA-icv-failure");
2231 if (audit_buf == NULL)
2232 return;
2233 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2234 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2235 u32 spi = ntohl(net_spi);
2236 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2237 spi, spi, ntohl(net_seq));
2238 }
2239 audit_log_end(audit_buf);
2240}
2241EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002242#endif /* CONFIG_AUDITSYSCALL */