blob: 84c1db6254d5a6d4c15089202c21cb36ec08e43b [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
Florian Westphal02efdff2016-08-09 12:16:05 +020040static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
41{
42 return atomic_inc_not_zero(&x->refcnt);
43}
44
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080045static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050046 const xfrm_address_t *daddr,
47 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070048 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070049 unsigned short family)
50{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080051 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070052}
53
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050055 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070057 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070058{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080059 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070060}
61
David S. Miller2575b652006-08-24 03:26:44 -070062static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050063xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
64 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070065{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080066 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070067}
68
David S. Millerf034b5d2006-08-24 03:08:07 -070069static void xfrm_hash_transfer(struct hlist_head *list,
70 struct hlist_head *ndsttable,
71 struct hlist_head *nsrctable,
72 struct hlist_head *nspitable,
73 unsigned int nhashmask)
74{
Sasha Levinb67bfe02013-02-27 17:06:00 -080075 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070076 struct xfrm_state *x;
77
Sasha Levinb67bfe02013-02-27 17:06:00 -080078 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070079 unsigned int h;
80
David S. Millerc1969f22006-08-24 04:00:03 -070081 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
82 x->props.reqid, x->props.family,
83 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020084 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070085
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070086 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
87 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070088 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020089 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070090
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070091 if (x->id.spi) {
92 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
93 x->id.proto, x->props.family,
94 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020095 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -070096 }
David S. Millerf034b5d2006-08-24 03:08:07 -070097 }
98}
99
Alexey Dobriyan63082732008-11-25 17:19:07 -0800100static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700101{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800102 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700103}
104
Alexey Dobriyan63082732008-11-25 17:19:07 -0800105static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700106{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800107 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700108 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
109 unsigned long nsize, osize;
110 unsigned int nhashmask, ohashmask;
111 int i;
112
Alexey Dobriyan63082732008-11-25 17:19:07 -0800113 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700114 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700115 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800116 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700117 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700118 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700119 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800120 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700121 }
David S. Miller44e36b42006-08-24 04:50:50 -0700122 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700123 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700124 xfrm_hash_free(ndst, nsize);
125 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800126 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 }
128
Fan Du283bc9f2013-11-07 17:47:50 +0800129 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700130
131 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Alexey Dobriyan63082732008-11-25 17:19:07 -0800132 for (i = net->xfrm.state_hmask; i >= 0; i--)
133 xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
David S. Millerf034b5d2006-08-24 03:08:07 -0700134 nhashmask);
135
Alexey Dobriyan63082732008-11-25 17:19:07 -0800136 odst = net->xfrm.state_bydst;
137 osrc = net->xfrm.state_bysrc;
138 ospi = net->xfrm.state_byspi;
139 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700140
Alexey Dobriyan63082732008-11-25 17:19:07 -0800141 net->xfrm.state_bydst = ndst;
142 net->xfrm.state_bysrc = nsrc;
143 net->xfrm.state_byspi = nspi;
144 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700145
Fan Du283bc9f2013-11-07 17:47:50 +0800146 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700147
148 osize = (ohashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700149 xfrm_hash_free(odst, osize);
150 xfrm_hash_free(osrc, osize);
151 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700152}
153
Cong Wang44abdc32013-01-16 16:05:05 +0800154static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
155static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static DEFINE_SPINLOCK(xfrm_state_gc_lock);
158
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800159int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800161int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200162bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000163void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Cong Wang7a9885b2013-01-17 16:34:11 +0800165static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800166int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700167{
Cong Wang7a9885b2013-01-17 16:34:11 +0800168 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800169 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700170 int err = 0;
171
172 if (unlikely(afinfo == NULL))
173 return -EAFNOSUPPORT;
174 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800175 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700176
177 if (likely(typemap[type->proto] == NULL))
178 typemap[type->proto] = type;
179 else
180 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800181 spin_unlock_bh(&xfrm_type_lock);
182 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700183 return err;
184}
185EXPORT_SYMBOL(xfrm_register_type);
186
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800187int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700188{
Cong Wang7a9885b2013-01-17 16:34:11 +0800189 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800190 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700191 int err = 0;
192
193 if (unlikely(afinfo == NULL))
194 return -EAFNOSUPPORT;
195 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800196 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700197
198 if (unlikely(typemap[type->proto] != type))
199 err = -ENOENT;
200 else
201 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800202 spin_unlock_bh(&xfrm_type_lock);
203 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700204 return err;
205}
206EXPORT_SYMBOL(xfrm_unregister_type);
207
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800208static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700209{
210 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800211 const struct xfrm_type **typemap;
212 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700213 int modload_attempted = 0;
214
215retry:
216 afinfo = xfrm_state_get_afinfo(family);
217 if (unlikely(afinfo == NULL))
218 return NULL;
219 typemap = afinfo->type_map;
220
221 type = typemap[proto];
222 if (unlikely(type && !try_module_get(type->owner)))
223 type = NULL;
224 if (!type && !modload_attempted) {
225 xfrm_state_put_afinfo(afinfo);
226 request_module("xfrm-type-%d-%d", family, proto);
227 modload_attempted = 1;
228 goto retry;
229 }
230
231 xfrm_state_put_afinfo(afinfo);
232 return type;
233}
234
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800235static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700236{
237 module_put(type->owner);
238}
239
Cong Wang7a9885b2013-01-17 16:34:11 +0800240static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700241int xfrm_register_mode(struct xfrm_mode *mode, int family)
242{
243 struct xfrm_state_afinfo *afinfo;
244 struct xfrm_mode **modemap;
245 int err;
246
247 if (unlikely(mode->encap >= XFRM_MODE_MAX))
248 return -EINVAL;
249
Cong Wang7a9885b2013-01-17 16:34:11 +0800250 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700251 if (unlikely(afinfo == NULL))
252 return -EAFNOSUPPORT;
253
254 err = -EEXIST;
255 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800256 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700257 if (modemap[mode->encap])
258 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700259
Herbert Xu17c2a422007-10-17 21:33:12 -0700260 err = -ENOENT;
261 if (!try_module_get(afinfo->owner))
262 goto out;
263
264 mode->afinfo = afinfo;
265 modemap[mode->encap] = mode;
266 err = 0;
267
268out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800269 spin_unlock_bh(&xfrm_mode_lock);
270 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700271 return err;
272}
273EXPORT_SYMBOL(xfrm_register_mode);
274
275int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
276{
277 struct xfrm_state_afinfo *afinfo;
278 struct xfrm_mode **modemap;
279 int err;
280
281 if (unlikely(mode->encap >= XFRM_MODE_MAX))
282 return -EINVAL;
283
Cong Wang7a9885b2013-01-17 16:34:11 +0800284 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700285 if (unlikely(afinfo == NULL))
286 return -EAFNOSUPPORT;
287
288 err = -ENOENT;
289 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800290 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700291 if (likely(modemap[mode->encap] == mode)) {
292 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700293 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700294 err = 0;
295 }
296
Cong Wang7a9885b2013-01-17 16:34:11 +0800297 spin_unlock_bh(&xfrm_mode_lock);
298 xfrm_state_put_afinfo(afinfo);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700299 return err;
300}
301EXPORT_SYMBOL(xfrm_unregister_mode);
302
303static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
304{
305 struct xfrm_state_afinfo *afinfo;
306 struct xfrm_mode *mode;
307 int modload_attempted = 0;
308
309 if (unlikely(encap >= XFRM_MODE_MAX))
310 return NULL;
311
312retry:
313 afinfo = xfrm_state_get_afinfo(family);
314 if (unlikely(afinfo == NULL))
315 return NULL;
316
317 mode = afinfo->mode_map[encap];
318 if (unlikely(mode && !try_module_get(mode->owner)))
319 mode = NULL;
320 if (!mode && !modload_attempted) {
321 xfrm_state_put_afinfo(afinfo);
322 request_module("xfrm-mode-%d-%d", family, encap);
323 modload_attempted = 1;
324 goto retry;
325 }
326
327 xfrm_state_put_afinfo(afinfo);
328 return mode;
329}
330
331static void xfrm_put_mode(struct xfrm_mode *mode)
332{
333 module_put(mode->owner);
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static void xfrm_state_gc_destroy(struct xfrm_state *x)
337{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800338 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700339 del_timer_sync(&x->rtimer);
Jesper Juhla51482b2005-11-08 09:41:34 -0800340 kfree(x->aalg);
341 kfree(x->ealg);
342 kfree(x->calg);
343 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700344 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000345 kfree(x->replay_esn);
346 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700347 if (x->inner_mode)
348 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700349 if (x->inner_mode_iaf)
350 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700351 if (x->outer_mode)
352 xfrm_put_mode(x->outer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (x->type) {
354 x->type->destructor(x);
355 xfrm_put_type(x->type);
356 }
Trent Jaegerdf718372005-12-13 23:12:27 -0800357 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 kfree(x);
359}
360
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800361static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800363 struct net *net = container_of(work, struct net, xfrm.state_gc_work);
Herbert Xu12a169e2008-10-01 07:03:24 -0700364 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800365 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700366 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800369 hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock_bh(&xfrm_state_gc_lock);
371
Sasha Levinb67bfe02013-02-27 17:06:00 -0800372 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376static inline unsigned long make_jiffies(long secs)
377{
378 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
379 return MAX_SCHEDULE_TIMEOUT-1;
380 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900381 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800384static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800386 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
387 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800388 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 long next = LONG_MAX;
390 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600391 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 spin_lock(&x->lock);
394 if (x->km.state == XFRM_STATE_DEAD)
395 goto out;
396 if (x->km.state == XFRM_STATE_EXPIRED)
397 goto expired;
398 if (x->lft.hard_add_expires_seconds) {
399 long tmo = x->lft.hard_add_expires_seconds +
400 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000401 if (tmo <= 0) {
402 if (x->xflags & XFRM_SOFT_EXPIRE) {
403 /* enter hard expire without soft expire first?!
404 * setting a new date could trigger this.
405 * workarbound: fix x->curflt.add_time by below:
406 */
407 x->curlft.add_time = now - x->saved_tmo - 1;
408 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
409 } else
410 goto expired;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (tmo < next)
413 next = tmo;
414 }
415 if (x->lft.hard_use_expires_seconds) {
416 long tmo = x->lft.hard_use_expires_seconds +
417 (x->curlft.use_time ? : now) - now;
418 if (tmo <= 0)
419 goto expired;
420 if (tmo < next)
421 next = tmo;
422 }
423 if (x->km.dying)
424 goto resched;
425 if (x->lft.soft_add_expires_seconds) {
426 long tmo = x->lft.soft_add_expires_seconds +
427 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000428 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000430 x->xflags &= ~XFRM_SOFT_EXPIRE;
431 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000433 x->xflags |= XFRM_SOFT_EXPIRE;
434 x->saved_tmo = tmo;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 if (x->lft.soft_use_expires_seconds) {
438 long tmo = x->lft.soft_use_expires_seconds +
439 (x->curlft.use_time ? : now) - now;
440 if (tmo <= 0)
441 warn = 1;
442 else if (tmo < next)
443 next = tmo;
444 }
445
Herbert Xu4666faa2005-06-18 22:43:22 -0700446 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800448 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800450 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800451 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
452 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto out;
455
456expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200457 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600459
460 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200461 if (!err)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800462 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Tetsuo Handa2e710292014-04-22 21:48:30 +0900464 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466out:
467 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800468 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
David S. Miller0ac84752006-03-20 19:18:23 -0800471static void xfrm_replay_timer_handler(unsigned long data);
472
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800473struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct xfrm_state *x;
476
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700477 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800480 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 atomic_set(&x->refcnt, 1);
482 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700483 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700484 INIT_HLIST_NODE(&x->bydst);
485 INIT_HLIST_NODE(&x->bysrc);
486 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800487 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
488 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800489 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
490 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800491 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 x->lft.soft_byte_limit = XFRM_INF;
493 x->lft.soft_packet_limit = XFRM_INF;
494 x->lft.hard_byte_limit = XFRM_INF;
495 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800496 x->replay_maxage = 0;
497 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700498 x->inner_mode = NULL;
499 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spin_lock_init(&x->lock);
501 }
502 return x;
503}
504EXPORT_SYMBOL(xfrm_state_alloc);
505
506void __xfrm_state_destroy(struct xfrm_state *x)
507{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800508 struct net *net = xs_net(x);
509
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700510 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 spin_lock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800513 hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_unlock_bh(&xfrm_state_gc_lock);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800515 schedule_work(&net->xfrm.state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517EXPORT_SYMBOL(__xfrm_state_destroy);
518
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800519int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800521 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700522 int err = -ESRCH;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (x->km.state != XFRM_STATE_DEAD) {
525 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800526 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700527 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200528 hlist_del_rcu(&x->bydst);
529 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700530 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200531 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800532 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800533 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* All xfrm_state objects are created by xfrm_state_alloc.
536 * The xfrm_state_alloc call gives a reference, and that
537 * is what we are dropping here.
538 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800539 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700540 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700542
543 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800545EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700547int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700549 int err;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700552 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554
555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557EXPORT_SYMBOL(xfrm_state_delete);
558
Joy Latten4aa2e622007-06-04 19:05:57 -0400559#ifdef CONFIG_SECURITY_NETWORK_XFRM
560static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900561xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Joy Latten4aa2e622007-06-04 19:05:57 -0400563 int i, err = 0;
564
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800565 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400566 struct xfrm_state *x;
567
Sasha Levinb67bfe02013-02-27 17:06:00 -0800568 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400569 if (xfrm_id_proto_match(x->id.proto, proto) &&
570 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900571 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400572 return err;
573 }
574 }
575 }
576
577 return err;
578}
579#else
580static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900581xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400582{
583 return 0;
584}
585#endif
586
Tetsuo Handa2e710292014-04-22 21:48:30 +0900587int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400588{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000589 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Fan Du283bc9f2013-11-07 17:47:50 +0800591 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900592 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400593 if (err)
594 goto out;
595
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000596 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800597 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700598 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800600 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700602 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800604 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Joy Latten161a09e2006-11-27 13:11:54 -0600606 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700607 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900608 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000610 if (!err)
611 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Fan Du283bc9f2013-11-07 17:47:50 +0800613 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto restart;
615 }
616 }
617 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000618 if (cnt)
619 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400620
621out:
Fan Du283bc9f2013-11-07 17:47:50 +0800622 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625EXPORT_SYMBOL(xfrm_state_flush);
626
Alexey Dobriyane0710412010-01-23 13:37:10 +0000627void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700628{
Fan Du283bc9f2013-11-07 17:47:50 +0800629 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000630 si->sadcnt = net->xfrm.state_num;
631 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700632 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800633 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700634}
635EXPORT_SYMBOL(xfrm_sad_getinfo);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static int
David S. Miller1a898592011-02-22 18:22:34 -0800638xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500639 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500640 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700641 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
644 if (!afinfo)
645 return -1;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700646 afinfo->init_tempsel(&x->sel, fl);
647
648 if (family != tmpl->encap_family) {
649 xfrm_state_put_afinfo(afinfo);
650 afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
651 if (!afinfo)
652 return -1;
653 }
654 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 xfrm_state_put_afinfo(afinfo);
656 return 0;
657}
658
David S. Miller9aa60082011-02-24 01:51:36 -0500659static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
660 const xfrm_address_t *daddr,
661 __be32 spi, u8 proto,
662 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700663{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800664 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700665 struct xfrm_state *x;
666
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200667 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700668 if (x->props.family != family ||
669 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000670 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000671 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700672 continue;
673
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000674 if ((mark & x->mark.m) != x->mark.v)
675 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200676 if (!xfrm_state_hold_rcu(x))
677 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700678 return x;
679 }
680
681 return NULL;
682}
683
David S. Miller9aa60082011-02-24 01:51:36 -0500684static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
685 const xfrm_address_t *daddr,
686 const xfrm_address_t *saddr,
687 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700688{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800689 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700690 struct xfrm_state *x;
691
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200692 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700693 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000694 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000695 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
696 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700697 continue;
698
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000699 if ((mark & x->mark.m) != x->mark.v)
700 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200701 if (!xfrm_state_hold_rcu(x))
702 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700703 return x;
704 }
705
706 return NULL;
707}
708
709static inline struct xfrm_state *
710__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
711{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800712 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800713 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800714
David S. Milleredcd5822006-08-24 00:42:45 -0700715 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800716 return __xfrm_state_lookup(net, mark, &x->id.daddr,
717 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700718 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800719 return __xfrm_state_lookup_byaddr(net, mark,
720 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700721 &x->props.saddr,
722 x->id.proto, family);
723}
724
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800725static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700726{
727 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800728 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
729 net->xfrm.state_num > net->xfrm.state_hmask)
730 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700731}
732
David S. Miller08ec9af2009-03-13 14:22:40 -0700733static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800734 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700735 struct xfrm_state **best, int *acq_in_progress,
736 int *error)
737{
738 /* Resolution logic:
739 * 1. There is a valid state with matching selector. Done.
740 * 2. Valid state with inappropriate selector. Skip.
741 *
742 * Entering area of "sysdeps".
743 *
744 * 3. If state is not valid, selector is temporary, it selects
745 * only session which triggered previous resolution. Key
746 * manager will do something to install a state with proper
747 * selector.
748 */
749 if (x->km.state == XFRM_STATE_VALID) {
750 if ((x->sel.family &&
751 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
752 !security_xfrm_state_pol_flow_match(x, pol, fl))
753 return;
754
755 if (!*best ||
756 (*best)->km.dying > x->km.dying ||
757 ((*best)->km.dying == x->km.dying &&
758 (*best)->curlft.add_time < x->curlft.add_time))
759 *best = x;
760 } else if (x->km.state == XFRM_STATE_ACQ) {
761 *acq_in_progress = 1;
762 } else if (x->km.state == XFRM_STATE_ERROR ||
763 x->km.state == XFRM_STATE_EXPIRED) {
764 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
765 security_xfrm_state_pol_flow_match(x, pol, fl))
766 *error = -ESRCH;
767 }
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500771xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800772 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct xfrm_policy *pol, int *err,
774 unsigned short family)
775{
David S. Miller08ec9af2009-03-13 14:22:40 -0700776 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800777 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700778 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700779 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int acquire_in_progress = 0;
781 int error = 0;
782 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800783 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700784 unsigned short encap_family = tmpl->encap_family;
Horia Geanta0f245582014-02-12 16:20:06 +0200785 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900786
David S. Miller37b08e32008-09-02 20:14:15 -0700787 to_put = NULL;
788
Fan Du283bc9f2013-11-07 17:47:50 +0800789 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700790 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200791 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700792 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000794 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700795 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700796 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 tmpl->mode == x->props.mode &&
798 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700799 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500800 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700801 &best, &acquire_in_progress, &error);
802 }
Fan Du6f115632013-09-23 17:18:25 +0800803 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700804 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Thomas Egerer8444cf72010-09-20 11:11:38 -0700806 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200807 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700808 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700809 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000810 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700811 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800812 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700813 tmpl->mode == x->props.mode &&
814 tmpl->id.proto == x->id.proto &&
815 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500816 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700817 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
David S. Miller08ec9af2009-03-13 14:22:40 -0700820found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 x = best;
822 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700823 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800824 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700825 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700826 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 error = -EEXIST;
828 goto out;
829 }
Horia Geanta0f245582014-02-12 16:20:06 +0200830
831 c.net = net;
832 /* If the KMs have no listeners (yet...), avoid allocating an SA
833 * for each and every packet - garbage collection might not
834 * handle the flood.
835 */
836 if (!km_is_alive(&c)) {
837 error = -ESRCH;
838 goto out;
839 }
840
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800841 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (x == NULL) {
843 error = -ENOMEM;
844 goto out;
845 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700846 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700848 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800849 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
David S. Miller1d28f422011-03-12 00:29:39 -0500851 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700852 if (error) {
853 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700854 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700855 x = NULL;
856 goto out;
857 }
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (km_query(x, tmpl, pol) == 0) {
860 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800861 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200862 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700863 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200864 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700866 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200867 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800869 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800870 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800871 net->xfrm.state_num++;
872 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
874 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700875 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 x = NULL;
877 error = -ESRCH;
878 }
879 }
880out:
Florian Westphal02efdff2016-08-09 12:16:05 +0200881 if (x) {
882 if (!xfrm_state_hold_rcu(x)) {
883 *err = -EAGAIN;
884 x = NULL;
885 }
886 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +0200888 }
Fan Du283bc9f2013-11-07 17:47:50 +0800889 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller37b08e32008-09-02 20:14:15 -0700890 if (to_put)
891 xfrm_state_put(to_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return x;
893}
894
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700895struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800896xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800897 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700898 unsigned short family, u8 mode, u8 proto, u32 reqid)
899{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -0800900 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700901 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700902
Fan Du4ae770b2014-01-03 11:18:29 +0800903 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800904 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800905 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700906 if (x->props.family == family &&
907 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000908 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700909 !(x->props.flags & XFRM_STATE_WILDRECV) &&
910 xfrm_state_addr_check(x, daddr, saddr, family) &&
911 mode == x->props.mode &&
912 proto == x->id.proto &&
913 x->km.state == XFRM_STATE_VALID) {
914 rx = x;
915 break;
916 }
917 }
918
919 if (rx)
920 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +0800921 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -0700922
923
924 return rx;
925}
926EXPORT_SYMBOL(xfrm_stateonly_find);
927
Fan Duc4549972014-01-03 11:18:32 +0800928struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
929 unsigned short family)
930{
931 struct xfrm_state *x;
932 struct xfrm_state_walk *w;
933
934 spin_lock_bh(&net->xfrm.xfrm_state_lock);
935 list_for_each_entry(w, &net->xfrm.state_all, all) {
936 x = container_of(w, struct xfrm_state, km);
937 if (x->props.family != family ||
938 x->id.spi != spi)
939 continue;
940
Fan Duc4549972014-01-03 11:18:32 +0800941 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +0800942 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +0800943 return x;
944 }
945 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
946 return NULL;
947}
948EXPORT_SYMBOL(xfrm_state_lookup_byspi);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static void __xfrm_state_insert(struct xfrm_state *x)
951{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800952 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -0700953 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800955 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -0800956
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800957 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -0700958 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200959 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800961 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200962 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700964 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800965 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700966 x->props.family);
967
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200968 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -0700969 }
970
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800971 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -0700972 if (x->replay_maxage)
973 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800974
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800975 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -0700976
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800977 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Fan Du283bc9f2013-11-07 17:47:50 +0800980/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -0700981static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
982{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800983 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -0700984 unsigned short family = xnew->props.family;
985 u32 reqid = xnew->props.reqid;
986 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700987 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000988 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700989
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800990 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800991 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -0700992 if (x->props.family == family &&
993 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000994 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000995 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
996 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +0000997 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -0700998 }
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001void xfrm_state_insert(struct xfrm_state *x)
1002{
Fan Du283bc9f2013-11-07 17:47:50 +08001003 struct net *net = xs_net(x);
1004
1005 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001006 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001008 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010EXPORT_SYMBOL(xfrm_state_insert);
1011
Fan Du283bc9f2013-11-07 17:47:50 +08001012/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001013static struct xfrm_state *__find_acq_core(struct net *net,
1014 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001015 unsigned short family, u8 mode,
1016 u32 reqid, u8 proto,
1017 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001018 const xfrm_address_t *saddr,
1019 int create)
David S. Miller27708342006-08-24 00:13:10 -07001020{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001021 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001022 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001023 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001024
Sasha Levinb67bfe02013-02-27 17:06:00 -08001025 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001026 if (x->props.reqid != reqid ||
1027 x->props.mode != mode ||
1028 x->props.family != family ||
1029 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001030 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001031 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001032 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001033 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1034 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001035 continue;
1036
David S. Miller27708342006-08-24 00:13:10 -07001037 xfrm_state_hold(x);
1038 return x;
1039 }
1040
1041 if (!create)
1042 return NULL;
1043
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001044 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001045 if (likely(x)) {
1046 switch (family) {
1047 case AF_INET:
1048 x->sel.daddr.a4 = daddr->a4;
1049 x->sel.saddr.a4 = saddr->a4;
1050 x->sel.prefixlen_d = 32;
1051 x->sel.prefixlen_s = 32;
1052 x->props.saddr.a4 = saddr->a4;
1053 x->id.daddr.a4 = daddr->a4;
1054 break;
1055
1056 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001057 x->sel.daddr.in6 = daddr->in6;
1058 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001059 x->sel.prefixlen_d = 128;
1060 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001061 x->props.saddr.in6 = saddr->in6;
1062 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001063 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001064 }
David S. Miller27708342006-08-24 00:13:10 -07001065
1066 x->km.state = XFRM_STATE_ACQ;
1067 x->id.proto = proto;
1068 x->props.family = family;
1069 x->props.mode = mode;
1070 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001071 x->mark.v = m->v;
1072 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001073 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001074 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001075 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001076 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001077 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001078 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001079 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001080
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001081 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001082
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001083 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001084 }
1085
1086 return x;
1087}
1088
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001089static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091int xfrm_state_add(struct xfrm_state *x)
1092{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001093 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001094 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 int family;
1096 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001097 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001098 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
David S. Miller37b08e32008-09-02 20:14:15 -07001102 to_put = NULL;
1103
Fan Du283bc9f2013-11-07 17:47:50 +08001104 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
David S. Milleredcd5822006-08-24 00:42:45 -07001106 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001108 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 x1 = NULL;
1110 err = -EEXIST;
1111 goto out;
1112 }
1113
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001114 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001115 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001116 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001117 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001118 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 x1 = NULL;
1120 }
1121 }
1122
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001123 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001124 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1125 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001126 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
David S. Millerc7f5ea32006-08-24 03:29:04 -07001128 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 __xfrm_state_insert(x);
1130 err = 0;
1131
1132out:
Fan Du283bc9f2013-11-07 17:47:50 +08001133 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (x1) {
1136 xfrm_state_delete(x1);
1137 xfrm_state_put(x1);
1138 }
1139
David S. Miller37b08e32008-09-02 20:14:15 -07001140 if (to_put)
1141 xfrm_state_put(to_put);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return err;
1144}
1145EXPORT_SYMBOL(xfrm_state_add);
1146
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001147#ifdef CONFIG_XFRM_MIGRATE
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001148static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001149{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001150 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001151 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001152 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001153 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001154
1155 memcpy(&x->id, &orig->id, sizeof(x->id));
1156 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1157 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1158 x->props.mode = orig->props.mode;
1159 x->props.replay_window = orig->props.replay_window;
1160 x->props.reqid = orig->props.reqid;
1161 x->props.family = orig->props.family;
1162 x->props.saddr = orig->props.saddr;
1163
1164 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001165 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001166 if (!x->aalg)
1167 goto error;
1168 }
1169 x->props.aalgo = orig->props.aalgo;
1170
Steffen Klassertee5c2312014-02-19 13:33:24 +01001171 if (orig->aead) {
1172 x->aead = xfrm_algo_aead_clone(orig->aead);
1173 if (!x->aead)
1174 goto error;
1175 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001176 if (orig->ealg) {
1177 x->ealg = xfrm_algo_clone(orig->ealg);
1178 if (!x->ealg)
1179 goto error;
1180 }
1181 x->props.ealgo = orig->props.ealgo;
1182
1183 if (orig->calg) {
1184 x->calg = xfrm_algo_clone(orig->calg);
1185 if (!x->calg)
1186 goto error;
1187 }
1188 x->props.calgo = orig->props.calgo;
1189
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001190 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001191 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1192 if (!x->encap)
1193 goto error;
1194 }
1195
1196 if (orig->coaddr) {
1197 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1198 GFP_KERNEL);
1199 if (!x->coaddr)
1200 goto error;
1201 }
1202
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001203 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001204 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001205 goto error;
1206 }
1207
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001208 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1209
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001210 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001211 goto error;
1212
1213 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001214 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001215
Steffen Klassertee5c2312014-02-19 13:33:24 +01001216 x->tfcpad = orig->tfcpad;
1217 x->replay_maxdiff = orig->replay_maxdiff;
1218 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001219 x->curlft.add_time = orig->curlft.add_time;
1220 x->km.state = orig->km.state;
1221 x->km.seq = orig->km.seq;
1222
1223 return x;
1224
1225 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001226 xfrm_state_put(x);
1227out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001228 return NULL;
1229}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001230
Fan Du283bc9f2013-11-07 17:47:50 +08001231struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001232{
1233 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001234 struct xfrm_state *x = NULL;
1235
1236 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001237
1238 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001239 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001240 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001241 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001242 if (x->props.mode != m->mode ||
1243 x->id.proto != m->proto)
1244 continue;
1245 if (m->reqid && x->props.reqid != m->reqid)
1246 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001247 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1248 m->old_family) ||
1249 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1250 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001251 continue;
1252 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001253 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001254 }
1255 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001256 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001257 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001258 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001259 if (x->props.mode != m->mode ||
1260 x->id.proto != m->proto)
1261 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001262 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1263 m->old_family) ||
1264 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1265 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001266 continue;
1267 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001268 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001269 }
1270 }
1271
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001272 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1273
1274 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001275}
1276EXPORT_SYMBOL(xfrm_migrate_state_find);
1277
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001278struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1279 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001280{
1281 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001282
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001283 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001284 if (!xc)
1285 return NULL;
1286
1287 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1288 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1289
1290 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001291 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001292 /* a care is needed when the destination address of the
1293 state is to be updated as it is a part of triplet */
1294 xfrm_state_insert(xc);
1295 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001296 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001297 goto error;
1298 }
1299
1300 return xc;
1301error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001302 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001303 return NULL;
1304}
1305EXPORT_SYMBOL(xfrm_state_migrate);
1306#endif
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308int xfrm_state_update(struct xfrm_state *x)
1309{
David S. Miller37b08e32008-09-02 20:14:15 -07001310 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001312 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001313 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
David S. Miller37b08e32008-09-02 20:14:15 -07001315 to_put = NULL;
1316
Fan Du283bc9f2013-11-07 17:47:50 +08001317 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001318 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 err = -ESRCH;
1321 if (!x1)
1322 goto out;
1323
1324 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001325 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 err = -EEXIST;
1327 goto out;
1328 }
1329
1330 if (x1->km.state == XFRM_STATE_ACQ) {
1331 __xfrm_state_insert(x);
1332 x = NULL;
1333 }
1334 err = 0;
1335
1336out:
Fan Du283bc9f2013-11-07 17:47:50 +08001337 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
David S. Miller37b08e32008-09-02 20:14:15 -07001339 if (to_put)
1340 xfrm_state_put(to_put);
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (err)
1343 return err;
1344
1345 if (!x) {
1346 xfrm_state_delete(x1);
1347 xfrm_state_put(x1);
1348 return 0;
1349 }
1350
1351 err = -EINVAL;
1352 spin_lock_bh(&x1->lock);
1353 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1354 if (x->encap && x1->encap)
1355 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001356 if (x->coaddr && x1->coaddr) {
1357 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1358 }
1359 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1360 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1362 x1->km.dying = 0;
1363
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001364 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (x1->curlft.use_time)
1366 xfrm_state_check_expire(x1);
1367
1368 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001369 x->km.state = XFRM_STATE_DEAD;
1370 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 spin_unlock_bh(&x1->lock);
1373
1374 xfrm_state_put(x1);
1375
1376 return err;
1377}
1378EXPORT_SYMBOL(xfrm_state_update);
1379
1380int xfrm_state_check_expire(struct xfrm_state *x)
1381{
1382 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001383 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1386 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001387 x->km.state = XFRM_STATE_EXPIRED;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001388 tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return -EINVAL;
1390 }
1391
1392 if (!x->km.dying &&
1393 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001394 x->curlft.packets >= x->lft.soft_packet_limit)) {
1395 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001396 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399}
1400EXPORT_SYMBOL(xfrm_state_check_expire);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001403xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001404 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Fan Du283bc9f2013-11-07 17:47:50 +08001408 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001409 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001410 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return x;
1412}
1413EXPORT_SYMBOL(xfrm_state_lookup);
1414
1415struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001416xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001417 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001418 u8 proto, unsigned short family)
1419{
1420 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001421
Fan Du283bc9f2013-11-07 17:47:50 +08001422 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001423 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001424 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001425 return x;
1426}
1427EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1428
1429struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001430xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1431 u8 proto, const xfrm_address_t *daddr,
1432 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Fan Du283bc9f2013-11-07 17:47:50 +08001436 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001437 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001438 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return x;
1441}
1442EXPORT_SYMBOL(xfrm_find_acq);
1443
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001444#ifdef CONFIG_XFRM_SUB_POLICY
1445int
1446xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001447 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001448{
1449 int err = 0;
1450 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1451 if (!afinfo)
1452 return -EAFNOSUPPORT;
1453
Fan Du283bc9f2013-11-07 17:47:50 +08001454 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001455 if (afinfo->tmpl_sort)
1456 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001457 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001458 xfrm_state_put_afinfo(afinfo);
1459 return err;
1460}
1461EXPORT_SYMBOL(xfrm_tmpl_sort);
1462
1463int
1464xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1465 unsigned short family)
1466{
1467 int err = 0;
1468 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001469 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001470
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001471 if (!afinfo)
1472 return -EAFNOSUPPORT;
1473
Fan Du283bc9f2013-11-07 17:47:50 +08001474 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001475 if (afinfo->state_sort)
1476 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001477 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001478 xfrm_state_put_afinfo(afinfo);
1479 return err;
1480}
1481EXPORT_SYMBOL(xfrm_state_sort);
1482#endif
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/* Silly enough, but I'm lazy to build resolution list */
1485
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001486static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001490 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001491 struct xfrm_state *x;
1492
Sasha Levinb67bfe02013-02-27 17:06:00 -08001493 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001494 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001495 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001496 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 xfrm_state_hold(x);
1498 return x;
1499 }
1500 }
1501 }
1502 return NULL;
1503}
1504
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001505struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct xfrm_state *x;
1508
Fan Du283bc9f2013-11-07 17:47:50 +08001509 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001510 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001511 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return x;
1513}
1514EXPORT_SYMBOL(xfrm_find_acq_byseq);
1515
1516u32 xfrm_get_acqseq(void)
1517{
1518 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001519 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
jamal6836b9b2010-02-16 02:01:22 +00001521 do {
1522 res = atomic_inc_return(&acqseq);
1523 } while (!res);
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return res;
1526}
1527EXPORT_SYMBOL(xfrm_get_acqseq);
1528
Fan Du776e9dd2013-12-16 18:47:49 +08001529int verify_spi_info(u8 proto, u32 min, u32 max)
1530{
1531 switch (proto) {
1532 case IPPROTO_AH:
1533 case IPPROTO_ESP:
1534 break;
1535
1536 case IPPROTO_COMP:
1537 /* IPCOMP spi is 16-bits. */
1538 if (max >= 0x10000)
1539 return -EINVAL;
1540 break;
1541
1542 default:
1543 return -EINVAL;
1544 }
1545
1546 if (min > max)
1547 return -EINVAL;
1548
1549 return 0;
1550}
1551EXPORT_SYMBOL(verify_spi_info);
1552
Herbert Xu658b2192007-10-09 13:29:52 -07001553int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001555 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001556 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001558 int err = -ENOENT;
1559 __be32 minspi = htonl(low);
1560 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001561 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Herbert Xu658b2192007-10-09 13:29:52 -07001563 spin_lock_bh(&x->lock);
1564 if (x->km.state == XFRM_STATE_DEAD)
1565 goto unlock;
1566
1567 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001569 goto unlock;
1570
1571 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001574 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (x0) {
1576 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001577 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579 x->id.spi = minspi;
1580 } else {
1581 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001582 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001583 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001584 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (x0 == NULL) {
1586 x->id.spi = htonl(spi);
1587 break;
1588 }
1589 xfrm_state_put(x0);
1590 }
1591 }
1592 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001593 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001594 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001595 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001596 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001597
1598 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Herbert Xu658b2192007-10-09 13:29:52 -07001600
1601unlock:
1602 spin_unlock_bh(&x->lock);
1603
1604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606EXPORT_SYMBOL(xfrm_alloc_spi);
1607
Nicolas Dichteld3623092014-02-14 15:30:36 +01001608static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001609 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001610{
1611 if (filter) {
1612 if ((filter->family == AF_INET ||
1613 filter->family == AF_INET6) &&
1614 x->props.family != filter->family)
1615 return false;
1616
1617 return addr_match(&x->props.saddr, &filter->saddr,
1618 filter->splen) &&
1619 addr_match(&x->id.daddr, &filter->daddr,
1620 filter->dplen);
1621 }
1622 return true;
1623}
1624
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001625int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001626 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 void *data)
1628{
Herbert Xu12a169e2008-10-01 07:03:24 -07001629 struct xfrm_state *state;
1630 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int err = 0;
1632
Herbert Xu12a169e2008-10-01 07:03:24 -07001633 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001634 return 0;
1635
Fan Du283bc9f2013-11-07 17:47:50 +08001636 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001637 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001638 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001639 else
Li RongQing80077702015-04-22 17:09:54 +08001640 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001641 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001642 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001643 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001644 state = container_of(x, struct xfrm_state, km);
1645 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001646 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001647 if (!__xfrm_state_filter_match(state, walk->filter))
1648 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001649 err = func(state, walk->seq, data);
1650 if (err) {
1651 list_move_tail(&walk->all, &x->all);
1652 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001654 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001656 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 err = -ENOENT;
1658 goto out;
1659 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001660 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661out:
Fan Du283bc9f2013-11-07 17:47:50 +08001662 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return err;
1664}
1665EXPORT_SYMBOL(xfrm_state_walk);
1666
Nicolas Dichteld3623092014-02-14 15:30:36 +01001667void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001668 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001669{
Herbert Xu12a169e2008-10-01 07:03:24 -07001670 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001671 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001672 walk->state = XFRM_STATE_DEAD;
1673 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001674 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001675}
1676EXPORT_SYMBOL(xfrm_state_walk_init);
1677
Fan Du283bc9f2013-11-07 17:47:50 +08001678void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001679{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001680 kfree(walk->filter);
1681
Herbert Xu12a169e2008-10-01 07:03:24 -07001682 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001683 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001684
Fan Du283bc9f2013-11-07 17:47:50 +08001685 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001686 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001687 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001688}
1689EXPORT_SYMBOL(xfrm_state_walk_done);
1690
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001691static void xfrm_replay_timer_handler(unsigned long data)
1692{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001693 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001694
1695 spin_lock(&x->lock);
1696
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001697 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001698 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001699 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001700 else
1701 x->xflags |= XFRM_TIME_DEFER;
1702 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001703
1704 spin_unlock(&x->lock);
1705}
1706
Denis Chengdf018122007-12-07 00:51:11 -08001707static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
David S. Miller214e0052011-02-24 00:02:38 -05001709void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct xfrm_mgr *km;
1712
Cong Wang85168c02013-01-16 16:05:06 +08001713 rcu_read_lock();
1714 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001715 if (km->notify_policy)
1716 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001717 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718}
1719
David S. Miller214e0052011-02-24 00:02:38 -05001720void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721{
1722 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001723 rcu_read_lock();
1724 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001725 if (km->notify)
1726 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001727 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001728}
1729
1730EXPORT_SYMBOL(km_policy_notify);
1731EXPORT_SYMBOL(km_state_notify);
1732
Eric W. Biederman15e47302012-09-07 20:12:54 +00001733void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001734{
1735 struct km_event c;
1736
Herbert Xubf08867f92005-06-18 22:44:00 -07001737 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001738 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001739 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001740 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001743EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744/*
1745 * We send to all registered managers regardless of failure
1746 * We are happy with one success
1747*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001748int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001750 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct xfrm_mgr *km;
1752
Cong Wang85168c02013-01-16 16:05:06 +08001753 rcu_read_lock();
1754 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001755 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001756 if (!acqret)
1757 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Cong Wang85168c02013-01-16 16:05:06 +08001759 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return err;
1761}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001762EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Al Viro5d36b182006-11-08 00:24:06 -08001764int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 int err = -EINVAL;
1767 struct xfrm_mgr *km;
1768
Cong Wang85168c02013-01-16 16:05:06 +08001769 rcu_read_lock();
1770 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (km->new_mapping)
1772 err = km->new_mapping(x, ipaddr, sport);
1773 if (!err)
1774 break;
1775 }
Cong Wang85168c02013-01-16 16:05:06 +08001776 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return err;
1778}
1779EXPORT_SYMBOL(km_new_mapping);
1780
Eric W. Biederman15e47302012-09-07 20:12:54 +00001781void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001783 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Herbert Xubf08867f92005-06-18 22:44:00 -07001785 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001786 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001787 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001788 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
David S. Millera70fcb02006-03-20 19:18:52 -08001790EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001792#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001793int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1794 const struct xfrm_migrate *m, int num_migrate,
1795 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001796{
1797 int err = -EINVAL;
1798 int ret;
1799 struct xfrm_mgr *km;
1800
Cong Wang85168c02013-01-16 16:05:06 +08001801 rcu_read_lock();
1802 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001803 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001804 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001805 if (!ret)
1806 err = ret;
1807 }
1808 }
Cong Wang85168c02013-01-16 16:05:06 +08001809 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001810 return err;
1811}
1812EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001813#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001814
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001815int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001816{
1817 int err = -EINVAL;
1818 int ret;
1819 struct xfrm_mgr *km;
1820
Cong Wang85168c02013-01-16 16:05:06 +08001821 rcu_read_lock();
1822 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001823 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001824 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001825 if (!ret)
1826 err = ret;
1827 }
1828 }
Cong Wang85168c02013-01-16 16:05:06 +08001829 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001830 return err;
1831}
1832EXPORT_SYMBOL(km_report);
1833
Horia Geanta0f245582014-02-12 16:20:06 +02001834bool km_is_alive(const struct km_event *c)
1835{
1836 struct xfrm_mgr *km;
1837 bool is_alive = false;
1838
1839 rcu_read_lock();
1840 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1841 if (km->is_alive && km->is_alive(c)) {
1842 is_alive = true;
1843 break;
1844 }
1845 }
1846 rcu_read_unlock();
1847
1848 return is_alive;
1849}
1850EXPORT_SYMBOL(km_is_alive);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1853{
1854 int err;
1855 u8 *data;
1856 struct xfrm_mgr *km;
1857 struct xfrm_policy *pol = NULL;
1858
1859 if (optlen <= 0 || optlen > PAGE_SIZE)
1860 return -EMSGSIZE;
1861
1862 data = kmalloc(optlen, GFP_KERNEL);
1863 if (!data)
1864 return -ENOMEM;
1865
1866 err = -EFAULT;
1867 if (copy_from_user(data, optval, optlen))
1868 goto out;
1869
1870 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08001871 rcu_read_lock();
1872 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07001873 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 optlen, &err);
1875 if (err >= 0)
1876 break;
1877 }
Cong Wang85168c02013-01-16 16:05:06 +08001878 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 if (err >= 0) {
1881 xfrm_sk_policy_insert(sk, err, pol);
1882 xfrm_pol_put(pol);
1883 err = 0;
1884 }
1885
1886out:
1887 kfree(data);
1888 return err;
1889}
1890EXPORT_SYMBOL(xfrm_user_policy);
1891
Cong Wang85168c02013-01-16 16:05:06 +08001892static DEFINE_SPINLOCK(xfrm_km_lock);
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894int xfrm_register_km(struct xfrm_mgr *km)
1895{
Cong Wang85168c02013-01-16 16:05:06 +08001896 spin_lock_bh(&xfrm_km_lock);
1897 list_add_tail_rcu(&km->list, &xfrm_km_list);
1898 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return 0;
1900}
1901EXPORT_SYMBOL(xfrm_register_km);
1902
1903int xfrm_unregister_km(struct xfrm_mgr *km)
1904{
Cong Wang85168c02013-01-16 16:05:06 +08001905 spin_lock_bh(&xfrm_km_lock);
1906 list_del_rcu(&km->list);
1907 spin_unlock_bh(&xfrm_km_lock);
1908 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return 0;
1910}
1911EXPORT_SYMBOL(xfrm_unregister_km);
1912
1913int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1914{
1915 int err = 0;
1916 if (unlikely(afinfo == NULL))
1917 return -EINVAL;
1918 if (unlikely(afinfo->family >= NPROTO))
1919 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001920 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08001922 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07001923 else
Cong Wang44abdc32013-01-16 16:05:05 +08001924 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1925 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return err;
1927}
1928EXPORT_SYMBOL(xfrm_state_register_afinfo);
1929
1930int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1931{
1932 int err = 0;
1933 if (unlikely(afinfo == NULL))
1934 return -EINVAL;
1935 if (unlikely(afinfo->family >= NPROTO))
1936 return -EAFNOSUPPORT;
Cong Wang44abdc32013-01-16 16:05:05 +08001937 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1939 if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
1940 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07001941 else
Cong Wang44abdc32013-01-16 16:05:05 +08001942 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Cong Wang44abdc32013-01-16 16:05:05 +08001944 spin_unlock_bh(&xfrm_state_afinfo_lock);
1945 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return err;
1947}
1948EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1949
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001950struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 struct xfrm_state_afinfo *afinfo;
1953 if (unlikely(family >= NPROTO))
1954 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08001955 rcu_read_lock();
1956 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07001957 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08001958 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return afinfo;
1960}
1961
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02001962void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Cong Wang44abdc32013-01-16 16:05:05 +08001964 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1968void xfrm_state_delete_tunnel(struct xfrm_state *x)
1969{
1970 if (x->tunnel) {
1971 struct xfrm_state *t = x->tunnel;
1972
1973 if (atomic_read(&t->tunnel_users) == 2)
1974 xfrm_state_delete(t);
1975 atomic_dec(&t->tunnel_users);
1976 xfrm_state_put(t);
1977 x->tunnel = NULL;
1978 }
1979}
1980EXPORT_SYMBOL(xfrm_state_delete_tunnel);
1981
1982int xfrm_state_mtu(struct xfrm_state *x, int mtu)
1983{
Patrick McHardyc5c25232007-04-09 11:47:18 -07001984 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Patrick McHardyc5c25232007-04-09 11:47:18 -07001986 spin_lock_bh(&x->lock);
1987 if (x->km.state == XFRM_STATE_VALID &&
1988 x->type && x->type->get_mtu)
1989 res = x->type->get_mtu(x, mtu);
1990 else
Patrick McHardy28121612007-06-18 22:30:15 -07001991 res = mtu - x->props.header_len;
Patrick McHardyc5c25232007-04-09 11:47:18 -07001992 spin_unlock_bh(&x->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return res;
1994}
1995
Wei Yongjuna454f0c2011-03-21 18:08:28 -07001996int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07001997{
Herbert Xud094cd82005-06-20 13:19:41 -07001998 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07001999 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002000 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002001 int err;
2002
Herbert Xud094cd82005-06-20 13:19:41 -07002003 err = -EAFNOSUPPORT;
2004 afinfo = xfrm_state_get_afinfo(family);
2005 if (!afinfo)
2006 goto error;
2007
2008 err = 0;
2009 if (afinfo->init_flags)
2010 err = afinfo->init_flags(x);
2011
2012 xfrm_state_put_afinfo(afinfo);
2013
2014 if (err)
2015 goto error;
2016
2017 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002018
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002019 if (x->sel.family != AF_UNSPEC) {
2020 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2021 if (inner_mode == NULL)
2022 goto error;
2023
2024 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2025 family != x->sel.family) {
2026 xfrm_put_mode(inner_mode);
2027 goto error;
2028 }
2029
2030 x->inner_mode = inner_mode;
2031 } else {
2032 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002033 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002034
Martin Willid81d2282008-12-03 15:38:07 -08002035 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002036 if (inner_mode == NULL)
2037 goto error;
2038
2039 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2040 xfrm_put_mode(inner_mode);
2041 goto error;
2042 }
Martin Willid81d2282008-12-03 15:38:07 -08002043 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002044
Martin Willid81d2282008-12-03 15:38:07 -08002045 if (x->props.family == AF_INET)
2046 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002047
Martin Willid81d2282008-12-03 15:38:07 -08002048 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2049 if (inner_mode_iaf) {
2050 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2051 x->inner_mode_iaf = inner_mode_iaf;
2052 else
2053 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002054 }
2055 }
Herbert Xu13996372007-10-17 21:35:51 -07002056
Herbert Xud094cd82005-06-20 13:19:41 -07002057 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002058 if (x->type == NULL)
2059 goto error;
2060
2061 err = x->type->init_state(x);
2062 if (err)
2063 goto error;
2064
Herbert Xu13996372007-10-17 21:35:51 -07002065 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002066 if (x->outer_mode == NULL) {
2067 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002068 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002069 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002070
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002071 if (init_replay) {
2072 err = xfrm_init_replay(x);
2073 if (err)
2074 goto error;
2075 }
2076
Herbert Xu72cb6962005-06-20 13:18:08 -07002077 x->km.state = XFRM_STATE_VALID;
2078
2079error:
2080 return err;
2081}
2082
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002083EXPORT_SYMBOL(__xfrm_init_state);
2084
2085int xfrm_init_state(struct xfrm_state *x)
2086{
2087 return __xfrm_init_state(x, true);
2088}
2089
Herbert Xu72cb6962005-06-20 13:18:08 -07002090EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002091
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002092int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
David S. Millerf034b5d2006-08-24 03:08:07 -07002094 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002096 INIT_LIST_HEAD(&net->xfrm.state_all);
2097
David S. Millerf034b5d2006-08-24 03:08:07 -07002098 sz = sizeof(struct hlist_head) * 8;
2099
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002100 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2101 if (!net->xfrm.state_bydst)
2102 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002103 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2104 if (!net->xfrm.state_bysrc)
2105 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002106 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2107 if (!net->xfrm.state_byspi)
2108 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002109 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002110
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002111 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002112 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Alexey Dobriyanb8a0ae22008-11-25 17:20:11 -08002113 INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
Alexey Dobriyanc7837142008-11-25 17:20:36 -08002114 INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
Fan Du283bc9f2013-11-07 17:47:50 +08002115 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002116 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002117
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002118out_byspi:
2119 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002120out_bysrc:
2121 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002122out_bydst:
2123 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002124}
2125
2126void xfrm_state_fini(struct net *net)
2127{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002128 unsigned int sz;
2129
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002130 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002131 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002132 flush_work(&net->xfrm.state_gc_work);
2133
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002134 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002135
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002136 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002137 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2138 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002139 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2140 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002141 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2142 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Joy Lattenab5f5e82007-09-17 11:51:22 -07002145#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002146static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2147 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002148{
Paul Moore68277ac2007-12-20 20:49:33 -08002149 struct xfrm_sec_ctx *ctx = x->security;
2150 u32 spi = ntohl(x->id.spi);
2151
2152 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002153 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002154 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002155
Weilong Chen9b7a7872013-12-24 09:43:46 +08002156 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002157 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002158 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2159 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002160 break;
2161 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002162 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002163 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002164 break;
2165 }
Paul Moore68277ac2007-12-20 20:49:33 -08002166
2167 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002168}
2169
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002170static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2171 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002172{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002173 const struct iphdr *iph4;
2174 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002175
2176 switch (family) {
2177 case AF_INET:
2178 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002179 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2180 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002181 break;
2182 case AF_INET6:
2183 iph6 = ipv6_hdr(skb);
2184 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002185 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002186 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002187 iph6->flow_lbl[0] & 0x0f,
2188 iph6->flow_lbl[1],
2189 iph6->flow_lbl[2]);
2190 break;
2191 }
2192}
2193
Tetsuo Handa2e710292014-04-22 21:48:30 +09002194void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002195{
2196 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002197
Paul Mooreafeb14b2007-12-21 14:58:11 -08002198 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002199 if (audit_buf == NULL)
2200 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002201 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002202 xfrm_audit_helper_sainfo(x, audit_buf);
2203 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002204 audit_log_end(audit_buf);
2205}
2206EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2207
Tetsuo Handa2e710292014-04-22 21:48:30 +09002208void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002209{
2210 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002211
Paul Mooreafeb14b2007-12-21 14:58:11 -08002212 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002213 if (audit_buf == NULL)
2214 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002215 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002216 xfrm_audit_helper_sainfo(x, audit_buf);
2217 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002218 audit_log_end(audit_buf);
2219}
2220EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002221
2222void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2223 struct sk_buff *skb)
2224{
2225 struct audit_buffer *audit_buf;
2226 u32 spi;
2227
2228 audit_buf = xfrm_audit_start("SA-replay-overflow");
2229 if (audit_buf == NULL)
2230 return;
2231 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2232 /* don't record the sequence number because it's inherent in this kind
2233 * of audit message */
2234 spi = ntohl(x->id.spi);
2235 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2236 audit_log_end(audit_buf);
2237}
2238EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2239
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002240void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002241 struct sk_buff *skb, __be32 net_seq)
2242{
2243 struct audit_buffer *audit_buf;
2244 u32 spi;
2245
2246 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2247 if (audit_buf == NULL)
2248 return;
2249 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2250 spi = ntohl(x->id.spi);
2251 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2252 spi, spi, ntohl(net_seq));
2253 audit_log_end(audit_buf);
2254}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002255EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002256
2257void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2258{
2259 struct audit_buffer *audit_buf;
2260
2261 audit_buf = xfrm_audit_start("SA-notfound");
2262 if (audit_buf == NULL)
2263 return;
2264 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2265 audit_log_end(audit_buf);
2266}
2267EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2268
2269void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2270 __be32 net_spi, __be32 net_seq)
2271{
2272 struct audit_buffer *audit_buf;
2273 u32 spi;
2274
2275 audit_buf = xfrm_audit_start("SA-notfound");
2276 if (audit_buf == NULL)
2277 return;
2278 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2279 spi = ntohl(net_spi);
2280 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2281 spi, spi, ntohl(net_seq));
2282 audit_log_end(audit_buf);
2283}
2284EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2285
2286void xfrm_audit_state_icvfail(struct xfrm_state *x,
2287 struct sk_buff *skb, u8 proto)
2288{
2289 struct audit_buffer *audit_buf;
2290 __be32 net_spi;
2291 __be32 net_seq;
2292
2293 audit_buf = xfrm_audit_start("SA-icv-failure");
2294 if (audit_buf == NULL)
2295 return;
2296 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2297 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2298 u32 spi = ntohl(net_spi);
2299 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2300 spi, spi, ntohl(net_seq));
2301 }
2302 audit_log_end(audit_buf);
2303}
2304EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002305#endif /* CONFIG_AUDITSYSCALL */