blob: a26b7aa794755f970756cadb3c817bfc500956d2 [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
David S. Millerf034b5d2006-08-24 03:08:07 -070038static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080040static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050041 const xfrm_address_t *daddr,
42 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070043 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070044 unsigned short family)
45{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080046 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070047}
48
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080049static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050050 const xfrm_address_t *daddr,
51 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070052 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070053{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070055}
56
David S. Miller2575b652006-08-24 03:26:44 -070057static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050058xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
59 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070060{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080061 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070062}
63
David S. Millerf034b5d2006-08-24 03:08:07 -070064static void xfrm_hash_transfer(struct hlist_head *list,
65 struct hlist_head *ndsttable,
66 struct hlist_head *nsrctable,
67 struct hlist_head *nspitable,
68 unsigned int nhashmask)
69{
Sasha Levinb67bfe02013-02-27 17:06:00 -080070 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070071 struct xfrm_state *x;
72
Sasha Levinb67bfe02013-02-27 17:06:00 -080073 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070074 unsigned int h;
75
David S. Millerc1969f22006-08-24 04:00:03 -070076 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
77 x->props.reqid, x->props.family,
78 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070079 hlist_add_head(&x->bydst, ndsttable+h);
80
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070081 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
82 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070083 nhashmask);
84 hlist_add_head(&x->bysrc, nsrctable+h);
85
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070086 if (x->id.spi) {
87 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
88 x->id.proto, x->props.family,
89 nhashmask);
90 hlist_add_head(&x->byspi, nspitable+h);
91 }
David S. Millerf034b5d2006-08-24 03:08:07 -070092 }
93}
94
Alexey Dobriyan63082732008-11-25 17:19:07 -080095static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -070096{
Alexey Dobriyan63082732008-11-25 17:19:07 -080097 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -070098}
99
100static DEFINE_MUTEX(hash_resize_mutex);
101
Alexey Dobriyan63082732008-11-25 17:19:07 -0800102static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700103{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800104 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700105 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
106 unsigned long nsize, osize;
107 unsigned int nhashmask, ohashmask;
108 int i;
109
110 mutex_lock(&hash_resize_mutex);
111
Alexey Dobriyan63082732008-11-25 17:19:07 -0800112 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700113 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700114 if (!ndst)
115 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700116 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700117 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700118 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 goto out_unlock;
120 }
David S. Miller44e36b42006-08-24 04:50:50 -0700121 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700123 xfrm_hash_free(ndst, nsize);
124 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700125 goto out_unlock;
126 }
127
Fan Du283bc9f2013-11-07 17:47:50 +0800128 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700129
130 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800131 for (i = net->xfrm.state_hmask; i >= 0; i--)
132 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700133 nhashmask);
134
Alexey Dobriyan63082732008-11-25 17:19:07 -0800135 odst = net->xfrm.state_bydst;
136 osrc = net->xfrm.state_bysrc;
137 ospi = net->xfrm.state_byspi;
138 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700139
Alexey Dobriyan63082732008-11-25 17:19:07 -0800140 net->xfrm.state_bydst = ndst;
141 net->xfrm.state_bysrc = nsrc;
142 net->xfrm.state_byspi = nspi;
143 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700144
Fan Du283bc9f2013-11-07 17:47:50 +0800145 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700146
147 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700148 xfrm_hash_free(odst, osize);
149 xfrm_hash_free(osrc, osize);
150 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700151
152out_unlock:
153 mutex_unlock(&hash_resize_mutex);
154}
155
Cong Wang44abdc32013-01-16 16:05:05 +0800156static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
157static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static DEFINE_SPINLOCK(xfrm_state_gc_lock);
160
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800161int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800163int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000164void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Cong Wang7a9885b2013-01-17 16:34:11 +0800166static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800167int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700168{
Cong Wang7a9885b2013-01-17 16:34:11 +0800169 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800170 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700171 int err = 0;
172
173 if (unlikely(afinfo == NULL))
174 return -EAFNOSUPPORT;
175 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800176 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700177
178 if (likely(typemap[type->proto] == NULL))
179 typemap[type->proto] = type;
180 else
181 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800182 spin_unlock_bh(&xfrm_type_lock);
183 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700184 return err;
185}
186EXPORT_SYMBOL(xfrm_register_type);
187
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800188int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700189{
Cong Wang7a9885b2013-01-17 16:34:11 +0800190 struct xfrm_state_afinfo *afinfo = xfrm_state_get_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;
Cong Wang7a9885b2013-01-17 16:34:11 +0800197 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700198
199 if (unlikely(typemap[type->proto] != type))
200 err = -ENOENT;
201 else
202 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800203 spin_unlock_bh(&xfrm_type_lock);
204 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700205 return err;
206}
207EXPORT_SYMBOL(xfrm_unregister_type);
208
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800209static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700210{
211 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800212 const struct xfrm_type **typemap;
213 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700214 int modload_attempted = 0;
215
216retry:
217 afinfo = xfrm_state_get_afinfo(family);
218 if (unlikely(afinfo == NULL))
219 return NULL;
220 typemap = afinfo->type_map;
221
222 type = typemap[proto];
223 if (unlikely(type && !try_module_get(type->owner)))
224 type = NULL;
225 if (!type && !modload_attempted) {
226 xfrm_state_put_afinfo(afinfo);
227 request_module("xfrm-type-%d-%d", family, proto);
228 modload_attempted = 1;
229 goto retry;
230 }
231
232 xfrm_state_put_afinfo(afinfo);
233 return type;
234}
235
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800236static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700237{
238 module_put(type->owner);
239}
240
Cong Wang7a9885b2013-01-17 16:34:11 +0800241static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700242int xfrm_register_mode(struct xfrm_mode *mode, int family)
243{
244 struct xfrm_state_afinfo *afinfo;
245 struct xfrm_mode **modemap;
246 int err;
247
248 if (unlikely(mode->encap >= XFRM_MODE_MAX))
249 return -EINVAL;
250
Cong Wang7a9885b2013-01-17 16:34:11 +0800251 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700252 if (unlikely(afinfo == NULL))
253 return -EAFNOSUPPORT;
254
255 err = -EEXIST;
256 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800257 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700258 if (modemap[mode->encap])
259 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700260
Herbert Xu17c2a422007-10-17 21:33:12 -0700261 err = -ENOENT;
262 if (!try_module_get(afinfo->owner))
263 goto out;
264
265 mode->afinfo = afinfo;
266 modemap[mode->encap] = mode;
267 err = 0;
268
269out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800270 spin_unlock_bh(&xfrm_mode_lock);
271 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700272 return err;
273}
274EXPORT_SYMBOL(xfrm_register_mode);
275
276int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
277{
278 struct xfrm_state_afinfo *afinfo;
279 struct xfrm_mode **modemap;
280 int err;
281
282 if (unlikely(mode->encap >= XFRM_MODE_MAX))
283 return -EINVAL;
284
Cong Wang7a9885b2013-01-17 16:34:11 +0800285 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700286 if (unlikely(afinfo == NULL))
287 return -EAFNOSUPPORT;
288
289 err = -ENOENT;
290 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800291 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700292 if (likely(modemap[mode->encap] == mode)) {
293 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700294 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700295 err = 0;
296 }
297
Cong Wang7a9885b2013-01-17 16:34:11 +0800298 spin_unlock_bh(&xfrm_mode_lock);
299 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700300 return err;
301}
302EXPORT_SYMBOL(xfrm_unregister_mode);
303
304static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
305{
306 struct xfrm_state_afinfo *afinfo;
307 struct xfrm_mode *mode;
308 int modload_attempted = 0;
309
310 if (unlikely(encap >= XFRM_MODE_MAX))
311 return NULL;
312
313retry:
314 afinfo = xfrm_state_get_afinfo(family);
315 if (unlikely(afinfo == NULL))
316 return NULL;
317
318 mode = afinfo->mode_map[encap];
319 if (unlikely(mode && !try_module_get(mode->owner)))
320 mode = NULL;
321 if (!mode && !modload_attempted) {
322 xfrm_state_put_afinfo(afinfo);
323 request_module("xfrm-mode-%d-%d", family, encap);
324 modload_attempted = 1;
325 goto retry;
326 }
327
328 xfrm_state_put_afinfo(afinfo);
329 return mode;
330}
331
332static void xfrm_put_mode(struct xfrm_mode *mode)
333{
334 module_put(mode->owner);
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static void xfrm_state_gc_destroy(struct xfrm_state *x)
338{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800339 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700340 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800341 kfree(x->aalg);
342 kfree(x->ealg);
343 kfree(x->calg);
344 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700345 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000346 kfree(x->replay_esn);
347 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700348 if (x->inner_mode)
349 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700350 if (x->inner_mode_iaf)
351 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700352 if (x->outer_mode)
353 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (x->type) {
355 x->type->destructor(x);
356 xfrm_put_type(x->type);
357 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800358 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 kfree(x);
360}
361
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800362static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800364 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700365 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800366 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700367 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800370 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 spin_unlock_bh(&xfrm_state_gc_lock);
372
Sasha Levinb67bfe02013-02-27 17:06:00 -0800373 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377static inline unsigned long make_jiffies(long secs)
378{
379 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
380 return MAX_SCHEDULE_TIMEOUT-1;
381 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900382 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800385static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800387 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
388 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800389 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 long next = LONG_MAX;
391 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600392 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 spin_lock(&x->lock);
395 if (x->km.state == XFRM_STATE_DEAD)
396 goto out;
397 if (x->km.state == XFRM_STATE_EXPIRED)
398 goto expired;
399 if (x->lft.hard_add_expires_seconds) {
400 long tmo = x->lft.hard_add_expires_seconds +
401 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000402 if (tmo <= 0) {
403 if (x->xflags & XFRM_SOFT_EXPIRE) {
404 /* enter hard expire without soft expire first?!
405 * setting a new date could trigger this.
406 * workarbound: fix x->curflt.add_time by below:
407 */
408 x->curlft.add_time = now - x->saved_tmo - 1;
409 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
410 } else
411 goto expired;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (tmo < next)
414 next = tmo;
415 }
416 if (x->lft.hard_use_expires_seconds) {
417 long tmo = x->lft.hard_use_expires_seconds +
418 (x->curlft.use_time ? : now) - now;
419 if (tmo <= 0)
420 goto expired;
421 if (tmo < next)
422 next = tmo;
423 }
424 if (x->km.dying)
425 goto resched;
426 if (x->lft.soft_add_expires_seconds) {
427 long tmo = x->lft.soft_add_expires_seconds +
428 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000429 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000431 x->xflags &= ~XFRM_SOFT_EXPIRE;
432 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000434 x->xflags |= XFRM_SOFT_EXPIRE;
435 x->saved_tmo = tmo;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 if (x->lft.soft_use_expires_seconds) {
439 long tmo = x->lft.soft_use_expires_seconds +
440 (x->curlft.use_time ? : now) - now;
441 if (tmo <= 0)
442 warn = 1;
443 else if (tmo < next)
444 next = tmo;
445 }
446
Herbert Xu4666faa2005-06-18 22:43:22 -0700447 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800449 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800451 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800452 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
453 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto out;
456
457expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200458 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600460
461 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200462 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800463 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Joy Lattenab5f5e82007-09-17 11:51:22 -0700465 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400466 audit_get_loginuid(current),
467 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469out:
470 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800471 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
David S. Miller0ac84752006-03-20 19:18:23 -0800474static void xfrm_replay_timer_handler(unsigned long data);
475
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800476struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct xfrm_state *x;
479
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700480 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800483 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 atomic_set(&x->refcnt, 1);
485 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700486 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700487 INIT_HLIST_NODE(&x->bydst);
488 INIT_HLIST_NODE(&x->bysrc);
489 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800490 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
491 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800492 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
493 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800494 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 x->lft.soft_byte_limit = XFRM_INF;
496 x->lft.soft_packet_limit = XFRM_INF;
497 x->lft.hard_byte_limit = XFRM_INF;
498 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800499 x->replay_maxage = 0;
500 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700501 x->inner_mode = NULL;
502 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 spin_lock_init(&x->lock);
504 }
505 return x;
506}
507EXPORT_SYMBOL(xfrm_state_alloc);
508
509void __xfrm_state_destroy(struct xfrm_state *x)
510{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800511 struct net *net = xs_net(x);
512
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700513 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800516 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800518 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520EXPORT_SYMBOL(__xfrm_state_destroy);
521
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800522int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800524 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700525 int err = -ESRCH;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (x->km.state != XFRM_STATE_DEAD) {
528 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800529 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700530 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700531 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700532 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700533 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700534 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800535 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800536 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* All xfrm_state objects are created by xfrm_state_alloc.
539 * The xfrm_state_alloc call gives a reference, and that
540 * is what we are dropping here.
541 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800542 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700543 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700545
546 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800548EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700550int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700552 int err;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700555 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700557
558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560EXPORT_SYMBOL(xfrm_state_delete);
561
Joy Latten4aa2e622007-06-04 19:05:57 -0400562#ifdef CONFIG_SECURITY_NETWORK_XFRM
563static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800564xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Joy Latten4aa2e622007-06-04 19:05:57 -0400566 int i, err = 0;
567
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800568 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400569 struct xfrm_state *x;
570
Sasha Levinb67bfe02013-02-27 17:06:00 -0800571 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400572 if (xfrm_id_proto_match(x->id.proto, proto) &&
573 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700574 xfrm_audit_state_delete(x, 0,
575 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400576 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700577 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400578 return err;
579 }
580 }
581 }
582
583 return err;
584}
585#else
586static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800587xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400588{
589 return 0;
590}
591#endif
592
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800593int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400594{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000595 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Fan Du283bc9f2013-11-07 17:47:50 +0800597 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800598 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400599 if (err)
600 goto out;
601
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000602 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800603 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700604 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800606 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700608 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800610 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Joy Latten161a09e2006-11-27 13:11:54 -0600612 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700613 xfrm_audit_state_delete(x, err ? 0 : 1,
614 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400615 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700616 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000618 if (!err)
619 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Fan Du283bc9f2013-11-07 17:47:50 +0800621 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto restart;
623 }
624 }
625 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000626 if (cnt)
627 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400628
629out:
Fan Du283bc9f2013-11-07 17:47:50 +0800630 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400631 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633EXPORT_SYMBOL(xfrm_state_flush);
634
Alexey Dobriyane0710412010-01-23 13:37:10 +0000635void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700636{
Fan Du283bc9f2013-11-07 17:47:50 +0800637 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000638 si->sadcnt = net->xfrm.state_num;
639 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700640 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800641 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700642}
643EXPORT_SYMBOL(xfrm_sad_getinfo);
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static int
David S. Miller1a898592011-02-22 18:22:34 -0800646xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500647 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500648 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700649 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
652 if (!afinfo)
653 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700654 afinfo->init_tempsel(&x->sel, fl);
655
656 if (family != tmpl->encap_family) {
657 xfrm_state_put_afinfo(afinfo);
658 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
659 if (!afinfo)
660 return -1;
661 }
662 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfrm_state_put_afinfo(afinfo);
664 return 0;
665}
666
David S. Miller9aa60082011-02-24 01:51:36 -0500667static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
668 const xfrm_address_t *daddr,
669 __be32 spi, u8 proto,
670 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700671{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800672 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700673 struct xfrm_state *x;
674
Sasha Levinb67bfe02013-02-27 17:06:00 -0800675 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700676 if (x->props.family != family ||
677 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000678 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000679 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700680 continue;
681
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000682 if ((mark & x->mark.m) != x->mark.v)
683 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700684 xfrm_state_hold(x);
685 return x;
686 }
687
688 return NULL;
689}
690
David S. Miller9aa60082011-02-24 01:51:36 -0500691static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
692 const xfrm_address_t *daddr,
693 const xfrm_address_t *saddr,
694 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700695{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800696 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700697 struct xfrm_state *x;
698
Sasha Levinb67bfe02013-02-27 17:06:00 -0800699 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700700 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000701 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000702 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
703 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700704 continue;
705
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000706 if ((mark & x->mark.m) != x->mark.v)
707 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700708 xfrm_state_hold(x);
709 return x;
710 }
711
712 return NULL;
713}
714
715static inline struct xfrm_state *
716__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
717{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800718 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800719 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800720
David S. Milleredcd5822006-08-24 00:42:45 -0700721 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800722 return __xfrm_state_lookup(net, mark, &x->id.daddr,
723 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700724 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800725 return __xfrm_state_lookup_byaddr(net, mark,
726 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700727 &x->props.saddr,
728 x->id.proto, family);
729}
730
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800731static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700732{
733 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800734 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
735 net->xfrm.state_num > net->xfrm.state_hmask)
736 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700737}
738
David S. Miller08ec9af2009-03-13 14:22:40 -0700739static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800740 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700741 struct xfrm_state **best, int *acq_in_progress,
742 int *error)
743{
744 /* Resolution logic:
745 * 1. There is a valid state with matching selector. Done.
746 * 2. Valid state with inappropriate selector. Skip.
747 *
748 * Entering area of "sysdeps".
749 *
750 * 3. If state is not valid, selector is temporary, it selects
751 * only session which triggered previous resolution. Key
752 * manager will do something to install a state with proper
753 * selector.
754 */
755 if (x->km.state == XFRM_STATE_VALID) {
756 if ((x->sel.family &&
757 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
758 !security_xfrm_state_pol_flow_match(x, pol, fl))
759 return;
760
761 if (!*best ||
762 (*best)->km.dying > x->km.dying ||
763 ((*best)->km.dying == x->km.dying &&
764 (*best)->curlft.add_time < x->curlft.add_time))
765 *best = x;
766 } else if (x->km.state == XFRM_STATE_ACQ) {
767 *acq_in_progress = 1;
768 } else if (x->km.state == XFRM_STATE_ERROR ||
769 x->km.state == XFRM_STATE_EXPIRED) {
770 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
771 security_xfrm_state_pol_flow_match(x, pol, fl))
772 *error = -ESRCH;
773 }
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500777xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800778 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct xfrm_policy *pol, int *err,
780 unsigned short family)
781{
David S. Miller08ec9af2009-03-13 14:22:40 -0700782 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800783 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700784 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700785 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int acquire_in_progress = 0;
787 int error = 0;
788 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800789 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700790 unsigned short encap_family = tmpl->encap_family;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900791
David S. Miller37b08e32008-09-02 20:14:15 -0700792 to_put = NULL;
793
Fan Du283bc9f2013-11-07 17:47:50 +0800794 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700795 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800796 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700797 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000799 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700800 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700801 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 tmpl->mode == x->props.mode &&
803 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700804 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500805 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700806 &best, &acquire_in_progress, &error);
807 }
Fan Du6f115632013-09-23 17:18:25 +0800808 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700809 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Thomas Egerer8444cf72010-09-20 11:11:38 -0700811 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800812 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700813 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700814 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000815 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700816 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800817 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700818 tmpl->mode == x->props.mode &&
819 tmpl->id.proto == x->id.proto &&
820 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500821 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700822 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
David S. Miller08ec9af2009-03-13 14:22:40 -0700825found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 x = best;
827 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700828 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800829 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700830 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700831 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 error = -EEXIST;
833 goto out;
834 }
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800835 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (x == NULL) {
837 error = -ENOMEM;
838 goto out;
839 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700840 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700842 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800843 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
David S. Miller1d28f422011-03-12 00:29:39 -0500845 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700846 if (error) {
847 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700848 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700849 x = NULL;
850 goto out;
851 }
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (km_query(x, tmpl, pol) == 0) {
854 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800855 list_add(&x->km.all, &net->xfrm.state_all);
856 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700857 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800858 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700860 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800861 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800863 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800864 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800865 net->xfrm.state_num++;
866 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else {
868 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700869 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 x = NULL;
871 error = -ESRCH;
872 }
873 }
874out:
875 if (x)
876 xfrm_state_hold(x);
877 else
878 *err = acquire_in_progress ? -EAGAIN : error;
Fan Du283bc9f2013-11-07 17:47:50 +0800879 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700880 if (to_put)
881 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return x;
883}
884
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700885struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800886xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800887 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700888 unsigned short family, u8 mode, u8 proto, u32 reqid)
889{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800890 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700891 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700892
Fan Du4ae770b2014-01-03 11:18:29 +0800893 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800894 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800895 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700896 if (x->props.family == family &&
897 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000898 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700899 !(x->props.flags & XFRM_STATE_WILDRECV) &&
900 xfrm_state_addr_check(x, daddr, saddr, family) &&
901 mode == x->props.mode &&
902 proto == x->id.proto &&
903 x->km.state == XFRM_STATE_VALID) {
904 rx = x;
905 break;
906 }
907 }
908
909 if (rx)
910 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +0800911 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700912
913
914 return rx;
915}
916EXPORT_SYMBOL(xfrm_stateonly_find);
917
Fan Duc4549972014-01-03 11:18:32 +0800918struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
919 unsigned short family)
920{
921 struct xfrm_state *x;
922 struct xfrm_state_walk *w;
923
924 spin_lock_bh(&net->xfrm.xfrm_state_lock);
925 list_for_each_entry(w, &net->xfrm.state_all, all) {
926 x = container_of(w, struct xfrm_state, km);
927 if (x->props.family != family ||
928 x->id.spi != spi)
929 continue;
930
931 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
932 xfrm_state_hold(x);
933 return x;
934 }
935 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
936 return NULL;
937}
938EXPORT_SYMBOL(xfrm_state_lookup_byspi);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940static void __xfrm_state_insert(struct xfrm_state *x)
941{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800942 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700943 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800945 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800946
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800947 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700948 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800949 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800951 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
952 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700954 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700956 x->props.family);
957
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800958 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700959 }
960
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800961 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700962 if (x->replay_maxage)
963 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800964
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800965 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700966
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800967 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Fan Du283bc9f2013-11-07 17:47:50 +0800970/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -0700971static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
972{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800973 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700974 unsigned short family = xnew->props.family;
975 u32 reqid = xnew->props.reqid;
976 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700977 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000978 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700979
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800980 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800981 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700982 if (x->props.family == family &&
983 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000984 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000985 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
986 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000987 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700988 }
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991void xfrm_state_insert(struct xfrm_state *x)
992{
Fan Du283bc9f2013-11-07 17:47:50 +0800993 struct net *net = xs_net(x);
994
995 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700996 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800998 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000EXPORT_SYMBOL(xfrm_state_insert);
1001
Fan Du283bc9f2013-11-07 17:47:50 +08001002/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001003static struct xfrm_state *__find_acq_core(struct net *net,
1004 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001005 unsigned short family, u8 mode,
1006 u32 reqid, u8 proto,
1007 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001008 const xfrm_address_t *saddr,
1009 int create)
David S. Miller27708342006-08-24 00:13:10 -07001010{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001011 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001012 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001013 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001014
Sasha Levinb67bfe02013-02-27 17:06:00 -08001015 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001016 if (x->props.reqid != reqid ||
1017 x->props.mode != mode ||
1018 x->props.family != family ||
1019 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001020 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001021 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001022 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001023 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1024 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001025 continue;
1026
David S. Miller27708342006-08-24 00:13:10 -07001027 xfrm_state_hold(x);
1028 return x;
1029 }
1030
1031 if (!create)
1032 return NULL;
1033
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001034 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001035 if (likely(x)) {
1036 switch (family) {
1037 case AF_INET:
1038 x->sel.daddr.a4 = daddr->a4;
1039 x->sel.saddr.a4 = saddr->a4;
1040 x->sel.prefixlen_d = 32;
1041 x->sel.prefixlen_s = 32;
1042 x->props.saddr.a4 = saddr->a4;
1043 x->id.daddr.a4 = daddr->a4;
1044 break;
1045
1046 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001047 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1048 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001049 x->sel.prefixlen_d = 128;
1050 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001051 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1052 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001053 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001054 }
David S. Miller27708342006-08-24 00:13:10 -07001055
1056 x->km.state = XFRM_STATE_ACQ;
1057 x->id.proto = proto;
1058 x->props.family = family;
1059 x->props.mode = mode;
1060 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001061 x->mark.v = m->v;
1062 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001063 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001064 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001065 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001066 list_add(&x->km.all, &net->xfrm.state_all);
1067 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1068 h = xfrm_src_hash(net, daddr, saddr, family);
1069 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001070
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001071 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001072
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001073 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001074 }
1075
1076 return x;
1077}
1078
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001079static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081int xfrm_state_add(struct xfrm_state *x)
1082{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001083 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001084 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int family;
1086 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001087 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001088 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
David S. Miller37b08e32008-09-02 20:14:15 -07001092 to_put = NULL;
1093
Fan Du283bc9f2013-11-07 17:47:50 +08001094 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
David S. Milleredcd5822006-08-24 00:42:45 -07001096 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001098 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 x1 = NULL;
1100 err = -EEXIST;
1101 goto out;
1102 }
1103
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001104 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001105 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001106 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001107 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001108 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 x1 = NULL;
1110 }
1111 }
1112
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001113 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001114 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1115 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001116 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
David S. Millerc7f5ea32006-08-24 03:29:04 -07001118 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 __xfrm_state_insert(x);
1120 err = 0;
1121
1122out:
Fan Du283bc9f2013-11-07 17:47:50 +08001123 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (x1) {
1126 xfrm_state_delete(x1);
1127 xfrm_state_put(x1);
1128 }
1129
David S. Miller37b08e32008-09-02 20:14:15 -07001130 if (to_put)
1131 xfrm_state_put(to_put);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return err;
1134}
1135EXPORT_SYMBOL(xfrm_state_add);
1136
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001137#ifdef CONFIG_XFRM_MIGRATE
Eric Dumazet66663512008-01-08 01:35:52 -08001138static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001139{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001140 struct net *net = xs_net(orig);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001141 int err = -ENOMEM;
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001142 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001143 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001144 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001145
1146 memcpy(&x->id, &orig->id, sizeof(x->id));
1147 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1148 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1149 x->props.mode = orig->props.mode;
1150 x->props.replay_window = orig->props.replay_window;
1151 x->props.reqid = orig->props.reqid;
1152 x->props.family = orig->props.family;
1153 x->props.saddr = orig->props.saddr;
1154
1155 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001156 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001157 if (!x->aalg)
1158 goto error;
1159 }
1160 x->props.aalgo = orig->props.aalgo;
1161
1162 if (orig->ealg) {
1163 x->ealg = xfrm_algo_clone(orig->ealg);
1164 if (!x->ealg)
1165 goto error;
1166 }
1167 x->props.ealgo = orig->props.ealgo;
1168
1169 if (orig->calg) {
1170 x->calg = xfrm_algo_clone(orig->calg);
1171 if (!x->calg)
1172 goto error;
1173 }
1174 x->props.calgo = orig->props.calgo;
1175
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001176 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001177 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1178 if (!x->encap)
1179 goto error;
1180 }
1181
1182 if (orig->coaddr) {
1183 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1184 GFP_KERNEL);
1185 if (!x->coaddr)
1186 goto error;
1187 }
1188
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001189 if (orig->replay_esn) {
1190 err = xfrm_replay_clone(x, orig);
1191 if (err)
1192 goto error;
1193 }
1194
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001195 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1196
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001197 err = xfrm_init_state(x);
1198 if (err)
1199 goto error;
1200
1201 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001202 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001203
1204 x->curlft.add_time = orig->curlft.add_time;
1205 x->km.state = orig->km.state;
1206 x->km.seq = orig->km.seq;
1207
1208 return x;
1209
1210 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001211 xfrm_state_put(x);
1212out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001213 if (errp)
1214 *errp = err;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001215 return NULL;
1216}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001217
Fan Du283bc9f2013-11-07 17:47:50 +08001218/* net->xfrm.xfrm_state_lock is held */
1219struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001220{
1221 unsigned int h;
1222 struct xfrm_state *x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001223
1224 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001225 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001226 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001227 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001228 if (x->props.mode != m->mode ||
1229 x->id.proto != m->proto)
1230 continue;
1231 if (m->reqid && x->props.reqid != m->reqid)
1232 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001233 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1234 m->old_family) ||
1235 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1236 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001237 continue;
1238 xfrm_state_hold(x);
1239 return x;
1240 }
1241 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001242 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001243 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001244 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001245 if (x->props.mode != m->mode ||
1246 x->id.proto != m->proto)
1247 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001248 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1249 m->old_family) ||
1250 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1251 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001252 continue;
1253 xfrm_state_hold(x);
1254 return x;
1255 }
1256 }
1257
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001258 return NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001259}
1260EXPORT_SYMBOL(xfrm_migrate_state_find);
1261
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001262struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1263 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001264{
1265 struct xfrm_state *xc;
1266 int err;
1267
1268 xc = xfrm_state_clone(x, &err);
1269 if (!xc)
1270 return NULL;
1271
1272 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1273 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1274
1275 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001276 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001277 /* a care is needed when the destination address of the
1278 state is to be updated as it is a part of triplet */
1279 xfrm_state_insert(xc);
1280 } else {
1281 if ((err = xfrm_state_add(xc)) < 0)
1282 goto error;
1283 }
1284
1285 return xc;
1286error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001287 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001288 return NULL;
1289}
1290EXPORT_SYMBOL(xfrm_state_migrate);
1291#endif
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293int xfrm_state_update(struct xfrm_state *x)
1294{
David S. Miller37b08e32008-09-02 20:14:15 -07001295 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001297 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001298 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
David S. Miller37b08e32008-09-02 20:14:15 -07001300 to_put = NULL;
1301
Fan Du283bc9f2013-11-07 17:47:50 +08001302 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001303 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 err = -ESRCH;
1306 if (!x1)
1307 goto out;
1308
1309 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001310 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 err = -EEXIST;
1312 goto out;
1313 }
1314
1315 if (x1->km.state == XFRM_STATE_ACQ) {
1316 __xfrm_state_insert(x);
1317 x = NULL;
1318 }
1319 err = 0;
1320
1321out:
Fan Du283bc9f2013-11-07 17:47:50 +08001322 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
David S. Miller37b08e32008-09-02 20:14:15 -07001324 if (to_put)
1325 xfrm_state_put(to_put);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (err)
1328 return err;
1329
1330 if (!x) {
1331 xfrm_state_delete(x1);
1332 xfrm_state_put(x1);
1333 return 0;
1334 }
1335
1336 err = -EINVAL;
1337 spin_lock_bh(&x1->lock);
1338 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1339 if (x->encap && x1->encap)
1340 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001341 if (x->coaddr && x1->coaddr) {
1342 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1343 }
1344 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1345 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1347 x1->km.dying = 0;
1348
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001349 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (x1->curlft.use_time)
1351 xfrm_state_check_expire(x1);
1352
1353 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001354 x->km.state = XFRM_STATE_DEAD;
1355 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 spin_unlock_bh(&x1->lock);
1358
1359 xfrm_state_put(x1);
1360
1361 return err;
1362}
1363EXPORT_SYMBOL(xfrm_state_update);
1364
1365int xfrm_state_check_expire(struct xfrm_state *x)
1366{
1367 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001368 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1371 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001372 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001373 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EINVAL;
1375 }
1376
1377 if (!x->km.dying &&
1378 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001379 x->curlft.packets >= x->lft.soft_packet_limit)) {
1380 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001381 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384}
1385EXPORT_SYMBOL(xfrm_state_check_expire);
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001388xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001389 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Fan Du283bc9f2013-11-07 17:47:50 +08001393 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001394 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001395 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return x;
1397}
1398EXPORT_SYMBOL(xfrm_state_lookup);
1399
1400struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001401xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001402 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001403 u8 proto, unsigned short family)
1404{
1405 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001406
Fan Du283bc9f2013-11-07 17:47:50 +08001407 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001408 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001409 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001410 return x;
1411}
1412EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1413
1414struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001415xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1416 u8 proto, const xfrm_address_t *daddr,
1417 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Fan Du283bc9f2013-11-07 17:47:50 +08001421 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001422 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001423 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return x;
1426}
1427EXPORT_SYMBOL(xfrm_find_acq);
1428
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001429#ifdef CONFIG_XFRM_SUB_POLICY
1430int
1431xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001432 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001433{
1434 int err = 0;
1435 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1436 if (!afinfo)
1437 return -EAFNOSUPPORT;
1438
Fan Du283bc9f2013-11-07 17:47:50 +08001439 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001440 if (afinfo->tmpl_sort)
1441 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001442 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001443 xfrm_state_put_afinfo(afinfo);
1444 return err;
1445}
1446EXPORT_SYMBOL(xfrm_tmpl_sort);
1447
1448int
1449xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1450 unsigned short family)
1451{
1452 int err = 0;
1453 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Fan Du283bc9f2013-11-07 17:47:50 +08001454 struct net *net = xs_net(*dst);
1455
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001456 if (!afinfo)
1457 return -EAFNOSUPPORT;
1458
Fan Du283bc9f2013-11-07 17:47:50 +08001459 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001460 if (afinfo->state_sort)
1461 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001462 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001463 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 xfrm_state *x;
1477
Sasha Levinb67bfe02013-02-27 17:06:00 -08001478 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001479 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001480 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001481 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 xfrm_state_hold(x);
1483 return x;
1484 }
1485 }
1486 }
1487 return NULL;
1488}
1489
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001490struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct xfrm_state *x;
1493
Fan Du283bc9f2013-11-07 17:47:50 +08001494 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001495 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001496 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return x;
1498}
1499EXPORT_SYMBOL(xfrm_find_acq_byseq);
1500
1501u32 xfrm_get_acqseq(void)
1502{
1503 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001504 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
jamal6836b9b2010-02-16 02:01:22 +00001506 do {
1507 res = atomic_inc_return(&acqseq);
1508 } while (!res);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return res;
1511}
1512EXPORT_SYMBOL(xfrm_get_acqseq);
1513
Fan Du776e9dd2013-12-16 18:47:49 +08001514int verify_spi_info(u8 proto, u32 min, u32 max)
1515{
1516 switch (proto) {
1517 case IPPROTO_AH:
1518 case IPPROTO_ESP:
1519 break;
1520
1521 case IPPROTO_COMP:
1522 /* IPCOMP spi is 16-bits. */
1523 if (max >= 0x10000)
1524 return -EINVAL;
1525 break;
1526
1527 default:
1528 return -EINVAL;
1529 }
1530
1531 if (min > max)
1532 return -EINVAL;
1533
1534 return 0;
1535}
1536EXPORT_SYMBOL(verify_spi_info);
1537
Herbert Xu658b2192007-10-09 13:29:52 -07001538int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001540 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001541 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001543 int err = -ENOENT;
1544 __be32 minspi = htonl(low);
1545 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001546 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Herbert Xu658b2192007-10-09 13:29:52 -07001548 spin_lock_bh(&x->lock);
1549 if (x->km.state == XFRM_STATE_DEAD)
1550 goto unlock;
1551
1552 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001554 goto unlock;
1555
1556 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001559 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (x0) {
1561 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001562 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 x->id.spi = minspi;
1565 } else {
1566 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001567 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001568 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001569 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (x0 == NULL) {
1571 x->id.spi = htonl(spi);
1572 break;
1573 }
1574 xfrm_state_put(x0);
1575 }
1576 }
1577 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001578 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001579 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1580 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Fan Du283bc9f2013-11-07 17:47:50 +08001581 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001582
1583 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Herbert Xu658b2192007-10-09 13:29:52 -07001585
1586unlock:
1587 spin_unlock_bh(&x->lock);
1588
1589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591EXPORT_SYMBOL(xfrm_alloc_spi);
1592
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001593int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001594 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 void *data)
1596{
Herbert Xu12a169e2008-10-01 07:03:24 -07001597 struct xfrm_state *state;
1598 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int err = 0;
1600
Herbert Xu12a169e2008-10-01 07:03:24 -07001601 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001602 return 0;
1603
Fan Du283bc9f2013-11-07 17:47:50 +08001604 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001605 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001606 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001607 else
1608 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001609 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001610 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001611 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001612 state = container_of(x, struct xfrm_state, km);
1613 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001614 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001615 err = func(state, walk->seq, data);
1616 if (err) {
1617 list_move_tail(&walk->all, &x->all);
1618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001620 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001622 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 err = -ENOENT;
1624 goto out;
1625 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001626 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627out:
Fan Du283bc9f2013-11-07 17:47:50 +08001628 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return err;
1630}
1631EXPORT_SYMBOL(xfrm_state_walk);
1632
Herbert Xu5c182452008-09-22 19:48:19 -07001633void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
1634{
Herbert Xu12a169e2008-10-01 07:03:24 -07001635 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001636 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001637 walk->state = XFRM_STATE_DEAD;
1638 walk->seq = 0;
Herbert Xu5c182452008-09-22 19:48:19 -07001639}
1640EXPORT_SYMBOL(xfrm_state_walk_init);
1641
Fan Du283bc9f2013-11-07 17:47:50 +08001642void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001643{
Herbert Xu12a169e2008-10-01 07:03:24 -07001644 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001645 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001646
Fan Du283bc9f2013-11-07 17:47:50 +08001647 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001648 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001649 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001650}
1651EXPORT_SYMBOL(xfrm_state_walk_done);
1652
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001653static void xfrm_replay_timer_handler(unsigned long data)
1654{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001655 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001656
1657 spin_lock(&x->lock);
1658
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001659 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001660 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001661 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001662 else
1663 x->xflags |= XFRM_TIME_DEFER;
1664 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001665
1666 spin_unlock(&x->lock);
1667}
1668
Denis Chengdf018122007-12-07 00:51:11 -08001669static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
David S. Miller214e0052011-02-24 00:02:38 -05001671void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct xfrm_mgr *km;
1674
Cong Wang85168c02013-01-16 16:05:06 +08001675 rcu_read_lock();
1676 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001677 if (km->notify_policy)
1678 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001679 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680}
1681
David S. Miller214e0052011-02-24 00:02:38 -05001682void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001683{
1684 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001685 rcu_read_lock();
1686 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001687 if (km->notify)
1688 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001689 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001690}
1691
1692EXPORT_SYMBOL(km_policy_notify);
1693EXPORT_SYMBOL(km_state_notify);
1694
Eric W. Biederman15e47302012-09-07 20:12:54 +00001695void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696{
1697 struct km_event c;
1698
Herbert Xubf088672005-06-18 22:44:00 -07001699 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001700 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001701 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001702 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001705EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001706/*
1707 * We send to all registered managers regardless of failure
1708 * We are happy with one success
1709*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001710int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct xfrm_mgr *km;
1714
Cong Wang85168c02013-01-16 16:05:06 +08001715 rcu_read_lock();
1716 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001717 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718 if (!acqret)
1719 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
Cong Wang85168c02013-01-16 16:05:06 +08001721 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return err;
1723}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001724EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Al Viro5d36b182006-11-08 00:24:06 -08001726int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 int err = -EINVAL;
1729 struct xfrm_mgr *km;
1730
Cong Wang85168c02013-01-16 16:05:06 +08001731 rcu_read_lock();
1732 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (km->new_mapping)
1734 err = km->new_mapping(x, ipaddr, sport);
1735 if (!err)
1736 break;
1737 }
Cong Wang85168c02013-01-16 16:05:06 +08001738 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return err;
1740}
1741EXPORT_SYMBOL(km_new_mapping);
1742
Eric W. Biederman15e47302012-09-07 20:12:54 +00001743void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001745 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Herbert Xubf088672005-06-18 22:44:00 -07001747 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001748 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001749 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001750 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
David S. Millera70fcb02006-03-20 19:18:52 -08001752EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001754#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001755int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1756 const struct xfrm_migrate *m, int num_migrate,
1757 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001758{
1759 int err = -EINVAL;
1760 int ret;
1761 struct xfrm_mgr *km;
1762
Cong Wang85168c02013-01-16 16:05:06 +08001763 rcu_read_lock();
1764 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001765 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001766 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001767 if (!ret)
1768 err = ret;
1769 }
1770 }
Cong Wang85168c02013-01-16 16:05:06 +08001771 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001772 return err;
1773}
1774EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001775#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001776
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001777int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001778{
1779 int err = -EINVAL;
1780 int ret;
1781 struct xfrm_mgr *km;
1782
Cong Wang85168c02013-01-16 16:05:06 +08001783 rcu_read_lock();
1784 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001785 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001786 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001787 if (!ret)
1788 err = ret;
1789 }
1790 }
Cong Wang85168c02013-01-16 16:05:06 +08001791 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001792 return err;
1793}
1794EXPORT_SYMBOL(km_report);
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1797{
1798 int err;
1799 u8 *data;
1800 struct xfrm_mgr *km;
1801 struct xfrm_policy *pol = NULL;
1802
1803 if (optlen <= 0 || optlen > PAGE_SIZE)
1804 return -EMSGSIZE;
1805
1806 data = kmalloc(optlen, GFP_KERNEL);
1807 if (!data)
1808 return -ENOMEM;
1809
1810 err = -EFAULT;
1811 if (copy_from_user(data, optval, optlen))
1812 goto out;
1813
1814 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001815 rcu_read_lock();
1816 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001817 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 optlen, &err);
1819 if (err >= 0)
1820 break;
1821 }
Cong Wang85168c02013-01-16 16:05:06 +08001822 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (err >= 0) {
1825 xfrm_sk_policy_insert(sk, err, pol);
1826 xfrm_pol_put(pol);
1827 err = 0;
1828 }
1829
1830out:
1831 kfree(data);
1832 return err;
1833}
1834EXPORT_SYMBOL(xfrm_user_policy);
1835
Cong Wang85168c02013-01-16 16:05:06 +08001836static DEFINE_SPINLOCK(xfrm_km_lock);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838int xfrm_register_km(struct xfrm_mgr *km)
1839{
Cong Wang85168c02013-01-16 16:05:06 +08001840 spin_lock_bh(&xfrm_km_lock);
1841 list_add_tail_rcu(&km->list, &xfrm_km_list);
1842 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return 0;
1844}
1845EXPORT_SYMBOL(xfrm_register_km);
1846
1847int xfrm_unregister_km(struct xfrm_mgr *km)
1848{
Cong Wang85168c02013-01-16 16:05:06 +08001849 spin_lock_bh(&xfrm_km_lock);
1850 list_del_rcu(&km->list);
1851 spin_unlock_bh(&xfrm_km_lock);
1852 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854}
1855EXPORT_SYMBOL(xfrm_unregister_km);
1856
1857int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1858{
1859 int err = 0;
1860 if (unlikely(afinfo == NULL))
1861 return -EINVAL;
1862 if (unlikely(afinfo->family >= NPROTO))
1863 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001864 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1866 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001867 else
Cong Wang44abdc32013-01-16 16:05:05 +08001868 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1869 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return err;
1871}
1872EXPORT_SYMBOL(xfrm_state_register_afinfo);
1873
1874int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1875{
1876 int err = 0;
1877 if (unlikely(afinfo == NULL))
1878 return -EINVAL;
1879 if (unlikely(afinfo->family >= NPROTO))
1880 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001881 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1883 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1884 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001885 else
Cong Wang44abdc32013-01-16 16:05:05 +08001886 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
Cong Wang44abdc32013-01-16 16:05:05 +08001888 spin_unlock_bh(&xfrm_state_afinfo_lock);
1889 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return err;
1891}
1892EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1893
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001894struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 struct xfrm_state_afinfo *afinfo;
1897 if (unlikely(family >= NPROTO))
1898 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001899 rcu_read_lock();
1900 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001901 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001902 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return afinfo;
1904}
1905
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001906void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Cong Wang44abdc32013-01-16 16:05:05 +08001908 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1912void xfrm_state_delete_tunnel(struct xfrm_state *x)
1913{
1914 if (x->tunnel) {
1915 struct xfrm_state *t = x->tunnel;
1916
1917 if (atomic_read(&t->tunnel_users) == 2)
1918 xfrm_state_delete(t);
1919 atomic_dec(&t->tunnel_users);
1920 xfrm_state_put(t);
1921 x->tunnel = NULL;
1922 }
1923}
1924EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1925
1926int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1927{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001928 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Patrick McHardyc5c25232007-04-09 11:47:18 -07001930 spin_lock_bh(&x->lock);
1931 if (x->km.state == XFRM_STATE_VALID &&
1932 x->type && x->type->get_mtu)
1933 res = x->type->get_mtu(x, mtu);
1934 else
Patrick McHardy28121612007-06-18 22:30:15 -07001935 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001936 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return res;
1938}
1939
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001940int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001941{
Herbert Xud094cd82005-06-20 13:19:41 -07001942 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001943 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001944 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001945 int err;
1946
Herbert Xud094cd82005-06-20 13:19:41 -07001947 err = -EAFNOSUPPORT;
1948 afinfo = xfrm_state_get_afinfo(family);
1949 if (!afinfo)
1950 goto error;
1951
1952 err = 0;
1953 if (afinfo->init_flags)
1954 err = afinfo->init_flags(x);
1955
1956 xfrm_state_put_afinfo(afinfo);
1957
1958 if (err)
1959 goto error;
1960
1961 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07001962
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001963 if (x->sel.family != AF_UNSPEC) {
1964 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1965 if (inner_mode == NULL)
1966 goto error;
1967
1968 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1969 family != x->sel.family) {
1970 xfrm_put_mode(inner_mode);
1971 goto error;
1972 }
1973
1974 x->inner_mode = inner_mode;
1975 } else {
1976 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08001977 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001978
Martin Willid81d2282008-12-03 15:38:07 -08001979 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001980 if (inner_mode == NULL)
1981 goto error;
1982
1983 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1984 xfrm_put_mode(inner_mode);
1985 goto error;
1986 }
Martin Willid81d2282008-12-03 15:38:07 -08001987 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001988
Martin Willid81d2282008-12-03 15:38:07 -08001989 if (x->props.family == AF_INET)
1990 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001991
Martin Willid81d2282008-12-03 15:38:07 -08001992 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1993 if (inner_mode_iaf) {
1994 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1995 x->inner_mode_iaf = inner_mode_iaf;
1996 else
1997 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001998 }
1999 }
Herbert Xu13996372007-10-17 21:35:51 -07002000
Herbert Xud094cd82005-06-20 13:19:41 -07002001 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002002 if (x->type == NULL)
2003 goto error;
2004
2005 err = x->type->init_state(x);
2006 if (err)
2007 goto error;
2008
Herbert Xu13996372007-10-17 21:35:51 -07002009 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002010 if (x->outer_mode == NULL) {
2011 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002012 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002013 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002014
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002015 if (init_replay) {
2016 err = xfrm_init_replay(x);
2017 if (err)
2018 goto error;
2019 }
2020
Herbert Xu72cb6962005-06-20 13:18:08 -07002021 x->km.state = XFRM_STATE_VALID;
2022
2023error:
2024 return err;
2025}
2026
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002027EXPORT_SYMBOL(__xfrm_init_state);
2028
2029int xfrm_init_state(struct xfrm_state *x)
2030{
2031 return __xfrm_init_state(x, true);
2032}
2033
Herbert Xu72cb6962005-06-20 13:18:08 -07002034EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002035
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002036int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
David S. Millerf034b5d2006-08-24 03:08:07 -07002038 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002040 INIT_LIST_HEAD(&net->xfrm.state_all);
2041
David S. Millerf034b5d2006-08-24 03:08:07 -07002042 sz = sizeof(struct hlist_head) * 8;
2043
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002044 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2045 if (!net->xfrm.state_bydst)
2046 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002047 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2048 if (!net->xfrm.state_bysrc)
2049 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002050 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2051 if (!net->xfrm.state_byspi)
2052 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002053 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002054
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002055 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002056 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002057 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002058 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Fan Du283bc9f2013-11-07 17:47:50 +08002059 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002060 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002061
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002062out_byspi:
2063 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002064out_bysrc:
2065 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002066out_bydst:
2067 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002068}
2069
2070void xfrm_state_fini(struct net *net)
2071{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002072 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002073 unsigned int sz;
2074
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002075 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002076 audit_info.loginuid = INVALID_UID;
Eric Paris4440e852013-11-27 17:35:17 -05002077 audit_info.sessionid = (unsigned int)-1;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002078 audit_info.secid = 0;
2079 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2080 flush_work(&net->xfrm.state_gc_work);
2081
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002082 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002083
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002084 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002085 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2086 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002087 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2088 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002089 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2090 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Joy Lattenab5f5e82007-09-17 11:51:22 -07002093#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002094static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2095 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002096{
Paul Moore68277ac2007-12-20 20:49:33 -08002097 struct xfrm_sec_ctx *ctx = x->security;
2098 u32 spi = ntohl(x->id.spi);
2099
2100 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002101 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002102 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002103
Weilong Chen9b7a7872013-12-24 09:43:46 +08002104 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002105 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002106 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2107 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002108 break;
2109 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002110 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002111 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002112 break;
2113 }
Paul Moore68277ac2007-12-20 20:49:33 -08002114
2115 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002116}
2117
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002118static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2119 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002120{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002121 const struct iphdr *iph4;
2122 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002123
2124 switch (family) {
2125 case AF_INET:
2126 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002127 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2128 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002129 break;
2130 case AF_INET6:
2131 iph6 = ipv6_hdr(skb);
2132 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002133 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002134 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002135 iph6->flow_lbl[0] & 0x0f,
2136 iph6->flow_lbl[1],
2137 iph6->flow_lbl[2]);
2138 break;
2139 }
2140}
2141
Paul Moore68277ac2007-12-20 20:49:33 -08002142void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris4440e852013-11-27 17:35:17 -05002143 kuid_t auid, unsigned int 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-add");
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_add);
2156
Paul Moore68277ac2007-12-20 20:49:33 -08002157void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris4440e852013-11-27 17:35:17 -05002158 kuid_t auid, unsigned int sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002159{
2160 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002161
Paul Mooreafeb14b2007-12-21 14:58:11 -08002162 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002163 if (audit_buf == NULL)
2164 return;
Eric Paris25323862008-04-18 10:09:25 -04002165 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002166 xfrm_audit_helper_sainfo(x, audit_buf);
2167 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002168 audit_log_end(audit_buf);
2169}
2170EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002171
2172void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2173 struct sk_buff *skb)
2174{
2175 struct audit_buffer *audit_buf;
2176 u32 spi;
2177
2178 audit_buf = xfrm_audit_start("SA-replay-overflow");
2179 if (audit_buf == NULL)
2180 return;
2181 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2182 /* don't record the sequence number because it's inherent in this kind
2183 * of audit message */
2184 spi = ntohl(x->id.spi);
2185 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2186 audit_log_end(audit_buf);
2187}
2188EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2189
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002190void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002191 struct sk_buff *skb, __be32 net_seq)
2192{
2193 struct audit_buffer *audit_buf;
2194 u32 spi;
2195
2196 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2197 if (audit_buf == NULL)
2198 return;
2199 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2200 spi = ntohl(x->id.spi);
2201 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2202 spi, spi, ntohl(net_seq));
2203 audit_log_end(audit_buf);
2204}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002205EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002206
2207void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2208{
2209 struct audit_buffer *audit_buf;
2210
2211 audit_buf = xfrm_audit_start("SA-notfound");
2212 if (audit_buf == NULL)
2213 return;
2214 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2215 audit_log_end(audit_buf);
2216}
2217EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2218
2219void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2220 __be32 net_spi, __be32 net_seq)
2221{
2222 struct audit_buffer *audit_buf;
2223 u32 spi;
2224
2225 audit_buf = xfrm_audit_start("SA-notfound");
2226 if (audit_buf == NULL)
2227 return;
2228 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2229 spi = ntohl(net_spi);
2230 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2231 spi, spi, ntohl(net_seq));
2232 audit_log_end(audit_buf);
2233}
2234EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2235
2236void xfrm_audit_state_icvfail(struct xfrm_state *x,
2237 struct sk_buff *skb, u8 proto)
2238{
2239 struct audit_buffer *audit_buf;
2240 __be32 net_spi;
2241 __be32 net_seq;
2242
2243 audit_buf = xfrm_audit_start("SA-icv-failure");
2244 if (audit_buf == NULL)
2245 return;
2246 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2247 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2248 u32 spi = ntohl(net_spi);
2249 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2250 spi, spi, ntohl(net_seq));
2251 }
2252 audit_log_end(audit_buf);
2253}
2254EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002255#endif /* CONFIG_AUDITSYSCALL */