blob: 1522f19f273a0749d3808a7d429aa40e4efb0699 [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
Cong Wang44abdc32013-01-16 16:05:05 +0800161static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
162static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static DEFINE_SPINLOCK(xfrm_state_gc_lock);
165
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800166int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800168int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000169void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
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;
Cong Wang44abdc32013-01-16 16:05:05 +0800176 spin_lock_bh(&xfrm_state_afinfo_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700177 afinfo = xfrm_state_afinfo[family];
178 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +0800179 spin_unlock_bh(&xfrm_state_afinfo_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700180 return afinfo;
181}
182
183static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
184{
Cong Wang44abdc32013-01-16 16:05:05 +0800185 spin_unlock_bh(&xfrm_state_afinfo_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700186}
187
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800188int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700189{
190 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800191 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700192 int err = 0;
193
194 if (unlikely(afinfo == NULL))
195 return -EAFNOSUPPORT;
196 typemap = afinfo->type_map;
197
198 if (likely(typemap[type->proto] == NULL))
199 typemap[type->proto] = type;
200 else
201 err = -EEXIST;
202 xfrm_state_unlock_afinfo(afinfo);
203 return err;
204}
205EXPORT_SYMBOL(xfrm_register_type);
206
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800207int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700208{
209 struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800210 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700211 int err = 0;
212
213 if (unlikely(afinfo == NULL))
214 return -EAFNOSUPPORT;
215 typemap = afinfo->type_map;
216
217 if (unlikely(typemap[type->proto] != type))
218 err = -ENOENT;
219 else
220 typemap[type->proto] = NULL;
221 xfrm_state_unlock_afinfo(afinfo);
222 return err;
223}
224EXPORT_SYMBOL(xfrm_unregister_type);
225
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800226static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700227{
228 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800229 const struct xfrm_type **typemap;
230 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700231 int modload_attempted = 0;
232
233retry:
234 afinfo = xfrm_state_get_afinfo(family);
235 if (unlikely(afinfo == NULL))
236 return NULL;
237 typemap = afinfo->type_map;
238
239 type = typemap[proto];
240 if (unlikely(type && !try_module_get(type->owner)))
241 type = NULL;
242 if (!type && !modload_attempted) {
243 xfrm_state_put_afinfo(afinfo);
244 request_module("xfrm-type-%d-%d", family, proto);
245 modload_attempted = 1;
246 goto retry;
247 }
248
249 xfrm_state_put_afinfo(afinfo);
250 return type;
251}
252
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800253static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700254{
255 module_put(type->owner);
256}
257
258int xfrm_register_mode(struct xfrm_mode *mode, int family)
259{
260 struct xfrm_state_afinfo *afinfo;
261 struct xfrm_mode **modemap;
262 int err;
263
264 if (unlikely(mode->encap >= XFRM_MODE_MAX))
265 return -EINVAL;
266
267 afinfo = xfrm_state_lock_afinfo(family);
268 if (unlikely(afinfo == NULL))
269 return -EAFNOSUPPORT;
270
271 err = -EEXIST;
272 modemap = afinfo->mode_map;
Herbert Xu17c2a422007-10-17 21:33:12 -0700273 if (modemap[mode->encap])
274 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700275
Herbert Xu17c2a422007-10-17 21:33:12 -0700276 err = -ENOENT;
277 if (!try_module_get(afinfo->owner))
278 goto out;
279
280 mode->afinfo = afinfo;
281 modemap[mode->encap] = mode;
282 err = 0;
283
284out:
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700285 xfrm_state_unlock_afinfo(afinfo);
286 return err;
287}
288EXPORT_SYMBOL(xfrm_register_mode);
289
290int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
291{
292 struct xfrm_state_afinfo *afinfo;
293 struct xfrm_mode **modemap;
294 int err;
295
296 if (unlikely(mode->encap >= XFRM_MODE_MAX))
297 return -EINVAL;
298
299 afinfo = xfrm_state_lock_afinfo(family);
300 if (unlikely(afinfo == NULL))
301 return -EAFNOSUPPORT;
302
303 err = -ENOENT;
304 modemap = afinfo->mode_map;
305 if (likely(modemap[mode->encap] == mode)) {
306 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700307 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700308 err = 0;
309 }
310
311 xfrm_state_unlock_afinfo(afinfo);
312 return err;
313}
314EXPORT_SYMBOL(xfrm_unregister_mode);
315
316static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
317{
318 struct xfrm_state_afinfo *afinfo;
319 struct xfrm_mode *mode;
320 int modload_attempted = 0;
321
322 if (unlikely(encap >= XFRM_MODE_MAX))
323 return NULL;
324
325retry:
326 afinfo = xfrm_state_get_afinfo(family);
327 if (unlikely(afinfo == NULL))
328 return NULL;
329
330 mode = afinfo->mode_map[encap];
331 if (unlikely(mode && !try_module_get(mode->owner)))
332 mode = NULL;
333 if (!mode && !modload_attempted) {
334 xfrm_state_put_afinfo(afinfo);
335 request_module("xfrm-mode-%d-%d", family, encap);
336 modload_attempted = 1;
337 goto retry;
338 }
339
340 xfrm_state_put_afinfo(afinfo);
341 return mode;
342}
343
344static void xfrm_put_mode(struct xfrm_mode *mode)
345{
346 module_put(mode->owner);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static void xfrm_state_gc_destroy(struct xfrm_state *x)
350{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800351 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700352 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800353 kfree(x->aalg);
354 kfree(x->ealg);
355 kfree(x->calg);
356 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700357 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000358 kfree(x->replay_esn);
359 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700360 if (x->inner_mode)
361 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700362 if (x->inner_mode_iaf)
363 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700364 if (x->outer_mode)
365 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (x->type) {
367 x->type->destructor(x);
368 xfrm_put_type(x->type);
369 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800370 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 kfree(x);
372}
373
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800374static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800376 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700377 struct xfrm_state *x;
378 struct hlist_node *entry, *tmp;
379 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800382 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_unlock_bh(&xfrm_state_gc_lock);
384
Herbert Xu12a169e2008-10-01 07:03:24 -0700385 hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 xfrm_state_gc_destroy(x);
David S. Miller8f126e32006-08-24 02:45:07 -0700387
Alexey Dobriyan50a30652008-11-25 17:21:01 -0800388 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391static inline unsigned long make_jiffies(long secs)
392{
393 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
394 return MAX_SCHEDULE_TIMEOUT-1;
395 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900396 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800399static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800401 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
402 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800403 struct net *net = xs_net(x);
James Morris9d729f72007-03-04 16:12:44 -0800404 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 long next = LONG_MAX;
406 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600407 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 spin_lock(&x->lock);
410 if (x->km.state == XFRM_STATE_DEAD)
411 goto out;
412 if (x->km.state == XFRM_STATE_EXPIRED)
413 goto expired;
414 if (x->lft.hard_add_expires_seconds) {
415 long tmo = x->lft.hard_add_expires_seconds +
416 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000417 if (tmo <= 0) {
418 if (x->xflags & XFRM_SOFT_EXPIRE) {
419 /* enter hard expire without soft expire first?!
420 * setting a new date could trigger this.
421 * workarbound: fix x->curflt.add_time by below:
422 */
423 x->curlft.add_time = now - x->saved_tmo - 1;
424 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
425 } else
426 goto expired;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (tmo < next)
429 next = tmo;
430 }
431 if (x->lft.hard_use_expires_seconds) {
432 long tmo = x->lft.hard_use_expires_seconds +
433 (x->curlft.use_time ? : now) - now;
434 if (tmo <= 0)
435 goto expired;
436 if (tmo < next)
437 next = tmo;
438 }
439 if (x->km.dying)
440 goto resched;
441 if (x->lft.soft_add_expires_seconds) {
442 long tmo = x->lft.soft_add_expires_seconds +
443 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000444 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000446 x->xflags &= ~XFRM_SOFT_EXPIRE;
447 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000449 x->xflags |= XFRM_SOFT_EXPIRE;
450 x->saved_tmo = tmo;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 if (x->lft.soft_use_expires_seconds) {
454 long tmo = x->lft.soft_use_expires_seconds +
455 (x->curlft.use_time ? : now) - now;
456 if (tmo <= 0)
457 warn = 1;
458 else if (tmo < next)
459 next = tmo;
460 }
461
Herbert Xu4666faa2005-06-18 22:43:22 -0700462 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800464 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465resched:
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800466 if (next != LONG_MAX){
467 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
468 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto out;
471
472expired:
473 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
474 x->km.state = XFRM_STATE_EXPIRED;
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800475 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 next = 2;
477 goto resched;
478 }
Joy Latten161a09e2006-11-27 13:11:54 -0600479
480 err = __xfrm_state_delete(x);
481 if (!err && x->id.spi)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800482 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Joy Lattenab5f5e82007-09-17 11:51:22 -0700484 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400485 audit_get_loginuid(current),
486 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488out:
489 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800490 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
David S. Miller0ac84752006-03-20 19:18:23 -0800493static void xfrm_replay_timer_handler(unsigned long data);
494
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800495struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct xfrm_state *x;
498
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700499 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800502 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 atomic_set(&x->refcnt, 1);
504 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700505 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700506 INIT_HLIST_NODE(&x->bydst);
507 INIT_HLIST_NODE(&x->bysrc);
508 INIT_HLIST_NODE(&x->byspi);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800509 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800510 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
511 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800512 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 x->lft.soft_byte_limit = XFRM_INF;
514 x->lft.soft_packet_limit = XFRM_INF;
515 x->lft.hard_byte_limit = XFRM_INF;
516 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800517 x->replay_maxage = 0;
518 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700519 x->inner_mode = NULL;
520 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 spin_lock_init(&x->lock);
522 }
523 return x;
524}
525EXPORT_SYMBOL(xfrm_state_alloc);
526
527void __xfrm_state_destroy(struct xfrm_state *x)
528{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800529 struct net *net = xs_net(x);
530
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700531 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800534 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800536 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538EXPORT_SYMBOL(__xfrm_state_destroy);
539
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800540int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800542 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700543 int err = -ESRCH;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (x->km.state != XFRM_STATE_DEAD) {
546 x->km.state = XFRM_STATE_DEAD;
547 spin_lock(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700548 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700549 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700550 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700551 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700552 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800553 net->xfrm.state_num--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 spin_unlock(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* All xfrm_state objects are created by xfrm_state_alloc.
557 * The xfrm_state_alloc call gives a reference, and that
558 * is what we are dropping here.
559 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800560 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700561 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700563
564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800566EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700568int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700570 int err;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700573 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700575
576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578EXPORT_SYMBOL(xfrm_state_delete);
579
Joy Latten4aa2e622007-06-04 19:05:57 -0400580#ifdef CONFIG_SECURITY_NETWORK_XFRM
581static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800582xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Joy Latten4aa2e622007-06-04 19:05:57 -0400584 int i, err = 0;
585
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800586 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400587 struct hlist_node *entry;
588 struct xfrm_state *x;
589
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800590 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400591 if (xfrm_id_proto_match(x->id.proto, proto) &&
592 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700593 xfrm_audit_state_delete(x, 0,
594 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400595 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700596 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400597 return err;
598 }
599 }
600 }
601
602 return err;
603}
604#else
605static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800606xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400607{
608 return 0;
609}
610#endif
611
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800612int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400613{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000614 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800617 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400618 if (err)
619 goto out;
620
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000621 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800622 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700623 struct hlist_node *entry;
624 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625restart:
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800626 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700628 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 xfrm_state_hold(x);
630 spin_unlock_bh(&xfrm_state_lock);
631
Joy Latten161a09e2006-11-27 13:11:54 -0600632 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700633 xfrm_audit_state_delete(x, err ? 0 : 1,
634 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400635 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700636 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000638 if (!err)
639 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 spin_lock_bh(&xfrm_state_lock);
642 goto restart;
643 }
644 }
645 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000646 if (cnt)
647 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400648
649out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 spin_unlock_bh(&xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800651 wake_up(&net->xfrm.km_waitq);
Joy Latten4aa2e622007-06-04 19:05:57 -0400652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654EXPORT_SYMBOL(xfrm_state_flush);
655
Alexey Dobriyane0710412010-01-23 13:37:10 +0000656void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700657{
658 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000659 si->sadcnt = net->xfrm.state_num;
660 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700661 si->sadhmcnt = xfrm_state_hashmax;
662 spin_unlock_bh(&xfrm_state_lock);
663}
664EXPORT_SYMBOL(xfrm_sad_getinfo);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static int
David S. Miller1a898592011-02-22 18:22:34 -0800667xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500668 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500669 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700670 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
673 if (!afinfo)
674 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700675 afinfo->init_tempsel(&x->sel, fl);
676
677 if (family != tmpl->encap_family) {
678 xfrm_state_put_afinfo(afinfo);
679 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
680 if (!afinfo)
681 return -1;
682 }
683 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 xfrm_state_put_afinfo(afinfo);
685 return 0;
686}
687
David S. Miller9aa60082011-02-24 01:51:36 -0500688static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
689 const xfrm_address_t *daddr,
690 __be32 spi, u8 proto,
691 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700692{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800693 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700694 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700695 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700696
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800697 hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700698 if (x->props.family != family ||
699 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000700 x->id.proto != proto ||
701 xfrm_addr_cmp(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700702 continue;
703
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000704 if ((mark & x->mark.m) != x->mark.v)
705 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700706 xfrm_state_hold(x);
707 return x;
708 }
709
710 return NULL;
711}
712
David S. Miller9aa60082011-02-24 01:51:36 -0500713static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
714 const xfrm_address_t *daddr,
715 const xfrm_address_t *saddr,
716 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700717{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800718 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700719 struct xfrm_state *x;
David S. Miller8f126e32006-08-24 02:45:07 -0700720 struct hlist_node *entry;
David S. Milleredcd5822006-08-24 00:42:45 -0700721
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800722 hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700723 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000724 x->id.proto != proto ||
725 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
726 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700727 continue;
728
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000729 if ((mark & x->mark.m) != x->mark.v)
730 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700731 xfrm_state_hold(x);
732 return x;
733 }
734
735 return NULL;
736}
737
738static inline struct xfrm_state *
739__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
740{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800741 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800742 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800743
David S. Milleredcd5822006-08-24 00:42:45 -0700744 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800745 return __xfrm_state_lookup(net, mark, &x->id.daddr,
746 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700747 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800748 return __xfrm_state_lookup_byaddr(net, mark,
749 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700750 &x->props.saddr,
751 x->id.proto, family);
752}
753
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800754static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700755{
756 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800757 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
758 net->xfrm.state_num > net->xfrm.state_hmask)
759 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700760}
761
David S. Miller08ec9af2009-03-13 14:22:40 -0700762static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800763 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700764 struct xfrm_state **best, int *acq_in_progress,
765 int *error)
766{
767 /* Resolution logic:
768 * 1. There is a valid state with matching selector. Done.
769 * 2. Valid state with inappropriate selector. Skip.
770 *
771 * Entering area of "sysdeps".
772 *
773 * 3. If state is not valid, selector is temporary, it selects
774 * only session which triggered previous resolution. Key
775 * manager will do something to install a state with proper
776 * selector.
777 */
778 if (x->km.state == XFRM_STATE_VALID) {
779 if ((x->sel.family &&
780 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
781 !security_xfrm_state_pol_flow_match(x, pol, fl))
782 return;
783
784 if (!*best ||
785 (*best)->km.dying > x->km.dying ||
786 ((*best)->km.dying == x->km.dying &&
787 (*best)->curlft.add_time < x->curlft.add_time))
788 *best = x;
789 } else if (x->km.state == XFRM_STATE_ACQ) {
790 *acq_in_progress = 1;
791 } else if (x->km.state == XFRM_STATE_ERROR ||
792 x->km.state == XFRM_STATE_EXPIRED) {
793 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
794 security_xfrm_state_pol_flow_match(x, pol, fl))
795 *error = -ESRCH;
796 }
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500800xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800801 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct xfrm_policy *pol, int *err,
803 unsigned short family)
804{
David S. Miller08ec9af2009-03-13 14:22:40 -0700805 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800806 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700807 unsigned int h, h_wildcard;
David S. Miller8f126e32006-08-24 02:45:07 -0700808 struct hlist_node *entry;
David S. Miller37b08e32008-09-02 20:14:15 -0700809 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int acquire_in_progress = 0;
811 int error = 0;
812 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800813 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700814 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900815
David S. Miller37b08e32008-09-02 20:14:15 -0700816 to_put = NULL;
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_lock_bh(&xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700819 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800820 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700821 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000823 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700824 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700825 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 tmpl->mode == x->props.mode &&
827 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700828 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500829 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700830 &best, &acquire_in_progress, &error);
831 }
832 if (best)
833 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Thomas Egerer8444cf72010-09-20 11:11:38 -0700835 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700836 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700837 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700838 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000839 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700840 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700841 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700842 tmpl->mode == x->props.mode &&
843 tmpl->id.proto == x->id.proto &&
844 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500845 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700846 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848
David S. Miller08ec9af2009-03-13 14:22:40 -0700849found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 x = best;
851 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700852 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800853 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700854 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700855 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 error = -EEXIST;
857 goto out;
858 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800859 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (x == NULL) {
861 error = -ENOMEM;
862 goto out;
863 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700864 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700866 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800867 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
David S. Miller1d28f422011-03-12 00:29:39 -0500869 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700870 if (error) {
871 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700872 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700873 x = NULL;
874 goto out;
875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (km_query(x, tmpl, pol) == 0) {
878 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800879 list_add(&x->km.all, &net->xfrm.state_all);
880 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700881 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800882 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700884 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800885 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800887 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800888 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800889 net->xfrm.state_num++;
890 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 } else {
892 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700893 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 x = NULL;
895 error = -ESRCH;
896 }
897 }
898out:
899 if (x)
900 xfrm_state_hold(x);
901 else
902 *err = acquire_in_progress ? -EAGAIN : error;
903 spin_unlock_bh(&xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700904 if (to_put)
905 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return x;
907}
908
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800910xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800911 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700912 unsigned short family, u8 mode, u8 proto, u32 reqid)
913{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800914 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700915 struct xfrm_state *rx = NULL, *x = NULL;
916 struct hlist_node *entry;
917
918 spin_lock(&xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800919 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
920 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700921 if (x->props.family == family &&
922 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000923 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700924 !(x->props.flags & XFRM_STATE_WILDRECV) &&
925 xfrm_state_addr_check(x, daddr, saddr, family) &&
926 mode == x->props.mode &&
927 proto == x->id.proto &&
928 x->km.state == XFRM_STATE_VALID) {
929 rx = x;
930 break;
931 }
932 }
933
934 if (rx)
935 xfrm_state_hold(rx);
936 spin_unlock(&xfrm_state_lock);
937
938
939 return rx;
940}
941EXPORT_SYMBOL(xfrm_stateonly_find);
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943static void __xfrm_state_insert(struct xfrm_state *x)
944{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800945 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700946 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800948 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800949
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800950 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700951 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800952 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800954 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
955 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700957 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800958 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700959 x->props.family);
960
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800961 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700962 }
963
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800964 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700965 if (x->replay_maxage)
966 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800967
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800968 wake_up(&net->xfrm.km_waitq);
David S. Millerf034b5d2006-08-24 03:08:07 -0700969
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800970 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700971
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800972 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
David S. Millerc7f5ea32006-08-24 03:29:04 -0700975/* xfrm_state_lock is held */
976static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
977{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800978 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700979 unsigned short family = xnew->props.family;
980 u32 reqid = xnew->props.reqid;
981 struct xfrm_state *x;
982 struct hlist_node *entry;
983 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000984 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700985
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800986 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
987 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700988 if (x->props.family == family &&
989 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000990 (mark & x->mark.m) == x->mark.v &&
David S. Millerc1969f22006-08-24 04:00:03 -0700991 !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
992 !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000993 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700994 }
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997void xfrm_state_insert(struct xfrm_state *x)
998{
999 spin_lock_bh(&xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001000 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 __xfrm_state_insert(x);
1002 spin_unlock_bh(&xfrm_state_lock);
1003}
1004EXPORT_SYMBOL(xfrm_state_insert);
1005
David S. Miller27708342006-08-24 00:13:10 -07001006/* xfrm_state_lock is held */
David S. Millera70486f2011-02-27 23:17:24 -08001007static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
1008 unsigned short family, u8 mode,
1009 u32 reqid, u8 proto,
1010 const xfrm_address_t *daddr,
1011 const xfrm_address_t *saddr, int create)
David S. Miller27708342006-08-24 00:13:10 -07001012{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001013 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller8f126e32006-08-24 02:45:07 -07001014 struct hlist_node *entry;
David S. Miller27708342006-08-24 00:13:10 -07001015 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001016 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001017
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001018 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001019 if (x->props.reqid != reqid ||
1020 x->props.mode != mode ||
1021 x->props.family != family ||
1022 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001023 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001024 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001025 (mark & x->mark.m) != x->mark.v ||
Wei Yongjun18025712009-06-28 18:42:53 +00001026 xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
1027 xfrm_addr_cmp(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001028 continue;
1029
David S. Miller27708342006-08-24 00:13:10 -07001030 xfrm_state_hold(x);
1031 return x;
1032 }
1033
1034 if (!create)
1035 return NULL;
1036
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001037 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001038 if (likely(x)) {
1039 switch (family) {
1040 case AF_INET:
1041 x->sel.daddr.a4 = daddr->a4;
1042 x->sel.saddr.a4 = saddr->a4;
1043 x->sel.prefixlen_d = 32;
1044 x->sel.prefixlen_s = 32;
1045 x->props.saddr.a4 = saddr->a4;
1046 x->id.daddr.a4 = daddr->a4;
1047 break;
1048
1049 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001050 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1051 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001052 x->sel.prefixlen_d = 128;
1053 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001054 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1055 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001056 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001057 }
David S. Miller27708342006-08-24 00:13:10 -07001058
1059 x->km.state = XFRM_STATE_ACQ;
1060 x->id.proto = proto;
1061 x->props.family = family;
1062 x->props.mode = mode;
1063 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001064 x->mark.v = m->v;
1065 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001066 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001067 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001068 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001069 list_add(&x->km.all, &net->xfrm.state_all);
1070 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1071 h = xfrm_src_hash(net, daddr, saddr, family);
1072 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001073
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001074 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001075
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001076 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001077 }
1078
1079 return x;
1080}
1081
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001082static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084int xfrm_state_add(struct xfrm_state *x)
1085{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001086 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001087 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int family;
1089 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001090 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001091 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
David S. Miller37b08e32008-09-02 20:14:15 -07001095 to_put = NULL;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_lock_bh(&xfrm_state_lock);
1098
David S. Milleredcd5822006-08-24 00:42:45 -07001099 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001101 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 x1 = NULL;
1103 err = -EEXIST;
1104 goto out;
1105 }
1106
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001107 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001108 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001109 if (x1 && ((x1->id.proto != x->id.proto) ||
1110 xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001111 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 x1 = NULL;
1113 }
1114 }
1115
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001116 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001117 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1118 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001119 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
David S. Millerc7f5ea32006-08-24 03:29:04 -07001121 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 __xfrm_state_insert(x);
1123 err = 0;
1124
1125out:
1126 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (x1) {
1129 xfrm_state_delete(x1);
1130 xfrm_state_put(x1);
1131 }
1132
David S. Miller37b08e32008-09-02 20:14:15 -07001133 if (to_put)
1134 xfrm_state_put(to_put);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return err;
1137}
1138EXPORT_SYMBOL(xfrm_state_add);
1139
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001140#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001141static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001142{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001143 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001144 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001145 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001146 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001147 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001148
1149 memcpy(&x->id, &orig->id, sizeof(x->id));
1150 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1151 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1152 x->props.mode = orig->props.mode;
1153 x->props.replay_window = orig->props.replay_window;
1154 x->props.reqid = orig->props.reqid;
1155 x->props.family = orig->props.family;
1156 x->props.saddr = orig->props.saddr;
1157
1158 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001159 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001160 if (!x->aalg)
1161 goto error;
1162 }
1163 x->props.aalgo = orig->props.aalgo;
1164
1165 if (orig->ealg) {
1166 x->ealg = xfrm_algo_clone(orig->ealg);
1167 if (!x->ealg)
1168 goto error;
1169 }
1170 x->props.ealgo = orig->props.ealgo;
1171
1172 if (orig->calg) {
1173 x->calg = xfrm_algo_clone(orig->calg);
1174 if (!x->calg)
1175 goto error;
1176 }
1177 x->props.calgo = orig->props.calgo;
1178
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001179 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001180 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1181 if (!x->encap)
1182 goto error;
1183 }
1184
1185 if (orig->coaddr) {
1186 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1187 GFP_KERNEL);
1188 if (!x->coaddr)
1189 goto error;
1190 }
1191
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001192 if (orig->replay_esn) {
1193 err = xfrm_replay_clone(x, orig);
1194 if (err)
1195 goto error;
1196 }
1197
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001198 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1199
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001200 err = xfrm_init_state(x);
1201 if (err)
1202 goto error;
1203
1204 x->props.flags = orig->props.flags;
1205
1206 x->curlft.add_time = orig->curlft.add_time;
1207 x->km.state = orig->km.state;
1208 x->km.seq = orig->km.seq;
1209
1210 return x;
1211
1212 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001213 xfrm_state_put(x);
1214out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001215 if (errp)
1216 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001217 return NULL;
1218}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001219
1220/* xfrm_state_lock is held */
1221struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
1222{
1223 unsigned int h;
1224 struct xfrm_state *x;
1225 struct hlist_node *entry;
1226
1227 if (m->reqid) {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001228 h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001229 m->reqid, m->old_family);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08001230 hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001231 if (x->props.mode != m->mode ||
1232 x->id.proto != m->proto)
1233 continue;
1234 if (m->reqid && x->props.reqid != m->reqid)
1235 continue;
1236 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1237 m->old_family) ||
1238 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1239 m->old_family))
1240 continue;
1241 xfrm_state_hold(x);
1242 return x;
1243 }
1244 } else {
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -08001245 h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001246 m->old_family);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08001247 hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001248 if (x->props.mode != m->mode ||
1249 x->id.proto != m->proto)
1250 continue;
1251 if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
1252 m->old_family) ||
1253 xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
1254 m->old_family))
1255 continue;
1256 xfrm_state_hold(x);
1257 return x;
1258 }
1259 }
1260
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001261 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001262}
1263EXPORT_SYMBOL(xfrm_migrate_state_find);
1264
1265struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
1266 struct xfrm_migrate *m)
1267{
1268 struct xfrm_state *xc;
1269 int err;
1270
1271 xc = xfrm_state_clone(x, &err);
1272 if (!xc)
1273 return NULL;
1274
1275 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1276 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1277
1278 /* add state */
1279 if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
1280 /* a care is needed when the destination address of the
1281 state is to be updated as it is a part of triplet */
1282 xfrm_state_insert(xc);
1283 } else {
1284 if ((err = xfrm_state_add(xc)) < 0)
1285 goto error;
1286 }
1287
1288 return xc;
1289error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001290 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001291 return NULL;
1292}
1293EXPORT_SYMBOL(xfrm_state_migrate);
1294#endif
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296int xfrm_state_update(struct xfrm_state *x)
1297{
David S. Miller37b08e32008-09-02 20:14:15 -07001298 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001300 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
David S. Miller37b08e32008-09-02 20:14:15 -07001302 to_put = NULL;
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 spin_lock_bh(&xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001305 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 err = -ESRCH;
1308 if (!x1)
1309 goto out;
1310
1311 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001312 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 err = -EEXIST;
1314 goto out;
1315 }
1316
1317 if (x1->km.state == XFRM_STATE_ACQ) {
1318 __xfrm_state_insert(x);
1319 x = NULL;
1320 }
1321 err = 0;
1322
1323out:
1324 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
David S. Miller37b08e32008-09-02 20:14:15 -07001326 if (to_put)
1327 xfrm_state_put(to_put);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (err)
1330 return err;
1331
1332 if (!x) {
1333 xfrm_state_delete(x1);
1334 xfrm_state_put(x1);
1335 return 0;
1336 }
1337
1338 err = -EINVAL;
1339 spin_lock_bh(&x1->lock);
1340 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1341 if (x->encap && x1->encap)
1342 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001343 if (x->coaddr && x1->coaddr) {
1344 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1345 }
1346 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1347 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1349 x1->km.dying = 0;
1350
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001351 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (x1->curlft.use_time)
1353 xfrm_state_check_expire(x1);
1354
1355 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001356 x->km.state = XFRM_STATE_DEAD;
1357 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 spin_unlock_bh(&x1->lock);
1360
1361 xfrm_state_put(x1);
1362
1363 return err;
1364}
1365EXPORT_SYMBOL(xfrm_state_update);
1366
1367int xfrm_state_check_expire(struct xfrm_state *x)
1368{
1369 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001370 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1373 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001374 x->km.state = XFRM_STATE_EXPIRED;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001375 tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return -EINVAL;
1377 }
1378
1379 if (!x->km.dying &&
1380 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001381 x->curlft.packets >= x->lft.soft_packet_limit)) {
1382 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001383 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386}
1387EXPORT_SYMBOL(xfrm_state_check_expire);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001390xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001391 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001396 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 spin_unlock_bh(&xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return x;
1399}
1400EXPORT_SYMBOL(xfrm_state_lookup);
1401
1402struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001403xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001404 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001405 u8 proto, unsigned short family)
1406{
1407 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001408
1409 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001410 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001411 spin_unlock_bh(&xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001412 return x;
1413}
1414EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1415
1416struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001417xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001418 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int create, unsigned short family)
1420{
1421 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001424 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 spin_unlock_bh(&xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return x;
1428}
1429EXPORT_SYMBOL(xfrm_find_acq);
1430
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001431#ifdef CONFIG_XFRM_SUB_POLICY
1432int
1433xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1434 unsigned short family)
1435{
1436 int err = 0;
1437 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1438 if (!afinfo)
1439 return -EAFNOSUPPORT;
1440
1441 spin_lock_bh(&xfrm_state_lock);
1442 if (afinfo->tmpl_sort)
1443 err = afinfo->tmpl_sort(dst, src, n);
1444 spin_unlock_bh(&xfrm_state_lock);
1445 xfrm_state_put_afinfo(afinfo);
1446 return err;
1447}
1448EXPORT_SYMBOL(xfrm_tmpl_sort);
1449
1450int
1451xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1452 unsigned short family)
1453{
1454 int err = 0;
1455 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1456 if (!afinfo)
1457 return -EAFNOSUPPORT;
1458
1459 spin_lock_bh(&xfrm_state_lock);
1460 if (afinfo->state_sort)
1461 err = afinfo->state_sort(dst, src, n);
1462 spin_unlock_bh(&xfrm_state_lock);
1463 xfrm_state_put_afinfo(afinfo);
1464 return err;
1465}
1466EXPORT_SYMBOL(xfrm_state_sort);
1467#endif
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469/* Silly enough, but I'm lazy to build resolution list */
1470
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001471static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001475 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001476 struct hlist_node *entry;
1477 struct xfrm_state *x;
1478
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001479 hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001480 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001481 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001482 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 xfrm_state_hold(x);
1484 return x;
1485 }
1486 }
1487 }
1488 return NULL;
1489}
1490
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001491struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct xfrm_state *x;
1494
1495 spin_lock_bh(&xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001496 x = __xfrm_find_acq_byseq(net, mark, seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 spin_unlock_bh(&xfrm_state_lock);
1498 return x;
1499}
1500EXPORT_SYMBOL(xfrm_find_acq_byseq);
1501
1502u32 xfrm_get_acqseq(void)
1503{
1504 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001505 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
jamal6836b9b2010-02-16 02:01:22 +00001507 do {
1508 res = atomic_inc_return(&acqseq);
1509 } while (!res);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return res;
1512}
1513EXPORT_SYMBOL(xfrm_get_acqseq);
1514
Herbert Xu658b2192007-10-09 13:29:52 -07001515int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001517 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001518 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001520 int err = -ENOENT;
1521 __be32 minspi = htonl(low);
1522 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001523 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Herbert Xu658b2192007-10-09 13:29:52 -07001525 spin_lock_bh(&x->lock);
1526 if (x->km.state == XFRM_STATE_DEAD)
1527 goto unlock;
1528
1529 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001531 goto unlock;
1532
1533 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001536 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (x0) {
1538 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001539 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541 x->id.spi = minspi;
1542 } else {
1543 u32 spi = 0;
Al Viro26977b42006-09-27 18:47:05 -07001544 for (h=0; h<high-low+1; h++) {
1545 spi = low + net_random()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001546 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (x0 == NULL) {
1548 x->id.spi = htonl(spi);
1549 break;
1550 }
1551 xfrm_state_put(x0);
1552 }
1553 }
1554 if (x->id.spi) {
1555 spin_lock_bh(&xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001556 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1557 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 spin_unlock_bh(&xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001559
1560 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Herbert Xu658b2192007-10-09 13:29:52 -07001562
1563unlock:
1564 spin_unlock_bh(&x->lock);
1565
1566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568EXPORT_SYMBOL(xfrm_alloc_spi);
1569
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001570int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001571 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 void *data)
1573{
Herbert Xu12a169e2008-10-01 07:03:24 -07001574 struct xfrm_state *state;
1575 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int err = 0;
1577
Herbert Xu12a169e2008-10-01 07:03:24 -07001578 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001579 return 0;
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 spin_lock_bh(&xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001582 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001583 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001584 else
1585 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001586 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001587 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001588 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001589 state = container_of(x, struct xfrm_state, km);
1590 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001591 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001592 err = func(state, walk->seq, data);
1593 if (err) {
1594 list_move_tail(&walk->all, &x->all);
1595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001597 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001599 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 err = -ENOENT;
1601 goto out;
1602 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001603 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604out:
1605 spin_unlock_bh(&xfrm_state_lock);
1606 return err;
1607}
1608EXPORT_SYMBOL(xfrm_state_walk);
1609
Herbert Xu5c182452008-09-22 19:48:19 -07001610void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1611{
Herbert Xu12a169e2008-10-01 07:03:24 -07001612 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001613 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001614 walk->state = XFRM_STATE_DEAD;
1615 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001616}
1617EXPORT_SYMBOL(xfrm_state_walk_init);
1618
Herbert Xuabb81c42008-09-09 19:58:29 -07001619void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1620{
Herbert Xu12a169e2008-10-01 07:03:24 -07001621 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001622 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001623
Herbert Xu12a169e2008-10-01 07:03:24 -07001624 spin_lock_bh(&xfrm_state_lock);
1625 list_del(&walk->all);
Chuck Ebbert7d0b5912009-03-27 00:22:01 -07001626 spin_unlock_bh(&xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001627}
1628EXPORT_SYMBOL(xfrm_state_walk_done);
1629
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001630static void xfrm_replay_timer_handler(unsigned long data)
1631{
1632 struct xfrm_state *x = (struct xfrm_state*)data;
1633
1634 spin_lock(&x->lock);
1635
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001636 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001637 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001638 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001639 else
1640 x->xflags |= XFRM_TIME_DEFER;
1641 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001642
1643 spin_unlock(&x->lock);
1644}
1645
Denis Chengdf018122007-12-07 00:51:11 -08001646static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
David S. Miller214e0052011-02-24 00:02:38 -05001648void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 struct xfrm_mgr *km;
1651
Cong Wang85168c02013-01-16 16:05:06 +08001652 rcu_read_lock();
1653 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001654 if (km->notify_policy)
1655 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001656 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001657}
1658
David S. Miller214e0052011-02-24 00:02:38 -05001659void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001660{
1661 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001662 rcu_read_lock();
1663 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001664 if (km->notify)
1665 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001666 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001667}
1668
1669EXPORT_SYMBOL(km_policy_notify);
1670EXPORT_SYMBOL(km_state_notify);
1671
Eric W. Biederman15e47302012-09-07 20:12:54 +00001672void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001673{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001674 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001675 struct km_event c;
1676
Herbert Xubf088672005-06-18 22:44:00 -07001677 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001678 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001679 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001683 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001686EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001687/*
1688 * We send to all registered managers regardless of failure
1689 * We are happy with one success
1690*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001691int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001693 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct xfrm_mgr *km;
1695
Cong Wang85168c02013-01-16 16:05:06 +08001696 rcu_read_lock();
1697 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001698 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699 if (!acqret)
1700 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Cong Wang85168c02013-01-16 16:05:06 +08001702 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return err;
1704}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001705EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Al Viro5d36b182006-11-08 00:24:06 -08001707int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 int err = -EINVAL;
1710 struct xfrm_mgr *km;
1711
Cong Wang85168c02013-01-16 16:05:06 +08001712 rcu_read_lock();
1713 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (km->new_mapping)
1715 err = km->new_mapping(x, ipaddr, sport);
1716 if (!err)
1717 break;
1718 }
Cong Wang85168c02013-01-16 16:05:06 +08001719 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return err;
1721}
1722EXPORT_SYMBOL(km_new_mapping);
1723
Eric W. Biederman15e47302012-09-07 20:12:54 +00001724void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001726 struct net *net = xp_net(pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001727 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Herbert Xubf088672005-06-18 22:44:00 -07001729 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001730 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001731 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001732 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 if (hard)
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001735 wake_up(&net->xfrm.km_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
David S. Millera70fcb02006-03-20 19:18:52 -08001737EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001739#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001740int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1741 const struct xfrm_migrate *m, int num_migrate,
1742 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001743{
1744 int err = -EINVAL;
1745 int ret;
1746 struct xfrm_mgr *km;
1747
Cong Wang85168c02013-01-16 16:05:06 +08001748 rcu_read_lock();
1749 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001750 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001751 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001752 if (!ret)
1753 err = ret;
1754 }
1755 }
Cong Wang85168c02013-01-16 16:05:06 +08001756 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001757 return err;
1758}
1759EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001760#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001761
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001762int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001763{
1764 int err = -EINVAL;
1765 int ret;
1766 struct xfrm_mgr *km;
1767
Cong Wang85168c02013-01-16 16:05:06 +08001768 rcu_read_lock();
1769 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001770 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001771 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001772 if (!ret)
1773 err = ret;
1774 }
1775 }
Cong Wang85168c02013-01-16 16:05:06 +08001776 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001777 return err;
1778}
1779EXPORT_SYMBOL(km_report);
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1782{
1783 int err;
1784 u8 *data;
1785 struct xfrm_mgr *km;
1786 struct xfrm_policy *pol = NULL;
1787
1788 if (optlen <= 0 || optlen > PAGE_SIZE)
1789 return -EMSGSIZE;
1790
1791 data = kmalloc(optlen, GFP_KERNEL);
1792 if (!data)
1793 return -ENOMEM;
1794
1795 err = -EFAULT;
1796 if (copy_from_user(data, optval, optlen))
1797 goto out;
1798
1799 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001800 rcu_read_lock();
1801 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001802 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 optlen, &err);
1804 if (err >= 0)
1805 break;
1806 }
Cong Wang85168c02013-01-16 16:05:06 +08001807 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (err >= 0) {
1810 xfrm_sk_policy_insert(sk, err, pol);
1811 xfrm_pol_put(pol);
1812 err = 0;
1813 }
1814
1815out:
1816 kfree(data);
1817 return err;
1818}
1819EXPORT_SYMBOL(xfrm_user_policy);
1820
Cong Wang85168c02013-01-16 16:05:06 +08001821static DEFINE_SPINLOCK(xfrm_km_lock);
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823int xfrm_register_km(struct xfrm_mgr *km)
1824{
Cong Wang85168c02013-01-16 16:05:06 +08001825 spin_lock_bh(&xfrm_km_lock);
1826 list_add_tail_rcu(&km->list, &xfrm_km_list);
1827 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
1829}
1830EXPORT_SYMBOL(xfrm_register_km);
1831
1832int xfrm_unregister_km(struct xfrm_mgr *km)
1833{
Cong Wang85168c02013-01-16 16:05:06 +08001834 spin_lock_bh(&xfrm_km_lock);
1835 list_del_rcu(&km->list);
1836 spin_unlock_bh(&xfrm_km_lock);
1837 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return 0;
1839}
1840EXPORT_SYMBOL(xfrm_unregister_km);
1841
1842int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1843{
1844 int err = 0;
1845 if (unlikely(afinfo == NULL))
1846 return -EINVAL;
1847 if (unlikely(afinfo->family >= NPROTO))
1848 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001849 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1851 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001852 else
Cong Wang44abdc32013-01-16 16:05:05 +08001853 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1854 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return err;
1856}
1857EXPORT_SYMBOL(xfrm_state_register_afinfo);
1858
1859int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1860{
1861 int err = 0;
1862 if (unlikely(afinfo == NULL))
1863 return -EINVAL;
1864 if (unlikely(afinfo->family >= NPROTO))
1865 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001866 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1868 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1869 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001870 else
Cong Wang44abdc32013-01-16 16:05:05 +08001871 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Cong Wang44abdc32013-01-16 16:05:05 +08001873 spin_unlock_bh(&xfrm_state_afinfo_lock);
1874 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return err;
1876}
1877EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1878
Herbert Xu17c2a422007-10-17 21:33:12 -07001879static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 struct xfrm_state_afinfo *afinfo;
1882 if (unlikely(family >= NPROTO))
1883 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001884 rcu_read_lock();
1885 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001886 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001887 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return afinfo;
1889}
1890
Herbert Xu17c2a422007-10-17 21:33:12 -07001891static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Cong Wang44abdc32013-01-16 16:05:05 +08001893 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1897void xfrm_state_delete_tunnel(struct xfrm_state *x)
1898{
1899 if (x->tunnel) {
1900 struct xfrm_state *t = x->tunnel;
1901
1902 if (atomic_read(&t->tunnel_users) == 2)
1903 xfrm_state_delete(t);
1904 atomic_dec(&t->tunnel_users);
1905 xfrm_state_put(t);
1906 x->tunnel = NULL;
1907 }
1908}
1909EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1910
1911int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1912{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001913 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Patrick McHardyc5c25232007-04-09 11:47:18 -07001915 spin_lock_bh(&x->lock);
1916 if (x->km.state == XFRM_STATE_VALID &&
1917 x->type && x->type->get_mtu)
1918 res = x->type->get_mtu(x, mtu);
1919 else
Patrick McHardy28121612007-06-18 22:30:15 -07001920 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001921 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return res;
1923}
1924
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001925int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001926{
Herbert Xud094cd82005-06-20 13:19:41 -07001927 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001928 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001929 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001930 int err;
1931
Herbert Xud094cd82005-06-20 13:19:41 -07001932 err = -EAFNOSUPPORT;
1933 afinfo = xfrm_state_get_afinfo(family);
1934 if (!afinfo)
1935 goto error;
1936
1937 err = 0;
1938 if (afinfo->init_flags)
1939 err = afinfo->init_flags(x);
1940
1941 xfrm_state_put_afinfo(afinfo);
1942
1943 if (err)
1944 goto error;
1945
1946 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001947
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001948 if (x->sel.family != AF_UNSPEC) {
1949 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1950 if (inner_mode == NULL)
1951 goto error;
1952
1953 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1954 family != x->sel.family) {
1955 xfrm_put_mode(inner_mode);
1956 goto error;
1957 }
1958
1959 x->inner_mode = inner_mode;
1960 } else {
1961 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001962 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001963
Martin Willid81d2282008-12-03 15:38:07 -08001964 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001965 if (inner_mode == NULL)
1966 goto error;
1967
1968 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1969 xfrm_put_mode(inner_mode);
1970 goto error;
1971 }
Martin Willid81d2282008-12-03 15:38:07 -08001972 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001973
Martin Willid81d2282008-12-03 15:38:07 -08001974 if (x->props.family == AF_INET)
1975 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001976
Martin Willid81d2282008-12-03 15:38:07 -08001977 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1978 if (inner_mode_iaf) {
1979 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1980 x->inner_mode_iaf = inner_mode_iaf;
1981 else
1982 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001983 }
1984 }
Herbert Xu13996372007-10-17 21:35:51 -07001985
Herbert Xud094cd82005-06-20 13:19:41 -07001986 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07001987 if (x->type == NULL)
1988 goto error;
1989
1990 err = x->type->init_state(x);
1991 if (err)
1992 goto error;
1993
Herbert Xu13996372007-10-17 21:35:51 -07001994 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00001995 if (x->outer_mode == NULL) {
1996 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07001997 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00001998 }
Herbert Xub59f45d2006-05-27 23:05:54 -07001999
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002000 if (init_replay) {
2001 err = xfrm_init_replay(x);
2002 if (err)
2003 goto error;
2004 }
2005
Herbert Xu72cb6962005-06-20 13:18:08 -07002006 x->km.state = XFRM_STATE_VALID;
2007
2008error:
2009 return err;
2010}
2011
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002012EXPORT_SYMBOL(__xfrm_init_state);
2013
2014int xfrm_init_state(struct xfrm_state *x)
2015{
2016 return __xfrm_init_state(x, true);
2017}
2018
Herbert Xu72cb6962005-06-20 13:18:08 -07002019EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002020
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002021int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
David S. Millerf034b5d2006-08-24 03:08:07 -07002023 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002025 INIT_LIST_HEAD(&net->xfrm.state_all);
2026
David S. Millerf034b5d2006-08-24 03:08:07 -07002027 sz = sizeof(struct hlist_head) * 8;
2028
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002029 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2030 if (!net->xfrm.state_bydst)
2031 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002032 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2033 if (!net->xfrm.state_bysrc)
2034 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002035 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2036 if (!net->xfrm.state_byspi)
2037 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002038 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002039
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002040 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002041 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002042 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002043 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Alexey Dobriyan50a30652008-11-25 17:21:01 -08002044 init_waitqueue_head(&net->xfrm.km_waitq);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002045 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002046
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002047out_byspi:
2048 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002049out_bysrc:
2050 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002051out_bydst:
2052 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002053}
2054
2055void xfrm_state_fini(struct net *net)
2056{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002057 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002058 unsigned int sz;
2059
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002060 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002061 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002062 audit_info.sessionid = -1;
2063 audit_info.secid = 0;
2064 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2065 flush_work(&net->xfrm.state_gc_work);
2066
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002067 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002068
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002069 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002070 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2071 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002072 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2073 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002074 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2075 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Joy Lattenab5f5e82007-09-17 11:51:22 -07002078#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002079static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2080 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002081{
Paul Moore68277ac2007-12-20 20:49:33 -08002082 struct xfrm_sec_ctx *ctx = x->security;
2083 u32 spi = ntohl(x->id.spi);
2084
2085 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002086 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002087 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002088
2089 switch(x->props.family) {
2090 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002091 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2092 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002093 break;
2094 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002095 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002096 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002097 break;
2098 }
Paul Moore68277ac2007-12-20 20:49:33 -08002099
2100 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002101}
2102
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002103static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2104 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002105{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002106 const struct iphdr *iph4;
2107 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002108
2109 switch (family) {
2110 case AF_INET:
2111 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002112 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2113 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002114 break;
2115 case AF_INET6:
2116 iph6 = ipv6_hdr(skb);
2117 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002118 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002119 &iph6->saddr,&iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002120 iph6->flow_lbl[0] & 0x0f,
2121 iph6->flow_lbl[1],
2122 iph6->flow_lbl[2]);
2123 break;
2124 }
2125}
2126
Paul Moore68277ac2007-12-20 20:49:33 -08002127void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002128 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002129{
2130 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002131
Paul Mooreafeb14b2007-12-21 14:58:11 -08002132 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002133 if (audit_buf == NULL)
2134 return;
Eric Paris25323862008-04-18 10:09:25 -04002135 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002136 xfrm_audit_helper_sainfo(x, audit_buf);
2137 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002138 audit_log_end(audit_buf);
2139}
2140EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2141
Paul Moore68277ac2007-12-20 20:49:33 -08002142void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002143 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002144{
2145 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002146
Paul Mooreafeb14b2007-12-21 14:58:11 -08002147 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002148 if (audit_buf == NULL)
2149 return;
Eric Paris25323862008-04-18 10:09:25 -04002150 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002151 xfrm_audit_helper_sainfo(x, audit_buf);
2152 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002153 audit_log_end(audit_buf);
2154}
2155EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002156
2157void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2158 struct sk_buff *skb)
2159{
2160 struct audit_buffer *audit_buf;
2161 u32 spi;
2162
2163 audit_buf = xfrm_audit_start("SA-replay-overflow");
2164 if (audit_buf == NULL)
2165 return;
2166 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2167 /* don't record the sequence number because it's inherent in this kind
2168 * of audit message */
2169 spi = ntohl(x->id.spi);
2170 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2171 audit_log_end(audit_buf);
2172}
2173EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2174
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002175void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002176 struct sk_buff *skb, __be32 net_seq)
2177{
2178 struct audit_buffer *audit_buf;
2179 u32 spi;
2180
2181 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2182 if (audit_buf == NULL)
2183 return;
2184 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2185 spi = ntohl(x->id.spi);
2186 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2187 spi, spi, ntohl(net_seq));
2188 audit_log_end(audit_buf);
2189}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002190EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002191
2192void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2193{
2194 struct audit_buffer *audit_buf;
2195
2196 audit_buf = xfrm_audit_start("SA-notfound");
2197 if (audit_buf == NULL)
2198 return;
2199 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2200 audit_log_end(audit_buf);
2201}
2202EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2203
2204void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2205 __be32 net_spi, __be32 net_seq)
2206{
2207 struct audit_buffer *audit_buf;
2208 u32 spi;
2209
2210 audit_buf = xfrm_audit_start("SA-notfound");
2211 if (audit_buf == NULL)
2212 return;
2213 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2214 spi = ntohl(net_spi);
2215 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2216 spi, spi, ntohl(net_seq));
2217 audit_log_end(audit_buf);
2218}
2219EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2220
2221void xfrm_audit_state_icvfail(struct xfrm_state *x,
2222 struct sk_buff *skb, u8 proto)
2223{
2224 struct audit_buffer *audit_buf;
2225 __be32 net_spi;
2226 __be32 net_seq;
2227
2228 audit_buf = xfrm_audit_start("SA-icv-failure");
2229 if (audit_buf == NULL)
2230 return;
2231 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2232 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2233 u32 spi = ntohl(net_spi);
2234 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2235 spi, spi, ntohl(net_seq));
2236 }
2237 audit_log_end(audit_buf);
2238}
2239EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002240#endif /* CONFIG_AUDITSYSCALL */