blob: 0ab54134bb40b84cf825df6035a9acf7cb3cf760 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm_state.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16#include <linux/workqueue.h>
17#include <net/xfrm.h>
18#include <linux/pfkeyv2.h>
19#include <linux/ipsec.h>
20#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070021#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080022#include <linux/audit.h>
Jesper Juhlb5890d82007-08-10 15:20:21 -070023#include <asm/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080024#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080026#include <linux/interrupt.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David S. Miller44e36b42006-08-24 04:50:50 -070029#include "xfrm_hash.h"
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* Each xfrm_state may be linked to two tables:
32
33 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070034 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 destination/tunnel endpoint. (output)
36 */
37
David S. Millerf034b5d2006-08-24 03:08:07 -070038static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080040static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050041 const xfrm_address_t *daddr,
42 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070043 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070044 unsigned short family)
45{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080046 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070047}
48
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080049static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050050 const xfrm_address_t *daddr,
51 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070052 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070053{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070055}
56
David S. Miller2575b652006-08-24 03:26:44 -070057static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050058xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
59 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070060{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080061 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070062}
63
David S. Millerf034b5d2006-08-24 03:08:07 -070064static void xfrm_hash_transfer(struct hlist_head *list,
65 struct hlist_head *ndsttable,
66 struct hlist_head *nsrctable,
67 struct hlist_head *nspitable,
68 unsigned int nhashmask)
69{
Sasha Levinb67bfe02013-02-27 17:06:00 -080070 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070071 struct xfrm_state *x;
72
Sasha Levinb67bfe02013-02-27 17:06:00 -080073 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070074 unsigned int h;
75
David S. Millerc1969f22006-08-24 04:00:03 -070076 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
77 x->props.reqid, x->props.family,
78 nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070079 hlist_add_head(&x->bydst, ndsttable+h);
80
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070081 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
82 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070083 nhashmask);
84 hlist_add_head(&x->bysrc, nsrctable+h);
85
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070086 if (x->id.spi) {
87 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
88 x->id.proto, x->props.family,
89 nhashmask);
90 hlist_add_head(&x->byspi, nspitable+h);
91 }
David S. Millerf034b5d2006-08-24 03:08:07 -070092 }
93}
94
Alexey Dobriyan63082732008-11-25 17:19:07 -080095static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -070096{
Alexey Dobriyan63082732008-11-25 17:19:07 -080097 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -070098}
99
100static DEFINE_MUTEX(hash_resize_mutex);
101
Alexey Dobriyan63082732008-11-25 17:19:07 -0800102static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700103{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800104 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700105 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
106 unsigned long nsize, osize;
107 unsigned int nhashmask, ohashmask;
108 int i;
109
110 mutex_lock(&hash_resize_mutex);
111
Alexey Dobriyan63082732008-11-25 17:19:07 -0800112 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700113 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700114 if (!ndst)
115 goto out_unlock;
David S. Miller44e36b42006-08-24 04:50:50 -0700116 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700117 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700118 xfrm_hash_free(ndst, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 goto out_unlock;
120 }
David S. Miller44e36b42006-08-24 04:50:50 -0700121 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700122 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700123 xfrm_hash_free(ndst, nsize);
124 xfrm_hash_free(nsrc, nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700125 goto out_unlock;
126 }
127
Fan Du283bc9f2013-11-07 17:47:50 +0800128 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700129
130 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800131 for (i = net->xfrm.state_hmask; i >= 0; i--)
132 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700133 nhashmask);
134
Alexey Dobriyan63082732008-11-25 17:19:07 -0800135 odst = net->xfrm.state_bydst;
136 osrc = net->xfrm.state_bysrc;
137 ospi = net->xfrm.state_byspi;
138 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700139
Alexey Dobriyan63082732008-11-25 17:19:07 -0800140 net->xfrm.state_bydst = ndst;
141 net->xfrm.state_bysrc = nsrc;
142 net->xfrm.state_byspi = nspi;
143 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700144
Fan Du283bc9f2013-11-07 17:47:50 +0800145 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700146
147 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700148 xfrm_hash_free(odst, osize);
149 xfrm_hash_free(osrc, osize);
150 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700151
152out_unlock:
153 mutex_unlock(&hash_resize_mutex);
154}
155
Cong Wang44abdc32013-01-16 16:05:05 +0800156static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
157static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static DEFINE_SPINLOCK(xfrm_state_gc_lock);
160
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800161int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800163int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200164bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000165void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Cong Wang7a9885b2013-01-17 16:34:11 +0800167static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800168int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700169{
Cong Wang7a9885b2013-01-17 16:34:11 +0800170 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800171 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700172 int err = 0;
173
174 if (unlikely(afinfo == NULL))
175 return -EAFNOSUPPORT;
176 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800177 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700178
179 if (likely(typemap[type->proto] == NULL))
180 typemap[type->proto] = type;
181 else
182 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800183 spin_unlock_bh(&xfrm_type_lock);
184 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700185 return err;
186}
187EXPORT_SYMBOL(xfrm_register_type);
188
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800189int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700190{
Cong Wang7a9885b2013-01-17 16:34:11 +0800191 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800192 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700193 int err = 0;
194
195 if (unlikely(afinfo == NULL))
196 return -EAFNOSUPPORT;
197 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800198 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700199
200 if (unlikely(typemap[type->proto] != type))
201 err = -ENOENT;
202 else
203 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800204 spin_unlock_bh(&xfrm_type_lock);
205 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700206 return err;
207}
208EXPORT_SYMBOL(xfrm_unregister_type);
209
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800210static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700211{
212 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800213 const struct xfrm_type **typemap;
214 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700215 int modload_attempted = 0;
216
217retry:
218 afinfo = xfrm_state_get_afinfo(family);
219 if (unlikely(afinfo == NULL))
220 return NULL;
221 typemap = afinfo->type_map;
222
223 type = typemap[proto];
224 if (unlikely(type && !try_module_get(type->owner)))
225 type = NULL;
226 if (!type && !modload_attempted) {
227 xfrm_state_put_afinfo(afinfo);
228 request_module("xfrm-type-%d-%d", family, proto);
229 modload_attempted = 1;
230 goto retry;
231 }
232
233 xfrm_state_put_afinfo(afinfo);
234 return type;
235}
236
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800237static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700238{
239 module_put(type->owner);
240}
241
Cong Wang7a9885b2013-01-17 16:34:11 +0800242static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700243int xfrm_register_mode(struct xfrm_mode *mode, int family)
244{
245 struct xfrm_state_afinfo *afinfo;
246 struct xfrm_mode **modemap;
247 int err;
248
249 if (unlikely(mode->encap >= XFRM_MODE_MAX))
250 return -EINVAL;
251
Cong Wang7a9885b2013-01-17 16:34:11 +0800252 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700253 if (unlikely(afinfo == NULL))
254 return -EAFNOSUPPORT;
255
256 err = -EEXIST;
257 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800258 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700259 if (modemap[mode->encap])
260 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700261
Herbert Xu17c2a422007-10-17 21:33:12 -0700262 err = -ENOENT;
263 if (!try_module_get(afinfo->owner))
264 goto out;
265
266 mode->afinfo = afinfo;
267 modemap[mode->encap] = mode;
268 err = 0;
269
270out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800271 spin_unlock_bh(&xfrm_mode_lock);
272 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700273 return err;
274}
275EXPORT_SYMBOL(xfrm_register_mode);
276
277int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
278{
279 struct xfrm_state_afinfo *afinfo;
280 struct xfrm_mode **modemap;
281 int err;
282
283 if (unlikely(mode->encap >= XFRM_MODE_MAX))
284 return -EINVAL;
285
Cong Wang7a9885b2013-01-17 16:34:11 +0800286 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700287 if (unlikely(afinfo == NULL))
288 return -EAFNOSUPPORT;
289
290 err = -ENOENT;
291 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800292 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700293 if (likely(modemap[mode->encap] == mode)) {
294 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700295 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700296 err = 0;
297 }
298
Cong Wang7a9885b2013-01-17 16:34:11 +0800299 spin_unlock_bh(&xfrm_mode_lock);
300 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700301 return err;
302}
303EXPORT_SYMBOL(xfrm_unregister_mode);
304
305static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
306{
307 struct xfrm_state_afinfo *afinfo;
308 struct xfrm_mode *mode;
309 int modload_attempted = 0;
310
311 if (unlikely(encap >= XFRM_MODE_MAX))
312 return NULL;
313
314retry:
315 afinfo = xfrm_state_get_afinfo(family);
316 if (unlikely(afinfo == NULL))
317 return NULL;
318
319 mode = afinfo->mode_map[encap];
320 if (unlikely(mode && !try_module_get(mode->owner)))
321 mode = NULL;
322 if (!mode && !modload_attempted) {
323 xfrm_state_put_afinfo(afinfo);
324 request_module("xfrm-mode-%d-%d", family, encap);
325 modload_attempted = 1;
326 goto retry;
327 }
328
329 xfrm_state_put_afinfo(afinfo);
330 return mode;
331}
332
333static void xfrm_put_mode(struct xfrm_mode *mode)
334{
335 module_put(mode->owner);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static void xfrm_state_gc_destroy(struct xfrm_state *x)
339{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800340 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700341 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800342 kfree(x->aalg);
343 kfree(x->ealg);
344 kfree(x->calg);
345 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700346 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000347 kfree(x->replay_esn);
348 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700349 if (x->inner_mode)
350 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700351 if (x->inner_mode_iaf)
352 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700353 if (x->outer_mode)
354 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (x->type) {
356 x->type->destructor(x);
357 xfrm_put_type(x->type);
358 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800359 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 kfree(x);
361}
362
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800363static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800365 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700366 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800367 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700368 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800371 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock_bh(&xfrm_state_gc_lock);
373
Sasha Levinb67bfe02013-02-27 17:06:00 -0800374 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static inline unsigned long make_jiffies(long secs)
379{
380 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
381 return MAX_SCHEDULE_TIMEOUT-1;
382 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900383 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800386static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800388 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
389 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800390 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 long next = LONG_MAX;
392 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600393 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 spin_lock(&x->lock);
396 if (x->km.state == XFRM_STATE_DEAD)
397 goto out;
398 if (x->km.state == XFRM_STATE_EXPIRED)
399 goto expired;
400 if (x->lft.hard_add_expires_seconds) {
401 long tmo = x->lft.hard_add_expires_seconds +
402 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000403 if (tmo <= 0) {
404 if (x->xflags & XFRM_SOFT_EXPIRE) {
405 /* enter hard expire without soft expire first?!
406 * setting a new date could trigger this.
407 * workarbound: fix x->curflt.add_time by below:
408 */
409 x->curlft.add_time = now - x->saved_tmo - 1;
410 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
411 } else
412 goto expired;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (tmo < next)
415 next = tmo;
416 }
417 if (x->lft.hard_use_expires_seconds) {
418 long tmo = x->lft.hard_use_expires_seconds +
419 (x->curlft.use_time ? : now) - now;
420 if (tmo <= 0)
421 goto expired;
422 if (tmo < next)
423 next = tmo;
424 }
425 if (x->km.dying)
426 goto resched;
427 if (x->lft.soft_add_expires_seconds) {
428 long tmo = x->lft.soft_add_expires_seconds +
429 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000430 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000432 x->xflags &= ~XFRM_SOFT_EXPIRE;
433 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000435 x->xflags |= XFRM_SOFT_EXPIRE;
436 x->saved_tmo = tmo;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 if (x->lft.soft_use_expires_seconds) {
440 long tmo = x->lft.soft_use_expires_seconds +
441 (x->curlft.use_time ? : now) - now;
442 if (tmo <= 0)
443 warn = 1;
444 else if (tmo < next)
445 next = tmo;
446 }
447
Herbert Xu4666faa2005-06-18 22:43:22 -0700448 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800450 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800452 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800453 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
454 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto out;
457
458expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200459 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600461
462 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200463 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800464 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Tetsuo Handa2e710292014-04-22 21:48:30 +0900466 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468out:
469 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800470 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
David S. Miller0ac84752006-03-20 19:18:23 -0800473static void xfrm_replay_timer_handler(unsigned long data);
474
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800475struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct xfrm_state *x;
478
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700479 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800482 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 atomic_set(&x->refcnt, 1);
484 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700485 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700486 INIT_HLIST_NODE(&x->bydst);
487 INIT_HLIST_NODE(&x->bysrc);
488 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800489 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
490 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800491 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
492 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800493 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 x->lft.soft_byte_limit = XFRM_INF;
495 x->lft.soft_packet_limit = XFRM_INF;
496 x->lft.hard_byte_limit = XFRM_INF;
497 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800498 x->replay_maxage = 0;
499 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700500 x->inner_mode = NULL;
501 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 spin_lock_init(&x->lock);
503 }
504 return x;
505}
506EXPORT_SYMBOL(xfrm_state_alloc);
507
508void __xfrm_state_destroy(struct xfrm_state *x)
509{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800510 struct net *net = xs_net(x);
511
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700512 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800515 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800517 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519EXPORT_SYMBOL(__xfrm_state_destroy);
520
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800521int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800523 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700524 int err = -ESRCH;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (x->km.state != XFRM_STATE_DEAD) {
527 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800528 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700529 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700530 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700531 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700532 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700533 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800534 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800535 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* All xfrm_state objects are created by xfrm_state_alloc.
538 * The xfrm_state_alloc call gives a reference, and that
539 * is what we are dropping here.
540 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800541 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700542 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700544
545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800547EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700549int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700551 int err;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700556
557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559EXPORT_SYMBOL(xfrm_state_delete);
560
Joy Latten4aa2e622007-06-04 19:05:57 -0400561#ifdef CONFIG_SECURITY_NETWORK_XFRM
562static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900563xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Joy Latten4aa2e622007-06-04 19:05:57 -0400565 int i, err = 0;
566
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800567 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400568 struct xfrm_state *x;
569
Sasha Levinb67bfe02013-02-27 17:06:00 -0800570 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400571 if (xfrm_id_proto_match(x->id.proto, proto) &&
572 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900573 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400574 return err;
575 }
576 }
577 }
578
579 return err;
580}
581#else
582static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900583xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400584{
585 return 0;
586}
587#endif
588
Tetsuo Handa2e710292014-04-22 21:48:30 +0900589int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400590{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000591 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Fan Du283bc9f2013-11-07 17:47:50 +0800593 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900594 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400595 if (err)
596 goto out;
597
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000598 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800599 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700600 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800602 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700604 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800606 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Joy Latten161a09e2006-11-27 13:11:54 -0600608 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700609 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900610 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000612 if (!err)
613 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Fan Du283bc9f2013-11-07 17:47:50 +0800615 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto restart;
617 }
618 }
619 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000620 if (cnt)
621 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400622
623out:
Fan Du283bc9f2013-11-07 17:47:50 +0800624 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400625 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627EXPORT_SYMBOL(xfrm_state_flush);
628
Alexey Dobriyane0710412010-01-23 13:37:10 +0000629void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700630{
Fan Du283bc9f2013-11-07 17:47:50 +0800631 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000632 si->sadcnt = net->xfrm.state_num;
633 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700634 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800635 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700636}
637EXPORT_SYMBOL(xfrm_sad_getinfo);
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static int
David S. Miller1a898592011-02-22 18:22:34 -0800640xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500641 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500642 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700643 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
646 if (!afinfo)
647 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700648 afinfo->init_tempsel(&x->sel, fl);
649
650 if (family != tmpl->encap_family) {
651 xfrm_state_put_afinfo(afinfo);
652 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
653 if (!afinfo)
654 return -1;
655 }
656 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 xfrm_state_put_afinfo(afinfo);
658 return 0;
659}
660
David S. Miller9aa60082011-02-24 01:51:36 -0500661static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
662 const xfrm_address_t *daddr,
663 __be32 spi, u8 proto,
664 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700665{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800666 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700667 struct xfrm_state *x;
668
Sasha Levinb67bfe02013-02-27 17:06:00 -0800669 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700670 if (x->props.family != family ||
671 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000672 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000673 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700674 continue;
675
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000676 if ((mark & x->mark.m) != x->mark.v)
677 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700678 xfrm_state_hold(x);
679 return x;
680 }
681
682 return NULL;
683}
684
David S. Miller9aa60082011-02-24 01:51:36 -0500685static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
686 const xfrm_address_t *daddr,
687 const xfrm_address_t *saddr,
688 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700689{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800690 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700691 struct xfrm_state *x;
692
Sasha Levinb67bfe02013-02-27 17:06:00 -0800693 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700694 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000695 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000696 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
697 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700698 continue;
699
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000700 if ((mark & x->mark.m) != x->mark.v)
701 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700702 xfrm_state_hold(x);
703 return x;
704 }
705
706 return NULL;
707}
708
709static inline struct xfrm_state *
710__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
711{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800712 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800713 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800714
David S. Milleredcd5822006-08-24 00:42:45 -0700715 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800716 return __xfrm_state_lookup(net, mark, &x->id.daddr,
717 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700718 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800719 return __xfrm_state_lookup_byaddr(net, mark,
720 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700721 &x->props.saddr,
722 x->id.proto, family);
723}
724
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800725static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700726{
727 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800728 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
729 net->xfrm.state_num > net->xfrm.state_hmask)
730 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700731}
732
David S. Miller08ec9af2009-03-13 14:22:40 -0700733static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800734 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700735 struct xfrm_state **best, int *acq_in_progress,
736 int *error)
737{
738 /* Resolution logic:
739 * 1. There is a valid state with matching selector. Done.
740 * 2. Valid state with inappropriate selector. Skip.
741 *
742 * Entering area of "sysdeps".
743 *
744 * 3. If state is not valid, selector is temporary, it selects
745 * only session which triggered previous resolution. Key
746 * manager will do something to install a state with proper
747 * selector.
748 */
749 if (x->km.state == XFRM_STATE_VALID) {
750 if ((x->sel.family &&
751 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
752 !security_xfrm_state_pol_flow_match(x, pol, fl))
753 return;
754
755 if (!*best ||
756 (*best)->km.dying > x->km.dying ||
757 ((*best)->km.dying == x->km.dying &&
758 (*best)->curlft.add_time < x->curlft.add_time))
759 *best = x;
760 } else if (x->km.state == XFRM_STATE_ACQ) {
761 *acq_in_progress = 1;
762 } else if (x->km.state == XFRM_STATE_ERROR ||
763 x->km.state == XFRM_STATE_EXPIRED) {
764 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
765 security_xfrm_state_pol_flow_match(x, pol, fl))
766 *error = -ESRCH;
767 }
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500771xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800772 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct xfrm_policy *pol, int *err,
774 unsigned short family)
775{
David S. Miller08ec9af2009-03-13 14:22:40 -0700776 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800777 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700778 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700779 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int acquire_in_progress = 0;
781 int error = 0;
782 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800783 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700784 unsigned short encap_family = tmpl->encap_family;
Horia Geanta0f245582014-02-12 16:20:06 +0200785 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900786
David S. Miller37b08e32008-09-02 20:14:15 -0700787 to_put = NULL;
788
Fan Du283bc9f2013-11-07 17:47:50 +0800789 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700790 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800791 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700792 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000794 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700795 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700796 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 tmpl->mode == x->props.mode &&
798 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700799 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500800 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700801 &best, &acquire_in_progress, &error);
802 }
Fan Du6f115632013-09-23 17:18:25 +0800803 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700804 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Thomas Egerer8444cf72010-09-20 11:11:38 -0700806 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800807 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700809 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000810 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700811 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800812 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700813 tmpl->mode == x->props.mode &&
814 tmpl->id.proto == x->id.proto &&
815 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500816 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700817 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
David S. Miller08ec9af2009-03-13 14:22:40 -0700820found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 x = best;
822 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700823 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800824 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700825 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700826 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 error = -EEXIST;
828 goto out;
829 }
Horia Geanta0f245582014-02-12 16:20:06 +0200830
831 c.net = net;
832 /* If the KMs have no listeners (yet...), avoid allocating an SA
833 * for each and every packet - garbage collection might not
834 * handle the flood.
835 */
836 if (!km_is_alive(&c)) {
837 error = -ESRCH;
838 goto out;
839 }
840
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800841 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (x == NULL) {
843 error = -ENOMEM;
844 goto out;
845 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700846 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700848 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800849 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David S. Miller1d28f422011-03-12 00:29:39 -0500851 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700852 if (error) {
853 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700854 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700855 x = NULL;
856 goto out;
857 }
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (km_query(x, tmpl, pol) == 0) {
860 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800861 list_add(&x->km.all, &net->xfrm.state_all);
862 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700863 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800864 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700866 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800867 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800869 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800870 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800871 net->xfrm.state_num++;
872 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
874 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700875 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 x = NULL;
877 error = -ESRCH;
878 }
879 }
880out:
881 if (x)
882 xfrm_state_hold(x);
883 else
884 *err = acquire_in_progress ? -EAGAIN : error;
Fan Du283bc9f2013-11-07 17:47:50 +0800885 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700886 if (to_put)
887 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return x;
889}
890
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700891struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800892xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800893 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700894 unsigned short family, u8 mode, u8 proto, u32 reqid)
895{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800896 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700897 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700898
Fan Du4ae770b2014-01-03 11:18:29 +0800899 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800900 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800901 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700902 if (x->props.family == family &&
903 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000904 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700905 !(x->props.flags & XFRM_STATE_WILDRECV) &&
906 xfrm_state_addr_check(x, daddr, saddr, family) &&
907 mode == x->props.mode &&
908 proto == x->id.proto &&
909 x->km.state == XFRM_STATE_VALID) {
910 rx = x;
911 break;
912 }
913 }
914
915 if (rx)
916 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +0800917 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700918
919
920 return rx;
921}
922EXPORT_SYMBOL(xfrm_stateonly_find);
923
Fan Duc4549972014-01-03 11:18:32 +0800924struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
925 unsigned short family)
926{
927 struct xfrm_state *x;
928 struct xfrm_state_walk *w;
929
930 spin_lock_bh(&net->xfrm.xfrm_state_lock);
931 list_for_each_entry(w, &net->xfrm.state_all, all) {
932 x = container_of(w, struct xfrm_state, km);
933 if (x->props.family != family ||
934 x->id.spi != spi)
935 continue;
936
937 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
938 xfrm_state_hold(x);
939 return x;
940 }
941 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
942 return NULL;
943}
944EXPORT_SYMBOL(xfrm_state_lookup_byspi);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static void __xfrm_state_insert(struct xfrm_state *x)
947{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800948 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700949 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800951 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800952
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800953 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700954 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800957 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
958 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700960 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800961 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700962 x->props.family);
963
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800964 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700965 }
966
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800967 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700968 if (x->replay_maxage)
969 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800970
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800971 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700972
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800973 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Fan Du283bc9f2013-11-07 17:47:50 +0800976/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -0700977static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
978{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800979 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700980 unsigned short family = xnew->props.family;
981 u32 reqid = xnew->props.reqid;
982 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700983 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000984 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700985
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800986 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800987 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700988 if (x->props.family == family &&
989 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000990 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000991 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
992 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000993 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700994 }
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997void xfrm_state_insert(struct xfrm_state *x)
998{
Fan Du283bc9f2013-11-07 17:47:50 +0800999 struct net *net = xs_net(x);
1000
1001 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001002 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001004 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006EXPORT_SYMBOL(xfrm_state_insert);
1007
Fan Du283bc9f2013-11-07 17:47:50 +08001008/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001009static struct xfrm_state *__find_acq_core(struct net *net,
1010 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001011 unsigned short family, u8 mode,
1012 u32 reqid, u8 proto,
1013 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001014 const xfrm_address_t *saddr,
1015 int create)
David S. Miller27708342006-08-24 00:13:10 -07001016{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001017 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001018 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001019 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001020
Sasha Levinb67bfe02013-02-27 17:06:00 -08001021 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001022 if (x->props.reqid != reqid ||
1023 x->props.mode != mode ||
1024 x->props.family != family ||
1025 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001026 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001027 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001028 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001029 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1030 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001031 continue;
1032
David S. Miller27708342006-08-24 00:13:10 -07001033 xfrm_state_hold(x);
1034 return x;
1035 }
1036
1037 if (!create)
1038 return NULL;
1039
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001040 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001041 if (likely(x)) {
1042 switch (family) {
1043 case AF_INET:
1044 x->sel.daddr.a4 = daddr->a4;
1045 x->sel.saddr.a4 = saddr->a4;
1046 x->sel.prefixlen_d = 32;
1047 x->sel.prefixlen_s = 32;
1048 x->props.saddr.a4 = saddr->a4;
1049 x->id.daddr.a4 = daddr->a4;
1050 break;
1051
1052 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001053 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1054 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001055 x->sel.prefixlen_d = 128;
1056 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001057 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1058 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001059 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001060 }
David S. Miller27708342006-08-24 00:13:10 -07001061
1062 x->km.state = XFRM_STATE_ACQ;
1063 x->id.proto = proto;
1064 x->props.family = family;
1065 x->props.mode = mode;
1066 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001067 x->mark.v = m->v;
1068 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001069 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001070 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001071 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001072 list_add(&x->km.all, &net->xfrm.state_all);
1073 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1074 h = xfrm_src_hash(net, daddr, saddr, family);
1075 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001076
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001077 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001078
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001079 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001080 }
1081
1082 return x;
1083}
1084
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001085static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087int xfrm_state_add(struct xfrm_state *x)
1088{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001089 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001090 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int family;
1092 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001093 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001094 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
David S. Miller37b08e32008-09-02 20:14:15 -07001098 to_put = NULL;
1099
Fan Du283bc9f2013-11-07 17:47:50 +08001100 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
David S. Milleredcd5822006-08-24 00:42:45 -07001102 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001104 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 x1 = NULL;
1106 err = -EEXIST;
1107 goto out;
1108 }
1109
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001110 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001111 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001112 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001113 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001114 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 x1 = NULL;
1116 }
1117 }
1118
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001119 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001120 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1121 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001122 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David S. Millerc7f5ea32006-08-24 03:29:04 -07001124 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 __xfrm_state_insert(x);
1126 err = 0;
1127
1128out:
Fan Du283bc9f2013-11-07 17:47:50 +08001129 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (x1) {
1132 xfrm_state_delete(x1);
1133 xfrm_state_put(x1);
1134 }
1135
David S. Miller37b08e32008-09-02 20:14:15 -07001136 if (to_put)
1137 xfrm_state_put(to_put);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return err;
1140}
1141EXPORT_SYMBOL(xfrm_state_add);
1142
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001143#ifdef CONFIG_XFRM_MIGRATE
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001144static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001145{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001146 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001147 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001148 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001149 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001150
1151 memcpy(&x->id, &orig->id, sizeof(x->id));
1152 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1153 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1154 x->props.mode = orig->props.mode;
1155 x->props.replay_window = orig->props.replay_window;
1156 x->props.reqid = orig->props.reqid;
1157 x->props.family = orig->props.family;
1158 x->props.saddr = orig->props.saddr;
1159
1160 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001161 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001162 if (!x->aalg)
1163 goto error;
1164 }
1165 x->props.aalgo = orig->props.aalgo;
1166
Steffen Klassertee5c2312014-02-19 13:33:24 +01001167 if (orig->aead) {
1168 x->aead = xfrm_algo_aead_clone(orig->aead);
1169 if (!x->aead)
1170 goto error;
1171 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001172 if (orig->ealg) {
1173 x->ealg = xfrm_algo_clone(orig->ealg);
1174 if (!x->ealg)
1175 goto error;
1176 }
1177 x->props.ealgo = orig->props.ealgo;
1178
1179 if (orig->calg) {
1180 x->calg = xfrm_algo_clone(orig->calg);
1181 if (!x->calg)
1182 goto error;
1183 }
1184 x->props.calgo = orig->props.calgo;
1185
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001186 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001187 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1188 if (!x->encap)
1189 goto error;
1190 }
1191
1192 if (orig->coaddr) {
1193 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1194 GFP_KERNEL);
1195 if (!x->coaddr)
1196 goto error;
1197 }
1198
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001199 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001200 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001201 goto error;
1202 }
1203
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001204 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1205
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001206 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001207 goto error;
1208
1209 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001210 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001211
Steffen Klassertee5c2312014-02-19 13:33:24 +01001212 x->tfcpad = orig->tfcpad;
1213 x->replay_maxdiff = orig->replay_maxdiff;
1214 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001215 x->curlft.add_time = orig->curlft.add_time;
1216 x->km.state = orig->km.state;
1217 x->km.seq = orig->km.seq;
1218
1219 return x;
1220
1221 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001222 xfrm_state_put(x);
1223out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001224 return NULL;
1225}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001226
Fan Du283bc9f2013-11-07 17:47:50 +08001227struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001228{
1229 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001230 struct xfrm_state *x = NULL;
1231
1232 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001233
1234 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001235 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001236 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001237 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001238 if (x->props.mode != m->mode ||
1239 x->id.proto != m->proto)
1240 continue;
1241 if (m->reqid && x->props.reqid != m->reqid)
1242 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001243 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1244 m->old_family) ||
1245 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1246 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001247 continue;
1248 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001249 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001250 }
1251 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001252 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001253 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001254 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001255 if (x->props.mode != m->mode ||
1256 x->id.proto != m->proto)
1257 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001258 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1259 m->old_family) ||
1260 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1261 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001262 continue;
1263 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001264 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001265 }
1266 }
1267
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001268 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1269
1270 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001271}
1272EXPORT_SYMBOL(xfrm_migrate_state_find);
1273
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001274struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1275 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001276{
1277 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001278
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001279 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001280 if (!xc)
1281 return NULL;
1282
1283 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1284 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1285
1286 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001287 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001288 /* a care is needed when the destination address of the
1289 state is to be updated as it is a part of triplet */
1290 xfrm_state_insert(xc);
1291 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001292 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001293 goto error;
1294 }
1295
1296 return xc;
1297error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001298 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001299 return NULL;
1300}
1301EXPORT_SYMBOL(xfrm_state_migrate);
1302#endif
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304int xfrm_state_update(struct xfrm_state *x)
1305{
David S. Miller37b08e32008-09-02 20:14:15 -07001306 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001308 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001309 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
David S. Miller37b08e32008-09-02 20:14:15 -07001311 to_put = NULL;
1312
Fan Du283bc9f2013-11-07 17:47:50 +08001313 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001314 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 err = -ESRCH;
1317 if (!x1)
1318 goto out;
1319
1320 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001321 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 err = -EEXIST;
1323 goto out;
1324 }
1325
1326 if (x1->km.state == XFRM_STATE_ACQ) {
1327 __xfrm_state_insert(x);
1328 x = NULL;
1329 }
1330 err = 0;
1331
1332out:
Fan Du283bc9f2013-11-07 17:47:50 +08001333 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
David S. Miller37b08e32008-09-02 20:14:15 -07001335 if (to_put)
1336 xfrm_state_put(to_put);
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (err)
1339 return err;
1340
1341 if (!x) {
1342 xfrm_state_delete(x1);
1343 xfrm_state_put(x1);
1344 return 0;
1345 }
1346
1347 err = -EINVAL;
1348 spin_lock_bh(&x1->lock);
1349 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1350 if (x->encap && x1->encap)
1351 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001352 if (x->coaddr && x1->coaddr) {
1353 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1354 }
1355 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1356 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1358 x1->km.dying = 0;
1359
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001360 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (x1->curlft.use_time)
1362 xfrm_state_check_expire(x1);
1363
1364 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001365 x->km.state = XFRM_STATE_DEAD;
1366 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 spin_unlock_bh(&x1->lock);
1369
1370 xfrm_state_put(x1);
1371
1372 return err;
1373}
1374EXPORT_SYMBOL(xfrm_state_update);
1375
1376int xfrm_state_check_expire(struct xfrm_state *x)
1377{
1378 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001379 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1382 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001383 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001384 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -EINVAL;
1386 }
1387
1388 if (!x->km.dying &&
1389 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001390 x->curlft.packets >= x->lft.soft_packet_limit)) {
1391 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001392 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return 0;
1395}
1396EXPORT_SYMBOL(xfrm_state_check_expire);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001399xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001400 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Fan Du283bc9f2013-11-07 17:47:50 +08001404 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001405 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001406 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return x;
1408}
1409EXPORT_SYMBOL(xfrm_state_lookup);
1410
1411struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001412xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001413 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001414 u8 proto, unsigned short family)
1415{
1416 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001417
Fan Du283bc9f2013-11-07 17:47:50 +08001418 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001419 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001420 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001421 return x;
1422}
1423EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1424
1425struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001426xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1427 u8 proto, const xfrm_address_t *daddr,
1428 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Fan Du283bc9f2013-11-07 17:47:50 +08001432 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001433 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001434 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return x;
1437}
1438EXPORT_SYMBOL(xfrm_find_acq);
1439
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001440#ifdef CONFIG_XFRM_SUB_POLICY
1441int
1442xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001443 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001444{
1445 int err = 0;
1446 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1447 if (!afinfo)
1448 return -EAFNOSUPPORT;
1449
Fan Du283bc9f2013-11-07 17:47:50 +08001450 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001451 if (afinfo->tmpl_sort)
1452 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001453 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001454 xfrm_state_put_afinfo(afinfo);
1455 return err;
1456}
1457EXPORT_SYMBOL(xfrm_tmpl_sort);
1458
1459int
1460xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1461 unsigned short family)
1462{
1463 int err = 0;
1464 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001465 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001466
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001467 if (!afinfo)
1468 return -EAFNOSUPPORT;
1469
Fan Du283bc9f2013-11-07 17:47:50 +08001470 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001471 if (afinfo->state_sort)
1472 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001473 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001474 xfrm_state_put_afinfo(afinfo);
1475 return err;
1476}
1477EXPORT_SYMBOL(xfrm_state_sort);
1478#endif
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/* Silly enough, but I'm lazy to build resolution list */
1481
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001482static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001486 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001487 struct xfrm_state *x;
1488
Sasha Levinb67bfe02013-02-27 17:06:00 -08001489 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001490 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001491 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001492 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 xfrm_state_hold(x);
1494 return x;
1495 }
1496 }
1497 }
1498 return NULL;
1499}
1500
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001501struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct xfrm_state *x;
1504
Fan Du283bc9f2013-11-07 17:47:50 +08001505 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001506 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001507 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return x;
1509}
1510EXPORT_SYMBOL(xfrm_find_acq_byseq);
1511
1512u32 xfrm_get_acqseq(void)
1513{
1514 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001515 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
jamal6836b9b2010-02-16 02:01:22 +00001517 do {
1518 res = atomic_inc_return(&acqseq);
1519 } while (!res);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return res;
1522}
1523EXPORT_SYMBOL(xfrm_get_acqseq);
1524
Fan Du776e9dd2013-12-16 18:47:49 +08001525int verify_spi_info(u8 proto, u32 min, u32 max)
1526{
1527 switch (proto) {
1528 case IPPROTO_AH:
1529 case IPPROTO_ESP:
1530 break;
1531
1532 case IPPROTO_COMP:
1533 /* IPCOMP spi is 16-bits. */
1534 if (max >= 0x10000)
1535 return -EINVAL;
1536 break;
1537
1538 default:
1539 return -EINVAL;
1540 }
1541
1542 if (min > max)
1543 return -EINVAL;
1544
1545 return 0;
1546}
1547EXPORT_SYMBOL(verify_spi_info);
1548
Herbert Xu658b2192007-10-09 13:29:52 -07001549int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001551 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001552 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001554 int err = -ENOENT;
1555 __be32 minspi = htonl(low);
1556 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001557 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Herbert Xu658b2192007-10-09 13:29:52 -07001559 spin_lock_bh(&x->lock);
1560 if (x->km.state == XFRM_STATE_DEAD)
1561 goto unlock;
1562
1563 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001565 goto unlock;
1566
1567 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001570 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (x0) {
1572 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001573 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 x->id.spi = minspi;
1576 } else {
1577 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001578 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001579 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001580 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (x0 == NULL) {
1582 x->id.spi = htonl(spi);
1583 break;
1584 }
1585 xfrm_state_put(x0);
1586 }
1587 }
1588 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001589 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001590 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1591 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Fan Du283bc9f2013-11-07 17:47:50 +08001592 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001593
1594 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Herbert Xu658b2192007-10-09 13:29:52 -07001596
1597unlock:
1598 spin_unlock_bh(&x->lock);
1599
1600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602EXPORT_SYMBOL(xfrm_alloc_spi);
1603
Nicolas Dichteld3623092014-02-14 15:30:36 +01001604static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001605 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001606{
1607 if (filter) {
1608 if ((filter->family == AF_INET ||
1609 filter->family == AF_INET6) &&
1610 x->props.family != filter->family)
1611 return false;
1612
1613 return addr_match(&x->props.saddr, &filter->saddr,
1614 filter->splen) &&
1615 addr_match(&x->id.daddr, &filter->daddr,
1616 filter->dplen);
1617 }
1618 return true;
1619}
1620
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001621int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001622 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 void *data)
1624{
Herbert Xu12a169e2008-10-01 07:03:24 -07001625 struct xfrm_state *state;
1626 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int err = 0;
1628
Herbert Xu12a169e2008-10-01 07:03:24 -07001629 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001630 return 0;
1631
Fan Du283bc9f2013-11-07 17:47:50 +08001632 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001633 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001634 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001635 else
1636 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001637 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001638 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001639 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001640 state = container_of(x, struct xfrm_state, km);
1641 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001642 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001643 if (!__xfrm_state_filter_match(state, walk->filter))
1644 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001645 err = func(state, walk->seq, data);
1646 if (err) {
1647 list_move_tail(&walk->all, &x->all);
1648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001650 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001652 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 err = -ENOENT;
1654 goto out;
1655 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001656 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657out:
Fan Du283bc9f2013-11-07 17:47:50 +08001658 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return err;
1660}
1661EXPORT_SYMBOL(xfrm_state_walk);
1662
Nicolas Dichteld3623092014-02-14 15:30:36 +01001663void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001664 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001665{
Herbert Xu12a169e2008-10-01 07:03:24 -07001666 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001667 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001668 walk->state = XFRM_STATE_DEAD;
1669 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001670 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001671}
1672EXPORT_SYMBOL(xfrm_state_walk_init);
1673
Fan Du283bc9f2013-11-07 17:47:50 +08001674void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001675{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001676 kfree(walk->filter);
1677
Herbert Xu12a169e2008-10-01 07:03:24 -07001678 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001679 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001680
Fan Du283bc9f2013-11-07 17:47:50 +08001681 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001682 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001683 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001684}
1685EXPORT_SYMBOL(xfrm_state_walk_done);
1686
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001687static void xfrm_replay_timer_handler(unsigned long data)
1688{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001689 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001690
1691 spin_lock(&x->lock);
1692
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001693 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001694 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001695 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001696 else
1697 x->xflags |= XFRM_TIME_DEFER;
1698 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001699
1700 spin_unlock(&x->lock);
1701}
1702
Denis Chengdf018122007-12-07 00:51:11 -08001703static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
David S. Miller214e0052011-02-24 00:02:38 -05001705void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct xfrm_mgr *km;
1708
Cong Wang85168c02013-01-16 16:05:06 +08001709 rcu_read_lock();
1710 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001711 if (km->notify_policy)
1712 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001713 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001714}
1715
David S. Miller214e0052011-02-24 00:02:38 -05001716void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001717{
1718 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001719 rcu_read_lock();
1720 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721 if (km->notify)
1722 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001723 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724}
1725
1726EXPORT_SYMBOL(km_policy_notify);
1727EXPORT_SYMBOL(km_state_notify);
1728
Eric W. Biederman15e47302012-09-07 20:12:54 +00001729void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001730{
1731 struct km_event c;
1732
Herbert Xubf088672005-06-18 22:44:00 -07001733 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001734 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001735 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001736 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001739EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001740/*
1741 * We send to all registered managers regardless of failure
1742 * We are happy with one success
1743*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001744int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001746 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 struct xfrm_mgr *km;
1748
Cong Wang85168c02013-01-16 16:05:06 +08001749 rcu_read_lock();
1750 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001751 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001752 if (!acqret)
1753 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
Cong Wang85168c02013-01-16 16:05:06 +08001755 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return err;
1757}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001758EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Al Viro5d36b182006-11-08 00:24:06 -08001760int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int err = -EINVAL;
1763 struct xfrm_mgr *km;
1764
Cong Wang85168c02013-01-16 16:05:06 +08001765 rcu_read_lock();
1766 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (km->new_mapping)
1768 err = km->new_mapping(x, ipaddr, sport);
1769 if (!err)
1770 break;
1771 }
Cong Wang85168c02013-01-16 16:05:06 +08001772 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return err;
1774}
1775EXPORT_SYMBOL(km_new_mapping);
1776
Eric W. Biederman15e47302012-09-07 20:12:54 +00001777void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001779 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Herbert Xubf088672005-06-18 22:44:00 -07001781 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001782 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001783 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001784 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
David S. Millera70fcb02006-03-20 19:18:52 -08001786EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001788#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001789int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1790 const struct xfrm_migrate *m, int num_migrate,
1791 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001792{
1793 int err = -EINVAL;
1794 int ret;
1795 struct xfrm_mgr *km;
1796
Cong Wang85168c02013-01-16 16:05:06 +08001797 rcu_read_lock();
1798 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001799 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001800 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001801 if (!ret)
1802 err = ret;
1803 }
1804 }
Cong Wang85168c02013-01-16 16:05:06 +08001805 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001806 return err;
1807}
1808EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001809#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001810
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001811int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001812{
1813 int err = -EINVAL;
1814 int ret;
1815 struct xfrm_mgr *km;
1816
Cong Wang85168c02013-01-16 16:05:06 +08001817 rcu_read_lock();
1818 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001819 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001820 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001821 if (!ret)
1822 err = ret;
1823 }
1824 }
Cong Wang85168c02013-01-16 16:05:06 +08001825 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001826 return err;
1827}
1828EXPORT_SYMBOL(km_report);
1829
Horia Geanta0f245582014-02-12 16:20:06 +02001830bool km_is_alive(const struct km_event *c)
1831{
1832 struct xfrm_mgr *km;
1833 bool is_alive = false;
1834
1835 rcu_read_lock();
1836 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1837 if (km->is_alive && km->is_alive(c)) {
1838 is_alive = true;
1839 break;
1840 }
1841 }
1842 rcu_read_unlock();
1843
1844 return is_alive;
1845}
1846EXPORT_SYMBOL(km_is_alive);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1849{
1850 int err;
1851 u8 *data;
1852 struct xfrm_mgr *km;
1853 struct xfrm_policy *pol = NULL;
1854
1855 if (optlen <= 0 || optlen > PAGE_SIZE)
1856 return -EMSGSIZE;
1857
1858 data = kmalloc(optlen, GFP_KERNEL);
1859 if (!data)
1860 return -ENOMEM;
1861
1862 err = -EFAULT;
1863 if (copy_from_user(data, optval, optlen))
1864 goto out;
1865
1866 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001867 rcu_read_lock();
1868 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001869 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 optlen, &err);
1871 if (err >= 0)
1872 break;
1873 }
Cong Wang85168c02013-01-16 16:05:06 +08001874 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 if (err >= 0) {
1877 xfrm_sk_policy_insert(sk, err, pol);
1878 xfrm_pol_put(pol);
1879 err = 0;
1880 }
1881
1882out:
1883 kfree(data);
1884 return err;
1885}
1886EXPORT_SYMBOL(xfrm_user_policy);
1887
Cong Wang85168c02013-01-16 16:05:06 +08001888static DEFINE_SPINLOCK(xfrm_km_lock);
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890int xfrm_register_km(struct xfrm_mgr *km)
1891{
Cong Wang85168c02013-01-16 16:05:06 +08001892 spin_lock_bh(&xfrm_km_lock);
1893 list_add_tail_rcu(&km->list, &xfrm_km_list);
1894 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return 0;
1896}
1897EXPORT_SYMBOL(xfrm_register_km);
1898
1899int xfrm_unregister_km(struct xfrm_mgr *km)
1900{
Cong Wang85168c02013-01-16 16:05:06 +08001901 spin_lock_bh(&xfrm_km_lock);
1902 list_del_rcu(&km->list);
1903 spin_unlock_bh(&xfrm_km_lock);
1904 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return 0;
1906}
1907EXPORT_SYMBOL(xfrm_unregister_km);
1908
1909int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1910{
1911 int err = 0;
1912 if (unlikely(afinfo == NULL))
1913 return -EINVAL;
1914 if (unlikely(afinfo->family >= NPROTO))
1915 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001916 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1918 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001919 else
Cong Wang44abdc32013-01-16 16:05:05 +08001920 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1921 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return err;
1923}
1924EXPORT_SYMBOL(xfrm_state_register_afinfo);
1925
1926int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1927{
1928 int err = 0;
1929 if (unlikely(afinfo == NULL))
1930 return -EINVAL;
1931 if (unlikely(afinfo->family >= NPROTO))
1932 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001933 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1935 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1936 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001937 else
Cong Wang44abdc32013-01-16 16:05:05 +08001938 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Cong Wang44abdc32013-01-16 16:05:05 +08001940 spin_unlock_bh(&xfrm_state_afinfo_lock);
1941 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return err;
1943}
1944EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1945
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001946struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct xfrm_state_afinfo *afinfo;
1949 if (unlikely(family >= NPROTO))
1950 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001951 rcu_read_lock();
1952 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001953 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001954 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return afinfo;
1956}
1957
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001958void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Cong Wang44abdc32013-01-16 16:05:05 +08001960 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1964void xfrm_state_delete_tunnel(struct xfrm_state *x)
1965{
1966 if (x->tunnel) {
1967 struct xfrm_state *t = x->tunnel;
1968
1969 if (atomic_read(&t->tunnel_users) == 2)
1970 xfrm_state_delete(t);
1971 atomic_dec(&t->tunnel_users);
1972 xfrm_state_put(t);
1973 x->tunnel = NULL;
1974 }
1975}
1976EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1977
1978int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1979{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001980 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Patrick McHardyc5c25232007-04-09 11:47:18 -07001982 spin_lock_bh(&x->lock);
1983 if (x->km.state == XFRM_STATE_VALID &&
1984 x->type && x->type->get_mtu)
1985 res = x->type->get_mtu(x, mtu);
1986 else
Patrick McHardy28121612007-06-18 22:30:15 -07001987 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001988 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return res;
1990}
1991
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001992int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001993{
Herbert Xud094cd82005-06-20 13:19:41 -07001994 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001995 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07001996 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07001997 int err;
1998
Herbert Xud094cd82005-06-20 13:19:41 -07001999 err = -EAFNOSUPPORT;
2000 afinfo = xfrm_state_get_afinfo(family);
2001 if (!afinfo)
2002 goto error;
2003
2004 err = 0;
2005 if (afinfo->init_flags)
2006 err = afinfo->init_flags(x);
2007
2008 xfrm_state_put_afinfo(afinfo);
2009
2010 if (err)
2011 goto error;
2012
2013 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002014
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002015 if (x->sel.family != AF_UNSPEC) {
2016 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2017 if (inner_mode == NULL)
2018 goto error;
2019
2020 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2021 family != x->sel.family) {
2022 xfrm_put_mode(inner_mode);
2023 goto error;
2024 }
2025
2026 x->inner_mode = inner_mode;
2027 } else {
2028 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002029 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002030
Martin Willid81d2282008-12-03 15:38:07 -08002031 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002032 if (inner_mode == NULL)
2033 goto error;
2034
2035 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2036 xfrm_put_mode(inner_mode);
2037 goto error;
2038 }
Martin Willid81d2282008-12-03 15:38:07 -08002039 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002040
Martin Willid81d2282008-12-03 15:38:07 -08002041 if (x->props.family == AF_INET)
2042 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002043
Martin Willid81d2282008-12-03 15:38:07 -08002044 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2045 if (inner_mode_iaf) {
2046 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2047 x->inner_mode_iaf = inner_mode_iaf;
2048 else
2049 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002050 }
2051 }
Herbert Xu13996372007-10-17 21:35:51 -07002052
Herbert Xud094cd82005-06-20 13:19:41 -07002053 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002054 if (x->type == NULL)
2055 goto error;
2056
2057 err = x->type->init_state(x);
2058 if (err)
2059 goto error;
2060
Herbert Xu13996372007-10-17 21:35:51 -07002061 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002062 if (x->outer_mode == NULL) {
2063 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002064 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002065 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002066
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002067 if (init_replay) {
2068 err = xfrm_init_replay(x);
2069 if (err)
2070 goto error;
2071 }
2072
Herbert Xu72cb6962005-06-20 13:18:08 -07002073 x->km.state = XFRM_STATE_VALID;
2074
2075error:
2076 return err;
2077}
2078
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002079EXPORT_SYMBOL(__xfrm_init_state);
2080
2081int xfrm_init_state(struct xfrm_state *x)
2082{
2083 return __xfrm_init_state(x, true);
2084}
2085
Herbert Xu72cb6962005-06-20 13:18:08 -07002086EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002087
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002088int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
David S. Millerf034b5d2006-08-24 03:08:07 -07002090 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002092 INIT_LIST_HEAD(&net->xfrm.state_all);
2093
David S. Millerf034b5d2006-08-24 03:08:07 -07002094 sz = sizeof(struct hlist_head) * 8;
2095
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002096 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2097 if (!net->xfrm.state_bydst)
2098 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002099 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2100 if (!net->xfrm.state_bysrc)
2101 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002102 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2103 if (!net->xfrm.state_byspi)
2104 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002105 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002106
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002107 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002108 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002109 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002110 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Fan Du283bc9f2013-11-07 17:47:50 +08002111 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002112 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002113
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002114out_byspi:
2115 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002116out_bysrc:
2117 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002118out_bydst:
2119 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002120}
2121
2122void xfrm_state_fini(struct net *net)
2123{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002124 unsigned int sz;
2125
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002126 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002127 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002128 flush_work(&net->xfrm.state_gc_work);
2129
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002130 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002131
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002132 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002133 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2134 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002135 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2136 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002137 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2138 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Joy Lattenab5f5e82007-09-17 11:51:22 -07002141#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002142static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2143 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002144{
Paul Moore68277ac2007-12-20 20:49:33 -08002145 struct xfrm_sec_ctx *ctx = x->security;
2146 u32 spi = ntohl(x->id.spi);
2147
2148 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002149 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002150 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002151
Weilong Chen9b7a7872013-12-24 09:43:46 +08002152 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002153 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002154 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2155 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002156 break;
2157 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002158 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002159 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002160 break;
2161 }
Paul Moore68277ac2007-12-20 20:49:33 -08002162
2163 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002164}
2165
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002166static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2167 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002168{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002169 const struct iphdr *iph4;
2170 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002171
2172 switch (family) {
2173 case AF_INET:
2174 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002175 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2176 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002177 break;
2178 case AF_INET6:
2179 iph6 = ipv6_hdr(skb);
2180 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002181 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002182 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002183 iph6->flow_lbl[0] & 0x0f,
2184 iph6->flow_lbl[1],
2185 iph6->flow_lbl[2]);
2186 break;
2187 }
2188}
2189
Tetsuo Handa2e710292014-04-22 21:48:30 +09002190void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002191{
2192 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002193
Paul Mooreafeb14b2007-12-21 14:58:11 -08002194 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002195 if (audit_buf == NULL)
2196 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002197 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002198 xfrm_audit_helper_sainfo(x, audit_buf);
2199 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002200 audit_log_end(audit_buf);
2201}
2202EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2203
Tetsuo Handa2e710292014-04-22 21:48:30 +09002204void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002205{
2206 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002207
Paul Mooreafeb14b2007-12-21 14:58:11 -08002208 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002209 if (audit_buf == NULL)
2210 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002211 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002212 xfrm_audit_helper_sainfo(x, audit_buf);
2213 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002214 audit_log_end(audit_buf);
2215}
2216EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002217
2218void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2219 struct sk_buff *skb)
2220{
2221 struct audit_buffer *audit_buf;
2222 u32 spi;
2223
2224 audit_buf = xfrm_audit_start("SA-replay-overflow");
2225 if (audit_buf == NULL)
2226 return;
2227 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2228 /* don't record the sequence number because it's inherent in this kind
2229 * of audit message */
2230 spi = ntohl(x->id.spi);
2231 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2232 audit_log_end(audit_buf);
2233}
2234EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2235
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002236void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002237 struct sk_buff *skb, __be32 net_seq)
2238{
2239 struct audit_buffer *audit_buf;
2240 u32 spi;
2241
2242 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2243 if (audit_buf == NULL)
2244 return;
2245 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2246 spi = ntohl(x->id.spi);
2247 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2248 spi, spi, ntohl(net_seq));
2249 audit_log_end(audit_buf);
2250}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002251EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002252
2253void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2254{
2255 struct audit_buffer *audit_buf;
2256
2257 audit_buf = xfrm_audit_start("SA-notfound");
2258 if (audit_buf == NULL)
2259 return;
2260 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2261 audit_log_end(audit_buf);
2262}
2263EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2264
2265void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2266 __be32 net_spi, __be32 net_seq)
2267{
2268 struct audit_buffer *audit_buf;
2269 u32 spi;
2270
2271 audit_buf = xfrm_audit_start("SA-notfound");
2272 if (audit_buf == NULL)
2273 return;
2274 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2275 spi = ntohl(net_spi);
2276 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2277 spi, spi, ntohl(net_seq));
2278 audit_log_end(audit_buf);
2279}
2280EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2281
2282void xfrm_audit_state_icvfail(struct xfrm_state *x,
2283 struct sk_buff *skb, u8 proto)
2284{
2285 struct audit_buffer *audit_buf;
2286 __be32 net_spi;
2287 __be32 net_seq;
2288
2289 audit_buf = xfrm_audit_start("SA-icv-failure");
2290 if (audit_buf == NULL)
2291 return;
2292 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2293 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2294 u32 spi = ntohl(net_spi);
2295 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2296 spi, spi, ntohl(net_seq));
2297 }
2298 audit_log_end(audit_buf);
2299}
2300EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002301#endif /* CONFIG_AUDITSYSCALL */