blob: 8e9c781a6bbaaba83e4af4a31ac7a07a70dd6c71 [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
Joy Lattenab5f5e82007-09-17 11:51:22 -0700466 xfrm_audit_state_delete(x, err ? 0 : 1,
Eric Paris25323862008-04-18 10:09:25 -0400467 audit_get_loginuid(current),
468 audit_get_sessionid(current), 0);
Joy Latten161a09e2006-11-27 13:11:54 -0600469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470out:
471 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800472 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
David S. Miller0ac84752006-03-20 19:18:23 -0800475static void xfrm_replay_timer_handler(unsigned long data);
476
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800477struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct xfrm_state *x;
480
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700481 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800484 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 atomic_set(&x->refcnt, 1);
486 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700487 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700488 INIT_HLIST_NODE(&x->bydst);
489 INIT_HLIST_NODE(&x->bysrc);
490 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800491 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
492 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800493 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
494 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800495 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 x->lft.soft_byte_limit = XFRM_INF;
497 x->lft.soft_packet_limit = XFRM_INF;
498 x->lft.hard_byte_limit = XFRM_INF;
499 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800500 x->replay_maxage = 0;
501 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700502 x->inner_mode = NULL;
503 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 spin_lock_init(&x->lock);
505 }
506 return x;
507}
508EXPORT_SYMBOL(xfrm_state_alloc);
509
510void __xfrm_state_destroy(struct xfrm_state *x)
511{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800512 struct net *net = xs_net(x);
513
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700514 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800517 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800519 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521EXPORT_SYMBOL(__xfrm_state_destroy);
522
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800523int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800525 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700526 int err = -ESRCH;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (x->km.state != XFRM_STATE_DEAD) {
529 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800530 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700531 list_del(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700532 hlist_del(&x->bydst);
David S. Miller8f126e32006-08-24 02:45:07 -0700533 hlist_del(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700534 if (x->id.spi)
David S. Miller8f126e32006-08-24 02:45:07 -0700535 hlist_del(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800536 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800537 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /* All xfrm_state objects are created by xfrm_state_alloc.
540 * The xfrm_state_alloc call gives a reference, and that
541 * is what we are dropping here.
542 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800543 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700544 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700546
547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800549EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700551int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700553 int err;
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700556 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700558
559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561EXPORT_SYMBOL(xfrm_state_delete);
562
Joy Latten4aa2e622007-06-04 19:05:57 -0400563#ifdef CONFIG_SECURITY_NETWORK_XFRM
564static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800565xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Joy Latten4aa2e622007-06-04 19:05:57 -0400567 int i, err = 0;
568
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800569 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400570 struct xfrm_state *x;
571
Sasha Levinb67bfe02013-02-27 17:06:00 -0800572 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400573 if (xfrm_id_proto_match(x->id.proto, proto) &&
574 (err = security_xfrm_state_delete(x)) != 0) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700575 xfrm_audit_state_delete(x, 0,
576 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400577 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700578 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400579 return err;
580 }
581 }
582 }
583
584 return err;
585}
586#else
587static inline int
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800588xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400589{
590 return 0;
591}
592#endif
593
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800594int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400595{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000596 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Fan Du283bc9f2013-11-07 17:47:50 +0800598 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800599 err = xfrm_state_flush_secctx_check(net, proto, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400600 if (err)
601 goto out;
602
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000603 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800604 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700605 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800607 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700609 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800611 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Joy Latten161a09e2006-11-27 13:11:54 -0600613 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700614 xfrm_audit_state_delete(x, err ? 0 : 1,
615 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400616 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700617 audit_info->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000619 if (!err)
620 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Fan Du283bc9f2013-11-07 17:47:50 +0800622 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto restart;
624 }
625 }
626 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000627 if (cnt)
628 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400629
630out:
Fan Du283bc9f2013-11-07 17:47:50 +0800631 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400632 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634EXPORT_SYMBOL(xfrm_state_flush);
635
Alexey Dobriyane0710412010-01-23 13:37:10 +0000636void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700637{
Fan Du283bc9f2013-11-07 17:47:50 +0800638 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000639 si->sadcnt = net->xfrm.state_num;
640 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700641 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800642 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700643}
644EXPORT_SYMBOL(xfrm_sad_getinfo);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static int
David S. Miller1a898592011-02-22 18:22:34 -0800647xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500648 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500649 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700650 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
653 if (!afinfo)
654 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700655 afinfo->init_tempsel(&x->sel, fl);
656
657 if (family != tmpl->encap_family) {
658 xfrm_state_put_afinfo(afinfo);
659 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
660 if (!afinfo)
661 return -1;
662 }
663 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 xfrm_state_put_afinfo(afinfo);
665 return 0;
666}
667
David S. Miller9aa60082011-02-24 01:51:36 -0500668static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
669 const xfrm_address_t *daddr,
670 __be32 spi, u8 proto,
671 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700672{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800673 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700674 struct xfrm_state *x;
675
Sasha Levinb67bfe02013-02-27 17:06:00 -0800676 hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700677 if (x->props.family != family ||
678 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000679 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000680 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700681 continue;
682
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000683 if ((mark & x->mark.m) != x->mark.v)
684 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700685 xfrm_state_hold(x);
686 return x;
687 }
688
689 return NULL;
690}
691
David S. Miller9aa60082011-02-24 01:51:36 -0500692static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
693 const xfrm_address_t *daddr,
694 const xfrm_address_t *saddr,
695 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700696{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800697 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700698 struct xfrm_state *x;
699
Sasha Levinb67bfe02013-02-27 17:06:00 -0800700 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700701 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000702 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000703 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
704 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700705 continue;
706
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000707 if ((mark & x->mark.m) != x->mark.v)
708 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700709 xfrm_state_hold(x);
710 return x;
711 }
712
713 return NULL;
714}
715
716static inline struct xfrm_state *
717__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
718{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800719 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800720 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800721
David S. Milleredcd5822006-08-24 00:42:45 -0700722 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800723 return __xfrm_state_lookup(net, mark, &x->id.daddr,
724 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700725 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800726 return __xfrm_state_lookup_byaddr(net, mark,
727 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700728 &x->props.saddr,
729 x->id.proto, family);
730}
731
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800732static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700733{
734 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800735 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
736 net->xfrm.state_num > net->xfrm.state_hmask)
737 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700738}
739
David S. Miller08ec9af2009-03-13 14:22:40 -0700740static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800741 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700742 struct xfrm_state **best, int *acq_in_progress,
743 int *error)
744{
745 /* Resolution logic:
746 * 1. There is a valid state with matching selector. Done.
747 * 2. Valid state with inappropriate selector. Skip.
748 *
749 * Entering area of "sysdeps".
750 *
751 * 3. If state is not valid, selector is temporary, it selects
752 * only session which triggered previous resolution. Key
753 * manager will do something to install a state with proper
754 * selector.
755 */
756 if (x->km.state == XFRM_STATE_VALID) {
757 if ((x->sel.family &&
758 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
759 !security_xfrm_state_pol_flow_match(x, pol, fl))
760 return;
761
762 if (!*best ||
763 (*best)->km.dying > x->km.dying ||
764 ((*best)->km.dying == x->km.dying &&
765 (*best)->curlft.add_time < x->curlft.add_time))
766 *best = x;
767 } else if (x->km.state == XFRM_STATE_ACQ) {
768 *acq_in_progress = 1;
769 } else if (x->km.state == XFRM_STATE_ERROR ||
770 x->km.state == XFRM_STATE_EXPIRED) {
771 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
772 security_xfrm_state_pol_flow_match(x, pol, fl))
773 *error = -ESRCH;
774 }
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500778xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800779 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct xfrm_policy *pol, int *err,
781 unsigned short family)
782{
David S. Miller08ec9af2009-03-13 14:22:40 -0700783 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800784 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700785 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700786 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int acquire_in_progress = 0;
788 int error = 0;
789 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800790 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700791 unsigned short encap_family = tmpl->encap_family;
Horia Geanta0f245582014-02-12 16:20:06 +0200792 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900793
David S. Miller37b08e32008-09-02 20:14:15 -0700794 to_put = NULL;
795
Fan Du283bc9f2013-11-07 17:47:50 +0800796 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700797 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800798 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700799 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000801 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700802 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700803 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tmpl->mode == x->props.mode &&
805 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700806 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500807 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700808 &best, &acquire_in_progress, &error);
809 }
Fan Du6f115632013-09-23 17:18:25 +0800810 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700811 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Thomas Egerer8444cf72010-09-20 11:11:38 -0700813 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800814 hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700815 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700816 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000817 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700818 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800819 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700820 tmpl->mode == x->props.mode &&
821 tmpl->id.proto == x->id.proto &&
822 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500823 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700824 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
David S. Miller08ec9af2009-03-13 14:22:40 -0700827found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 x = best;
829 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700830 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800831 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700832 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700833 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 error = -EEXIST;
835 goto out;
836 }
Horia Geanta0f245582014-02-12 16:20:06 +0200837
838 c.net = net;
839 /* If the KMs have no listeners (yet...), avoid allocating an SA
840 * for each and every packet - garbage collection might not
841 * handle the flood.
842 */
843 if (!km_is_alive(&c)) {
844 error = -ESRCH;
845 goto out;
846 }
847
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800848 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (x == NULL) {
850 error = -ENOMEM;
851 goto out;
852 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700853 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700855 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800856 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David S. Miller1d28f422011-03-12 00:29:39 -0500858 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700859 if (error) {
860 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700861 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700862 x = NULL;
863 goto out;
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (km_query(x, tmpl, pol) == 0) {
867 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800868 list_add(&x->km.all, &net->xfrm.state_all);
869 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700870 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800871 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700873 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800874 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800876 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800877 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800878 net->xfrm.state_num++;
879 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } else {
881 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700882 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 x = NULL;
884 error = -ESRCH;
885 }
886 }
887out:
888 if (x)
889 xfrm_state_hold(x);
890 else
891 *err = acquire_in_progress ? -EAGAIN : error;
Fan Du283bc9f2013-11-07 17:47:50 +0800892 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700893 if (to_put)
894 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return x;
896}
897
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700898struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800899xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800900 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700901 unsigned short family, u8 mode, u8 proto, u32 reqid)
902{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800903 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700904 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700905
Fan Du4ae770b2014-01-03 11:18:29 +0800906 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800907 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800908 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909 if (x->props.family == family &&
910 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000911 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700912 !(x->props.flags & XFRM_STATE_WILDRECV) &&
913 xfrm_state_addr_check(x, daddr, saddr, family) &&
914 mode == x->props.mode &&
915 proto == x->id.proto &&
916 x->km.state == XFRM_STATE_VALID) {
917 rx = x;
918 break;
919 }
920 }
921
922 if (rx)
923 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +0800924 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700925
926
927 return rx;
928}
929EXPORT_SYMBOL(xfrm_stateonly_find);
930
Fan Duc4549972014-01-03 11:18:32 +0800931struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
932 unsigned short family)
933{
934 struct xfrm_state *x;
935 struct xfrm_state_walk *w;
936
937 spin_lock_bh(&net->xfrm.xfrm_state_lock);
938 list_for_each_entry(w, &net->xfrm.state_all, all) {
939 x = container_of(w, struct xfrm_state, km);
940 if (x->props.family != family ||
941 x->id.spi != spi)
942 continue;
943
944 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
945 xfrm_state_hold(x);
946 return x;
947 }
948 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
949 return NULL;
950}
951EXPORT_SYMBOL(xfrm_state_lookup_byspi);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static void __xfrm_state_insert(struct xfrm_state *x)
954{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700956 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800958 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800959
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800960 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700961 x->props.reqid, x->props.family);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800962 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800964 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
965 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700967 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800968 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700969 x->props.family);
970
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800971 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700972 }
973
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800974 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700975 if (x->replay_maxage)
976 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800977
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800978 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700979
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800980 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Fan Du283bc9f2013-11-07 17:47:50 +0800983/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -0700984static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
985{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800986 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700987 unsigned short family = xnew->props.family;
988 u32 reqid = xnew->props.reqid;
989 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700990 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000991 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700992
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800993 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800994 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700995 if (x->props.family == family &&
996 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000997 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000998 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
999 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001000 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001001 }
1002}
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004void xfrm_state_insert(struct xfrm_state *x)
1005{
Fan Du283bc9f2013-11-07 17:47:50 +08001006 struct net *net = xs_net(x);
1007
1008 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001009 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001011 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013EXPORT_SYMBOL(xfrm_state_insert);
1014
Fan Du283bc9f2013-11-07 17:47:50 +08001015/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001016static struct xfrm_state *__find_acq_core(struct net *net,
1017 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001018 unsigned short family, u8 mode,
1019 u32 reqid, u8 proto,
1020 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001021 const xfrm_address_t *saddr,
1022 int create)
David S. Miller27708342006-08-24 00:13:10 -07001023{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001024 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001025 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001026 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001027
Sasha Levinb67bfe02013-02-27 17:06:00 -08001028 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001029 if (x->props.reqid != reqid ||
1030 x->props.mode != mode ||
1031 x->props.family != family ||
1032 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001033 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001034 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001035 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001036 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1037 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001038 continue;
1039
David S. Miller27708342006-08-24 00:13:10 -07001040 xfrm_state_hold(x);
1041 return x;
1042 }
1043
1044 if (!create)
1045 return NULL;
1046
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001047 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001048 if (likely(x)) {
1049 switch (family) {
1050 case AF_INET:
1051 x->sel.daddr.a4 = daddr->a4;
1052 x->sel.saddr.a4 = saddr->a4;
1053 x->sel.prefixlen_d = 32;
1054 x->sel.prefixlen_s = 32;
1055 x->props.saddr.a4 = saddr->a4;
1056 x->id.daddr.a4 = daddr->a4;
1057 break;
1058
1059 case AF_INET6:
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001060 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
1061 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
David S. Miller27708342006-08-24 00:13:10 -07001062 x->sel.prefixlen_d = 128;
1063 x->sel.prefixlen_s = 128;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001064 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
1065 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
David S. Miller27708342006-08-24 00:13:10 -07001066 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001067 }
David S. Miller27708342006-08-24 00:13:10 -07001068
1069 x->km.state = XFRM_STATE_ACQ;
1070 x->id.proto = proto;
1071 x->props.family = family;
1072 x->props.mode = mode;
1073 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001074 x->mark.v = m->v;
1075 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001076 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001077 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001078 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001079 list_add(&x->km.all, &net->xfrm.state_all);
1080 hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
1081 h = xfrm_src_hash(net, daddr, saddr, family);
1082 hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
David S. Miller918049f2006-10-12 22:03:24 -07001083
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001084 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001085
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001086 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001087 }
1088
1089 return x;
1090}
1091
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001092static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094int xfrm_state_add(struct xfrm_state *x)
1095{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001096 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001097 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 int family;
1099 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001100 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001101 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
David S. Miller37b08e32008-09-02 20:14:15 -07001105 to_put = NULL;
1106
Fan Du283bc9f2013-11-07 17:47:50 +08001107 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
David S. Milleredcd5822006-08-24 00:42:45 -07001109 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001111 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 x1 = NULL;
1113 err = -EEXIST;
1114 goto out;
1115 }
1116
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001117 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001118 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001119 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001120 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001121 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 x1 = NULL;
1123 }
1124 }
1125
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001126 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001127 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1128 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001129 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
David S. Millerc7f5ea32006-08-24 03:29:04 -07001131 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 __xfrm_state_insert(x);
1133 err = 0;
1134
1135out:
Fan Du283bc9f2013-11-07 17:47:50 +08001136 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (x1) {
1139 xfrm_state_delete(x1);
1140 xfrm_state_put(x1);
1141 }
1142
David S. Miller37b08e32008-09-02 20:14:15 -07001143 if (to_put)
1144 xfrm_state_put(to_put);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return err;
1147}
1148EXPORT_SYMBOL(xfrm_state_add);
1149
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001150#ifdef CONFIG_XFRM_MIGRATE
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001151static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001152{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001153 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001154 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001155 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001156 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001157
1158 memcpy(&x->id, &orig->id, sizeof(x->id));
1159 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1160 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1161 x->props.mode = orig->props.mode;
1162 x->props.replay_window = orig->props.replay_window;
1163 x->props.reqid = orig->props.reqid;
1164 x->props.family = orig->props.family;
1165 x->props.saddr = orig->props.saddr;
1166
1167 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001168 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001169 if (!x->aalg)
1170 goto error;
1171 }
1172 x->props.aalgo = orig->props.aalgo;
1173
Steffen Klassertee5c2312014-02-19 13:33:24 +01001174 if (orig->aead) {
1175 x->aead = xfrm_algo_aead_clone(orig->aead);
1176 if (!x->aead)
1177 goto error;
1178 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001179 if (orig->ealg) {
1180 x->ealg = xfrm_algo_clone(orig->ealg);
1181 if (!x->ealg)
1182 goto error;
1183 }
1184 x->props.ealgo = orig->props.ealgo;
1185
1186 if (orig->calg) {
1187 x->calg = xfrm_algo_clone(orig->calg);
1188 if (!x->calg)
1189 goto error;
1190 }
1191 x->props.calgo = orig->props.calgo;
1192
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001193 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001194 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1195 if (!x->encap)
1196 goto error;
1197 }
1198
1199 if (orig->coaddr) {
1200 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1201 GFP_KERNEL);
1202 if (!x->coaddr)
1203 goto error;
1204 }
1205
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001206 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001207 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001208 goto error;
1209 }
1210
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001211 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1212
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001213 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001214 goto error;
1215
1216 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001217 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001218
Steffen Klassertee5c2312014-02-19 13:33:24 +01001219 x->tfcpad = orig->tfcpad;
1220 x->replay_maxdiff = orig->replay_maxdiff;
1221 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001222 x->curlft.add_time = orig->curlft.add_time;
1223 x->km.state = orig->km.state;
1224 x->km.seq = orig->km.seq;
1225
1226 return x;
1227
1228 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001229 xfrm_state_put(x);
1230out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001231 return NULL;
1232}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001233
Fan Du283bc9f2013-11-07 17:47:50 +08001234struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001235{
1236 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001237 struct xfrm_state *x = NULL;
1238
1239 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001240
1241 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001242 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001243 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001244 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001245 if (x->props.mode != m->mode ||
1246 x->id.proto != m->proto)
1247 continue;
1248 if (m->reqid && x->props.reqid != m->reqid)
1249 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001250 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1251 m->old_family) ||
1252 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1253 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001254 continue;
1255 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001256 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001257 }
1258 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001259 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001260 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001261 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001262 if (x->props.mode != m->mode ||
1263 x->id.proto != m->proto)
1264 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001265 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1266 m->old_family) ||
1267 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1268 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001269 continue;
1270 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001271 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001272 }
1273 }
1274
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001275 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1276
1277 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001278}
1279EXPORT_SYMBOL(xfrm_migrate_state_find);
1280
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001281struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1282 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001283{
1284 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001285
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001286 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001287 if (!xc)
1288 return NULL;
1289
1290 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1291 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1292
1293 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001294 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001295 /* a care is needed when the destination address of the
1296 state is to be updated as it is a part of triplet */
1297 xfrm_state_insert(xc);
1298 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001299 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001300 goto error;
1301 }
1302
1303 return xc;
1304error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001305 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001306 return NULL;
1307}
1308EXPORT_SYMBOL(xfrm_state_migrate);
1309#endif
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311int xfrm_state_update(struct xfrm_state *x)
1312{
David S. Miller37b08e32008-09-02 20:14:15 -07001313 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001315 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001316 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
David S. Miller37b08e32008-09-02 20:14:15 -07001318 to_put = NULL;
1319
Fan Du283bc9f2013-11-07 17:47:50 +08001320 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001321 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 err = -ESRCH;
1324 if (!x1)
1325 goto out;
1326
1327 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001328 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 err = -EEXIST;
1330 goto out;
1331 }
1332
1333 if (x1->km.state == XFRM_STATE_ACQ) {
1334 __xfrm_state_insert(x);
1335 x = NULL;
1336 }
1337 err = 0;
1338
1339out:
Fan Du283bc9f2013-11-07 17:47:50 +08001340 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
David S. Miller37b08e32008-09-02 20:14:15 -07001342 if (to_put)
1343 xfrm_state_put(to_put);
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (err)
1346 return err;
1347
1348 if (!x) {
1349 xfrm_state_delete(x1);
1350 xfrm_state_put(x1);
1351 return 0;
1352 }
1353
1354 err = -EINVAL;
1355 spin_lock_bh(&x1->lock);
1356 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1357 if (x->encap && x1->encap)
1358 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001359 if (x->coaddr && x1->coaddr) {
1360 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1361 }
1362 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1363 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1365 x1->km.dying = 0;
1366
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001367 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (x1->curlft.use_time)
1369 xfrm_state_check_expire(x1);
1370
1371 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001372 x->km.state = XFRM_STATE_DEAD;
1373 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 spin_unlock_bh(&x1->lock);
1376
1377 xfrm_state_put(x1);
1378
1379 return err;
1380}
1381EXPORT_SYMBOL(xfrm_state_update);
1382
1383int xfrm_state_check_expire(struct xfrm_state *x)
1384{
1385 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001386 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1389 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001390 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001391 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EINVAL;
1393 }
1394
1395 if (!x->km.dying &&
1396 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001397 x->curlft.packets >= x->lft.soft_packet_limit)) {
1398 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001399 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return 0;
1402}
1403EXPORT_SYMBOL(xfrm_state_check_expire);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001406xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001407 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Fan Du283bc9f2013-11-07 17:47:50 +08001411 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001412 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001413 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return x;
1415}
1416EXPORT_SYMBOL(xfrm_state_lookup);
1417
1418struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001419xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001420 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001421 u8 proto, unsigned short family)
1422{
1423 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001424
Fan Du283bc9f2013-11-07 17:47:50 +08001425 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001426 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001427 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001428 return x;
1429}
1430EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1431
1432struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001433xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1434 u8 proto, const xfrm_address_t *daddr,
1435 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Fan Du283bc9f2013-11-07 17:47:50 +08001439 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001440 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001441 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return x;
1444}
1445EXPORT_SYMBOL(xfrm_find_acq);
1446
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001447#ifdef CONFIG_XFRM_SUB_POLICY
1448int
1449xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001450 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001451{
1452 int err = 0;
1453 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1454 if (!afinfo)
1455 return -EAFNOSUPPORT;
1456
Fan Du283bc9f2013-11-07 17:47:50 +08001457 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001458 if (afinfo->tmpl_sort)
1459 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001460 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001461 xfrm_state_put_afinfo(afinfo);
1462 return err;
1463}
1464EXPORT_SYMBOL(xfrm_tmpl_sort);
1465
1466int
1467xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1468 unsigned short family)
1469{
1470 int err = 0;
1471 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001472 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001473
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001474 if (!afinfo)
1475 return -EAFNOSUPPORT;
1476
Fan Du283bc9f2013-11-07 17:47:50 +08001477 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001478 if (afinfo->state_sort)
1479 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001480 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001481 xfrm_state_put_afinfo(afinfo);
1482 return err;
1483}
1484EXPORT_SYMBOL(xfrm_state_sort);
1485#endif
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/* Silly enough, but I'm lazy to build resolution list */
1488
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001489static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001493 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001494 struct xfrm_state *x;
1495
Sasha Levinb67bfe02013-02-27 17:06:00 -08001496 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001497 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001498 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001499 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 xfrm_state_hold(x);
1501 return x;
1502 }
1503 }
1504 }
1505 return NULL;
1506}
1507
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001508struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct xfrm_state *x;
1511
Fan Du283bc9f2013-11-07 17:47:50 +08001512 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001513 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001514 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return x;
1516}
1517EXPORT_SYMBOL(xfrm_find_acq_byseq);
1518
1519u32 xfrm_get_acqseq(void)
1520{
1521 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001522 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
jamal6836b9b2010-02-16 02:01:22 +00001524 do {
1525 res = atomic_inc_return(&acqseq);
1526 } while (!res);
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return res;
1529}
1530EXPORT_SYMBOL(xfrm_get_acqseq);
1531
Fan Du776e9dd2013-12-16 18:47:49 +08001532int verify_spi_info(u8 proto, u32 min, u32 max)
1533{
1534 switch (proto) {
1535 case IPPROTO_AH:
1536 case IPPROTO_ESP:
1537 break;
1538
1539 case IPPROTO_COMP:
1540 /* IPCOMP spi is 16-bits. */
1541 if (max >= 0x10000)
1542 return -EINVAL;
1543 break;
1544
1545 default:
1546 return -EINVAL;
1547 }
1548
1549 if (min > max)
1550 return -EINVAL;
1551
1552 return 0;
1553}
1554EXPORT_SYMBOL(verify_spi_info);
1555
Herbert Xu658b2192007-10-09 13:29:52 -07001556int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001558 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001559 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001561 int err = -ENOENT;
1562 __be32 minspi = htonl(low);
1563 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001564 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Herbert Xu658b2192007-10-09 13:29:52 -07001566 spin_lock_bh(&x->lock);
1567 if (x->km.state == XFRM_STATE_DEAD)
1568 goto unlock;
1569
1570 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001572 goto unlock;
1573
1574 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001577 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (x0) {
1579 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001580 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 x->id.spi = minspi;
1583 } else {
1584 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001585 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001586 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001587 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (x0 == NULL) {
1589 x->id.spi = htonl(spi);
1590 break;
1591 }
1592 xfrm_state_put(x0);
1593 }
1594 }
1595 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001596 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001597 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1598 hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
Fan Du283bc9f2013-11-07 17:47:50 +08001599 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001600
1601 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Herbert Xu658b2192007-10-09 13:29:52 -07001603
1604unlock:
1605 spin_unlock_bh(&x->lock);
1606
1607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609EXPORT_SYMBOL(xfrm_alloc_spi);
1610
Nicolas Dichteld3623092014-02-14 15:30:36 +01001611static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001612 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001613{
1614 if (filter) {
1615 if ((filter->family == AF_INET ||
1616 filter->family == AF_INET6) &&
1617 x->props.family != filter->family)
1618 return false;
1619
1620 return addr_match(&x->props.saddr, &filter->saddr,
1621 filter->splen) &&
1622 addr_match(&x->id.daddr, &filter->daddr,
1623 filter->dplen);
1624 }
1625 return true;
1626}
1627
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001628int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001629 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 void *data)
1631{
Herbert Xu12a169e2008-10-01 07:03:24 -07001632 struct xfrm_state *state;
1633 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int err = 0;
1635
Herbert Xu12a169e2008-10-01 07:03:24 -07001636 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001637 return 0;
1638
Fan Du283bc9f2013-11-07 17:47:50 +08001639 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001640 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001641 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001642 else
1643 x = list_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001644 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001645 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001646 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001647 state = container_of(x, struct xfrm_state, km);
1648 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001649 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001650 if (!__xfrm_state_filter_match(state, walk->filter))
1651 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001652 err = func(state, walk->seq, data);
1653 if (err) {
1654 list_move_tail(&walk->all, &x->all);
1655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001657 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001659 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 err = -ENOENT;
1661 goto out;
1662 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001663 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664out:
Fan Du283bc9f2013-11-07 17:47:50 +08001665 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return err;
1667}
1668EXPORT_SYMBOL(xfrm_state_walk);
1669
Nicolas Dichteld3623092014-02-14 15:30:36 +01001670void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001671 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001672{
Herbert Xu12a169e2008-10-01 07:03:24 -07001673 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001674 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001675 walk->state = XFRM_STATE_DEAD;
1676 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001677 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001678}
1679EXPORT_SYMBOL(xfrm_state_walk_init);
1680
Fan Du283bc9f2013-11-07 17:47:50 +08001681void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001682{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001683 kfree(walk->filter);
1684
Herbert Xu12a169e2008-10-01 07:03:24 -07001685 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001686 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001687
Fan Du283bc9f2013-11-07 17:47:50 +08001688 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001689 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001690 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001691}
1692EXPORT_SYMBOL(xfrm_state_walk_done);
1693
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001694static void xfrm_replay_timer_handler(unsigned long data)
1695{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001696 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001697
1698 spin_lock(&x->lock);
1699
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001700 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001701 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001702 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001703 else
1704 x->xflags |= XFRM_TIME_DEFER;
1705 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001706
1707 spin_unlock(&x->lock);
1708}
1709
Denis Chengdf018122007-12-07 00:51:11 -08001710static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
David S. Miller214e0052011-02-24 00:02:38 -05001712void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct xfrm_mgr *km;
1715
Cong Wang85168c02013-01-16 16:05:06 +08001716 rcu_read_lock();
1717 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718 if (km->notify_policy)
1719 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001720 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721}
1722
David S. Miller214e0052011-02-24 00:02:38 -05001723void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724{
1725 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001726 rcu_read_lock();
1727 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728 if (km->notify)
1729 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001730 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001731}
1732
1733EXPORT_SYMBOL(km_policy_notify);
1734EXPORT_SYMBOL(km_state_notify);
1735
Eric W. Biederman15e47302012-09-07 20:12:54 +00001736void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001737{
1738 struct km_event c;
1739
Herbert Xubf088672005-06-18 22:44:00 -07001740 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001741 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001742 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001743 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001746EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001747/*
1748 * We send to all registered managers regardless of failure
1749 * We are happy with one success
1750*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001751int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001753 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct xfrm_mgr *km;
1755
Cong Wang85168c02013-01-16 16:05:06 +08001756 rcu_read_lock();
1757 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001758 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001759 if (!acqret)
1760 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Cong Wang85168c02013-01-16 16:05:06 +08001762 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return err;
1764}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001765EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Al Viro5d36b182006-11-08 00:24:06 -08001767int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 int err = -EINVAL;
1770 struct xfrm_mgr *km;
1771
Cong Wang85168c02013-01-16 16:05:06 +08001772 rcu_read_lock();
1773 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (km->new_mapping)
1775 err = km->new_mapping(x, ipaddr, sport);
1776 if (!err)
1777 break;
1778 }
Cong Wang85168c02013-01-16 16:05:06 +08001779 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return err;
1781}
1782EXPORT_SYMBOL(km_new_mapping);
1783
Eric W. Biederman15e47302012-09-07 20:12:54 +00001784void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001786 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Herbert Xubf088672005-06-18 22:44:00 -07001788 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001789 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001790 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001791 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
David S. Millera70fcb02006-03-20 19:18:52 -08001793EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001795#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001796int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1797 const struct xfrm_migrate *m, int num_migrate,
1798 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001799{
1800 int err = -EINVAL;
1801 int ret;
1802 struct xfrm_mgr *km;
1803
Cong Wang85168c02013-01-16 16:05:06 +08001804 rcu_read_lock();
1805 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001806 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001807 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001808 if (!ret)
1809 err = ret;
1810 }
1811 }
Cong Wang85168c02013-01-16 16:05:06 +08001812 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001813 return err;
1814}
1815EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001816#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001817
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001818int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001819{
1820 int err = -EINVAL;
1821 int ret;
1822 struct xfrm_mgr *km;
1823
Cong Wang85168c02013-01-16 16:05:06 +08001824 rcu_read_lock();
1825 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001826 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001827 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001828 if (!ret)
1829 err = ret;
1830 }
1831 }
Cong Wang85168c02013-01-16 16:05:06 +08001832 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001833 return err;
1834}
1835EXPORT_SYMBOL(km_report);
1836
Horia Geanta0f245582014-02-12 16:20:06 +02001837bool km_is_alive(const struct km_event *c)
1838{
1839 struct xfrm_mgr *km;
1840 bool is_alive = false;
1841
1842 rcu_read_lock();
1843 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1844 if (km->is_alive && km->is_alive(c)) {
1845 is_alive = true;
1846 break;
1847 }
1848 }
1849 rcu_read_unlock();
1850
1851 return is_alive;
1852}
1853EXPORT_SYMBOL(km_is_alive);
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1856{
1857 int err;
1858 u8 *data;
1859 struct xfrm_mgr *km;
1860 struct xfrm_policy *pol = NULL;
1861
1862 if (optlen <= 0 || optlen > PAGE_SIZE)
1863 return -EMSGSIZE;
1864
1865 data = kmalloc(optlen, GFP_KERNEL);
1866 if (!data)
1867 return -ENOMEM;
1868
1869 err = -EFAULT;
1870 if (copy_from_user(data, optval, optlen))
1871 goto out;
1872
1873 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001874 rcu_read_lock();
1875 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001876 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 optlen, &err);
1878 if (err >= 0)
1879 break;
1880 }
Cong Wang85168c02013-01-16 16:05:06 +08001881 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (err >= 0) {
1884 xfrm_sk_policy_insert(sk, err, pol);
1885 xfrm_pol_put(pol);
1886 err = 0;
1887 }
1888
1889out:
1890 kfree(data);
1891 return err;
1892}
1893EXPORT_SYMBOL(xfrm_user_policy);
1894
Cong Wang85168c02013-01-16 16:05:06 +08001895static DEFINE_SPINLOCK(xfrm_km_lock);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897int xfrm_register_km(struct xfrm_mgr *km)
1898{
Cong Wang85168c02013-01-16 16:05:06 +08001899 spin_lock_bh(&xfrm_km_lock);
1900 list_add_tail_rcu(&km->list, &xfrm_km_list);
1901 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 0;
1903}
1904EXPORT_SYMBOL(xfrm_register_km);
1905
1906int xfrm_unregister_km(struct xfrm_mgr *km)
1907{
Cong Wang85168c02013-01-16 16:05:06 +08001908 spin_lock_bh(&xfrm_km_lock);
1909 list_del_rcu(&km->list);
1910 spin_unlock_bh(&xfrm_km_lock);
1911 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
1913}
1914EXPORT_SYMBOL(xfrm_unregister_km);
1915
1916int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1917{
1918 int err = 0;
1919 if (unlikely(afinfo == NULL))
1920 return -EINVAL;
1921 if (unlikely(afinfo->family >= NPROTO))
1922 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001923 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1925 err = -ENOBUFS;
David S. Milleredcd5822006-08-24 00:42:45 -07001926 else
Cong Wang44abdc32013-01-16 16:05:05 +08001927 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1928 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return err;
1930}
1931EXPORT_SYMBOL(xfrm_state_register_afinfo);
1932
1933int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1934{
1935 int err = 0;
1936 if (unlikely(afinfo == NULL))
1937 return -EINVAL;
1938 if (unlikely(afinfo->family >= NPROTO))
1939 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001940 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1942 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1943 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001944 else
Cong Wang44abdc32013-01-16 16:05:05 +08001945 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Cong Wang44abdc32013-01-16 16:05:05 +08001947 spin_unlock_bh(&xfrm_state_afinfo_lock);
1948 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return err;
1950}
1951EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1952
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001953struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 struct xfrm_state_afinfo *afinfo;
1956 if (unlikely(family >= NPROTO))
1957 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001958 rcu_read_lock();
1959 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001960 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001961 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return afinfo;
1963}
1964
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001965void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Cong Wang44abdc32013-01-16 16:05:05 +08001967 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1971void xfrm_state_delete_tunnel(struct xfrm_state *x)
1972{
1973 if (x->tunnel) {
1974 struct xfrm_state *t = x->tunnel;
1975
1976 if (atomic_read(&t->tunnel_users) == 2)
1977 xfrm_state_delete(t);
1978 atomic_dec(&t->tunnel_users);
1979 xfrm_state_put(t);
1980 x->tunnel = NULL;
1981 }
1982}
1983EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1984
1985int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1986{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001987 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Patrick McHardyc5c25232007-04-09 11:47:18 -07001989 spin_lock_bh(&x->lock);
1990 if (x->km.state == XFRM_STATE_VALID &&
1991 x->type && x->type->get_mtu)
1992 res = x->type->get_mtu(x, mtu);
1993 else
Patrick McHardy28121612007-06-18 22:30:15 -07001994 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001995 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return res;
1997}
1998
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001999int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07002000{
Herbert Xud094cd82005-06-20 13:19:41 -07002001 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002002 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002003 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002004 int err;
2005
Herbert Xud094cd82005-06-20 13:19:41 -07002006 err = -EAFNOSUPPORT;
2007 afinfo = xfrm_state_get_afinfo(family);
2008 if (!afinfo)
2009 goto error;
2010
2011 err = 0;
2012 if (afinfo->init_flags)
2013 err = afinfo->init_flags(x);
2014
2015 xfrm_state_put_afinfo(afinfo);
2016
2017 if (err)
2018 goto error;
2019
2020 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002021
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002022 if (x->sel.family != AF_UNSPEC) {
2023 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2024 if (inner_mode == NULL)
2025 goto error;
2026
2027 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2028 family != x->sel.family) {
2029 xfrm_put_mode(inner_mode);
2030 goto error;
2031 }
2032
2033 x->inner_mode = inner_mode;
2034 } else {
2035 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002036 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002037
Martin Willid81d2282008-12-03 15:38:07 -08002038 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002039 if (inner_mode == NULL)
2040 goto error;
2041
2042 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2043 xfrm_put_mode(inner_mode);
2044 goto error;
2045 }
Martin Willid81d2282008-12-03 15:38:07 -08002046 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002047
Martin Willid81d2282008-12-03 15:38:07 -08002048 if (x->props.family == AF_INET)
2049 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002050
Martin Willid81d2282008-12-03 15:38:07 -08002051 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2052 if (inner_mode_iaf) {
2053 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2054 x->inner_mode_iaf = inner_mode_iaf;
2055 else
2056 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002057 }
2058 }
Herbert Xu13996372007-10-17 21:35:51 -07002059
Herbert Xud094cd82005-06-20 13:19:41 -07002060 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002061 if (x->type == NULL)
2062 goto error;
2063
2064 err = x->type->init_state(x);
2065 if (err)
2066 goto error;
2067
Herbert Xu13996372007-10-17 21:35:51 -07002068 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002069 if (x->outer_mode == NULL) {
2070 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002071 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002072 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002073
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002074 if (init_replay) {
2075 err = xfrm_init_replay(x);
2076 if (err)
2077 goto error;
2078 }
2079
Herbert Xu72cb6962005-06-20 13:18:08 -07002080 x->km.state = XFRM_STATE_VALID;
2081
2082error:
2083 return err;
2084}
2085
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002086EXPORT_SYMBOL(__xfrm_init_state);
2087
2088int xfrm_init_state(struct xfrm_state *x)
2089{
2090 return __xfrm_init_state(x, true);
2091}
2092
Herbert Xu72cb6962005-06-20 13:18:08 -07002093EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002094
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002095int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
David S. Millerf034b5d2006-08-24 03:08:07 -07002097 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002099 INIT_LIST_HEAD(&net->xfrm.state_all);
2100
David S. Millerf034b5d2006-08-24 03:08:07 -07002101 sz = sizeof(struct hlist_head) * 8;
2102
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002103 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2104 if (!net->xfrm.state_bydst)
2105 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002106 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2107 if (!net->xfrm.state_bysrc)
2108 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002109 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2110 if (!net->xfrm.state_byspi)
2111 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002112 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002113
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002114 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002115 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002116 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002117 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Fan Du283bc9f2013-11-07 17:47:50 +08002118 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002119 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002120
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002121out_byspi:
2122 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002123out_bysrc:
2124 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002125out_bydst:
2126 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002127}
2128
2129void xfrm_state_fini(struct net *net)
2130{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002131 struct xfrm_audit audit_info;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002132 unsigned int sz;
2133
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002134 flush_work(&net->xfrm.state_hash_work);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002135 audit_info.loginuid = INVALID_UID;
Eric Paris4440e852013-11-27 17:35:17 -05002136 audit_info.sessionid = (unsigned int)-1;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002137 audit_info.secid = 0;
2138 xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
2139 flush_work(&net->xfrm.state_gc_work);
2140
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002141 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002142
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002143 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002144 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2145 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002146 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2147 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002148 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2149 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Joy Lattenab5f5e82007-09-17 11:51:22 -07002152#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002153static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2154 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002155{
Paul Moore68277ac2007-12-20 20:49:33 -08002156 struct xfrm_sec_ctx *ctx = x->security;
2157 u32 spi = ntohl(x->id.spi);
2158
2159 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002160 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002161 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002162
Weilong Chen9b7a7872013-12-24 09:43:46 +08002163 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002164 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002165 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2166 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002167 break;
2168 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002169 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002170 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002171 break;
2172 }
Paul Moore68277ac2007-12-20 20:49:33 -08002173
2174 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002175}
2176
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002177static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2178 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002179{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002180 const struct iphdr *iph4;
2181 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002182
2183 switch (family) {
2184 case AF_INET:
2185 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002186 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2187 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002188 break;
2189 case AF_INET6:
2190 iph6 = ipv6_hdr(skb);
2191 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002192 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002193 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002194 iph6->flow_lbl[0] & 0x0f,
2195 iph6->flow_lbl[1],
2196 iph6->flow_lbl[2]);
2197 break;
2198 }
2199}
2200
Paul Moore68277ac2007-12-20 20:49:33 -08002201void xfrm_audit_state_add(struct xfrm_state *x, int result,
Eric Paris4440e852013-11-27 17:35:17 -05002202 kuid_t auid, unsigned int sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002203{
2204 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002205
Paul Mooreafeb14b2007-12-21 14:58:11 -08002206 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002207 if (audit_buf == NULL)
2208 return;
Eric Paris25323862008-04-18 10:09:25 -04002209 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002210 xfrm_audit_helper_sainfo(x, audit_buf);
2211 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002212 audit_log_end(audit_buf);
2213}
2214EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2215
Paul Moore68277ac2007-12-20 20:49:33 -08002216void xfrm_audit_state_delete(struct xfrm_state *x, int result,
Eric Paris4440e852013-11-27 17:35:17 -05002217 kuid_t auid, unsigned int sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002218{
2219 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002220
Paul Mooreafeb14b2007-12-21 14:58:11 -08002221 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002222 if (audit_buf == NULL)
2223 return;
Eric Paris25323862008-04-18 10:09:25 -04002224 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002225 xfrm_audit_helper_sainfo(x, audit_buf);
2226 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002227 audit_log_end(audit_buf);
2228}
2229EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002230
2231void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2232 struct sk_buff *skb)
2233{
2234 struct audit_buffer *audit_buf;
2235 u32 spi;
2236
2237 audit_buf = xfrm_audit_start("SA-replay-overflow");
2238 if (audit_buf == NULL)
2239 return;
2240 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2241 /* don't record the sequence number because it's inherent in this kind
2242 * of audit message */
2243 spi = ntohl(x->id.spi);
2244 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2245 audit_log_end(audit_buf);
2246}
2247EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2248
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002249void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002250 struct sk_buff *skb, __be32 net_seq)
2251{
2252 struct audit_buffer *audit_buf;
2253 u32 spi;
2254
2255 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2256 if (audit_buf == NULL)
2257 return;
2258 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2259 spi = ntohl(x->id.spi);
2260 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2261 spi, spi, ntohl(net_seq));
2262 audit_log_end(audit_buf);
2263}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002264EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002265
2266void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2267{
2268 struct audit_buffer *audit_buf;
2269
2270 audit_buf = xfrm_audit_start("SA-notfound");
2271 if (audit_buf == NULL)
2272 return;
2273 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2274 audit_log_end(audit_buf);
2275}
2276EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2277
2278void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2279 __be32 net_spi, __be32 net_seq)
2280{
2281 struct audit_buffer *audit_buf;
2282 u32 spi;
2283
2284 audit_buf = xfrm_audit_start("SA-notfound");
2285 if (audit_buf == NULL)
2286 return;
2287 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2288 spi = ntohl(net_spi);
2289 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2290 spi, spi, ntohl(net_seq));
2291 audit_log_end(audit_buf);
2292}
2293EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2294
2295void xfrm_audit_state_icvfail(struct xfrm_state *x,
2296 struct sk_buff *skb, u8 proto)
2297{
2298 struct audit_buffer *audit_buf;
2299 __be32 net_spi;
2300 __be32 net_seq;
2301
2302 audit_buf = xfrm_audit_start("SA-icv-failure");
2303 if (audit_buf == NULL)
2304 return;
2305 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2306 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2307 u32 spi = ntohl(net_spi);
2308 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2309 spi, spi, ntohl(net_seq));
2310 }
2311 audit_log_end(audit_buf);
2312}
2313EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002314#endif /* CONFIG_AUDITSYSCALL */