blob: 9414b9c5b1e4284b9ed90e9fe3ff39cdfc07e2ce [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);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800169void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
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;
418 if (tmo <= 0)
419 goto expired;
420 if (tmo < next)
421 next = tmo;
422 }
423 if (x->lft.hard_use_expires_seconds) {
424 long tmo = x->lft.hard_use_expires_seconds +
425 (x->curlft.use_time ? : now) - now;
426 if (tmo <= 0)
427 goto expired;
428 if (tmo < next)
429 next = tmo;
430 }
431 if (x->km.dying)
432 goto resched;
433 if (x->lft.soft_add_expires_seconds) {
434 long tmo = x->lft.soft_add_expires_seconds +
435 x->curlft.add_time - now;
436 if (tmo <= 0)
437 warn = 1;
438 else if (tmo < next)
439 next = tmo;
440 }
441 if (x->lft.soft_use_expires_seconds) {
442 long tmo = x->lft.soft_use_expires_seconds +
443 (x->curlft.use_time ? : now) - now;
444 if (tmo <= 0)
445 warn = 1;
446 else if (tmo < next)
447 next = tmo;
448 }
449
Herbert Xu4666faa2005-06-18 22:43:22 -0700450 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800452 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800454 if (next != LONG_MAX){
455 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
456 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto out;
459
460expired:
461 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
462 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800463 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 next = 2;
465 goto resched;
466 }
Joy Latten161a09e2006-11-27 13:11:54 -0600467
468 err = __xfrm_state_delete(x);
469 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800470 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Joy Lattenab5f5e82007-09-17 11:51:22 -0700472 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400473 audit_get_loginuid(current),
474 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476out:
477 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800478 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
David S. Miller0ac84752006-03-20 19:18:23 -0800481static void xfrm_replay_timer_handler(unsigned long data);
482
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800483struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct xfrm_state *x;
486
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700487 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800490 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 atomic_set(&x->refcnt, 1);
492 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700493 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700494 INIT_HLIST_NODE(&x->bydst);
495 INIT_HLIST_NODE(&x->bysrc);
496 INIT_HLIST_NODE(&x->byspi);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800497 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800498 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
499 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800500 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 x->lft.soft_byte_limit = XFRM_INF;
502 x->lft.soft_packet_limit = XFRM_INF;
503 x->lft.hard_byte_limit = XFRM_INF;
504 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800505 x->replay_maxage = 0;
506 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700507 x->inner_mode = NULL;
508 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 spin_lock_init(&x->lock);
510 }
511 return x;
512}
513EXPORT_SYMBOL(xfrm_state_alloc);
514
515void __xfrm_state_destroy(struct xfrm_state *x)
516{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800517 struct net *net = xs_net(x);
518
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700519 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800522 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800524 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526EXPORT_SYMBOL(__xfrm_state_destroy);
527
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800528int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800530 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700531 int err = -ESRCH;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (x->km.state != XFRM_STATE_DEAD) {
534 x->km.state = XFRM_STATE_DEAD;
535 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700536 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700537 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700538 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700539 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700540 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800541 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* All xfrm_state objects are created by xfrm_state_alloc.
545 * The xfrm_state_alloc call gives a reference, and that
546 * is what we are dropping here.
547 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800548 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700549 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700551
552 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800554EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700556int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700558 int err;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700561 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700563
564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566EXPORT_SYMBOL(xfrm_state_delete);
567
Joy Latten4aa2e622007-06-04 19:05:57 -0400568#ifdef CONFIG_SECURITY_NETWORK_XFRM
569static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800570xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Joy Latten4aa2e622007-06-04 19:05:57 -0400572 int i, err = 0;
573
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800574 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400575 struct hlist_node *entry;
576 struct xfrm_state *x;
577
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800578 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400579 if (xfrm_id_proto_match(x->id.proto, proto) &&
580 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700581 xfrm_audit_state_delete(x, 0,
582 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400583 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700584 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400585 return err;
586 }
587 }
588 }
589
590 return err;
591}
592#else
593static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800594xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400595{
596 return 0;
597}
598#endif
599
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800600int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400601{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000602 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800605 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400606 if (err)
607 goto out;
608
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000609 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800610 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700611 struct hlist_node *entry;
612 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613restart:
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800614 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700616 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfrm_state_hold(x);
618 spin_unlock_bh(&xfrm_state_lock);
619
Joy Latten161a09e2006-11-27 13:11:54 -0600620 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700621 xfrm_audit_state_delete(x, err ? 0 : 1,
622 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400623 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700624 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000626 if (!err)
627 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 spin_lock_bh(&xfrm_state_lock);
630 goto restart;
631 }
632 }
633 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000634 if (cnt)
635 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400636
637out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800639 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642EXPORT_SYMBOL(xfrm_state_flush);
643
Alexey Dobriyane0710412010-01-23 13:37:10 +0000644void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700645{
646 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000647 si->sadcnt = net->xfrm.state_num;
648 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700649 si->sadhmcnt = xfrm_state_hashmax;
650 spin_unlock_bh(&xfrm_state_lock);
651}
652EXPORT_SYMBOL(xfrm_sad_getinfo);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int
David S. Miller1a898592011-02-22 18:22:34 -0800655xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500656 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500657 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700658 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
661 if (!afinfo)
662 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700663 afinfo->init_tempsel(&x->sel, fl);
664
665 if (family != tmpl->encap_family) {
666 xfrm_state_put_afinfo(afinfo);
667 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
668 if (!afinfo)
669 return -1;
670 }
671 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 xfrm_state_put_afinfo(afinfo);
673 return 0;
674}
675
David S. Miller9aa60082011-02-24 01:51:36 -0500676static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
677 const xfrm_address_t *daddr,
678 __be32 spi, u8 proto,
679 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700680{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800681 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700682 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700683 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700684
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800685 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700686 if (x->props.family != family ||
687 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000688 x->id.proto != proto ||
689 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700690 continue;
691
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000692 if ((mark & x->mark.m) != x->mark.v)
693 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700694 xfrm_state_hold(x);
695 return x;
696 }
697
698 return NULL;
699}
700
David S. Miller9aa60082011-02-24 01:51:36 -0500701static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
702 const xfrm_address_t *daddr,
703 const xfrm_address_t *saddr,
704 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700705{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800706 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700707 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700708 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700709
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800710 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700711 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000712 x->id.proto != proto ||
713 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
714 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700715 continue;
716
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000717 if ((mark & x->mark.m) != x->mark.v)
718 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700719 xfrm_state_hold(x);
720 return x;
721 }
722
723 return NULL;
724}
725
726static inline struct xfrm_state *
727__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
728{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800729 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800730 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800731
David S. Milleredcd5822006-08-24 00:42:45 -0700732 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800733 return __xfrm_state_lookup(net, mark, &x->id.daddr,
734 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700735 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800736 return __xfrm_state_lookup_byaddr(net, mark,
737 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700738 &x->props.saddr,
739 x->id.proto, family);
740}
741
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800742static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700743{
744 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800745 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
746 net->xfrm.state_num > net->xfrm.state_hmask)
747 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700748}
749
David S. Miller08ec9af2009-03-13 14:22:40 -0700750static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800751 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700752 struct xfrm_state **best, int *acq_in_progress,
753 int *error)
754{
755 /* Resolution logic:
756 * 1. There is a valid state with matching selector. Done.
757 * 2. Valid state with inappropriate selector. Skip.
758 *
759 * Entering area of "sysdeps".
760 *
761 * 3. If state is not valid, selector is temporary, it selects
762 * only session which triggered previous resolution. Key
763 * manager will do something to install a state with proper
764 * selector.
765 */
766 if (x->km.state == XFRM_STATE_VALID) {
767 if ((x->sel.family &&
768 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
769 !security_xfrm_state_pol_flow_match(x, pol, fl))
770 return;
771
772 if (!*best ||
773 (*best)->km.dying > x->km.dying ||
774 ((*best)->km.dying == x->km.dying &&
775 (*best)->curlft.add_time < x->curlft.add_time))
776 *best = x;
777 } else if (x->km.state == XFRM_STATE_ACQ) {
778 *acq_in_progress = 1;
779 } else if (x->km.state == XFRM_STATE_ERROR ||
780 x->km.state == XFRM_STATE_EXPIRED) {
781 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
782 security_xfrm_state_pol_flow_match(x, pol, fl))
783 *error = -ESRCH;
784 }
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500788xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800789 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct xfrm_policy *pol, int *err,
791 unsigned short family)
792{
David S. Miller08ec9af2009-03-13 14:22:40 -0700793 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800794 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700795 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700796 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700797 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int acquire_in_progress = 0;
799 int error = 0;
800 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800801 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700802 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900803
David S. Miller37b08e32008-09-02 20:14:15 -0700804 to_put = NULL;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700807 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800808 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700809 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000811 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700812 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700813 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 tmpl->mode == x->props.mode &&
815 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700816 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500817 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700818 &best, &acquire_in_progress, &error);
819 }
820 if (best)
821 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Thomas Egerer8444cf72010-09-20 11:11:38 -0700823 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700824 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700825 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700826 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000827 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700828 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700829 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700830 tmpl->mode == x->props.mode &&
831 tmpl->id.proto == x->id.proto &&
832 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500833 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700834 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
David S. Miller08ec9af2009-03-13 14:22:40 -0700837found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 x = best;
839 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700840 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800841 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700842 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700843 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 error = -EEXIST;
845 goto out;
846 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800847 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (x == NULL) {
849 error = -ENOMEM;
850 goto out;
851 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700852 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700854 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800855 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
David S. Miller1d28f422011-03-12 00:29:39 -0500857 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700858 if (error) {
859 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700860 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700861 x = NULL;
862 goto out;
863 }
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (km_query(x, tmpl, pol) == 0) {
866 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800867 list_add(&x->km.all, &net->xfrm.state_all);
868 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700869 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800870 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700872 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800873 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800875 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800876 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800877 net->xfrm.state_num++;
878 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 } else {
880 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700881 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 x = NULL;
883 error = -ESRCH;
884 }
885 }
886out:
887 if (x)
888 xfrm_state_hold(x);
889 else
890 *err = acquire_in_progress ? -EAGAIN : error;
891 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700892 if (to_put)
893 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return x;
895}
896
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700897struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800898xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800899 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700900 unsigned short family, u8 mode, u8 proto, u32 reqid)
901{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800902 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700903 struct xfrm_state *rx = NULL, *x = NULL;
904 struct hlist_node *entry;
905
906 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800907 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
908 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909 if (x->props.family == family &&
910 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000911 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700912 !(x->props.flags & XFRM_STATE_WILDRECV) &&
913 xfrm_state_addr_check(x, daddr, saddr, family) &&
914 mode == x->props.mode &&
915 proto == x->id.proto &&
916 x->km.state == XFRM_STATE_VALID) {
917 rx = x;
918 break;
919 }
920 }
921
922 if (rx)
923 xfrm_state_hold(rx);
924 spin_unlock(&xfrm_state_lock);
925
926
927 return rx;
928}
929EXPORT_SYMBOL(xfrm_stateonly_find);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931static void __xfrm_state_insert(struct xfrm_state *x)
932{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800933 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700934 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800936 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800937
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800938 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700939 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800940 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800942 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
943 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700945 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800946 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700947 x->props.family);
948
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800949 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700950 }
951
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800952 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700953 if (x->replay_maxage)
954 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800955
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800956 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700957
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800958 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700959
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800960 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
David S. Millerc7f5ea32006-08-24 03:29:04 -0700963/* xfrm_state_lock is held */
964static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
965{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800966 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700967 unsigned short family = xnew->props.family;
968 u32 reqid = xnew->props.reqid;
969 struct xfrm_state *x;
970 struct hlist_node *entry;
971 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000972 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700973
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800974 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
975 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700976 if (x->props.family == family &&
977 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000978 (mark & x->mark.m) == x->mark.v &&
David S. Millerc1969f22006-08-24 04:00:03 -0700979 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
980 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000981 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700982 }
983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985void xfrm_state_insert(struct xfrm_state *x)
986{
987 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700988 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 __xfrm_state_insert(x);
990 spin_unlock_bh(&xfrm_state_lock);
991}
992EXPORT_SYMBOL(xfrm_state_insert);
993
David S. Miller27708342006-08-24 00:13:10 -0700994/* xfrm_state_lock is held */
David S. Millera70486f2011-02-27 23:17:24 -0800995static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
996 unsigned short family, u8 mode,
997 u32 reqid, u8 proto,
998 const xfrm_address_t *daddr,
999 const xfrm_address_t *saddr, int create)
David S. Miller27708342006-08-24 00:13:10 -07001000{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001001 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -07001002 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -07001003 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001004 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001005
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001006 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001007 if (x->props.reqid != reqid ||
1008 x->props.mode != mode ||
1009 x->props.family != family ||
1010 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001011 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001012 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001013 (mark & x->mark.m) != x->mark.v ||
Wei Yongjun18025712009-06-28 18:42:53 +00001014 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1015 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001016 continue;
1017
David S. Miller27708342006-08-24 00:13:10 -07001018 xfrm_state_hold(x);
1019 return x;
1020 }
1021
1022 if (!create)
1023 return NULL;
1024
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001025 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001026 if (likely(x)) {
1027 switch (family) {
1028 case AF_INET:
1029 x->sel.daddr.a4 = daddr->a4;
1030 x->sel.saddr.a4 = saddr->a4;
1031 x->sel.prefixlen_d = 32;
1032 x->sel.prefixlen_s = 32;
1033 x->props.saddr.a4 = saddr->a4;
1034 x->id.daddr.a4 = daddr->a4;
1035 break;
1036
1037 case AF_INET6:
1038 ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001039 (const struct in6_addr *)daddr);
David S. Miller27708342006-08-24 00:13:10 -07001040 ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001041 (const struct in6_addr *)saddr);
David S. Miller27708342006-08-24 00:13:10 -07001042 x->sel.prefixlen_d = 128;
1043 x->sel.prefixlen_s = 128;
1044 ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001045 (const struct in6_addr *)saddr);
David S. Miller27708342006-08-24 00:13:10 -07001046 ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001047 (const struct in6_addr *)daddr);
David S. Miller27708342006-08-24 00:13:10 -07001048 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001049 }
David S. Miller27708342006-08-24 00:13:10 -07001050
1051 x->km.state = XFRM_STATE_ACQ;
1052 x->id.proto = proto;
1053 x->props.family = family;
1054 x->props.mode = mode;
1055 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001056 x->mark.v = m->v;
1057 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001058 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001059 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001060 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001061 list_add(&x->km.all, &net->xfrm.state_all);
1062 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1063 h = xfrm_src_hash(net, daddr, saddr, family);
1064 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001065
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001066 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001067
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001068 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001069 }
1070
1071 return x;
1072}
1073
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001074static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076int xfrm_state_add(struct xfrm_state *x)
1077{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001078 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001079 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int family;
1081 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001082 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001083 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
David S. Miller37b08e32008-09-02 20:14:15 -07001087 to_put = NULL;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 spin_lock_bh(&xfrm_state_lock);
1090
David S. Milleredcd5822006-08-24 00:42:45 -07001091 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001093 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 x1 = NULL;
1095 err = -EEXIST;
1096 goto out;
1097 }
1098
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001099 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001100 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001101 if (x1 && ((x1->id.proto != x->id.proto) ||
1102 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001103 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 x1 = NULL;
1105 }
1106 }
1107
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001108 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001109 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1110 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001111 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
David S. Millerc7f5ea32006-08-24 03:29:04 -07001113 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 __xfrm_state_insert(x);
1115 err = 0;
1116
1117out:
1118 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if (x1) {
1121 xfrm_state_delete(x1);
1122 xfrm_state_put(x1);
1123 }
1124
David S. Miller37b08e32008-09-02 20:14:15 -07001125 if (to_put)
1126 xfrm_state_put(to_put);
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return err;
1129}
1130EXPORT_SYMBOL(xfrm_state_add);
1131
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001132#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001133static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001134{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001135 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001136 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001138 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001139 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001140
1141 memcpy(&x->id, &orig->id, sizeof(x->id));
1142 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1143 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1144 x->props.mode = orig->props.mode;
1145 x->props.replay_window = orig->props.replay_window;
1146 x->props.reqid = orig->props.reqid;
1147 x->props.family = orig->props.family;
1148 x->props.saddr = orig->props.saddr;
1149
1150 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001151 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001152 if (!x->aalg)
1153 goto error;
1154 }
1155 x->props.aalgo = orig->props.aalgo;
1156
1157 if (orig->ealg) {
1158 x->ealg = xfrm_algo_clone(orig->ealg);
1159 if (!x->ealg)
1160 goto error;
1161 }
1162 x->props.ealgo = orig->props.ealgo;
1163
1164 if (orig->calg) {
1165 x->calg = xfrm_algo_clone(orig->calg);
1166 if (!x->calg)
1167 goto error;
1168 }
1169 x->props.calgo = orig->props.calgo;
1170
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001171 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001172 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1173 if (!x->encap)
1174 goto error;
1175 }
1176
1177 if (orig->coaddr) {
1178 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1179 GFP_KERNEL);
1180 if (!x->coaddr)
1181 goto error;
1182 }
1183
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001184 if (orig->replay_esn) {
1185 err = xfrm_replay_clone(x, orig);
1186 if (err)
1187 goto error;
1188 }
1189
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001190 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1191
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001192 err = xfrm_init_state(x);
1193 if (err)
1194 goto error;
1195
1196 x->props.flags = orig->props.flags;
1197
1198 x->curlft.add_time = orig->curlft.add_time;
1199 x->km.state = orig->km.state;
1200 x->km.seq = orig->km.seq;
1201
1202 return x;
1203
1204 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001205 xfrm_state_put(x);
1206out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001207 if (errp)
1208 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001209 return NULL;
1210}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001211
1212/* xfrm_state_lock is held */
1213struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1214{
1215 unsigned int h;
1216 struct xfrm_state *x;
1217 struct hlist_node *entry;
1218
1219 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001220 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001221 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001222 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001223 if (x->props.mode != m->mode ||
1224 x->id.proto != m->proto)
1225 continue;
1226 if (m->reqid && x->props.reqid != m->reqid)
1227 continue;
1228 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1229 m->old_family) ||
1230 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1231 m->old_family))
1232 continue;
1233 xfrm_state_hold(x);
1234 return x;
1235 }
1236 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001237 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001238 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001239 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001240 if (x->props.mode != m->mode ||
1241 x->id.proto != m->proto)
1242 continue;
1243 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1244 m->old_family) ||
1245 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1246 m->old_family))
1247 continue;
1248 xfrm_state_hold(x);
1249 return x;
1250 }
1251 }
1252
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001253 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001254}
1255EXPORT_SYMBOL(xfrm_migrate_state_find);
1256
1257struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1258 struct xfrm_migrate *m)
1259{
1260 struct xfrm_state *xc;
1261 int err;
1262
1263 xc = xfrm_state_clone(x, &err);
1264 if (!xc)
1265 return NULL;
1266
1267 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1268 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1269
1270 /* add state */
1271 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1272 /* a care is needed when the destination address of the
1273 state is to be updated as it is a part of triplet */
1274 xfrm_state_insert(xc);
1275 } else {
1276 if ((err = xfrm_state_add(xc)) < 0)
1277 goto error;
1278 }
1279
1280 return xc;
1281error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001282 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001283 return NULL;
1284}
1285EXPORT_SYMBOL(xfrm_state_migrate);
1286#endif
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288int xfrm_state_update(struct xfrm_state *x)
1289{
David S. Miller37b08e32008-09-02 20:14:15 -07001290 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001292 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
David S. Miller37b08e32008-09-02 20:14:15 -07001294 to_put = NULL;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001297 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 err = -ESRCH;
1300 if (!x1)
1301 goto out;
1302
1303 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001304 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 err = -EEXIST;
1306 goto out;
1307 }
1308
1309 if (x1->km.state == XFRM_STATE_ACQ) {
1310 __xfrm_state_insert(x);
1311 x = NULL;
1312 }
1313 err = 0;
1314
1315out:
1316 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
David S. Miller37b08e32008-09-02 20:14:15 -07001318 if (to_put)
1319 xfrm_state_put(to_put);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (err)
1322 return err;
1323
1324 if (!x) {
1325 xfrm_state_delete(x1);
1326 xfrm_state_put(x1);
1327 return 0;
1328 }
1329
1330 err = -EINVAL;
1331 spin_lock_bh(&x1->lock);
1332 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1333 if (x->encap && x1->encap)
1334 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001335 if (x->coaddr && x1->coaddr) {
1336 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1337 }
1338 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1339 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1341 x1->km.dying = 0;
1342
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001343 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (x1->curlft.use_time)
1345 xfrm_state_check_expire(x1);
1346
1347 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001348 x->km.state = XFRM_STATE_DEAD;
1349 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351 spin_unlock_bh(&x1->lock);
1352
1353 xfrm_state_put(x1);
1354
1355 return err;
1356}
1357EXPORT_SYMBOL(xfrm_state_update);
1358
1359int xfrm_state_check_expire(struct xfrm_state *x)
1360{
1361 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001362 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (x->km.state != XFRM_STATE_VALID)
1365 return -EINVAL;
1366
1367 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1368 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001369 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001370 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EINVAL;
1372 }
1373
1374 if (!x->km.dying &&
1375 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001376 x->curlft.packets >= x->lft.soft_packet_limit)) {
1377 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001378 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return 0;
1381}
1382EXPORT_SYMBOL(xfrm_state_check_expire);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001385xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001386 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001391 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return x;
1394}
1395EXPORT_SYMBOL(xfrm_state_lookup);
1396
1397struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001398xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001399 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001400 u8 proto, unsigned short family)
1401{
1402 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001403
1404 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001405 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001406 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001407 return x;
1408}
1409EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1410
1411struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001412xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001413 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 int create, unsigned short family)
1415{
1416 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001419 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return x;
1423}
1424EXPORT_SYMBOL(xfrm_find_acq);
1425
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001426#ifdef CONFIG_XFRM_SUB_POLICY
1427int
1428xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1429 unsigned short family)
1430{
1431 int err = 0;
1432 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1433 if (!afinfo)
1434 return -EAFNOSUPPORT;
1435
1436 spin_lock_bh(&xfrm_state_lock);
1437 if (afinfo->tmpl_sort)
1438 err = afinfo->tmpl_sort(dst, src, n);
1439 spin_unlock_bh(&xfrm_state_lock);
1440 xfrm_state_put_afinfo(afinfo);
1441 return err;
1442}
1443EXPORT_SYMBOL(xfrm_tmpl_sort);
1444
1445int
1446xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1447 unsigned short family)
1448{
1449 int err = 0;
1450 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1451 if (!afinfo)
1452 return -EAFNOSUPPORT;
1453
1454 spin_lock_bh(&xfrm_state_lock);
1455 if (afinfo->state_sort)
1456 err = afinfo->state_sort(dst, src, n);
1457 spin_unlock_bh(&xfrm_state_lock);
1458 xfrm_state_put_afinfo(afinfo);
1459 return err;
1460}
1461EXPORT_SYMBOL(xfrm_state_sort);
1462#endif
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464/* Silly enough, but I'm lazy to build resolution list */
1465
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001466static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001470 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001471 struct hlist_node *entry;
1472 struct xfrm_state *x;
1473
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001474 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001475 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001476 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001477 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 xfrm_state_hold(x);
1479 return x;
1480 }
1481 }
1482 }
1483 return NULL;
1484}
1485
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001486struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct xfrm_state *x;
1489
1490 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001491 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 spin_unlock_bh(&xfrm_state_lock);
1493 return x;
1494}
1495EXPORT_SYMBOL(xfrm_find_acq_byseq);
1496
1497u32 xfrm_get_acqseq(void)
1498{
1499 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001500 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
jamal6836b9b2010-02-16 02:01:22 +00001502 do {
1503 res = atomic_inc_return(&acqseq);
1504 } while (!res);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return res;
1507}
1508EXPORT_SYMBOL(xfrm_get_acqseq);
1509
Herbert Xu658b2192007-10-09 13:29:52 -07001510int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001512 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001513 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001515 int err = -ENOENT;
1516 __be32 minspi = htonl(low);
1517 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001518 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Herbert Xu658b2192007-10-09 13:29:52 -07001520 spin_lock_bh(&x->lock);
1521 if (x->km.state == XFRM_STATE_DEAD)
1522 goto unlock;
1523
1524 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001526 goto unlock;
1527
1528 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001531 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (x0) {
1533 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001534 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536 x->id.spi = minspi;
1537 } else {
1538 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001539 for (h=0; h<high-low+1; h++) {
1540 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001541 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (x0 == NULL) {
1543 x->id.spi = htonl(spi);
1544 break;
1545 }
1546 xfrm_state_put(x0);
1547 }
1548 }
1549 if (x->id.spi) {
1550 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001551 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1552 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001554
1555 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Herbert Xu658b2192007-10-09 13:29:52 -07001557
1558unlock:
1559 spin_unlock_bh(&x->lock);
1560
1561 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563EXPORT_SYMBOL(xfrm_alloc_spi);
1564
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001565int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001566 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 void *data)
1568{
Herbert Xu12a169e2008-10-01 07:03:24 -07001569 struct xfrm_state *state;
1570 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int err = 0;
1572
Herbert Xu12a169e2008-10-01 07:03:24 -07001573 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001574 return 0;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001577 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001578 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001579 else
1580 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001581 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001582 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001583 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001584 state = container_of(x, struct xfrm_state, km);
1585 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001586 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001587 err = func(state, walk->seq, data);
1588 if (err) {
1589 list_move_tail(&walk->all, &x->all);
1590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001592 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001594 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 err = -ENOENT;
1596 goto out;
1597 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001598 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599out:
1600 spin_unlock_bh(&xfrm_state_lock);
1601 return err;
1602}
1603EXPORT_SYMBOL(xfrm_state_walk);
1604
Herbert Xu5c182452008-09-22 19:48:19 -07001605void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1606{
Herbert Xu12a169e2008-10-01 07:03:24 -07001607 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001608 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001609 walk->state = XFRM_STATE_DEAD;
1610 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001611}
1612EXPORT_SYMBOL(xfrm_state_walk_init);
1613
Herbert Xuabb81c42008-09-09 19:58:29 -07001614void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1615{
Herbert Xu12a169e2008-10-01 07:03:24 -07001616 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001617 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001618
Herbert Xu12a169e2008-10-01 07:03:24 -07001619 spin_lock_bh(&xfrm_state_lock);
1620 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001621 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001622}
1623EXPORT_SYMBOL(xfrm_state_walk_done);
1624
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001625static void xfrm_replay_timer_handler(unsigned long data)
1626{
1627 struct xfrm_state *x = (struct xfrm_state*)data;
1628
1629 spin_lock(&x->lock);
1630
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001631 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001632 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001633 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001634 else
1635 x->xflags |= XFRM_TIME_DEFER;
1636 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001637
1638 spin_unlock(&x->lock);
1639}
1640
Denis Chengdf018122007-12-07 00:51:11 -08001641static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642static DEFINE_RWLOCK(xfrm_km_lock);
1643
David S. Miller214e0052011-02-24 00:02:38 -05001644void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct xfrm_mgr *km;
1647
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001648 read_lock(&xfrm_km_lock);
1649 list_for_each_entry(km, &xfrm_km_list, list)
1650 if (km->notify_policy)
1651 km->notify_policy(xp, dir, c);
1652 read_unlock(&xfrm_km_lock);
1653}
1654
David S. Miller214e0052011-02-24 00:02:38 -05001655void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001656{
1657 struct xfrm_mgr *km;
1658 read_lock(&xfrm_km_lock);
1659 list_for_each_entry(km, &xfrm_km_list, list)
1660 if (km->notify)
1661 km->notify(x, c);
1662 read_unlock(&xfrm_km_lock);
1663}
1664
1665EXPORT_SYMBOL(km_policy_notify);
1666EXPORT_SYMBOL(km_state_notify);
1667
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001668void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001669{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001670 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001671 struct km_event c;
1672
Herbert Xubf088672005-06-18 22:44:00 -07001673 c.data.hard = hard;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001674 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001675 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001676 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001679 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001682EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001683/*
1684 * We send to all registered managers regardless of failure
1685 * We are happy with one success
1686*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001687int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001689 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct xfrm_mgr *km;
1691
1692 read_lock(&xfrm_km_lock);
1693 list_for_each_entry(km, &xfrm_km_list, list) {
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001694 acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
1695 if (!acqret)
1696 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698 read_unlock(&xfrm_km_lock);
1699 return err;
1700}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001701EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Al Viro5d36b182006-11-08 00:24:06 -08001703int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 int err = -EINVAL;
1706 struct xfrm_mgr *km;
1707
1708 read_lock(&xfrm_km_lock);
1709 list_for_each_entry(km, &xfrm_km_list, list) {
1710 if (km->new_mapping)
1711 err = km->new_mapping(x, ipaddr, sport);
1712 if (!err)
1713 break;
1714 }
1715 read_unlock(&xfrm_km_lock);
1716 return err;
1717}
1718EXPORT_SYMBOL(km_new_mapping);
1719
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001720void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001722 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001723 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Herbert Xubf088672005-06-18 22:44:00 -07001725 c.data.hard = hard;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001726 c.pid = pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001727 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001731 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
David S. Millera70fcb02006-03-20 19:18:52 -08001733EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001735#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001736int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1737 const struct xfrm_migrate *m, int num_migrate,
1738 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001739{
1740 int err = -EINVAL;
1741 int ret;
1742 struct xfrm_mgr *km;
1743
1744 read_lock(&xfrm_km_lock);
1745 list_for_each_entry(km, &xfrm_km_list, list) {
1746 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001747 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001748 if (!ret)
1749 err = ret;
1750 }
1751 }
1752 read_unlock(&xfrm_km_lock);
1753 return err;
1754}
1755EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001756#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001757
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001758int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001759{
1760 int err = -EINVAL;
1761 int ret;
1762 struct xfrm_mgr *km;
1763
1764 read_lock(&xfrm_km_lock);
1765 list_for_each_entry(km, &xfrm_km_list, list) {
1766 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001767 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001768 if (!ret)
1769 err = ret;
1770 }
1771 }
1772 read_unlock(&xfrm_km_lock);
1773 return err;
1774}
1775EXPORT_SYMBOL(km_report);
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1778{
1779 int err;
1780 u8 *data;
1781 struct xfrm_mgr *km;
1782 struct xfrm_policy *pol = NULL;
1783
1784 if (optlen <= 0 || optlen > PAGE_SIZE)
1785 return -EMSGSIZE;
1786
1787 data = kmalloc(optlen, GFP_KERNEL);
1788 if (!data)
1789 return -ENOMEM;
1790
1791 err = -EFAULT;
1792 if (copy_from_user(data, optval, optlen))
1793 goto out;
1794
1795 err = -EINVAL;
1796 read_lock(&xfrm_km_lock);
1797 list_for_each_entry(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001798 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 optlen, &err);
1800 if (err >= 0)
1801 break;
1802 }
1803 read_unlock(&xfrm_km_lock);
1804
1805 if (err >= 0) {
1806 xfrm_sk_policy_insert(sk, err, pol);
1807 xfrm_pol_put(pol);
1808 err = 0;
1809 }
1810
1811out:
1812 kfree(data);
1813 return err;
1814}
1815EXPORT_SYMBOL(xfrm_user_policy);
1816
1817int xfrm_register_km(struct xfrm_mgr *km)
1818{
1819 write_lock_bh(&xfrm_km_lock);
1820 list_add_tail(&km->list, &xfrm_km_list);
1821 write_unlock_bh(&xfrm_km_lock);
1822 return 0;
1823}
1824EXPORT_SYMBOL(xfrm_register_km);
1825
1826int xfrm_unregister_km(struct xfrm_mgr *km)
1827{
1828 write_lock_bh(&xfrm_km_lock);
1829 list_del(&km->list);
1830 write_unlock_bh(&xfrm_km_lock);
1831 return 0;
1832}
1833EXPORT_SYMBOL(xfrm_unregister_km);
1834
1835int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1836{
1837 int err = 0;
1838 if (unlikely(afinfo == NULL))
1839 return -EINVAL;
1840 if (unlikely(afinfo->family >= NPROTO))
1841 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001842 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1844 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001845 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 xfrm_state_afinfo[afinfo->family] = afinfo;
Ingo Molnarf3111502006-04-28 15:30:03 -07001847 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return err;
1849}
1850EXPORT_SYMBOL(xfrm_state_register_afinfo);
1851
1852int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1853{
1854 int err = 0;
1855 if (unlikely(afinfo == NULL))
1856 return -EINVAL;
1857 if (unlikely(afinfo->family >= NPROTO))
1858 return -EAFNOSUPPORT;
Ingo Molnarf3111502006-04-28 15:30:03 -07001859 write_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1861 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1862 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001863 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 xfrm_state_afinfo[afinfo->family] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
Ingo Molnarf3111502006-04-28 15:30:03 -07001866 write_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return err;
1868}
1869EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1870
Herbert Xu17c2a422007-10-17 21:33:12 -07001871static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 struct xfrm_state_afinfo *afinfo;
1874 if (unlikely(family >= NPROTO))
1875 return NULL;
1876 read_lock(&xfrm_state_afinfo_lock);
1877 afinfo = xfrm_state_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07001878 if (unlikely(!afinfo))
1879 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return afinfo;
1881}
1882
Herbert Xu17c2a422007-10-17 21:33:12 -07001883static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Eric Dumazet9a429c42008-01-01 21:58:02 -08001884 __releases(xfrm_state_afinfo_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Herbert Xu546be242006-05-27 23:03:58 -07001886 read_unlock(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1890void xfrm_state_delete_tunnel(struct xfrm_state *x)
1891{
1892 if (x->tunnel) {
1893 struct xfrm_state *t = x->tunnel;
1894
1895 if (atomic_read(&t->tunnel_users) == 2)
1896 xfrm_state_delete(t);
1897 atomic_dec(&t->tunnel_users);
1898 xfrm_state_put(t);
1899 x->tunnel = NULL;
1900 }
1901}
1902EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1903
1904int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1905{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001906 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Patrick McHardyc5c25232007-04-09 11:47:18 -07001908 spin_lock_bh(&x->lock);
1909 if (x->km.state == XFRM_STATE_VALID &&
1910 x->type && x->type->get_mtu)
1911 res = x->type->get_mtu(x, mtu);
1912 else
Patrick McHardy28121612007-06-18 22:30:15 -07001913 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001914 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return res;
1916}
1917
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001918int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001919{
Herbert Xud094cd82005-06-20 13:19:41 -07001920 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001921 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001922 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001923 int err;
1924
Herbert Xud094cd82005-06-20 13:19:41 -07001925 err = -EAFNOSUPPORT;
1926 afinfo = xfrm_state_get_afinfo(family);
1927 if (!afinfo)
1928 goto error;
1929
1930 err = 0;
1931 if (afinfo->init_flags)
1932 err = afinfo->init_flags(x);
1933
1934 xfrm_state_put_afinfo(afinfo);
1935
1936 if (err)
1937 goto error;
1938
1939 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001940
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001941 if (x->sel.family != AF_UNSPEC) {
1942 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1943 if (inner_mode == NULL)
1944 goto error;
1945
1946 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1947 family != x->sel.family) {
1948 xfrm_put_mode(inner_mode);
1949 goto error;
1950 }
1951
1952 x->inner_mode = inner_mode;
1953 } else {
1954 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001955 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001956
Martin Willid81d2282008-12-03 15:38:07 -08001957 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001958 if (inner_mode == NULL)
1959 goto error;
1960
1961 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1962 xfrm_put_mode(inner_mode);
1963 goto error;
1964 }
Martin Willid81d2282008-12-03 15:38:07 -08001965 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001966
Martin Willid81d2282008-12-03 15:38:07 -08001967 if (x->props.family == AF_INET)
1968 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001969
Martin Willid81d2282008-12-03 15:38:07 -08001970 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1971 if (inner_mode_iaf) {
1972 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1973 x->inner_mode_iaf = inner_mode_iaf;
1974 else
1975 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001976 }
1977 }
Herbert Xu13996372007-10-17 21:35:51 -07001978
Herbert Xud094cd82005-06-20 13:19:41 -07001979 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001980 if (x->type == NULL)
1981 goto error;
1982
1983 err = x->type->init_state(x);
1984 if (err)
1985 goto error;
1986
Herbert Xu13996372007-10-17 21:35:51 -07001987 x->outer_mode = xfrm_get_mode(x->props.mode, family);
1988 if (x->outer_mode == NULL)
Herbert Xub59f45d2006-05-27 23:05:54 -07001989 goto error;
1990
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001991 if (init_replay) {
1992 err = xfrm_init_replay(x);
1993 if (err)
1994 goto error;
1995 }
1996
Herbert Xu72cb6962005-06-20 13:18:08 -07001997 x->km.state = XFRM_STATE_VALID;
1998
1999error:
2000 return err;
2001}
2002
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002003EXPORT_SYMBOL(__xfrm_init_state);
2004
2005int xfrm_init_state(struct xfrm_state *x)
2006{
2007 return __xfrm_init_state(x, true);
2008}
2009
Herbert Xu72cb6962005-06-20 13:18:08 -07002010EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002011
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002012int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
David S. Millerf034b5d2006-08-24 03:08:07 -07002014 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002016 INIT_LIST_HEAD(&net->xfrm.state_all);
2017
David S. Millerf034b5d2006-08-24 03:08:07 -07002018 sz = sizeof(struct hlist_head) * 8;
2019
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002020 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2021 if (!net->xfrm.state_bydst)
2022 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002023 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2024 if (!net->xfrm.state_bysrc)
2025 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002026 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2027 if (!net->xfrm.state_byspi)
2028 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002029 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002030
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002031 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002032 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002033 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002034 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002035 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002036 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002037
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002038out_byspi:
2039 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002040out_bysrc:
2041 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002042out_bydst:
2043 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002044}
2045
2046void xfrm_state_fini(struct net *net)
2047{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002048 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002049 unsigned int sz;
2050
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002051 flush_work(&net->xfrm.state_hash_work);
2052 audit_info.loginuid = -1;
2053 audit_info.sessionid = -1;
2054 audit_info.secid = 0;
2055 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2056 flush_work(&net->xfrm.state_gc_work);
2057
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002058 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002059
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002060 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002061 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2062 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002063 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2064 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002065 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2066 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Joy Lattenab5f5e82007-09-17 11:51:22 -07002069#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002070static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2071 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002072{
Paul Moore68277ac2007-12-20 20:49:33 -08002073 struct xfrm_sec_ctx *ctx = x->security;
2074 u32 spi = ntohl(x->id.spi);
2075
2076 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002077 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002078 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002079
2080 switch(x->props.family) {
2081 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002082 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2083 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002084 break;
2085 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002086 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002087 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002088 break;
2089 }
Paul Moore68277ac2007-12-20 20:49:33 -08002090
2091 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002092}
2093
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002094static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2095 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002096{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002097 const struct iphdr *iph4;
2098 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002099
2100 switch (family) {
2101 case AF_INET:
2102 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002103 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2104 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002105 break;
2106 case AF_INET6:
2107 iph6 = ipv6_hdr(skb);
2108 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002109 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002110 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002111 iph6->flow_lbl[0] & 0x0f,
2112 iph6->flow_lbl[1],
2113 iph6->flow_lbl[2]);
2114 break;
2115 }
2116}
2117
Paul Moore68277ac2007-12-20 20:49:33 -08002118void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002119 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002120{
2121 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002122
Paul Mooreafeb14b2007-12-21 14:58:11 -08002123 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002124 if (audit_buf == NULL)
2125 return;
Eric Paris25323862008-04-18 10:09:25 -04002126 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002127 xfrm_audit_helper_sainfo(x, audit_buf);
2128 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002129 audit_log_end(audit_buf);
2130}
2131EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2132
Paul Moore68277ac2007-12-20 20:49:33 -08002133void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris25323862008-04-18 10:09:25 -04002134 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002135{
2136 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002137
Paul Mooreafeb14b2007-12-21 14:58:11 -08002138 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002139 if (audit_buf == NULL)
2140 return;
Eric Paris25323862008-04-18 10:09:25 -04002141 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002142 xfrm_audit_helper_sainfo(x, audit_buf);
2143 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002144 audit_log_end(audit_buf);
2145}
2146EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002147
2148void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2149 struct sk_buff *skb)
2150{
2151 struct audit_buffer *audit_buf;
2152 u32 spi;
2153
2154 audit_buf = xfrm_audit_start("SA-replay-overflow");
2155 if (audit_buf == NULL)
2156 return;
2157 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2158 /* don't record the sequence number because it's inherent in this kind
2159 * of audit message */
2160 spi = ntohl(x->id.spi);
2161 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2162 audit_log_end(audit_buf);
2163}
2164EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2165
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002166void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002167 struct sk_buff *skb, __be32 net_seq)
2168{
2169 struct audit_buffer *audit_buf;
2170 u32 spi;
2171
2172 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2173 if (audit_buf == NULL)
2174 return;
2175 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2176 spi = ntohl(x->id.spi);
2177 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2178 spi, spi, ntohl(net_seq));
2179 audit_log_end(audit_buf);
2180}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002181EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002182
2183void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2184{
2185 struct audit_buffer *audit_buf;
2186
2187 audit_buf = xfrm_audit_start("SA-notfound");
2188 if (audit_buf == NULL)
2189 return;
2190 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2191 audit_log_end(audit_buf);
2192}
2193EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2194
2195void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2196 __be32 net_spi, __be32 net_seq)
2197{
2198 struct audit_buffer *audit_buf;
2199 u32 spi;
2200
2201 audit_buf = xfrm_audit_start("SA-notfound");
2202 if (audit_buf == NULL)
2203 return;
2204 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2205 spi = ntohl(net_spi);
2206 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2207 spi, spi, ntohl(net_seq));
2208 audit_log_end(audit_buf);
2209}
2210EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2211
2212void xfrm_audit_state_icvfail(struct xfrm_state *x,
2213 struct sk_buff *skb, u8 proto)
2214{
2215 struct audit_buffer *audit_buf;
2216 __be32 net_spi;
2217 __be32 net_seq;
2218
2219 audit_buf = xfrm_audit_start("SA-icv-failure");
2220 if (audit_buf == NULL)
2221 return;
2222 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2223 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2224 u32 spi = ntohl(net_spi);
2225 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2226 spi, spi, ntohl(net_seq));
2227 }
2228 audit_log_end(audit_buf);
2229}
2230EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002231#endif /* CONFIG_AUDITSYSCALL */