blob: 2e291bc5f1fc1003ca62e40f67da8a6a46875c02 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/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
Florian Westphalc8406992016-08-09 12:16:08 +020031#define xfrm_state_deref_prot(table, net) \
32 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
33
Florian Westphal35db57bb2016-08-23 16:00:12 +020034static void xfrm_state_gc_task(struct work_struct *work);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Each xfrm_state may be linked to two tables:
37
38 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070039 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 destination/tunnel endpoint. (output)
41 */
42
David S. Millerf034b5d2006-08-24 03:08:07 -070043static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Florian Westphalb65e3d72016-08-09 12:16:07 +020044static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Florian Westphal35db57bb2016-08-23 16:00:12 +020046static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
47static HLIST_HEAD(xfrm_state_gc_list);
48
Florian Westphal02efdff2016-08-09 12:16:05 +020049static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
50{
51 return atomic_inc_not_zero(&x->refcnt);
52}
53
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080054static inline unsigned int xfrm_dst_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. Millerc1969f22006-08-24 04:00:03 -070057 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070058 unsigned short family)
59{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080060 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070061}
62
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080063static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050064 const xfrm_address_t *daddr,
65 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070066 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070067{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080068 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070069}
70
David S. Miller2575b652006-08-24 03:26:44 -070071static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050072xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
73 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070074{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080075 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070076}
77
David S. Millerf034b5d2006-08-24 03:08:07 -070078static void xfrm_hash_transfer(struct hlist_head *list,
79 struct hlist_head *ndsttable,
80 struct hlist_head *nsrctable,
81 struct hlist_head *nspitable,
82 unsigned int nhashmask)
83{
Sasha Levinb67bfe02013-02-27 17:06:00 -080084 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070085 struct xfrm_state *x;
86
Sasha Levinb67bfe02013-02-27 17:06:00 -080087 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070088 unsigned int h;
89
David S. Millerc1969f22006-08-24 04:00:03 -070090 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
91 x->props.reqid, x->props.family,
92 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020093 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070094
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070095 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
96 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070097 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020098 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070099
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700100 if (x->id.spi) {
101 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
102 x->id.proto, x->props.family,
103 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200104 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700105 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700106 }
107}
108
Alexey Dobriyan63082732008-11-25 17:19:07 -0800109static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700110{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800111 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700112}
113
Alexey Dobriyan63082732008-11-25 17:19:07 -0800114static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700115{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800116 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700117 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
118 unsigned long nsize, osize;
119 unsigned int nhashmask, ohashmask;
120 int i;
121
Alexey Dobriyan63082732008-11-25 17:19:07 -0800122 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700123 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700124 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800125 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700126 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700128 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800129 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 }
David S. Miller44e36b42006-08-24 04:50:50 -0700131 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700132 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700133 xfrm_hash_free(ndst, nsize);
134 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800135 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700136 }
137
Fan Du283bc9f2013-11-07 17:47:50 +0800138 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200139 write_seqcount_begin(&xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700140
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200142 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800143 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200144 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700145
Florian Westphalc8406992016-08-09 12:16:08 +0200146 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
147 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800148 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700149
Florian Westphalc8406992016-08-09 12:16:08 +0200150 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
151 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
152 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800153 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700154
Florian Westphalb65e3d72016-08-09 12:16:07 +0200155 write_seqcount_end(&xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700157
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200159
160 synchronize_rcu();
161
David S. Miller44e36b42006-08-24 04:50:50 -0700162 xfrm_hash_free(odst, osize);
163 xfrm_hash_free(osrc, osize);
164 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700165}
166
Cong Wang44abdc32013-01-16 16:05:05 +0800167static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
168static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static DEFINE_SPINLOCK(xfrm_state_gc_lock);
171
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800172int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800174int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200175bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000176void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Cong Wang7a9885b2013-01-17 16:34:11 +0800178static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800179int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700180{
Cong Wang7a9885b2013-01-17 16:34:11 +0800181 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800182 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700183 int err = 0;
184
185 if (unlikely(afinfo == NULL))
186 return -EAFNOSUPPORT;
187 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800188 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700189
190 if (likely(typemap[type->proto] == NULL))
191 typemap[type->proto] = type;
192 else
193 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800194 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100195 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700196 return err;
197}
198EXPORT_SYMBOL(xfrm_register_type);
199
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800200int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700201{
Cong Wang7a9885b2013-01-17 16:34:11 +0800202 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800203 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700204 int err = 0;
205
206 if (unlikely(afinfo == NULL))
207 return -EAFNOSUPPORT;
208 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800209 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700210
211 if (unlikely(typemap[type->proto] != type))
212 err = -ENOENT;
213 else
214 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800215 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100216 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700217 return err;
218}
219EXPORT_SYMBOL(xfrm_unregister_type);
220
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800221static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700222{
223 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800224 const struct xfrm_type **typemap;
225 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700226 int modload_attempted = 0;
227
228retry:
229 afinfo = xfrm_state_get_afinfo(family);
230 if (unlikely(afinfo == NULL))
231 return NULL;
232 typemap = afinfo->type_map;
233
Florian Westphal75cda622017-01-09 14:20:49 +0100234 type = READ_ONCE(typemap[proto]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700235 if (unlikely(type && !try_module_get(type->owner)))
236 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100237
238 rcu_read_unlock();
239
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700240 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700241 request_module("xfrm-type-%d-%d", family, proto);
242 modload_attempted = 1;
243 goto retry;
244 }
245
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700246 return type;
247}
248
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800249static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700250{
251 module_put(type->owner);
252}
253
Steffen Klassert9d389d72017-04-14 10:05:44 +0200254static DEFINE_SPINLOCK(xfrm_type_offload_lock);
255int xfrm_register_type_offload(const struct xfrm_type_offload *type,
256 unsigned short family)
257{
258 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
259 const struct xfrm_type_offload **typemap;
260 int err = 0;
261
262 if (unlikely(afinfo == NULL))
263 return -EAFNOSUPPORT;
264 typemap = afinfo->type_offload_map;
265 spin_lock_bh(&xfrm_type_offload_lock);
266
267 if (likely(typemap[type->proto] == NULL))
268 typemap[type->proto] = type;
269 else
270 err = -EEXIST;
271 spin_unlock_bh(&xfrm_type_offload_lock);
272 rcu_read_unlock();
273 return err;
274}
275EXPORT_SYMBOL(xfrm_register_type_offload);
276
277int xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
278 unsigned short family)
279{
280 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
281 const struct xfrm_type_offload **typemap;
282 int err = 0;
283
284 if (unlikely(afinfo == NULL))
285 return -EAFNOSUPPORT;
286 typemap = afinfo->type_offload_map;
287 spin_lock_bh(&xfrm_type_offload_lock);
288
289 if (unlikely(typemap[type->proto] != type))
290 err = -ENOENT;
291 else
292 typemap[type->proto] = NULL;
293 spin_unlock_bh(&xfrm_type_offload_lock);
294 rcu_read_unlock();
295 return err;
296}
297EXPORT_SYMBOL(xfrm_unregister_type_offload);
298
299static const struct xfrm_type_offload *xfrm_get_type_offload(u8 proto, unsigned short family)
300{
301 struct xfrm_state_afinfo *afinfo;
302 const struct xfrm_type_offload **typemap;
303 const struct xfrm_type_offload *type;
304
305 afinfo = xfrm_state_get_afinfo(family);
306 if (unlikely(afinfo == NULL))
307 return NULL;
308 typemap = afinfo->type_offload_map;
309
310 type = typemap[proto];
311 if ((type && !try_module_get(type->owner)))
312 type = NULL;
313
314 rcu_read_unlock();
315 return type;
316}
317
318static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
319{
320 module_put(type->owner);
321}
322
Cong Wang7a9885b2013-01-17 16:34:11 +0800323static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700324int xfrm_register_mode(struct xfrm_mode *mode, int family)
325{
326 struct xfrm_state_afinfo *afinfo;
327 struct xfrm_mode **modemap;
328 int err;
329
330 if (unlikely(mode->encap >= XFRM_MODE_MAX))
331 return -EINVAL;
332
Cong Wang7a9885b2013-01-17 16:34:11 +0800333 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700334 if (unlikely(afinfo == NULL))
335 return -EAFNOSUPPORT;
336
337 err = -EEXIST;
338 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800339 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700340 if (modemap[mode->encap])
341 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700342
Herbert Xu17c2a422007-10-17 21:33:12 -0700343 err = -ENOENT;
344 if (!try_module_get(afinfo->owner))
345 goto out;
346
347 mode->afinfo = afinfo;
348 modemap[mode->encap] = mode;
349 err = 0;
350
351out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800352 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100353 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700354 return err;
355}
356EXPORT_SYMBOL(xfrm_register_mode);
357
358int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
359{
360 struct xfrm_state_afinfo *afinfo;
361 struct xfrm_mode **modemap;
362 int err;
363
364 if (unlikely(mode->encap >= XFRM_MODE_MAX))
365 return -EINVAL;
366
Cong Wang7a9885b2013-01-17 16:34:11 +0800367 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700368 if (unlikely(afinfo == NULL))
369 return -EAFNOSUPPORT;
370
371 err = -ENOENT;
372 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800373 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700374 if (likely(modemap[mode->encap] == mode)) {
375 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700376 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700377 err = 0;
378 }
379
Cong Wang7a9885b2013-01-17 16:34:11 +0800380 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100381 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700382 return err;
383}
384EXPORT_SYMBOL(xfrm_unregister_mode);
385
386static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
387{
388 struct xfrm_state_afinfo *afinfo;
389 struct xfrm_mode *mode;
390 int modload_attempted = 0;
391
392 if (unlikely(encap >= XFRM_MODE_MAX))
393 return NULL;
394
395retry:
396 afinfo = xfrm_state_get_afinfo(family);
397 if (unlikely(afinfo == NULL))
398 return NULL;
399
Florian Westphal75cda622017-01-09 14:20:49 +0100400 mode = READ_ONCE(afinfo->mode_map[encap]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700401 if (unlikely(mode && !try_module_get(mode->owner)))
402 mode = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100403
404 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700405 if (!mode && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700406 request_module("xfrm-mode-%d-%d", family, encap);
407 modload_attempted = 1;
408 goto retry;
409 }
410
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700411 return mode;
412}
413
414static void xfrm_put_mode(struct xfrm_mode *mode)
415{
416 module_put(mode->owner);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static void xfrm_state_gc_destroy(struct xfrm_state *x)
420{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800421 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700422 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000423 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800424 kfree(x->aalg);
425 kfree(x->ealg);
426 kfree(x->calg);
427 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700428 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000429 kfree(x->replay_esn);
430 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700431 if (x->inner_mode)
432 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700433 if (x->inner_mode_iaf)
434 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700435 if (x->outer_mode)
436 xfrm_put_mode(x->outer_mode);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200437 if (x->type_offload)
438 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (x->type) {
440 x->type->destructor(x);
441 xfrm_put_type(x->type);
442 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200443 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800444 security_xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(x);
446}
447
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800448static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Herbert Xu12a169e2008-10-01 07:03:24 -0700450 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800451 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700452 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200455 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_unlock_bh(&xfrm_state_gc_lock);
457
Florian Westphaldf7274e2016-08-09 12:16:06 +0200458 synchronize_rcu();
459
Sasha Levinb67bfe02013-02-27 17:06:00 -0800460 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800464static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800466 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
467 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
James Morris9d729f72007-03-04 16:12:44 -0800468 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 long next = LONG_MAX;
470 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600471 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 spin_lock(&x->lock);
474 if (x->km.state == XFRM_STATE_DEAD)
475 goto out;
476 if (x->km.state == XFRM_STATE_EXPIRED)
477 goto expired;
478 if (x->lft.hard_add_expires_seconds) {
479 long tmo = x->lft.hard_add_expires_seconds +
480 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000481 if (tmo <= 0) {
482 if (x->xflags & XFRM_SOFT_EXPIRE) {
483 /* enter hard expire without soft expire first?!
484 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100485 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000486 */
487 x->curlft.add_time = now - x->saved_tmo - 1;
488 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
489 } else
490 goto expired;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (tmo < next)
493 next = tmo;
494 }
495 if (x->lft.hard_use_expires_seconds) {
496 long tmo = x->lft.hard_use_expires_seconds +
497 (x->curlft.use_time ? : now) - now;
498 if (tmo <= 0)
499 goto expired;
500 if (tmo < next)
501 next = tmo;
502 }
503 if (x->km.dying)
504 goto resched;
505 if (x->lft.soft_add_expires_seconds) {
506 long tmo = x->lft.soft_add_expires_seconds +
507 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000508 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000510 x->xflags &= ~XFRM_SOFT_EXPIRE;
511 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000513 x->xflags |= XFRM_SOFT_EXPIRE;
514 x->saved_tmo = tmo;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 if (x->lft.soft_use_expires_seconds) {
518 long tmo = x->lft.soft_use_expires_seconds +
519 (x->curlft.use_time ? : now) - now;
520 if (tmo <= 0)
521 warn = 1;
522 else if (tmo < next)
523 next = tmo;
524 }
525
Herbert Xu4666faa2005-06-18 22:43:22 -0700526 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800528 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529resched:
Weilong Chen9b7a7872013-12-24 09:43:46 +0800530 if (next != LONG_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800531 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
532 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto out;
535
536expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200537 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600539
540 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200541 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800542 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Tetsuo Handa2e710292014-04-22 21:48:30 +0900544 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546out:
547 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800548 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
David S. Miller0ac84752006-03-20 19:18:23 -0800551static void xfrm_replay_timer_handler(unsigned long data);
552
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800553struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct xfrm_state *x;
556
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700557 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800560 write_pnet(&x->xs_net, net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 atomic_set(&x->refcnt, 1);
562 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700563 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700564 INIT_HLIST_NODE(&x->bydst);
565 INIT_HLIST_NODE(&x->bysrc);
566 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800567 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
568 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800569 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
570 (unsigned long)x);
James Morris9d729f72007-03-04 16:12:44 -0800571 x->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 x->lft.soft_byte_limit = XFRM_INF;
573 x->lft.soft_packet_limit = XFRM_INF;
574 x->lft.hard_byte_limit = XFRM_INF;
575 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800576 x->replay_maxage = 0;
577 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700578 x->inner_mode = NULL;
579 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 spin_lock_init(&x->lock);
581 }
582 return x;
583}
584EXPORT_SYMBOL(xfrm_state_alloc);
585
586void __xfrm_state_destroy(struct xfrm_state *x)
587{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700588 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200591 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_unlock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200593 schedule_work(&xfrm_state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595EXPORT_SYMBOL(__xfrm_state_destroy);
596
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800597int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800599 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700600 int err = -ESRCH;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (x->km.state != XFRM_STATE_DEAD) {
603 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800604 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700605 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200606 hlist_del_rcu(&x->bydst);
607 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700608 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200609 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800610 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800611 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200613 xfrm_dev_state_delete(x);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* All xfrm_state objects are created by xfrm_state_alloc.
616 * The xfrm_state_alloc call gives a reference, and that
617 * is what we are dropping here.
618 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800619 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700620 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700622
623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800625EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700627int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700629 int err;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700632 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700634
635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637EXPORT_SYMBOL(xfrm_state_delete);
638
Joy Latten4aa2e622007-06-04 19:05:57 -0400639#ifdef CONFIG_SECURITY_NETWORK_XFRM
640static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900641xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Joy Latten4aa2e622007-06-04 19:05:57 -0400643 int i, err = 0;
644
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800645 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400646 struct xfrm_state *x;
647
Sasha Levinb67bfe02013-02-27 17:06:00 -0800648 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400649 if (xfrm_id_proto_match(x->id.proto, proto) &&
650 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900651 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400652 return err;
653 }
654 }
655 }
656
657 return err;
658}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200659
660static inline int
661xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
662{
663 int i, err = 0;
664
665 for (i = 0; i <= net->xfrm.state_hmask; i++) {
666 struct xfrm_state *x;
667 struct xfrm_state_offload *xso;
668
669 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
670 xso = &x->xso;
671
672 if (xso->dev == dev &&
673 (err = security_xfrm_state_delete(x)) != 0) {
674 xfrm_audit_state_delete(x, 0, task_valid);
675 return err;
676 }
677 }
678 }
679
680 return err;
681}
Joy Latten4aa2e622007-06-04 19:05:57 -0400682#else
683static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900684xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400685{
686 return 0;
687}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200688
689static inline int
690xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
691{
692 return 0;
693}
Joy Latten4aa2e622007-06-04 19:05:57 -0400694#endif
695
Tetsuo Handa2e710292014-04-22 21:48:30 +0900696int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400697{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000698 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Fan Du283bc9f2013-11-07 17:47:50 +0800700 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900701 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400702 if (err)
703 goto out;
704
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000705 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800706 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700707 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800709 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700711 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800713 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Joy Latten161a09e2006-11-27 13:11:54 -0600715 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700716 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900717 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000719 if (!err)
720 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Fan Du283bc9f2013-11-07 17:47:50 +0800722 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto restart;
724 }
725 }
726 }
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000727 if (cnt)
728 err = 0;
Joy Latten4aa2e622007-06-04 19:05:57 -0400729
730out:
Fan Du283bc9f2013-11-07 17:47:50 +0800731 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400732 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734EXPORT_SYMBOL(xfrm_state_flush);
735
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200736int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
737{
738 int i, err = 0, cnt = 0;
739
740 spin_lock_bh(&net->xfrm.xfrm_state_lock);
741 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
742 if (err)
743 goto out;
744
745 err = -ESRCH;
746 for (i = 0; i <= net->xfrm.state_hmask; i++) {
747 struct xfrm_state *x;
748 struct xfrm_state_offload *xso;
749restart:
750 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
751 xso = &x->xso;
752
753 if (!xfrm_state_kern(x) && xso->dev == dev) {
754 xfrm_state_hold(x);
755 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
756
757 err = xfrm_state_delete(x);
758 xfrm_audit_state_delete(x, err ? 0 : 1,
759 task_valid);
760 xfrm_state_put(x);
761 if (!err)
762 cnt++;
763
764 spin_lock_bh(&net->xfrm.xfrm_state_lock);
765 goto restart;
766 }
767 }
768 }
769 if (cnt)
770 err = 0;
771
772out:
773 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
774 return err;
775}
776EXPORT_SYMBOL(xfrm_dev_state_flush);
777
Alexey Dobriyane0710412010-01-23 13:37:10 +0000778void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700779{
Fan Du283bc9f2013-11-07 17:47:50 +0800780 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000781 si->sadcnt = net->xfrm.state_num;
782 si->sadhcnt = net->xfrm.state_hmask;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700783 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800784 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700785}
786EXPORT_SYMBOL(xfrm_sad_getinfo);
787
Florian Westphal711059b2017-01-09 14:20:48 +0100788static void
David S. Miller1a898592011-02-22 18:22:34 -0800789xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500790 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500791 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700792 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Florian Westphal711059b2017-01-09 14:20:48 +0100794 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
795
Florian Westphal3819a352017-01-13 14:55:14 +0100796 if (!afinfo)
797 return;
798
799 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700800
801 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100802 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700803 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100804 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700805 }
806 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
David S. Miller9aa60082011-02-24 01:51:36 -0500809static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
810 const xfrm_address_t *daddr,
811 __be32 spi, u8 proto,
812 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700813{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800814 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700815 struct xfrm_state *x;
816
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200817 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700818 if (x->props.family != family ||
819 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000820 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000821 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700822 continue;
823
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000824 if ((mark & x->mark.m) != x->mark.v)
825 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200826 if (!xfrm_state_hold_rcu(x))
827 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700828 return x;
829 }
830
831 return NULL;
832}
833
David S. Miller9aa60082011-02-24 01:51:36 -0500834static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
835 const xfrm_address_t *daddr,
836 const xfrm_address_t *saddr,
837 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700838{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800839 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700840 struct xfrm_state *x;
841
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200842 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700843 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000844 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000845 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
846 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700847 continue;
848
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000849 if ((mark & x->mark.m) != x->mark.v)
850 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200851 if (!xfrm_state_hold_rcu(x))
852 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700853 return x;
854 }
855
856 return NULL;
857}
858
859static inline struct xfrm_state *
860__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
861{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800862 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800863 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800864
David S. Milleredcd5822006-08-24 00:42:45 -0700865 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800866 return __xfrm_state_lookup(net, mark, &x->id.daddr,
867 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700868 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800869 return __xfrm_state_lookup_byaddr(net, mark,
870 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700871 &x->props.saddr,
872 x->id.proto, family);
873}
874
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800875static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700876{
877 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800878 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
879 net->xfrm.state_num > net->xfrm.state_hmask)
880 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700881}
882
David S. Miller08ec9af2009-03-13 14:22:40 -0700883static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800884 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700885 struct xfrm_state **best, int *acq_in_progress,
886 int *error)
887{
888 /* Resolution logic:
889 * 1. There is a valid state with matching selector. Done.
890 * 2. Valid state with inappropriate selector. Skip.
891 *
892 * Entering area of "sysdeps".
893 *
894 * 3. If state is not valid, selector is temporary, it selects
895 * only session which triggered previous resolution. Key
896 * manager will do something to install a state with proper
897 * selector.
898 */
899 if (x->km.state == XFRM_STATE_VALID) {
900 if ((x->sel.family &&
901 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
902 !security_xfrm_state_pol_flow_match(x, pol, fl))
903 return;
904
905 if (!*best ||
906 (*best)->km.dying > x->km.dying ||
907 ((*best)->km.dying == x->km.dying &&
908 (*best)->curlft.add_time < x->curlft.add_time))
909 *best = x;
910 } else if (x->km.state == XFRM_STATE_ACQ) {
911 *acq_in_progress = 1;
912 } else if (x->km.state == XFRM_STATE_ERROR ||
913 x->km.state == XFRM_STATE_EXPIRED) {
914 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
915 security_xfrm_state_pol_flow_match(x, pol, fl))
916 *error = -ESRCH;
917 }
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500921xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800922 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct xfrm_policy *pol, int *err,
924 unsigned short family)
925{
David S. Miller08ec9af2009-03-13 14:22:40 -0700926 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800927 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700928 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700929 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int acquire_in_progress = 0;
931 int error = 0;
932 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800933 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700934 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200935 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200936 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900937
David S. Miller37b08e32008-09-02 20:14:15 -0700938 to_put = NULL;
939
Florian Westphalb65e3d72016-08-09 12:16:07 +0200940 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
941
Florian Westphald737a582016-08-09 12:16:09 +0200942 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700943 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200944 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700945 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000947 (mark & x->mark.m) == x->mark.v &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700948 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700949 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 tmpl->mode == x->props.mode &&
951 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700952 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500953 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700954 &best, &acquire_in_progress, &error);
955 }
Fan Du6f115632013-09-23 17:18:25 +0800956 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700957 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Thomas Egerer8444cf72010-09-20 11:11:38 -0700959 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200960 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700961 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700962 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000963 (mark & x->mark.m) == x->mark.v &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700964 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800965 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700966 tmpl->mode == x->props.mode &&
967 tmpl->id.proto == x->id.proto &&
968 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500969 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700970 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
David S. Miller08ec9af2009-03-13 14:22:40 -0700973found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 x = best;
975 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700976 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800977 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700978 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700979 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 error = -EEXIST;
981 goto out;
982 }
Horia Geanta0f245582014-02-12 16:20:06 +0200983
984 c.net = net;
985 /* If the KMs have no listeners (yet...), avoid allocating an SA
986 * for each and every packet - garbage collection might not
987 * handle the flood.
988 */
989 if (!km_is_alive(&c)) {
990 error = -ESRCH;
991 goto out;
992 }
993
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800994 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (x == NULL) {
996 error = -ENOMEM;
997 goto out;
998 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700999 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001001 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001002 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
David S. Miller1d28f422011-03-12 00:29:39 -05001004 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001005 if (error) {
1006 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001007 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001008 x = NULL;
1009 goto out;
1010 }
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001013 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001015 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001016 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001017 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001018 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001020 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001021 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001023 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001024 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001025 net->xfrm.state_num++;
1026 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001027 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 } else {
1029 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001030 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 x = NULL;
1032 error = -ESRCH;
1033 }
1034 }
1035out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001036 if (x) {
1037 if (!xfrm_state_hold_rcu(x)) {
1038 *err = -EAGAIN;
1039 x = NULL;
1040 }
1041 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001043 }
Florian Westphald737a582016-08-09 12:16:09 +02001044 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001045 if (to_put)
1046 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001047
1048 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1049 *err = -EAGAIN;
1050 if (x) {
1051 xfrm_state_put(x);
1052 x = NULL;
1053 }
1054 }
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return x;
1057}
1058
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001059struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001060xfrm_stateonly_find(struct net *net, u32 mark,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001061 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001062 unsigned short family, u8 mode, u8 proto, u32 reqid)
1063{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001064 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001065 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001066
Fan Du4ae770b2014-01-03 11:18:29 +08001067 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001068 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001069 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001070 if (x->props.family == family &&
1071 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001072 (mark & x->mark.m) == x->mark.v &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001073 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1074 xfrm_state_addr_check(x, daddr, saddr, family) &&
1075 mode == x->props.mode &&
1076 proto == x->id.proto &&
1077 x->km.state == XFRM_STATE_VALID) {
1078 rx = x;
1079 break;
1080 }
1081 }
1082
1083 if (rx)
1084 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001085 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001086
1087
1088 return rx;
1089}
1090EXPORT_SYMBOL(xfrm_stateonly_find);
1091
Fan Duc4549972014-01-03 11:18:32 +08001092struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1093 unsigned short family)
1094{
1095 struct xfrm_state *x;
1096 struct xfrm_state_walk *w;
1097
1098 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1099 list_for_each_entry(w, &net->xfrm.state_all, all) {
1100 x = container_of(w, struct xfrm_state, km);
1101 if (x->props.family != family ||
1102 x->id.spi != spi)
1103 continue;
1104
Fan Duc4549972014-01-03 11:18:32 +08001105 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001106 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001107 return x;
1108 }
1109 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1110 return NULL;
1111}
1112EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114static void __xfrm_state_insert(struct xfrm_state *x)
1115{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001116 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001117 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001119 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001120
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001121 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001122 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001123 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001125 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001126 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001128 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001129 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001130 x->props.family);
1131
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001132 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001133 }
1134
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001135 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -07001136 if (x->replay_maxage)
1137 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001138
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001139 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001140
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001141 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Fan Du283bc9f2013-11-07 17:47:50 +08001144/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001145static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1146{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001147 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001148 unsigned short family = xnew->props.family;
1149 u32 reqid = xnew->props.reqid;
1150 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001151 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001152 u32 mark = xnew->mark.v & xnew->mark.m;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001153
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001154 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001155 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001156 if (x->props.family == family &&
1157 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001158 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001159 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1160 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001161 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001162 }
1163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165void xfrm_state_insert(struct xfrm_state *x)
1166{
Fan Du283bc9f2013-11-07 17:47:50 +08001167 struct net *net = xs_net(x);
1168
1169 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001170 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001172 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174EXPORT_SYMBOL(xfrm_state_insert);
1175
Fan Du283bc9f2013-11-07 17:47:50 +08001176/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001177static struct xfrm_state *__find_acq_core(struct net *net,
1178 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001179 unsigned short family, u8 mode,
1180 u32 reqid, u8 proto,
1181 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001182 const xfrm_address_t *saddr,
1183 int create)
David S. Miller27708342006-08-24 00:13:10 -07001184{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001185 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001186 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001187 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001188
Sasha Levinb67bfe02013-02-27 17:06:00 -08001189 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001190 if (x->props.reqid != reqid ||
1191 x->props.mode != mode ||
1192 x->props.family != family ||
1193 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001194 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001195 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001196 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001197 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1198 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001199 continue;
1200
David S. Miller27708342006-08-24 00:13:10 -07001201 xfrm_state_hold(x);
1202 return x;
1203 }
1204
1205 if (!create)
1206 return NULL;
1207
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001208 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001209 if (likely(x)) {
1210 switch (family) {
1211 case AF_INET:
1212 x->sel.daddr.a4 = daddr->a4;
1213 x->sel.saddr.a4 = saddr->a4;
1214 x->sel.prefixlen_d = 32;
1215 x->sel.prefixlen_s = 32;
1216 x->props.saddr.a4 = saddr->a4;
1217 x->id.daddr.a4 = daddr->a4;
1218 break;
1219
1220 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001221 x->sel.daddr.in6 = daddr->in6;
1222 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001223 x->sel.prefixlen_d = 128;
1224 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001225 x->props.saddr.in6 = saddr->in6;
1226 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001227 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001228 }
David S. Miller27708342006-08-24 00:13:10 -07001229
1230 x->km.state = XFRM_STATE_ACQ;
1231 x->id.proto = proto;
1232 x->props.family = family;
1233 x->props.mode = mode;
1234 x->props.reqid = reqid;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001235 x->mark.v = m->v;
1236 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001237 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001238 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001239 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001240 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001241 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001242 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001243 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001244
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001245 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001246
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001247 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001248 }
1249
1250 return x;
1251}
1252
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001253static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255int xfrm_state_add(struct xfrm_state *x)
1256{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001257 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001258 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int family;
1260 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001261 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001262 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
David S. Miller37b08e32008-09-02 20:14:15 -07001266 to_put = NULL;
1267
Fan Du283bc9f2013-11-07 17:47:50 +08001268 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
David S. Milleredcd5822006-08-24 00:42:45 -07001270 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001272 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 x1 = NULL;
1274 err = -EEXIST;
1275 goto out;
1276 }
1277
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001278 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001279 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001280 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001281 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001282 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 x1 = NULL;
1284 }
1285 }
1286
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001287 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001288 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1289 x->props.reqid, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001290 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
David S. Millerc7f5ea32006-08-24 03:29:04 -07001292 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 __xfrm_state_insert(x);
1294 err = 0;
1295
1296out:
Fan Du283bc9f2013-11-07 17:47:50 +08001297 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (x1) {
1300 xfrm_state_delete(x1);
1301 xfrm_state_put(x1);
1302 }
1303
David S. Miller37b08e32008-09-02 20:14:15 -07001304 if (to_put)
1305 xfrm_state_put(to_put);
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return err;
1308}
1309EXPORT_SYMBOL(xfrm_state_add);
1310
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001311#ifdef CONFIG_XFRM_MIGRATE
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001312static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001313{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001314 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001315 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001316 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001317 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001318
1319 memcpy(&x->id, &orig->id, sizeof(x->id));
1320 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1321 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1322 x->props.mode = orig->props.mode;
1323 x->props.replay_window = orig->props.replay_window;
1324 x->props.reqid = orig->props.reqid;
1325 x->props.family = orig->props.family;
1326 x->props.saddr = orig->props.saddr;
1327
1328 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001329 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001330 if (!x->aalg)
1331 goto error;
1332 }
1333 x->props.aalgo = orig->props.aalgo;
1334
Steffen Klassertee5c2312014-02-19 13:33:24 +01001335 if (orig->aead) {
1336 x->aead = xfrm_algo_aead_clone(orig->aead);
1337 if (!x->aead)
1338 goto error;
1339 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001340 if (orig->ealg) {
1341 x->ealg = xfrm_algo_clone(orig->ealg);
1342 if (!x->ealg)
1343 goto error;
1344 }
1345 x->props.ealgo = orig->props.ealgo;
1346
1347 if (orig->calg) {
1348 x->calg = xfrm_algo_clone(orig->calg);
1349 if (!x->calg)
1350 goto error;
1351 }
1352 x->props.calgo = orig->props.calgo;
1353
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001354 if (orig->encap) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001355 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1356 if (!x->encap)
1357 goto error;
1358 }
1359
1360 if (orig->coaddr) {
1361 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1362 GFP_KERNEL);
1363 if (!x->coaddr)
1364 goto error;
1365 }
1366
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001367 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001368 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001369 goto error;
1370 }
1371
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001372 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1373
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001374 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001375 goto error;
1376
1377 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001378 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001379
Steffen Klassertee5c2312014-02-19 13:33:24 +01001380 x->tfcpad = orig->tfcpad;
1381 x->replay_maxdiff = orig->replay_maxdiff;
1382 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001383 x->curlft.add_time = orig->curlft.add_time;
1384 x->km.state = orig->km.state;
1385 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001386 x->replay = orig->replay;
1387 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001388
1389 return x;
1390
1391 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001392 xfrm_state_put(x);
1393out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001394 return NULL;
1395}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001396
Fan Du283bc9f2013-11-07 17:47:50 +08001397struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001398{
1399 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001400 struct xfrm_state *x = NULL;
1401
1402 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001403
1404 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001405 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001406 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001407 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001408 if (x->props.mode != m->mode ||
1409 x->id.proto != m->proto)
1410 continue;
1411 if (m->reqid && x->props.reqid != m->reqid)
1412 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001413 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1414 m->old_family) ||
1415 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1416 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001417 continue;
1418 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001419 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001420 }
1421 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001422 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001423 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001424 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001425 if (x->props.mode != m->mode ||
1426 x->id.proto != m->proto)
1427 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001428 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1429 m->old_family) ||
1430 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1431 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001432 continue;
1433 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001434 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001435 }
1436 }
1437
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001438 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1439
1440 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001441}
1442EXPORT_SYMBOL(xfrm_migrate_state_find);
1443
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001444struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1445 struct xfrm_migrate *m)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001446{
1447 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001448
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001449 xc = xfrm_state_clone(x);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001450 if (!xc)
1451 return NULL;
1452
1453 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1454 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1455
1456 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001457 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001458 /* a care is needed when the destination address of the
1459 state is to be updated as it is a part of triplet */
1460 xfrm_state_insert(xc);
1461 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001462 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001463 goto error;
1464 }
1465
1466 return xc;
1467error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001468 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001469 return NULL;
1470}
1471EXPORT_SYMBOL(xfrm_state_migrate);
1472#endif
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474int xfrm_state_update(struct xfrm_state *x)
1475{
David S. Miller37b08e32008-09-02 20:14:15 -07001476 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001478 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001479 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
David S. Miller37b08e32008-09-02 20:14:15 -07001481 to_put = NULL;
1482
Fan Du283bc9f2013-11-07 17:47:50 +08001483 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001484 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 err = -ESRCH;
1487 if (!x1)
1488 goto out;
1489
1490 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001491 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 err = -EEXIST;
1493 goto out;
1494 }
1495
1496 if (x1->km.state == XFRM_STATE_ACQ) {
1497 __xfrm_state_insert(x);
1498 x = NULL;
1499 }
1500 err = 0;
1501
1502out:
Fan Du283bc9f2013-11-07 17:47:50 +08001503 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
David S. Miller37b08e32008-09-02 20:14:15 -07001505 if (to_put)
1506 xfrm_state_put(to_put);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (err)
1509 return err;
1510
1511 if (!x) {
1512 xfrm_state_delete(x1);
1513 xfrm_state_put(x1);
1514 return 0;
1515 }
1516
1517 err = -EINVAL;
1518 spin_lock_bh(&x1->lock);
1519 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1520 if (x->encap && x1->encap)
1521 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001522 if (x->coaddr && x1->coaddr) {
1523 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1524 }
1525 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1526 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1528 x1->km.dying = 0;
1529
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001530 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (x1->curlft.use_time)
1532 xfrm_state_check_expire(x1);
1533
1534 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001535 x->km.state = XFRM_STATE_DEAD;
1536 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 spin_unlock_bh(&x1->lock);
1539
1540 xfrm_state_put(x1);
1541
1542 return err;
1543}
1544EXPORT_SYMBOL(xfrm_state_update);
1545
1546int xfrm_state_check_expire(struct xfrm_state *x)
1547{
1548 if (!x->curlft.use_time)
James Morris9d729f72007-03-04 16:12:44 -08001549 x->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1552 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001553 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001554 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EINVAL;
1556 }
1557
1558 if (!x->km.dying &&
1559 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001560 x->curlft.packets >= x->lft.soft_packet_limit)) {
1561 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001562 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565}
1566EXPORT_SYMBOL(xfrm_state_check_expire);
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001569xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001570 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Florian Westphalc2f672f2016-09-20 15:45:26 +02001574 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001575 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001576 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return x;
1578}
1579EXPORT_SYMBOL(xfrm_state_lookup);
1580
1581struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001582xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001583 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001584 u8 proto, unsigned short family)
1585{
1586 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001587
Fan Du283bc9f2013-11-07 17:47:50 +08001588 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001589 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001590 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001591 return x;
1592}
1593EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1594
1595struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001596xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1597 u8 proto, const xfrm_address_t *daddr,
1598 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Fan Du283bc9f2013-11-07 17:47:50 +08001602 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001603 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001604 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return x;
1607}
1608EXPORT_SYMBOL(xfrm_find_acq);
1609
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001610#ifdef CONFIG_XFRM_SUB_POLICY
1611int
1612xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001613 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001614{
1615 int err = 0;
1616 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1617 if (!afinfo)
1618 return -EAFNOSUPPORT;
1619
Fan Du283bc9f2013-11-07 17:47:50 +08001620 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001621 if (afinfo->tmpl_sort)
1622 err = afinfo->tmpl_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001623 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001624 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001625 return err;
1626}
1627EXPORT_SYMBOL(xfrm_tmpl_sort);
1628
1629int
1630xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1631 unsigned short family)
1632{
1633 int err = 0;
1634 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001635 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001636
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001637 if (!afinfo)
1638 return -EAFNOSUPPORT;
1639
Fan Du283bc9f2013-11-07 17:47:50 +08001640 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001641 if (afinfo->state_sort)
1642 err = afinfo->state_sort(dst, src, n);
Fan Du283bc9f2013-11-07 17:47:50 +08001643 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001644 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001645 return err;
1646}
1647EXPORT_SYMBOL(xfrm_state_sort);
1648#endif
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650/* Silly enough, but I'm lazy to build resolution list */
1651
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001652static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001656 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001657 struct xfrm_state *x;
1658
Sasha Levinb67bfe02013-02-27 17:06:00 -08001659 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001660 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001661 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001662 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 xfrm_state_hold(x);
1664 return x;
1665 }
1666 }
1667 }
1668 return NULL;
1669}
1670
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001671struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct xfrm_state *x;
1674
Fan Du283bc9f2013-11-07 17:47:50 +08001675 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001676 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001677 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return x;
1679}
1680EXPORT_SYMBOL(xfrm_find_acq_byseq);
1681
1682u32 xfrm_get_acqseq(void)
1683{
1684 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001685 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
jamal6836b9b2010-02-16 02:01:22 +00001687 do {
1688 res = atomic_inc_return(&acqseq);
1689 } while (!res);
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return res;
1692}
1693EXPORT_SYMBOL(xfrm_get_acqseq);
1694
Fan Du776e9dd2013-12-16 18:47:49 +08001695int verify_spi_info(u8 proto, u32 min, u32 max)
1696{
1697 switch (proto) {
1698 case IPPROTO_AH:
1699 case IPPROTO_ESP:
1700 break;
1701
1702 case IPPROTO_COMP:
1703 /* IPCOMP spi is 16-bits. */
1704 if (max >= 0x10000)
1705 return -EINVAL;
1706 break;
1707
1708 default:
1709 return -EINVAL;
1710 }
1711
1712 if (min > max)
1713 return -EINVAL;
1714
1715 return 0;
1716}
1717EXPORT_SYMBOL(verify_spi_info);
1718
Herbert Xu658b2192007-10-09 13:29:52 -07001719int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001721 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001722 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001724 int err = -ENOENT;
1725 __be32 minspi = htonl(low);
1726 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001727 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Herbert Xu658b2192007-10-09 13:29:52 -07001729 spin_lock_bh(&x->lock);
1730 if (x->km.state == XFRM_STATE_DEAD)
1731 goto unlock;
1732
1733 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001735 goto unlock;
1736
1737 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001740 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (x0) {
1742 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001743 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745 x->id.spi = minspi;
1746 } else {
1747 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001748 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001749 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001750 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (x0 == NULL) {
1752 x->id.spi = htonl(spi);
1753 break;
1754 }
1755 xfrm_state_put(x0);
1756 }
1757 }
1758 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001759 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001760 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001761 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001762 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001763
1764 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
Herbert Xu658b2192007-10-09 13:29:52 -07001766
1767unlock:
1768 spin_unlock_bh(&x->lock);
1769
1770 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772EXPORT_SYMBOL(xfrm_alloc_spi);
1773
Nicolas Dichteld3623092014-02-14 15:30:36 +01001774static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001775 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001776{
1777 if (filter) {
1778 if ((filter->family == AF_INET ||
1779 filter->family == AF_INET6) &&
1780 x->props.family != filter->family)
1781 return false;
1782
1783 return addr_match(&x->props.saddr, &filter->saddr,
1784 filter->splen) &&
1785 addr_match(&x->id.daddr, &filter->daddr,
1786 filter->dplen);
1787 }
1788 return true;
1789}
1790
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001791int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001792 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 void *data)
1794{
Herbert Xu12a169e2008-10-01 07:03:24 -07001795 struct xfrm_state *state;
1796 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int err = 0;
1798
Herbert Xu12a169e2008-10-01 07:03:24 -07001799 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001800 return 0;
1801
Fan Du283bc9f2013-11-07 17:47:50 +08001802 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001803 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001804 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001805 else
Li RongQing80077702015-04-22 17:09:54 +08001806 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001807 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001808 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001809 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001810 state = container_of(x, struct xfrm_state, km);
1811 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001812 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001813 if (!__xfrm_state_filter_match(state, walk->filter))
1814 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001815 err = func(state, walk->seq, data);
1816 if (err) {
1817 list_move_tail(&walk->all, &x->all);
1818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001820 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001822 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 err = -ENOENT;
1824 goto out;
1825 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001826 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827out:
Fan Du283bc9f2013-11-07 17:47:50 +08001828 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return err;
1830}
1831EXPORT_SYMBOL(xfrm_state_walk);
1832
Nicolas Dichteld3623092014-02-14 15:30:36 +01001833void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001834 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001835{
Herbert Xu12a169e2008-10-01 07:03:24 -07001836 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001837 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001838 walk->state = XFRM_STATE_DEAD;
1839 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001840 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001841}
1842EXPORT_SYMBOL(xfrm_state_walk_init);
1843
Fan Du283bc9f2013-11-07 17:47:50 +08001844void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001845{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001846 kfree(walk->filter);
1847
Herbert Xu12a169e2008-10-01 07:03:24 -07001848 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001849 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001850
Fan Du283bc9f2013-11-07 17:47:50 +08001851 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001852 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001853 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001854}
1855EXPORT_SYMBOL(xfrm_state_walk_done);
1856
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001857static void xfrm_replay_timer_handler(unsigned long data)
1858{
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001859 struct xfrm_state *x = (struct xfrm_state *)data;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001860
1861 spin_lock(&x->lock);
1862
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001863 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001864 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001865 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001866 else
1867 x->xflags |= XFRM_TIME_DEFER;
1868 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001869
1870 spin_unlock(&x->lock);
1871}
1872
Denis Chengdf018122007-12-07 00:51:11 -08001873static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
David S. Miller214e0052011-02-24 00:02:38 -05001875void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 struct xfrm_mgr *km;
1878
Cong Wang85168c02013-01-16 16:05:06 +08001879 rcu_read_lock();
1880 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001881 if (km->notify_policy)
1882 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001883 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001884}
1885
David S. Miller214e0052011-02-24 00:02:38 -05001886void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001887{
1888 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001889 rcu_read_lock();
1890 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001891 if (km->notify)
1892 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001893 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001894}
1895
1896EXPORT_SYMBOL(km_policy_notify);
1897EXPORT_SYMBOL(km_state_notify);
1898
Eric W. Biederman15e47302012-09-07 20:12:54 +00001899void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001900{
1901 struct km_event c;
1902
Herbert Xubf088672005-06-18 22:44:00 -07001903 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001904 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001905 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001906 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001909EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001910/*
1911 * We send to all registered managers regardless of failure
1912 * We are happy with one success
1913*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001914int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001916 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 struct xfrm_mgr *km;
1918
Cong Wang85168c02013-01-16 16:05:06 +08001919 rcu_read_lock();
1920 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001921 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001922 if (!acqret)
1923 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Cong Wang85168c02013-01-16 16:05:06 +08001925 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return err;
1927}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001928EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Al Viro5d36b182006-11-08 00:24:06 -08001930int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 int err = -EINVAL;
1933 struct xfrm_mgr *km;
1934
Cong Wang85168c02013-01-16 16:05:06 +08001935 rcu_read_lock();
1936 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (km->new_mapping)
1938 err = km->new_mapping(x, ipaddr, sport);
1939 if (!err)
1940 break;
1941 }
Cong Wang85168c02013-01-16 16:05:06 +08001942 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return err;
1944}
1945EXPORT_SYMBOL(km_new_mapping);
1946
Eric W. Biederman15e47302012-09-07 20:12:54 +00001947void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001949 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Herbert Xubf088672005-06-18 22:44:00 -07001951 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001952 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001953 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001954 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
David S. Millera70fcb02006-03-20 19:18:52 -08001956EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001958#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001959int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1960 const struct xfrm_migrate *m, int num_migrate,
1961 const struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001962{
1963 int err = -EINVAL;
1964 int ret;
1965 struct xfrm_mgr *km;
1966
Cong Wang85168c02013-01-16 16:05:06 +08001967 rcu_read_lock();
1968 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001969 if (km->migrate) {
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001970 ret = km->migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001971 if (!ret)
1972 err = ret;
1973 }
1974 }
Cong Wang85168c02013-01-16 16:05:06 +08001975 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001976 return err;
1977}
1978EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001979#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001980
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001981int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001982{
1983 int err = -EINVAL;
1984 int ret;
1985 struct xfrm_mgr *km;
1986
Cong Wang85168c02013-01-16 16:05:06 +08001987 rcu_read_lock();
1988 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001989 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08001990 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001991 if (!ret)
1992 err = ret;
1993 }
1994 }
Cong Wang85168c02013-01-16 16:05:06 +08001995 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001996 return err;
1997}
1998EXPORT_SYMBOL(km_report);
1999
Horia Geanta0f245582014-02-12 16:20:06 +02002000bool km_is_alive(const struct km_event *c)
2001{
2002 struct xfrm_mgr *km;
2003 bool is_alive = false;
2004
2005 rcu_read_lock();
2006 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2007 if (km->is_alive && km->is_alive(c)) {
2008 is_alive = true;
2009 break;
2010 }
2011 }
2012 rcu_read_unlock();
2013
2014 return is_alive;
2015}
2016EXPORT_SYMBOL(km_is_alive);
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2019{
2020 int err;
2021 u8 *data;
2022 struct xfrm_mgr *km;
2023 struct xfrm_policy *pol = NULL;
2024
2025 if (optlen <= 0 || optlen > PAGE_SIZE)
2026 return -EMSGSIZE;
2027
2028 data = kmalloc(optlen, GFP_KERNEL);
2029 if (!data)
2030 return -ENOMEM;
2031
2032 err = -EFAULT;
2033 if (copy_from_user(data, optval, optlen))
2034 goto out;
2035
2036 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002037 rcu_read_lock();
2038 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002039 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 optlen, &err);
2041 if (err >= 0)
2042 break;
2043 }
Cong Wang85168c02013-01-16 16:05:06 +08002044 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 if (err >= 0) {
2047 xfrm_sk_policy_insert(sk, err, pol);
2048 xfrm_pol_put(pol);
2049 err = 0;
2050 }
2051
2052out:
2053 kfree(data);
2054 return err;
2055}
2056EXPORT_SYMBOL(xfrm_user_policy);
2057
Cong Wang85168c02013-01-16 16:05:06 +08002058static DEFINE_SPINLOCK(xfrm_km_lock);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060int xfrm_register_km(struct xfrm_mgr *km)
2061{
Cong Wang85168c02013-01-16 16:05:06 +08002062 spin_lock_bh(&xfrm_km_lock);
2063 list_add_tail_rcu(&km->list, &xfrm_km_list);
2064 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 0;
2066}
2067EXPORT_SYMBOL(xfrm_register_km);
2068
2069int xfrm_unregister_km(struct xfrm_mgr *km)
2070{
Cong Wang85168c02013-01-16 16:05:06 +08002071 spin_lock_bh(&xfrm_km_lock);
2072 list_del_rcu(&km->list);
2073 spin_unlock_bh(&xfrm_km_lock);
2074 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
2076}
2077EXPORT_SYMBOL(xfrm_unregister_km);
2078
2079int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2080{
2081 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002082
2083 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002085
Cong Wang44abdc32013-01-16 16:05:05 +08002086 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002088 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002089 else
Cong Wang44abdc32013-01-16 16:05:05 +08002090 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2091 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return err;
2093}
2094EXPORT_SYMBOL(xfrm_state_register_afinfo);
2095
2096int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2097{
Florian Westphal423826a2017-01-09 14:20:46 +01002098 int err = 0, family = afinfo->family;
2099
2100 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002102
Cong Wang44abdc32013-01-16 16:05:05 +08002103 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002105 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002107 else
Cong Wang44abdc32013-01-16 16:05:05 +08002108 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Cong Wang44abdc32013-01-16 16:05:05 +08002110 spin_unlock_bh(&xfrm_state_afinfo_lock);
2111 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return err;
2113}
2114EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2115
Florian Westphal711059b2017-01-09 14:20:48 +01002116struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2117{
2118 if (unlikely(family >= NPROTO))
2119 return NULL;
2120
2121 return rcu_dereference(xfrm_state_afinfo[family]);
2122}
2123
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002124struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 struct xfrm_state_afinfo *afinfo;
2127 if (unlikely(family >= NPROTO))
2128 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002129 rcu_read_lock();
2130 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002131 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002132 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return afinfo;
2134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2137void xfrm_state_delete_tunnel(struct xfrm_state *x)
2138{
2139 if (x->tunnel) {
2140 struct xfrm_state *t = x->tunnel;
2141
2142 if (atomic_read(&t->tunnel_users) == 2)
2143 xfrm_state_delete(t);
2144 atomic_dec(&t->tunnel_users);
2145 xfrm_state_put(t);
2146 x->tunnel = NULL;
2147 }
2148}
2149EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2150
2151int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2152{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002153 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Patrick McHardyc5c25232007-04-09 11:47:18 -07002155 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002156 type && type->get_mtu)
2157 return type->get_mtu(x, mtu);
2158
2159 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002162int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
Herbert Xu72cb6962005-06-20 13:18:08 -07002163{
Herbert Xud094cd82005-06-20 13:19:41 -07002164 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002165 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002166 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002167 int err;
2168
Herbert Xud094cd82005-06-20 13:19:41 -07002169 err = -EAFNOSUPPORT;
2170 afinfo = xfrm_state_get_afinfo(family);
2171 if (!afinfo)
2172 goto error;
2173
2174 err = 0;
2175 if (afinfo->init_flags)
2176 err = afinfo->init_flags(x);
2177
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002178 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002179
2180 if (err)
2181 goto error;
2182
2183 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002184
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002185 if (x->sel.family != AF_UNSPEC) {
2186 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2187 if (inner_mode == NULL)
2188 goto error;
2189
2190 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2191 family != x->sel.family) {
2192 xfrm_put_mode(inner_mode);
2193 goto error;
2194 }
2195
2196 x->inner_mode = inner_mode;
2197 } else {
2198 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002199 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002200
Martin Willid81d2282008-12-03 15:38:07 -08002201 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002202 if (inner_mode == NULL)
2203 goto error;
2204
2205 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2206 xfrm_put_mode(inner_mode);
2207 goto error;
2208 }
Martin Willid81d2282008-12-03 15:38:07 -08002209 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002210
Martin Willid81d2282008-12-03 15:38:07 -08002211 if (x->props.family == AF_INET)
2212 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002213
Martin Willid81d2282008-12-03 15:38:07 -08002214 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2215 if (inner_mode_iaf) {
2216 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2217 x->inner_mode_iaf = inner_mode_iaf;
2218 else
2219 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002220 }
2221 }
Herbert Xu13996372007-10-17 21:35:51 -07002222
Herbert Xud094cd82005-06-20 13:19:41 -07002223 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002224 if (x->type == NULL)
2225 goto error;
2226
Steffen Klassert9d389d72017-04-14 10:05:44 +02002227 x->type_offload = xfrm_get_type_offload(x->id.proto, family);
2228
Herbert Xu72cb6962005-06-20 13:18:08 -07002229 err = x->type->init_state(x);
2230 if (err)
2231 goto error;
2232
Herbert Xu13996372007-10-17 21:35:51 -07002233 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002234 if (x->outer_mode == NULL) {
2235 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002236 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002237 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002238
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002239 if (init_replay) {
2240 err = xfrm_init_replay(x);
2241 if (err)
2242 goto error;
2243 }
2244
Herbert Xu72cb6962005-06-20 13:18:08 -07002245 x->km.state = XFRM_STATE_VALID;
2246
2247error:
2248 return err;
2249}
2250
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002251EXPORT_SYMBOL(__xfrm_init_state);
2252
2253int xfrm_init_state(struct xfrm_state *x)
2254{
2255 return __xfrm_init_state(x, true);
2256}
2257
Herbert Xu72cb6962005-06-20 13:18:08 -07002258EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002259
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002260int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
David S. Millerf034b5d2006-08-24 03:08:07 -07002262 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002264 INIT_LIST_HEAD(&net->xfrm.state_all);
2265
David S. Millerf034b5d2006-08-24 03:08:07 -07002266 sz = sizeof(struct hlist_head) * 8;
2267
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002268 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2269 if (!net->xfrm.state_bydst)
2270 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002271 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2272 if (!net->xfrm.state_bysrc)
2273 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002274 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2275 if (!net->xfrm.state_byspi)
2276 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002277 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002278
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002279 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002280 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002281 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002282 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002283
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002284out_byspi:
2285 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002286out_bysrc:
2287 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002288out_bydst:
2289 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002290}
2291
2292void xfrm_state_fini(struct net *net)
2293{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002294 unsigned int sz;
2295
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002296 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002297 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002298 flush_work(&xfrm_state_gc_work);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002299
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002300 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002301
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002302 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002303 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2304 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002305 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2306 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002307 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2308 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Joy Lattenab5f5e82007-09-17 11:51:22 -07002311#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002312static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2313 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002314{
Paul Moore68277ac2007-12-20 20:49:33 -08002315 struct xfrm_sec_ctx *ctx = x->security;
2316 u32 spi = ntohl(x->id.spi);
2317
2318 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002319 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002320 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002321
Weilong Chen9b7a7872013-12-24 09:43:46 +08002322 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002323 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002324 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2325 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002326 break;
2327 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002328 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002329 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002330 break;
2331 }
Paul Moore68277ac2007-12-20 20:49:33 -08002332
2333 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002334}
2335
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002336static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2337 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002338{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002339 const struct iphdr *iph4;
2340 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002341
2342 switch (family) {
2343 case AF_INET:
2344 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002345 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2346 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002347 break;
2348 case AF_INET6:
2349 iph6 = ipv6_hdr(skb);
2350 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002351 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002352 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002353 iph6->flow_lbl[0] & 0x0f,
2354 iph6->flow_lbl[1],
2355 iph6->flow_lbl[2]);
2356 break;
2357 }
2358}
2359
Tetsuo Handa2e710292014-04-22 21:48:30 +09002360void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002361{
2362 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002363
Paul Mooreafeb14b2007-12-21 14:58:11 -08002364 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002365 if (audit_buf == NULL)
2366 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002367 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002368 xfrm_audit_helper_sainfo(x, audit_buf);
2369 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002370 audit_log_end(audit_buf);
2371}
2372EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2373
Tetsuo Handa2e710292014-04-22 21:48:30 +09002374void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002375{
2376 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002377
Paul Mooreafeb14b2007-12-21 14:58:11 -08002378 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002379 if (audit_buf == NULL)
2380 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002381 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002382 xfrm_audit_helper_sainfo(x, audit_buf);
2383 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002384 audit_log_end(audit_buf);
2385}
2386EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002387
2388void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2389 struct sk_buff *skb)
2390{
2391 struct audit_buffer *audit_buf;
2392 u32 spi;
2393
2394 audit_buf = xfrm_audit_start("SA-replay-overflow");
2395 if (audit_buf == NULL)
2396 return;
2397 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2398 /* don't record the sequence number because it's inherent in this kind
2399 * of audit message */
2400 spi = ntohl(x->id.spi);
2401 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2402 audit_log_end(audit_buf);
2403}
2404EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2405
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002406void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002407 struct sk_buff *skb, __be32 net_seq)
2408{
2409 struct audit_buffer *audit_buf;
2410 u32 spi;
2411
2412 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2413 if (audit_buf == NULL)
2414 return;
2415 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2416 spi = ntohl(x->id.spi);
2417 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2418 spi, spi, ntohl(net_seq));
2419 audit_log_end(audit_buf);
2420}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002421EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002422
2423void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2424{
2425 struct audit_buffer *audit_buf;
2426
2427 audit_buf = xfrm_audit_start("SA-notfound");
2428 if (audit_buf == NULL)
2429 return;
2430 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2431 audit_log_end(audit_buf);
2432}
2433EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2434
2435void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2436 __be32 net_spi, __be32 net_seq)
2437{
2438 struct audit_buffer *audit_buf;
2439 u32 spi;
2440
2441 audit_buf = xfrm_audit_start("SA-notfound");
2442 if (audit_buf == NULL)
2443 return;
2444 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2445 spi = ntohl(net_spi);
2446 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2447 spi, spi, ntohl(net_seq));
2448 audit_log_end(audit_buf);
2449}
2450EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2451
2452void xfrm_audit_state_icvfail(struct xfrm_state *x,
2453 struct sk_buff *skb, u8 proto)
2454{
2455 struct audit_buffer *audit_buf;
2456 __be32 net_spi;
2457 __be32 net_seq;
2458
2459 audit_buf = xfrm_audit_start("SA-icv-failure");
2460 if (audit_buf == NULL)
2461 return;
2462 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2463 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2464 u32 spi = ntohl(net_spi);
2465 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2466 spi, spi, ntohl(net_seq));
2467 }
2468 audit_log_end(audit_buf);
2469}
2470EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002471#endif /* CONFIG_AUDITSYSCALL */