blob: a30f898dc1c5a82fb6221248799f5adbb07f2236 [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
Alexey Dobriyan63082732008-11-25 17:19:07 -0800100static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700101{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800102 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700103 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
104 unsigned long nsize, osize;
105 unsigned int nhashmask, ohashmask;
106 int i;
107
Alexey Dobriyan63082732008-11-25 17:19:07 -0800108 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700109 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700110 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800111 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700112 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700113 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700114 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800115 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700116 }
David S. Miller44e36b42006-08-24 04:50:50 -0700117 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700118 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700119 xfrm_hash_free(ndst, nsize);
120 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800121 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 }
123
Fan Du283bc9f2013-11-07 17:47:50 +0800124 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700125
126 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800127 for (i = net->xfrm.state_hmask; i >= 0; i--)
128 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700129 nhashmask);
130
Alexey Dobriyan63082732008-11-25 17:19:07 -0800131 odst = net->xfrm.state_bydst;
132 osrc = net->xfrm.state_bysrc;
133 ospi = net->xfrm.state_byspi;
134 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700135
Alexey Dobriyan63082732008-11-25 17:19:07 -0800136 net->xfrm.state_bydst = ndst;
137 net->xfrm.state_bysrc = nsrc;
138 net->xfrm.state_byspi = nspi;
139 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700140
Fan Du283bc9f2013-11-07 17:47:50 +0800141 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700142
143 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700144 xfrm_hash_free(odst, osize);
145 xfrm_hash_free(osrc, osize);
146 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700147}
148
Cong Wang44abdc32013-01-16 16:05:05 +0800149static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
150static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static DEFINE_SPINLOCK(xfrm_state_gc_lock);
153
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800154int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800156int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200157bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000158void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Cong Wang7a9885b2013-01-17 16:34:11 +0800160static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800161int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700162{
Cong Wang7a9885b2013-01-17 16:34:11 +0800163 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800164 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700165 int err = 0;
166
167 if (unlikely(afinfo == NULL))
168 return -EAFNOSUPPORT;
169 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800170 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700171
172 if (likely(typemap[type->proto] == NULL))
173 typemap[type->proto] = type;
174 else
175 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800176 spin_unlock_bh(&xfrm_type_lock);
177 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700178 return err;
179}
180EXPORT_SYMBOL(xfrm_register_type);
181
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800182int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700183{
Cong Wang7a9885b2013-01-17 16:34:11 +0800184 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800185 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700186 int err = 0;
187
188 if (unlikely(afinfo == NULL))
189 return -EAFNOSUPPORT;
190 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800191 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700192
193 if (unlikely(typemap[type->proto] != type))
194 err = -ENOENT;
195 else
196 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800197 spin_unlock_bh(&xfrm_type_lock);
198 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700199 return err;
200}
201EXPORT_SYMBOL(xfrm_unregister_type);
202
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800203static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700204{
205 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800206 const struct xfrm_type **typemap;
207 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700208 int modload_attempted = 0;
209
210retry:
211 afinfo = xfrm_state_get_afinfo(family);
212 if (unlikely(afinfo == NULL))
213 return NULL;
214 typemap = afinfo->type_map;
215
216 type = typemap[proto];
217 if (unlikely(type && !try_module_get(type->owner)))
218 type = NULL;
219 if (!type && !modload_attempted) {
220 xfrm_state_put_afinfo(afinfo);
221 request_module("xfrm-type-%d-%d", family, proto);
222 modload_attempted = 1;
223 goto retry;
224 }
225
226 xfrm_state_put_afinfo(afinfo);
227 return type;
228}
229
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800230static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700231{
232 module_put(type->owner);
233}
234
Cong Wang7a9885b2013-01-17 16:34:11 +0800235static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700236int xfrm_register_mode(struct xfrm_mode *mode, int family)
237{
238 struct xfrm_state_afinfo *afinfo;
239 struct xfrm_mode **modemap;
240 int err;
241
242 if (unlikely(mode->encap >= XFRM_MODE_MAX))
243 return -EINVAL;
244
Cong Wang7a9885b2013-01-17 16:34:11 +0800245 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700246 if (unlikely(afinfo == NULL))
247 return -EAFNOSUPPORT;
248
249 err = -EEXIST;
250 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800251 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700252 if (modemap[mode->encap])
253 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700254
Herbert Xu17c2a422007-10-17 21:33:12 -0700255 err = -ENOENT;
256 if (!try_module_get(afinfo->owner))
257 goto out;
258
259 mode->afinfo = afinfo;
260 modemap[mode->encap] = mode;
261 err = 0;
262
263out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800264 spin_unlock_bh(&xfrm_mode_lock);
265 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700266 return err;
267}
268EXPORT_SYMBOL(xfrm_register_mode);
269
270int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
271{
272 struct xfrm_state_afinfo *afinfo;
273 struct xfrm_mode **modemap;
274 int err;
275
276 if (unlikely(mode->encap >= XFRM_MODE_MAX))
277 return -EINVAL;
278
Cong Wang7a9885b2013-01-17 16:34:11 +0800279 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700280 if (unlikely(afinfo == NULL))
281 return -EAFNOSUPPORT;
282
283 err = -ENOENT;
284 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800285 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700286 if (likely(modemap[mode->encap] == mode)) {
287 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700288 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700289 err = 0;
290 }
291
Cong Wang7a9885b2013-01-17 16:34:11 +0800292 spin_unlock_bh(&xfrm_mode_lock);
293 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700294 return err;
295}
296EXPORT_SYMBOL(xfrm_unregister_mode);
297
298static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
299{
300 struct xfrm_state_afinfo *afinfo;
301 struct xfrm_mode *mode;
302 int modload_attempted = 0;
303
304 if (unlikely(encap >= XFRM_MODE_MAX))
305 return NULL;
306
307retry:
308 afinfo = xfrm_state_get_afinfo(family);
309 if (unlikely(afinfo == NULL))
310 return NULL;
311
312 mode = afinfo->mode_map[encap];
313 if (unlikely(mode && !try_module_get(mode->owner)))
314 mode = NULL;
315 if (!mode && !modload_attempted) {
316 xfrm_state_put_afinfo(afinfo);
317 request_module("xfrm-mode-%d-%d", family, encap);
318 modload_attempted = 1;
319 goto retry;
320 }
321
322 xfrm_state_put_afinfo(afinfo);
323 return mode;
324}
325
326static void xfrm_put_mode(struct xfrm_mode *mode)
327{
328 module_put(mode->owner);
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static void xfrm_state_gc_destroy(struct xfrm_state *x)
332{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800333 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700334 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000335 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800336 kfree(x->aalg);
337 kfree(x->ealg);
338 kfree(x->calg);
339 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700340 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000341 kfree(x->replay_esn);
342 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700343 if (x->inner_mode)
344 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700345 if (x->inner_mode_iaf)
346 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700347 if (x->outer_mode)
348 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (x->type) {
350 x->type->destructor(x);
351 xfrm_put_type(x->type);
352 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800353 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kfree(x);
355}
356
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800357static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800359 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700360 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800361 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700362 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800365 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_unlock_bh(&xfrm_state_gc_lock);
367
Sasha Levinb67bfe02013-02-27 17:06:00 -0800368 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static inline unsigned long make_jiffies(long secs)
373{
374 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
375 return MAX_SCHEDULE_TIMEOUT-1;
376 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900377 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800380static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800382 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
383 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800384 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 long next = LONG_MAX;
386 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600387 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 spin_lock(&x->lock);
390 if (x->km.state == XFRM_STATE_DEAD)
391 goto out;
392 if (x->km.state == XFRM_STATE_EXPIRED)
393 goto expired;
394 if (x->lft.hard_add_expires_seconds) {
395 long tmo = x->lft.hard_add_expires_seconds +
396 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000397 if (tmo <= 0) {
398 if (x->xflags & XFRM_SOFT_EXPIRE) {
399 /* enter hard expire without soft expire first?!
400 * setting a new date could trigger this.
401 * workarbound: fix x->curflt.add_time by below:
402 */
403 x->curlft.add_time = now - x->saved_tmo - 1;
404 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
405 } else
406 goto expired;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (tmo < next)
409 next = tmo;
410 }
411 if (x->lft.hard_use_expires_seconds) {
412 long tmo = x->lft.hard_use_expires_seconds +
413 (x->curlft.use_time ? : now) - now;
414 if (tmo <= 0)
415 goto expired;
416 if (tmo < next)
417 next = tmo;
418 }
419 if (x->km.dying)
420 goto resched;
421 if (x->lft.soft_add_expires_seconds) {
422 long tmo = x->lft.soft_add_expires_seconds +
423 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000424 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000426 x->xflags &= ~XFRM_SOFT_EXPIRE;
427 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000429 x->xflags |= XFRM_SOFT_EXPIRE;
430 x->saved_tmo = tmo;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 if (x->lft.soft_use_expires_seconds) {
434 long tmo = x->lft.soft_use_expires_seconds +
435 (x->curlft.use_time ? : now) - now;
436 if (tmo <= 0)
437 warn = 1;
438 else if (tmo < next)
439 next = tmo;
440 }
441
Herbert Xu4666faa2005-06-18 22:43:22 -0700442 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800444 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800446 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800447 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
448 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto out;
451
452expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200453 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600455
456 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200457 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800458 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Tetsuo Handa2e710292014-04-22 21:48:30 +0900460 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462out:
463 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800464 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
David S. Miller0ac84752006-03-20 19:18:23 -0800467static void xfrm_replay_timer_handler(unsigned long data);
468
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800469struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct xfrm_state *x;
472
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700473 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800476 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 atomic_set(&x->refcnt, 1);
478 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700479 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700480 INIT_HLIST_NODE(&x->bydst);
481 INIT_HLIST_NODE(&x->bysrc);
482 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800483 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
484 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800485 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
486 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800487 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 x->lft.soft_byte_limit = XFRM_INF;
489 x->lft.soft_packet_limit = XFRM_INF;
490 x->lft.hard_byte_limit = XFRM_INF;
491 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800492 x->replay_maxage = 0;
493 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700494 x->inner_mode = NULL;
495 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 spin_lock_init(&x->lock);
497 }
498 return x;
499}
500EXPORT_SYMBOL(xfrm_state_alloc);
501
502void __xfrm_state_destroy(struct xfrm_state *x)
503{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800504 struct net *net = xs_net(x);
505
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700506 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800509 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800511 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513EXPORT_SYMBOL(__xfrm_state_destroy);
514
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800515int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800517 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700518 int err = -ESRCH;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (x->km.state != XFRM_STATE_DEAD) {
521 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800522 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700523 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700524 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700525 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700526 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700527 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800528 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800529 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* All xfrm_state objects are created by xfrm_state_alloc.
532 * The xfrm_state_alloc call gives a reference, and that
533 * is what we are dropping here.
534 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800535 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700536 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700538
539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800541EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700543int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700545 int err;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700548 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700550
551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553EXPORT_SYMBOL(xfrm_state_delete);
554
Joy Latten4aa2e622007-06-04 19:05:57 -0400555#ifdef CONFIG_SECURITY_NETWORK_XFRM
556static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900557xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Joy Latten4aa2e622007-06-04 19:05:57 -0400559 int i, err = 0;
560
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800561 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400562 struct xfrm_state *x;
563
Sasha Levinb67bfe02013-02-27 17:06:00 -0800564 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400565 if (xfrm_id_proto_match(x->id.proto, proto) &&
566 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900567 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400568 return err;
569 }
570 }
571 }
572
573 return err;
574}
575#else
576static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900577xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400578{
579 return 0;
580}
581#endif
582
Tetsuo Handa2e710292014-04-22 21:48:30 +0900583int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400584{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000585 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Fan Du283bc9f2013-11-07 17:47:50 +0800587 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900588 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400589 if (err)
590 goto out;
591
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000592 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800593 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700594 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800596 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700598 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800600 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Joy Latten161a09e2006-11-27 13:11:54 -0600602 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700603 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900604 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000606 if (!err)
607 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Fan Du283bc9f2013-11-07 17:47:50 +0800609 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto restart;
611 }
612 }
613 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000614 if (cnt)
615 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400616
617out:
Fan Du283bc9f2013-11-07 17:47:50 +0800618 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621EXPORT_SYMBOL(xfrm_state_flush);
622
Alexey Dobriyane0710412010-01-23 13:37:10 +0000623void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700624{
Fan Du283bc9f2013-11-07 17:47:50 +0800625 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000626 si->sadcnt = net->xfrm.state_num;
627 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700628 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800629 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700630}
631EXPORT_SYMBOL(xfrm_sad_getinfo);
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static int
David S. Miller1a898592011-02-22 18:22:34 -0800634xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500635 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500636 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700637 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
640 if (!afinfo)
641 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700642 afinfo->init_tempsel(&x->sel, fl);
643
644 if (family != tmpl->encap_family) {
645 xfrm_state_put_afinfo(afinfo);
646 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
647 if (!afinfo)
648 return -1;
649 }
650 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 xfrm_state_put_afinfo(afinfo);
652 return 0;
653}
654
David S. Miller9aa60082011-02-24 01:51:36 -0500655static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
656 const xfrm_address_t *daddr,
657 __be32 spi, u8 proto,
658 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700659{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800660 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700661 struct xfrm_state *x;
662
Sasha Levinb67bfe02013-02-27 17:06:00 -0800663 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700664 if (x->props.family != family ||
665 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000666 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000667 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700668 continue;
669
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000670 if ((mark & x->mark.m) != x->mark.v)
671 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700672 xfrm_state_hold(x);
673 return x;
674 }
675
676 return NULL;
677}
678
David S. Miller9aa60082011-02-24 01:51:36 -0500679static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
680 const xfrm_address_t *daddr,
681 const xfrm_address_t *saddr,
682 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700683{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800684 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700685 struct xfrm_state *x;
686
Sasha Levinb67bfe02013-02-27 17:06:00 -0800687 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700688 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000689 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000690 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
691 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700692 continue;
693
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000694 if ((mark & x->mark.m) != x->mark.v)
695 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700696 xfrm_state_hold(x);
697 return x;
698 }
699
700 return NULL;
701}
702
703static inline struct xfrm_state *
704__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
705{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800706 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800707 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800708
David S. Milleredcd5822006-08-24 00:42:45 -0700709 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800710 return __xfrm_state_lookup(net, mark, &x->id.daddr,
711 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700712 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800713 return __xfrm_state_lookup_byaddr(net, mark,
714 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700715 &x->props.saddr,
716 x->id.proto, family);
717}
718
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800719static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700720{
721 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800722 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
723 net->xfrm.state_num > net->xfrm.state_hmask)
724 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700725}
726
David S. Miller08ec9af2009-03-13 14:22:40 -0700727static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800728 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700729 struct xfrm_state **best, int *acq_in_progress,
730 int *error)
731{
732 /* Resolution logic:
733 * 1. There is a valid state with matching selector. Done.
734 * 2. Valid state with inappropriate selector. Skip.
735 *
736 * Entering area of "sysdeps".
737 *
738 * 3. If state is not valid, selector is temporary, it selects
739 * only session which triggered previous resolution. Key
740 * manager will do something to install a state with proper
741 * selector.
742 */
743 if (x->km.state == XFRM_STATE_VALID) {
744 if ((x->sel.family &&
745 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
746 !security_xfrm_state_pol_flow_match(x, pol, fl))
747 return;
748
749 if (!*best ||
750 (*best)->km.dying > x->km.dying ||
751 ((*best)->km.dying == x->km.dying &&
752 (*best)->curlft.add_time < x->curlft.add_time))
753 *best = x;
754 } else if (x->km.state == XFRM_STATE_ACQ) {
755 *acq_in_progress = 1;
756 } else if (x->km.state == XFRM_STATE_ERROR ||
757 x->km.state == XFRM_STATE_EXPIRED) {
758 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
759 security_xfrm_state_pol_flow_match(x, pol, fl))
760 *error = -ESRCH;
761 }
762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500765xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800766 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct xfrm_policy *pol, int *err,
768 unsigned short family)
769{
David S. Miller08ec9af2009-03-13 14:22:40 -0700770 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800771 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700772 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700773 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int acquire_in_progress = 0;
775 int error = 0;
776 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800777 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700778 unsigned short encap_family = tmpl->encap_family;
Horia Geanta0f245582014-02-12 16:20:06 +0200779 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900780
David S. Miller37b08e32008-09-02 20:14:15 -0700781 to_put = NULL;
782
Fan Du283bc9f2013-11-07 17:47:50 +0800783 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700784 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800785 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700786 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000788 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700789 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700790 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 tmpl->mode == x->props.mode &&
792 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700793 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500794 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700795 &best, &acquire_in_progress, &error);
796 }
Fan Du6f115632013-09-23 17:18:25 +0800797 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700798 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Thomas Egerer8444cf72010-09-20 11:11:38 -0700800 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800801 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700802 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700803 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000804 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700805 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800806 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700807 tmpl->mode == x->props.mode &&
808 tmpl->id.proto == x->id.proto &&
809 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500810 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700811 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
David S. Miller08ec9af2009-03-13 14:22:40 -0700814found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 x = best;
816 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700817 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800818 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700819 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700820 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 error = -EEXIST;
822 goto out;
823 }
Horia Geanta0f245582014-02-12 16:20:06 +0200824
825 c.net = net;
826 /* If the KMs have no listeners (yet...), avoid allocating an SA
827 * for each and every packet - garbage collection might not
828 * handle the flood.
829 */
830 if (!km_is_alive(&c)) {
831 error = -ESRCH;
832 goto out;
833 }
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
Fan Duc4549972014-01-03 11:18:32 +0800931 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +0800932 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +0800933 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:
Jiri Benc15e318b2015-03-29 16:59:24 +02001047 x->sel.daddr.in6 = daddr->in6;
1048 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001049 x->sel.prefixlen_d = 128;
1050 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001051 x->props.saddr.in6 = saddr->in6;
1052 x->id.daddr.in6 = daddr->in6;
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
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001138static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001139{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001140 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001141 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001142 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001143 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001144
1145 memcpy(&x->id, &orig->id, sizeof(x->id));
1146 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1147 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1148 x->props.mode = orig->props.mode;
1149 x->props.replay_window = orig->props.replay_window;
1150 x->props.reqid = orig->props.reqid;
1151 x->props.family = orig->props.family;
1152 x->props.saddr = orig->props.saddr;
1153
1154 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001155 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001156 if (!x->aalg)
1157 goto error;
1158 }
1159 x->props.aalgo = orig->props.aalgo;
1160
Steffen Klassertee5c2312014-02-19 13:33:24 +01001161 if (orig->aead) {
1162 x->aead = xfrm_algo_aead_clone(orig->aead);
1163 if (!x->aead)
1164 goto error;
1165 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001166 if (orig->ealg) {
1167 x->ealg = xfrm_algo_clone(orig->ealg);
1168 if (!x->ealg)
1169 goto error;
1170 }
1171 x->props.ealgo = orig->props.ealgo;
1172
1173 if (orig->calg) {
1174 x->calg = xfrm_algo_clone(orig->calg);
1175 if (!x->calg)
1176 goto error;
1177 }
1178 x->props.calgo = orig->props.calgo;
1179
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001180 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001181 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1182 if (!x->encap)
1183 goto error;
1184 }
1185
1186 if (orig->coaddr) {
1187 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1188 GFP_KERNEL);
1189 if (!x->coaddr)
1190 goto error;
1191 }
1192
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001193 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001194 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001195 goto error;
1196 }
1197
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001198 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1199
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001200 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001201 goto error;
1202
1203 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001204 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001205
Steffen Klassertee5c2312014-02-19 13:33:24 +01001206 x->tfcpad = orig->tfcpad;
1207 x->replay_maxdiff = orig->replay_maxdiff;
1208 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001209 x->curlft.add_time = orig->curlft.add_time;
1210 x->km.state = orig->km.state;
1211 x->km.seq = orig->km.seq;
1212
1213 return x;
1214
1215 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001216 xfrm_state_put(x);
1217out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001218 return NULL;
1219}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001220
Fan Du283bc9f2013-11-07 17:47:50 +08001221struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001222{
1223 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001224 struct xfrm_state *x = NULL;
1225
1226 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001227
1228 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001229 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001230 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001231 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001232 if (x->props.mode != m->mode ||
1233 x->id.proto != m->proto)
1234 continue;
1235 if (m->reqid && x->props.reqid != m->reqid)
1236 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001237 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1238 m->old_family) ||
1239 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1240 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001241 continue;
1242 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001243 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001244 }
1245 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001246 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001247 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001248 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001249 if (x->props.mode != m->mode ||
1250 x->id.proto != m->proto)
1251 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001252 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1253 m->old_family) ||
1254 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1255 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001256 continue;
1257 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001258 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001259 }
1260 }
1261
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001262 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1263
1264 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001265}
1266EXPORT_SYMBOL(xfrm_migrate_state_find);
1267
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001268struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1269 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001270{
1271 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001272
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001273 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001274 if (!xc)
1275 return NULL;
1276
1277 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1278 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1279
1280 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001281 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001282 /* a care is needed when the destination address of the
1283 state is to be updated as it is a part of triplet */
1284 xfrm_state_insert(xc);
1285 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001286 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001287 goto error;
1288 }
1289
1290 return xc;
1291error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001292 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001293 return NULL;
1294}
1295EXPORT_SYMBOL(xfrm_state_migrate);
1296#endif
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298int xfrm_state_update(struct xfrm_state *x)
1299{
David S. Miller37b08e32008-09-02 20:14:15 -07001300 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001302 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001303 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
David S. Miller37b08e32008-09-02 20:14:15 -07001305 to_put = NULL;
1306
Fan Du283bc9f2013-11-07 17:47:50 +08001307 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001308 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 err = -ESRCH;
1311 if (!x1)
1312 goto out;
1313
1314 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001315 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 err = -EEXIST;
1317 goto out;
1318 }
1319
1320 if (x1->km.state == XFRM_STATE_ACQ) {
1321 __xfrm_state_insert(x);
1322 x = NULL;
1323 }
1324 err = 0;
1325
1326out:
Fan Du283bc9f2013-11-07 17:47:50 +08001327 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
David S. Miller37b08e32008-09-02 20:14:15 -07001329 if (to_put)
1330 xfrm_state_put(to_put);
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (err)
1333 return err;
1334
1335 if (!x) {
1336 xfrm_state_delete(x1);
1337 xfrm_state_put(x1);
1338 return 0;
1339 }
1340
1341 err = -EINVAL;
1342 spin_lock_bh(&x1->lock);
1343 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1344 if (x->encap && x1->encap)
1345 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001346 if (x->coaddr && x1->coaddr) {
1347 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1348 }
1349 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1350 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1352 x1->km.dying = 0;
1353
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001354 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (x1->curlft.use_time)
1356 xfrm_state_check_expire(x1);
1357
1358 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001359 x->km.state = XFRM_STATE_DEAD;
1360 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 spin_unlock_bh(&x1->lock);
1363
1364 xfrm_state_put(x1);
1365
1366 return err;
1367}
1368EXPORT_SYMBOL(xfrm_state_update);
1369
1370int xfrm_state_check_expire(struct xfrm_state *x)
1371{
1372 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001373 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1376 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001377 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001378 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -EINVAL;
1380 }
1381
1382 if (!x->km.dying &&
1383 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001384 x->curlft.packets >= x->lft.soft_packet_limit)) {
1385 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001386 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 0;
1389}
1390EXPORT_SYMBOL(xfrm_state_check_expire);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001393xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001394 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Fan Du283bc9f2013-11-07 17:47:50 +08001398 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001399 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001400 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return x;
1402}
1403EXPORT_SYMBOL(xfrm_state_lookup);
1404
1405struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001406xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001407 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001408 u8 proto, unsigned short family)
1409{
1410 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001411
Fan Du283bc9f2013-11-07 17:47:50 +08001412 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001413 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001414 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001415 return x;
1416}
1417EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1418
1419struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001420xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1421 u8 proto, const xfrm_address_t *daddr,
1422 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Fan Du283bc9f2013-11-07 17:47:50 +08001426 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001427 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001428 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return x;
1431}
1432EXPORT_SYMBOL(xfrm_find_acq);
1433
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001434#ifdef CONFIG_XFRM_SUB_POLICY
1435int
1436xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001437 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001438{
1439 int err = 0;
1440 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1441 if (!afinfo)
1442 return -EAFNOSUPPORT;
1443
Fan Du283bc9f2013-11-07 17:47:50 +08001444 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001445 if (afinfo->tmpl_sort)
1446 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001447 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001448 xfrm_state_put_afinfo(afinfo);
1449 return err;
1450}
1451EXPORT_SYMBOL(xfrm_tmpl_sort);
1452
1453int
1454xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1455 unsigned short family)
1456{
1457 int err = 0;
1458 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001459 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001460
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001461 if (!afinfo)
1462 return -EAFNOSUPPORT;
1463
Fan Du283bc9f2013-11-07 17:47:50 +08001464 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001465 if (afinfo->state_sort)
1466 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001467 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001468 xfrm_state_put_afinfo(afinfo);
1469 return err;
1470}
1471EXPORT_SYMBOL(xfrm_state_sort);
1472#endif
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/* Silly enough, but I'm lazy to build resolution list */
1475
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001476static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001480 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001481 struct xfrm_state *x;
1482
Sasha Levinb67bfe02013-02-27 17:06:00 -08001483 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001484 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001485 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001486 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 xfrm_state_hold(x);
1488 return x;
1489 }
1490 }
1491 }
1492 return NULL;
1493}
1494
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001495struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct xfrm_state *x;
1498
Fan Du283bc9f2013-11-07 17:47:50 +08001499 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001500 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001501 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return x;
1503}
1504EXPORT_SYMBOL(xfrm_find_acq_byseq);
1505
1506u32 xfrm_get_acqseq(void)
1507{
1508 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001509 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
jamal6836b9b2010-02-16 02:01:22 +00001511 do {
1512 res = atomic_inc_return(&acqseq);
1513 } while (!res);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return res;
1516}
1517EXPORT_SYMBOL(xfrm_get_acqseq);
1518
Fan Du776e9dd2013-12-16 18:47:49 +08001519int verify_spi_info(u8 proto, u32 min, u32 max)
1520{
1521 switch (proto) {
1522 case IPPROTO_AH:
1523 case IPPROTO_ESP:
1524 break;
1525
1526 case IPPROTO_COMP:
1527 /* IPCOMP spi is 16-bits. */
1528 if (max >= 0x10000)
1529 return -EINVAL;
1530 break;
1531
1532 default:
1533 return -EINVAL;
1534 }
1535
1536 if (min > max)
1537 return -EINVAL;
1538
1539 return 0;
1540}
1541EXPORT_SYMBOL(verify_spi_info);
1542
Herbert Xu658b2192007-10-09 13:29:52 -07001543int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001545 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001546 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001548 int err = -ENOENT;
1549 __be32 minspi = htonl(low);
1550 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001551 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Herbert Xu658b2192007-10-09 13:29:52 -07001553 spin_lock_bh(&x->lock);
1554 if (x->km.state == XFRM_STATE_DEAD)
1555 goto unlock;
1556
1557 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001559 goto unlock;
1560
1561 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001564 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (x0) {
1566 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001567 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569 x->id.spi = minspi;
1570 } else {
1571 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001572 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001573 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001574 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (x0 == NULL) {
1576 x->id.spi = htonl(spi);
1577 break;
1578 }
1579 xfrm_state_put(x0);
1580 }
1581 }
1582 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001583 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001584 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1585 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Fan Du283bc9f2013-11-07 17:47:50 +08001586 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001587
1588 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Herbert Xu658b2192007-10-09 13:29:52 -07001590
1591unlock:
1592 spin_unlock_bh(&x->lock);
1593
1594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596EXPORT_SYMBOL(xfrm_alloc_spi);
1597
Nicolas Dichteld3623092014-02-14 15:30:36 +01001598static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001599 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001600{
1601 if (filter) {
1602 if ((filter->family == AF_INET ||
1603 filter->family == AF_INET6) &&
1604 x->props.family != filter->family)
1605 return false;
1606
1607 return addr_match(&x->props.saddr, &filter->saddr,
1608 filter->splen) &&
1609 addr_match(&x->id.daddr, &filter->daddr,
1610 filter->dplen);
1611 }
1612 return true;
1613}
1614
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001615int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001616 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 void *data)
1618{
Herbert Xu12a169e2008-10-01 07:03:24 -07001619 struct xfrm_state *state;
1620 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int err = 0;
1622
Herbert Xu12a169e2008-10-01 07:03:24 -07001623 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001624 return 0;
1625
Fan Du283bc9f2013-11-07 17:47:50 +08001626 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001627 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001628 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001629 else
Li RongQing80077702015-04-22 17:09:54 +08001630 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001631 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001632 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001633 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001634 state = container_of(x, struct xfrm_state, km);
1635 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001636 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001637 if (!__xfrm_state_filter_match(state, walk->filter))
1638 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001639 err = func(state, walk->seq, data);
1640 if (err) {
1641 list_move_tail(&walk->all, &x->all);
1642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001644 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001646 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 err = -ENOENT;
1648 goto out;
1649 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001650 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651out:
Fan Du283bc9f2013-11-07 17:47:50 +08001652 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return err;
1654}
1655EXPORT_SYMBOL(xfrm_state_walk);
1656
Nicolas Dichteld3623092014-02-14 15:30:36 +01001657void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001658 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001659{
Herbert Xu12a169e2008-10-01 07:03:24 -07001660 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001661 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001662 walk->state = XFRM_STATE_DEAD;
1663 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001664 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001665}
1666EXPORT_SYMBOL(xfrm_state_walk_init);
1667
Fan Du283bc9f2013-11-07 17:47:50 +08001668void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001669{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001670 kfree(walk->filter);
1671
Herbert Xu12a169e2008-10-01 07:03:24 -07001672 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001673 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001674
Fan Du283bc9f2013-11-07 17:47:50 +08001675 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001676 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001677 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001678}
1679EXPORT_SYMBOL(xfrm_state_walk_done);
1680
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001681static void xfrm_replay_timer_handler(unsigned long data)
1682{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001683 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001684
1685 spin_lock(&x->lock);
1686
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001687 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001688 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001689 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001690 else
1691 x->xflags |= XFRM_TIME_DEFER;
1692 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001693
1694 spin_unlock(&x->lock);
1695}
1696
Denis Chengdf018122007-12-07 00:51:11 -08001697static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
David S. Miller214e0052011-02-24 00:02:38 -05001699void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 struct xfrm_mgr *km;
1702
Cong Wang85168c02013-01-16 16:05:06 +08001703 rcu_read_lock();
1704 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001705 if (km->notify_policy)
1706 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001707 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001708}
1709
David S. Miller214e0052011-02-24 00:02:38 -05001710void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001711{
1712 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001713 rcu_read_lock();
1714 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001715 if (km->notify)
1716 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001717 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718}
1719
1720EXPORT_SYMBOL(km_policy_notify);
1721EXPORT_SYMBOL(km_state_notify);
1722
Eric W. Biederman15e47302012-09-07 20:12:54 +00001723void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724{
1725 struct km_event c;
1726
Herbert Xubf088672005-06-18 22:44:00 -07001727 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001728 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001729 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001730 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001733EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001734/*
1735 * We send to all registered managers regardless of failure
1736 * We are happy with one success
1737*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001738int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001740 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct xfrm_mgr *km;
1742
Cong Wang85168c02013-01-16 16:05:06 +08001743 rcu_read_lock();
1744 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001745 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001746 if (!acqret)
1747 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Cong Wang85168c02013-01-16 16:05:06 +08001749 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return err;
1751}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001752EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Al Viro5d36b182006-11-08 00:24:06 -08001754int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 int err = -EINVAL;
1757 struct xfrm_mgr *km;
1758
Cong Wang85168c02013-01-16 16:05:06 +08001759 rcu_read_lock();
1760 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (km->new_mapping)
1762 err = km->new_mapping(x, ipaddr, sport);
1763 if (!err)
1764 break;
1765 }
Cong Wang85168c02013-01-16 16:05:06 +08001766 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return err;
1768}
1769EXPORT_SYMBOL(km_new_mapping);
1770
Eric W. Biederman15e47302012-09-07 20:12:54 +00001771void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001773 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Herbert Xubf088672005-06-18 22:44:00 -07001775 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001776 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001777 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001778 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
David S. Millera70fcb02006-03-20 19:18:52 -08001780EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001782#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001783int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1784 const struct xfrm_migrate *m, int num_migrate,
1785 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001786{
1787 int err = -EINVAL;
1788 int ret;
1789 struct xfrm_mgr *km;
1790
Cong Wang85168c02013-01-16 16:05:06 +08001791 rcu_read_lock();
1792 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001793 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001794 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001795 if (!ret)
1796 err = ret;
1797 }
1798 }
Cong Wang85168c02013-01-16 16:05:06 +08001799 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001800 return err;
1801}
1802EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001803#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001804
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001805int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001806{
1807 int err = -EINVAL;
1808 int ret;
1809 struct xfrm_mgr *km;
1810
Cong Wang85168c02013-01-16 16:05:06 +08001811 rcu_read_lock();
1812 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001813 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001814 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001815 if (!ret)
1816 err = ret;
1817 }
1818 }
Cong Wang85168c02013-01-16 16:05:06 +08001819 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001820 return err;
1821}
1822EXPORT_SYMBOL(km_report);
1823
Horia Geanta0f245582014-02-12 16:20:06 +02001824bool km_is_alive(const struct km_event *c)
1825{
1826 struct xfrm_mgr *km;
1827 bool is_alive = false;
1828
1829 rcu_read_lock();
1830 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1831 if (km->is_alive && km->is_alive(c)) {
1832 is_alive = true;
1833 break;
1834 }
1835 }
1836 rcu_read_unlock();
1837
1838 return is_alive;
1839}
1840EXPORT_SYMBOL(km_is_alive);
1841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1843{
1844 int err;
1845 u8 *data;
1846 struct xfrm_mgr *km;
1847 struct xfrm_policy *pol = NULL;
1848
1849 if (optlen <= 0 || optlen > PAGE_SIZE)
1850 return -EMSGSIZE;
1851
1852 data = kmalloc(optlen, GFP_KERNEL);
1853 if (!data)
1854 return -ENOMEM;
1855
1856 err = -EFAULT;
1857 if (copy_from_user(data, optval, optlen))
1858 goto out;
1859
1860 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001861 rcu_read_lock();
1862 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001863 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 optlen, &err);
1865 if (err >= 0)
1866 break;
1867 }
Cong Wang85168c02013-01-16 16:05:06 +08001868 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 if (err >= 0) {
1871 xfrm_sk_policy_insert(sk, err, pol);
1872 xfrm_pol_put(pol);
1873 err = 0;
1874 }
1875
1876out:
1877 kfree(data);
1878 return err;
1879}
1880EXPORT_SYMBOL(xfrm_user_policy);
1881
Cong Wang85168c02013-01-16 16:05:06 +08001882static DEFINE_SPINLOCK(xfrm_km_lock);
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884int xfrm_register_km(struct xfrm_mgr *km)
1885{
Cong Wang85168c02013-01-16 16:05:06 +08001886 spin_lock_bh(&xfrm_km_lock);
1887 list_add_tail_rcu(&km->list, &xfrm_km_list);
1888 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 0;
1890}
1891EXPORT_SYMBOL(xfrm_register_km);
1892
1893int xfrm_unregister_km(struct xfrm_mgr *km)
1894{
Cong Wang85168c02013-01-16 16:05:06 +08001895 spin_lock_bh(&xfrm_km_lock);
1896 list_del_rcu(&km->list);
1897 spin_unlock_bh(&xfrm_km_lock);
1898 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900}
1901EXPORT_SYMBOL(xfrm_unregister_km);
1902
1903int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1904{
1905 int err = 0;
1906 if (unlikely(afinfo == NULL))
1907 return -EINVAL;
1908 if (unlikely(afinfo->family >= NPROTO))
1909 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001910 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08001912 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07001913 else
Cong Wang44abdc32013-01-16 16:05:05 +08001914 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1915 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return err;
1917}
1918EXPORT_SYMBOL(xfrm_state_register_afinfo);
1919
1920int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1921{
1922 int err = 0;
1923 if (unlikely(afinfo == NULL))
1924 return -EINVAL;
1925 if (unlikely(afinfo->family >= NPROTO))
1926 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001927 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1929 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1930 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001931 else
Cong Wang44abdc32013-01-16 16:05:05 +08001932 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
Cong Wang44abdc32013-01-16 16:05:05 +08001934 spin_unlock_bh(&xfrm_state_afinfo_lock);
1935 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return err;
1937}
1938EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1939
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001940struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 struct xfrm_state_afinfo *afinfo;
1943 if (unlikely(family >= NPROTO))
1944 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001945 rcu_read_lock();
1946 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001947 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001948 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return afinfo;
1950}
1951
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001952void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Cong Wang44abdc32013-01-16 16:05:05 +08001954 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1958void xfrm_state_delete_tunnel(struct xfrm_state *x)
1959{
1960 if (x->tunnel) {
1961 struct xfrm_state *t = x->tunnel;
1962
1963 if (atomic_read(&t->tunnel_users) == 2)
1964 xfrm_state_delete(t);
1965 atomic_dec(&t->tunnel_users);
1966 xfrm_state_put(t);
1967 x->tunnel = NULL;
1968 }
1969}
1970EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1971
1972int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1973{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001974 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Patrick McHardyc5c25232007-04-09 11:47:18 -07001976 spin_lock_bh(&x->lock);
1977 if (x->km.state == XFRM_STATE_VALID &&
1978 x->type && x->type->get_mtu)
1979 res = x->type->get_mtu(x, mtu);
1980 else
Patrick McHardy28121612007-06-18 22:30:15 -07001981 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001982 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return res;
1984}
1985
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001986int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001987{
Herbert Xud094cd82005-06-20 13:19:41 -07001988 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001989 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001990 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001991 int err;
1992
Herbert Xud094cd82005-06-20 13:19:41 -07001993 err = -EAFNOSUPPORT;
1994 afinfo = xfrm_state_get_afinfo(family);
1995 if (!afinfo)
1996 goto error;
1997
1998 err = 0;
1999 if (afinfo->init_flags)
2000 err = afinfo->init_flags(x);
2001
2002 xfrm_state_put_afinfo(afinfo);
2003
2004 if (err)
2005 goto error;
2006
2007 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002008
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002009 if (x->sel.family != AF_UNSPEC) {
2010 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2011 if (inner_mode == NULL)
2012 goto error;
2013
2014 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2015 family != x->sel.family) {
2016 xfrm_put_mode(inner_mode);
2017 goto error;
2018 }
2019
2020 x->inner_mode = inner_mode;
2021 } else {
2022 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002023 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002024
Martin Willid81d2282008-12-03 15:38:07 -08002025 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002026 if (inner_mode == NULL)
2027 goto error;
2028
2029 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2030 xfrm_put_mode(inner_mode);
2031 goto error;
2032 }
Martin Willid81d2282008-12-03 15:38:07 -08002033 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002034
Martin Willid81d2282008-12-03 15:38:07 -08002035 if (x->props.family == AF_INET)
2036 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002037
Martin Willid81d2282008-12-03 15:38:07 -08002038 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2039 if (inner_mode_iaf) {
2040 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2041 x->inner_mode_iaf = inner_mode_iaf;
2042 else
2043 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002044 }
2045 }
Herbert Xu13996372007-10-17 21:35:51 -07002046
Herbert Xud094cd82005-06-20 13:19:41 -07002047 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002048 if (x->type == NULL)
2049 goto error;
2050
2051 err = x->type->init_state(x);
2052 if (err)
2053 goto error;
2054
Herbert Xu13996372007-10-17 21:35:51 -07002055 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002056 if (x->outer_mode == NULL) {
2057 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002058 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002059 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002060
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002061 if (init_replay) {
2062 err = xfrm_init_replay(x);
2063 if (err)
2064 goto error;
2065 }
2066
Herbert Xu72cb6962005-06-20 13:18:08 -07002067 x->km.state = XFRM_STATE_VALID;
2068
2069error:
2070 return err;
2071}
2072
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002073EXPORT_SYMBOL(__xfrm_init_state);
2074
2075int xfrm_init_state(struct xfrm_state *x)
2076{
2077 return __xfrm_init_state(x, true);
2078}
2079
Herbert Xu72cb6962005-06-20 13:18:08 -07002080EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002081
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002082int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
David S. Millerf034b5d2006-08-24 03:08:07 -07002084 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002086 INIT_LIST_HEAD(&net->xfrm.state_all);
2087
David S. Millerf034b5d2006-08-24 03:08:07 -07002088 sz = sizeof(struct hlist_head) * 8;
2089
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002090 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2091 if (!net->xfrm.state_bydst)
2092 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002093 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2094 if (!net->xfrm.state_bysrc)
2095 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002096 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2097 if (!net->xfrm.state_byspi)
2098 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002099 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002100
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002101 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002102 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002103 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002104 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Fan Du283bc9f2013-11-07 17:47:50 +08002105 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002106 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002107
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002108out_byspi:
2109 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002110out_bysrc:
2111 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002112out_bydst:
2113 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002114}
2115
2116void xfrm_state_fini(struct net *net)
2117{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002118 unsigned int sz;
2119
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002120 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002121 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002122 flush_work(&net->xfrm.state_gc_work);
2123
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002124 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002125
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002126 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002127 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2128 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002129 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2130 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002131 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2132 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Joy Lattenab5f5e82007-09-17 11:51:22 -07002135#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002136static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2137 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002138{
Paul Moore68277ac2007-12-20 20:49:33 -08002139 struct xfrm_sec_ctx *ctx = x->security;
2140 u32 spi = ntohl(x->id.spi);
2141
2142 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002143 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002144 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002145
Weilong Chen9b7a7872013-12-24 09:43:46 +08002146 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002147 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002148 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2149 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002150 break;
2151 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002152 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002153 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002154 break;
2155 }
Paul Moore68277ac2007-12-20 20:49:33 -08002156
2157 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002158}
2159
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002160static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2161 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002162{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002163 const struct iphdr *iph4;
2164 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002165
2166 switch (family) {
2167 case AF_INET:
2168 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002169 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2170 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002171 break;
2172 case AF_INET6:
2173 iph6 = ipv6_hdr(skb);
2174 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002175 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002176 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002177 iph6->flow_lbl[0] & 0x0f,
2178 iph6->flow_lbl[1],
2179 iph6->flow_lbl[2]);
2180 break;
2181 }
2182}
2183
Tetsuo Handa2e710292014-04-22 21:48:30 +09002184void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002185{
2186 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002187
Paul Mooreafeb14b2007-12-21 14:58:11 -08002188 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002189 if (audit_buf == NULL)
2190 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002191 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002192 xfrm_audit_helper_sainfo(x, audit_buf);
2193 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002194 audit_log_end(audit_buf);
2195}
2196EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2197
Tetsuo Handa2e710292014-04-22 21:48:30 +09002198void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002199{
2200 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002201
Paul Mooreafeb14b2007-12-21 14:58:11 -08002202 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002203 if (audit_buf == NULL)
2204 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002205 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002206 xfrm_audit_helper_sainfo(x, audit_buf);
2207 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002208 audit_log_end(audit_buf);
2209}
2210EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002211
2212void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2213 struct sk_buff *skb)
2214{
2215 struct audit_buffer *audit_buf;
2216 u32 spi;
2217
2218 audit_buf = xfrm_audit_start("SA-replay-overflow");
2219 if (audit_buf == NULL)
2220 return;
2221 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2222 /* don't record the sequence number because it's inherent in this kind
2223 * of audit message */
2224 spi = ntohl(x->id.spi);
2225 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2226 audit_log_end(audit_buf);
2227}
2228EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2229
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002230void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002231 struct sk_buff *skb, __be32 net_seq)
2232{
2233 struct audit_buffer *audit_buf;
2234 u32 spi;
2235
2236 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2237 if (audit_buf == NULL)
2238 return;
2239 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2240 spi = ntohl(x->id.spi);
2241 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2242 spi, spi, ntohl(net_seq));
2243 audit_log_end(audit_buf);
2244}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002245EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002246
2247void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2248{
2249 struct audit_buffer *audit_buf;
2250
2251 audit_buf = xfrm_audit_start("SA-notfound");
2252 if (audit_buf == NULL)
2253 return;
2254 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2255 audit_log_end(audit_buf);
2256}
2257EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2258
2259void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2260 __be32 net_spi, __be32 net_seq)
2261{
2262 struct audit_buffer *audit_buf;
2263 u32 spi;
2264
2265 audit_buf = xfrm_audit_start("SA-notfound");
2266 if (audit_buf == NULL)
2267 return;
2268 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2269 spi = ntohl(net_spi);
2270 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2271 spi, spi, ntohl(net_seq));
2272 audit_log_end(audit_buf);
2273}
2274EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2275
2276void xfrm_audit_state_icvfail(struct xfrm_state *x,
2277 struct sk_buff *skb, u8 proto)
2278{
2279 struct audit_buffer *audit_buf;
2280 __be32 net_spi;
2281 __be32 net_seq;
2282
2283 audit_buf = xfrm_audit_start("SA-icv-failure");
2284 if (audit_buf == NULL)
2285 return;
2286 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2287 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2288 u32 spi = ntohl(net_spi);
2289 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2290 spi, spi, ntohl(net_seq));
2291 }
2292 audit_log_end(audit_buf);
2293}
2294EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002295#endif /* CONFIG_AUDITSYSCALL */