blob: 23c92891758a829e06dd776baf7ca340dec67b9f [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);
Mathias Krause565f0fa2018-05-03 10:55:07 +020045static struct kmem_cache *xfrm_state_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Florian Westphal35db57bb2016-08-23 16:00:12 +020047static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
48static HLIST_HEAD(xfrm_state_gc_list);
49
Florian Westphal02efdff2016-08-09 12:16:05 +020050static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
51{
Reshetova, Elena88755e9c2017-07-04 15:53:21 +030052 return refcount_inc_not_zero(&x->refcnt);
Florian Westphal02efdff2016-08-09 12:16:05 +020053}
54
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080055static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050056 const xfrm_address_t *daddr,
57 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070058 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070059 unsigned short family)
60{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080061 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070062}
63
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080064static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050065 const xfrm_address_t *daddr,
66 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070067 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070068{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080069 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070070}
71
David S. Miller2575b652006-08-24 03:26:44 -070072static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050073xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
74 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070075{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080076 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070077}
78
David S. Millerf034b5d2006-08-24 03:08:07 -070079static void xfrm_hash_transfer(struct hlist_head *list,
80 struct hlist_head *ndsttable,
81 struct hlist_head *nsrctable,
82 struct hlist_head *nspitable,
83 unsigned int nhashmask)
84{
Sasha Levinb67bfe02013-02-27 17:06:00 -080085 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070086 struct xfrm_state *x;
87
Sasha Levinb67bfe02013-02-27 17:06:00 -080088 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070089 unsigned int h;
90
David S. Millerc1969f22006-08-24 04:00:03 -070091 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
92 x->props.reqid, x->props.family,
93 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020094 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070095
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070096 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
97 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070098 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020099 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -0700100
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700101 if (x->id.spi) {
102 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
103 x->id.proto, x->props.family,
104 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200105 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700106 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700107 }
108}
109
Alexey Dobriyan63082732008-11-25 17:19:07 -0800110static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700111{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800112 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700113}
114
Alexey Dobriyan63082732008-11-25 17:19:07 -0800115static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700116{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800117 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700118 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
119 unsigned long nsize, osize;
120 unsigned int nhashmask, ohashmask;
121 int i;
122
Alexey Dobriyan63082732008-11-25 17:19:07 -0800123 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700124 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700125 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800126 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700127 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700128 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700129 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800130 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700131 }
David S. Miller44e36b42006-08-24 04:50:50 -0700132 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700133 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700134 xfrm_hash_free(ndst, nsize);
135 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800136 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700137 }
138
Fan Du283bc9f2013-11-07 17:47:50 +0800139 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200140 write_seqcount_begin(&xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700141
142 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200143 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800144 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200145 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700146
Florian Westphalc8406992016-08-09 12:16:08 +0200147 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
148 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800149 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700150
Florian Westphalc8406992016-08-09 12:16:08 +0200151 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
152 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
153 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800154 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700155
Florian Westphalb65e3d72016-08-09 12:16:07 +0200156 write_seqcount_end(&xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800157 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700158
159 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200160
161 synchronize_rcu();
162
David S. Miller44e36b42006-08-24 04:50:50 -0700163 xfrm_hash_free(odst, osize);
164 xfrm_hash_free(osrc, osize);
165 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700166}
167
Cong Wang44abdc32013-01-16 16:05:05 +0800168static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
169static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static DEFINE_SPINLOCK(xfrm_state_gc_lock);
172
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800173int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800175int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Horia Geanta0f245582014-02-12 16:20:06 +0200176bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000177void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Cong Wang7a9885b2013-01-17 16:34:11 +0800179static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800180int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700181{
Cong Wang7a9885b2013-01-17 16:34:11 +0800182 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800183 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700184 int err = 0;
185
186 if (unlikely(afinfo == NULL))
187 return -EAFNOSUPPORT;
188 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800189 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700190
191 if (likely(typemap[type->proto] == NULL))
192 typemap[type->proto] = type;
193 else
194 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800195 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100196 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700197 return err;
198}
199EXPORT_SYMBOL(xfrm_register_type);
200
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800201int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700202{
Cong Wang7a9885b2013-01-17 16:34:11 +0800203 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800204 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700205 int err = 0;
206
207 if (unlikely(afinfo == NULL))
208 return -EAFNOSUPPORT;
209 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800210 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700211
212 if (unlikely(typemap[type->proto] != type))
213 err = -ENOENT;
214 else
215 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800216 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100217 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700218 return err;
219}
220EXPORT_SYMBOL(xfrm_unregister_type);
221
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800222static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700223{
224 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800225 const struct xfrm_type **typemap;
226 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700227 int modload_attempted = 0;
228
229retry:
230 afinfo = xfrm_state_get_afinfo(family);
231 if (unlikely(afinfo == NULL))
232 return NULL;
233 typemap = afinfo->type_map;
234
Florian Westphal75cda622017-01-09 14:20:49 +0100235 type = READ_ONCE(typemap[proto]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700236 if (unlikely(type && !try_module_get(type->owner)))
237 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100238
239 rcu_read_unlock();
240
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700241 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700242 request_module("xfrm-type-%d-%d", family, proto);
243 modload_attempted = 1;
244 goto retry;
245 }
246
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700247 return type;
248}
249
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800250static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700251{
252 module_put(type->owner);
253}
254
Steffen Klassert9d389d72017-04-14 10:05:44 +0200255static DEFINE_SPINLOCK(xfrm_type_offload_lock);
256int xfrm_register_type_offload(const struct xfrm_type_offload *type,
257 unsigned short family)
258{
259 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
260 const struct xfrm_type_offload **typemap;
261 int err = 0;
262
263 if (unlikely(afinfo == NULL))
264 return -EAFNOSUPPORT;
265 typemap = afinfo->type_offload_map;
266 spin_lock_bh(&xfrm_type_offload_lock);
267
268 if (likely(typemap[type->proto] == NULL))
269 typemap[type->proto] = type;
270 else
271 err = -EEXIST;
272 spin_unlock_bh(&xfrm_type_offload_lock);
273 rcu_read_unlock();
274 return err;
275}
276EXPORT_SYMBOL(xfrm_register_type_offload);
277
278int xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
279 unsigned short family)
280{
281 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
282 const struct xfrm_type_offload **typemap;
283 int err = 0;
284
285 if (unlikely(afinfo == NULL))
286 return -EAFNOSUPPORT;
287 typemap = afinfo->type_offload_map;
288 spin_lock_bh(&xfrm_type_offload_lock);
289
290 if (unlikely(typemap[type->proto] != type))
291 err = -ENOENT;
292 else
293 typemap[type->proto] = NULL;
294 spin_unlock_bh(&xfrm_type_offload_lock);
295 rcu_read_unlock();
296 return err;
297}
298EXPORT_SYMBOL(xfrm_unregister_type_offload);
299
Ilan Tayariffdb5212017-08-01 12:49:08 +0300300static const struct xfrm_type_offload *
301xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200302{
303 struct xfrm_state_afinfo *afinfo;
304 const struct xfrm_type_offload **typemap;
305 const struct xfrm_type_offload *type;
306
Ilan Tayariffdb5212017-08-01 12:49:08 +0300307retry:
Steffen Klassert9d389d72017-04-14 10:05:44 +0200308 afinfo = xfrm_state_get_afinfo(family);
309 if (unlikely(afinfo == NULL))
310 return NULL;
311 typemap = afinfo->type_offload_map;
312
313 type = typemap[proto];
314 if ((type && !try_module_get(type->owner)))
315 type = NULL;
316
Sabrina Dubroca2f10a612017-12-31 16:18:56 +0100317 rcu_read_unlock();
318
Ilan Tayariffdb5212017-08-01 12:49:08 +0300319 if (!type && try_load) {
320 request_module("xfrm-offload-%d-%d", family, proto);
Gustavo A. R. Silva545d8ae2018-01-22 16:34:09 -0600321 try_load = false;
Ilan Tayariffdb5212017-08-01 12:49:08 +0300322 goto retry;
323 }
324
Steffen Klassert9d389d72017-04-14 10:05:44 +0200325 return type;
326}
327
328static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
329{
330 module_put(type->owner);
331}
332
Cong Wang7a9885b2013-01-17 16:34:11 +0800333static DEFINE_SPINLOCK(xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700334int xfrm_register_mode(struct xfrm_mode *mode, int family)
335{
336 struct xfrm_state_afinfo *afinfo;
337 struct xfrm_mode **modemap;
338 int err;
339
340 if (unlikely(mode->encap >= XFRM_MODE_MAX))
341 return -EINVAL;
342
Cong Wang7a9885b2013-01-17 16:34:11 +0800343 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700344 if (unlikely(afinfo == NULL))
345 return -EAFNOSUPPORT;
346
347 err = -EEXIST;
348 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800349 spin_lock_bh(&xfrm_mode_lock);
Herbert Xu17c2a422007-10-17 21:33:12 -0700350 if (modemap[mode->encap])
351 goto out;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700352
Herbert Xu17c2a422007-10-17 21:33:12 -0700353 err = -ENOENT;
354 if (!try_module_get(afinfo->owner))
355 goto out;
356
357 mode->afinfo = afinfo;
358 modemap[mode->encap] = mode;
359 err = 0;
360
361out:
Cong Wang7a9885b2013-01-17 16:34:11 +0800362 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100363 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700364 return err;
365}
366EXPORT_SYMBOL(xfrm_register_mode);
367
368int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
369{
370 struct xfrm_state_afinfo *afinfo;
371 struct xfrm_mode **modemap;
372 int err;
373
374 if (unlikely(mode->encap >= XFRM_MODE_MAX))
375 return -EINVAL;
376
Cong Wang7a9885b2013-01-17 16:34:11 +0800377 afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700378 if (unlikely(afinfo == NULL))
379 return -EAFNOSUPPORT;
380
381 err = -ENOENT;
382 modemap = afinfo->mode_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800383 spin_lock_bh(&xfrm_mode_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700384 if (likely(modemap[mode->encap] == mode)) {
385 modemap[mode->encap] = NULL;
Herbert Xu17c2a422007-10-17 21:33:12 -0700386 module_put(mode->afinfo->owner);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700387 err = 0;
388 }
389
Cong Wang7a9885b2013-01-17 16:34:11 +0800390 spin_unlock_bh(&xfrm_mode_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100391 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700392 return err;
393}
394EXPORT_SYMBOL(xfrm_unregister_mode);
395
396static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
397{
398 struct xfrm_state_afinfo *afinfo;
399 struct xfrm_mode *mode;
400 int modload_attempted = 0;
401
402 if (unlikely(encap >= XFRM_MODE_MAX))
403 return NULL;
404
405retry:
406 afinfo = xfrm_state_get_afinfo(family);
407 if (unlikely(afinfo == NULL))
408 return NULL;
409
Florian Westphal75cda622017-01-09 14:20:49 +0100410 mode = READ_ONCE(afinfo->mode_map[encap]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700411 if (unlikely(mode && !try_module_get(mode->owner)))
412 mode = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100413
414 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700415 if (!mode && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700416 request_module("xfrm-mode-%d-%d", family, encap);
417 modload_attempted = 1;
418 goto retry;
419 }
420
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700421 return mode;
422}
423
424static void xfrm_put_mode(struct xfrm_mode *mode)
425{
426 module_put(mode->owner);
427}
428
Mathias Krause4a135e52018-11-21 21:09:23 +0100429void xfrm_state_free(struct xfrm_state *x)
430{
431 kmem_cache_free(xfrm_state_cache, x);
432}
433EXPORT_SYMBOL(xfrm_state_free);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static void xfrm_state_gc_destroy(struct xfrm_state *x)
436{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800437 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700438 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000439 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800440 kfree(x->aalg);
441 kfree(x->ealg);
442 kfree(x->calg);
443 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700444 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000445 kfree(x->replay_esn);
446 kfree(x->preplay_esn);
Herbert Xu13996372007-10-17 21:35:51 -0700447 if (x->inner_mode)
448 xfrm_put_mode(x->inner_mode);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700449 if (x->inner_mode_iaf)
450 xfrm_put_mode(x->inner_mode_iaf);
Herbert Xu13996372007-10-17 21:35:51 -0700451 if (x->outer_mode)
452 xfrm_put_mode(x->outer_mode);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200453 if (x->type_offload)
454 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (x->type) {
456 x->type->destructor(x);
457 xfrm_put_type(x->type);
458 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200459 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800460 security_xfrm_state_free(x);
Mathias Krause4a135e52018-11-21 21:09:23 +0100461 xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800464static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Herbert Xu12a169e2008-10-01 07:03:24 -0700466 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800467 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700468 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200471 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 spin_unlock_bh(&xfrm_state_gc_lock);
473
Florian Westphaldf7274e2016-08-09 12:16:06 +0200474 synchronize_rcu();
475
Sasha Levinb67bfe02013-02-27 17:06:00 -0800476 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 xfrm_state_gc_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800480static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800482 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
483 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200484 time64_t now = ktime_get_real_seconds();
485 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600487 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 spin_lock(&x->lock);
490 if (x->km.state == XFRM_STATE_DEAD)
491 goto out;
492 if (x->km.state == XFRM_STATE_EXPIRED)
493 goto expired;
494 if (x->lft.hard_add_expires_seconds) {
495 long tmo = x->lft.hard_add_expires_seconds +
496 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000497 if (tmo <= 0) {
498 if (x->xflags & XFRM_SOFT_EXPIRE) {
499 /* enter hard expire without soft expire first?!
500 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100501 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000502 */
503 x->curlft.add_time = now - x->saved_tmo - 1;
504 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
505 } else
506 goto expired;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (tmo < next)
509 next = tmo;
510 }
511 if (x->lft.hard_use_expires_seconds) {
512 long tmo = x->lft.hard_use_expires_seconds +
513 (x->curlft.use_time ? : now) - now;
514 if (tmo <= 0)
515 goto expired;
516 if (tmo < next)
517 next = tmo;
518 }
519 if (x->km.dying)
520 goto resched;
521 if (x->lft.soft_add_expires_seconds) {
522 long tmo = x->lft.soft_add_expires_seconds +
523 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000524 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000526 x->xflags &= ~XFRM_SOFT_EXPIRE;
527 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000529 x->xflags |= XFRM_SOFT_EXPIRE;
530 x->saved_tmo = tmo;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 if (x->lft.soft_use_expires_seconds) {
534 long tmo = x->lft.soft_use_expires_seconds +
535 (x->curlft.use_time ? : now) - now;
536 if (tmo <= 0)
537 warn = 1;
538 else if (tmo < next)
539 next = tmo;
540 }
541
Herbert Xu4666faa2005-06-18 22:43:22 -0700542 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800544 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545resched:
Arnd Bergmann386c5682018-07-11 12:19:13 +0200546 if (next != TIME64_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800547 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
548 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto out;
551
552expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200553 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600555
556 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200557 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800558 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Tetsuo Handa2e710292014-04-22 21:48:30 +0900560 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562out:
563 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800564 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Kees Cooke99e88a2017-10-16 14:43:17 -0700567static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800568
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800569struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct xfrm_state *x;
572
Mathias Krause565f0fa2018-05-03 10:55:07 +0200573 x = kmem_cache_alloc(xfrm_state_cache, GFP_ATOMIC | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800576 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300577 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700579 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700580 INIT_HLIST_NODE(&x->bydst);
581 INIT_HLIST_NODE(&x->bysrc);
582 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800583 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
584 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Kees Cooke99e88a2017-10-16 14:43:17 -0700585 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200586 x->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 x->lft.soft_byte_limit = XFRM_INF;
588 x->lft.soft_packet_limit = XFRM_INF;
589 x->lft.hard_byte_limit = XFRM_INF;
590 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800591 x->replay_maxage = 0;
592 x->replay_maxdiff = 0;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700593 x->inner_mode = NULL;
594 x->inner_mode_iaf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock_init(&x->lock);
596 }
597 return x;
598}
599EXPORT_SYMBOL(xfrm_state_alloc);
600
601void __xfrm_state_destroy(struct xfrm_state *x)
602{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700603 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200606 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 spin_unlock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200608 schedule_work(&xfrm_state_gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610EXPORT_SYMBOL(__xfrm_state_destroy);
611
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800612int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800614 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700615 int err = -ESRCH;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (x->km.state != XFRM_STATE_DEAD) {
618 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800619 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700620 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200621 hlist_del_rcu(&x->bydst);
622 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700623 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200624 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800625 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800626 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200628 xfrm_dev_state_delete(x);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* All xfrm_state objects are created by xfrm_state_alloc.
631 * The xfrm_state_alloc call gives a reference, and that
632 * is what we are dropping here.
633 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800634 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700635 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700637
638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800640EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700642int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700644 int err;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700647 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700649
650 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652EXPORT_SYMBOL(xfrm_state_delete);
653
Joy Latten4aa2e622007-06-04 19:05:57 -0400654#ifdef CONFIG_SECURITY_NETWORK_XFRM
655static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900656xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Joy Latten4aa2e622007-06-04 19:05:57 -0400658 int i, err = 0;
659
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800660 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400661 struct xfrm_state *x;
662
Sasha Levinb67bfe02013-02-27 17:06:00 -0800663 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400664 if (xfrm_id_proto_match(x->id.proto, proto) &&
665 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900666 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400667 return err;
668 }
669 }
670 }
671
672 return err;
673}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200674
675static inline int
676xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
677{
678 int i, err = 0;
679
680 for (i = 0; i <= net->xfrm.state_hmask; i++) {
681 struct xfrm_state *x;
682 struct xfrm_state_offload *xso;
683
684 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
685 xso = &x->xso;
686
687 if (xso->dev == dev &&
688 (err = security_xfrm_state_delete(x)) != 0) {
689 xfrm_audit_state_delete(x, 0, task_valid);
690 return err;
691 }
692 }
693 }
694
695 return err;
696}
Joy Latten4aa2e622007-06-04 19:05:57 -0400697#else
698static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900699xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400700{
701 return 0;
702}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200703
704static inline int
705xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
706{
707 return 0;
708}
Joy Latten4aa2e622007-06-04 19:05:57 -0400709#endif
710
Tetsuo Handa2e710292014-04-22 21:48:30 +0900711int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400712{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000713 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Fan Du283bc9f2013-11-07 17:47:50 +0800715 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900716 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400717 if (err)
718 goto out;
719
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000720 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800721 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700722 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800724 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700726 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800728 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Joy Latten161a09e2006-11-27 13:11:54 -0600730 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700731 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900732 task_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000734 if (!err)
735 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Fan Du283bc9f2013-11-07 17:47:50 +0800737 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto restart;
739 }
740 }
741 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200742out:
743 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200744 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000745 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200746
Joy Latten4aa2e622007-06-04 19:05:57 -0400747 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749EXPORT_SYMBOL(xfrm_state_flush);
750
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200751int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
752{
753 int i, err = 0, cnt = 0;
754
755 spin_lock_bh(&net->xfrm.xfrm_state_lock);
756 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
757 if (err)
758 goto out;
759
760 err = -ESRCH;
761 for (i = 0; i <= net->xfrm.state_hmask; i++) {
762 struct xfrm_state *x;
763 struct xfrm_state_offload *xso;
764restart:
765 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
766 xso = &x->xso;
767
768 if (!xfrm_state_kern(x) && xso->dev == dev) {
769 xfrm_state_hold(x);
770 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
771
772 err = xfrm_state_delete(x);
773 xfrm_audit_state_delete(x, err ? 0 : 1,
774 task_valid);
775 xfrm_state_put(x);
776 if (!err)
777 cnt++;
778
779 spin_lock_bh(&net->xfrm.xfrm_state_lock);
780 goto restart;
781 }
782 }
783 }
784 if (cnt)
785 err = 0;
786
787out:
788 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
789 return err;
790}
791EXPORT_SYMBOL(xfrm_dev_state_flush);
792
Alexey Dobriyane0710412010-01-23 13:37:10 +0000793void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700794{
Fan Du283bc9f2013-11-07 17:47:50 +0800795 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000796 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900797 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700798 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800799 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700800}
801EXPORT_SYMBOL(xfrm_sad_getinfo);
802
Florian Westphal711059b2017-01-09 14:20:48 +0100803static void
David S. Miller1a898592011-02-22 18:22:34 -0800804xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500805 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500806 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700807 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Florian Westphal711059b2017-01-09 14:20:48 +0100809 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
810
Florian Westphal3819a352017-01-13 14:55:14 +0100811 if (!afinfo)
812 return;
813
814 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700815
816 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100817 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700818 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100819 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700820 }
821 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
David S. Miller9aa60082011-02-24 01:51:36 -0500824static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
825 const xfrm_address_t *daddr,
826 __be32 spi, u8 proto,
827 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700828{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800829 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700830 struct xfrm_state *x;
831
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200832 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700833 if (x->props.family != family ||
834 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000835 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000836 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700837 continue;
838
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000839 if ((mark & x->mark.m) != x->mark.v)
840 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200841 if (!xfrm_state_hold_rcu(x))
842 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700843 return x;
844 }
845
846 return NULL;
847}
848
David S. Miller9aa60082011-02-24 01:51:36 -0500849static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
850 const xfrm_address_t *daddr,
851 const xfrm_address_t *saddr,
852 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700853{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800854 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700855 struct xfrm_state *x;
856
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200857 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700858 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000859 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000860 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
861 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700862 continue;
863
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000864 if ((mark & x->mark.m) != x->mark.v)
865 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200866 if (!xfrm_state_hold_rcu(x))
867 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700868 return x;
869 }
870
871 return NULL;
872}
873
874static inline struct xfrm_state *
875__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
876{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800877 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800878 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800879
David S. Milleredcd5822006-08-24 00:42:45 -0700880 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800881 return __xfrm_state_lookup(net, mark, &x->id.daddr,
882 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700883 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800884 return __xfrm_state_lookup_byaddr(net, mark,
885 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700886 &x->props.saddr,
887 x->id.proto, family);
888}
889
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800890static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700891{
892 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800893 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
894 net->xfrm.state_num > net->xfrm.state_hmask)
895 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700896}
897
David S. Miller08ec9af2009-03-13 14:22:40 -0700898static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800899 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700900 struct xfrm_state **best, int *acq_in_progress,
901 int *error)
902{
903 /* Resolution logic:
904 * 1. There is a valid state with matching selector. Done.
905 * 2. Valid state with inappropriate selector. Skip.
906 *
907 * Entering area of "sysdeps".
908 *
909 * 3. If state is not valid, selector is temporary, it selects
910 * only session which triggered previous resolution. Key
911 * manager will do something to install a state with proper
912 * selector.
913 */
914 if (x->km.state == XFRM_STATE_VALID) {
915 if ((x->sel.family &&
916 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
917 !security_xfrm_state_pol_flow_match(x, pol, fl))
918 return;
919
920 if (!*best ||
921 (*best)->km.dying > x->km.dying ||
922 ((*best)->km.dying == x->km.dying &&
923 (*best)->curlft.add_time < x->curlft.add_time))
924 *best = x;
925 } else if (x->km.state == XFRM_STATE_ACQ) {
926 *acq_in_progress = 1;
927 } else if (x->km.state == XFRM_STATE_ERROR ||
928 x->km.state == XFRM_STATE_EXPIRED) {
929 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
930 security_xfrm_state_pol_flow_match(x, pol, fl))
931 *error = -ESRCH;
932 }
933}
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500936xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800937 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -0700939 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
David S. Miller08ec9af2009-03-13 14:22:40 -0700941 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800942 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700943 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700944 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int acquire_in_progress = 0;
946 int error = 0;
947 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800948 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700949 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200950 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200951 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900952
David S. Miller37b08e32008-09-02 20:14:15 -0700953 to_put = NULL;
954
Florian Westphalb65e3d72016-08-09 12:16:07 +0200955 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
956
Florian Westphald737a582016-08-09 12:16:09 +0200957 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700958 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200959 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700960 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000962 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200963 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700964 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700965 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tmpl->mode == x->props.mode &&
967 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700968 (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);
971 }
Fan Du6f115632013-09-23 17:18:25 +0800972 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700973 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Thomas Egerer8444cf72010-09-20 11:11:38 -0700975 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200976 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700977 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700978 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000979 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200980 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700981 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800982 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700983 tmpl->mode == x->props.mode &&
984 tmpl->id.proto == x->id.proto &&
985 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500986 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700987 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
David S. Miller08ec9af2009-03-13 14:22:40 -0700990found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 x = best;
992 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700993 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800994 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700995 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700996 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 error = -EEXIST;
998 goto out;
999 }
Horia Geanta0f245582014-02-12 16:20:06 +02001000
1001 c.net = net;
1002 /* If the KMs have no listeners (yet...), avoid allocating an SA
1003 * for each and every packet - garbage collection might not
1004 * handle the flood.
1005 */
1006 if (!km_is_alive(&c)) {
1007 error = -ESRCH;
1008 goto out;
1009 }
1010
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001011 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (x == NULL) {
1013 error = -ENOMEM;
1014 goto out;
1015 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001016 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001018 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001019 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +02001020 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
David S. Miller1d28f422011-03-12 00:29:39 -05001022 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001023 if (error) {
1024 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001025 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001026 x = NULL;
1027 goto out;
1028 }
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001031 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001033 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001034 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001035 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001036 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001038 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001039 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001041 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001042 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001043 net->xfrm.state_num++;
1044 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001045 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 } else {
1047 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001048 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 x = NULL;
1050 error = -ESRCH;
1051 }
1052 }
1053out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001054 if (x) {
1055 if (!xfrm_state_hold_rcu(x)) {
1056 *err = -EAGAIN;
1057 x = NULL;
1058 }
1059 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001061 }
Florian Westphald737a582016-08-09 12:16:09 +02001062 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001063 if (to_put)
1064 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001065
1066 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1067 *err = -EAGAIN;
1068 if (x) {
1069 xfrm_state_put(x);
1070 x = NULL;
1071 }
1072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return x;
1075}
1076
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001077struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001078xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001079 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001080 unsigned short family, u8 mode, u8 proto, u32 reqid)
1081{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001082 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001083 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001084
Fan Du4ae770b2014-01-03 11:18:29 +08001085 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001086 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001087 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001088 if (x->props.family == family &&
1089 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001090 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001091 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001092 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1093 xfrm_state_addr_check(x, daddr, saddr, family) &&
1094 mode == x->props.mode &&
1095 proto == x->id.proto &&
1096 x->km.state == XFRM_STATE_VALID) {
1097 rx = x;
1098 break;
1099 }
1100 }
1101
1102 if (rx)
1103 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001104 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001105
1106
1107 return rx;
1108}
1109EXPORT_SYMBOL(xfrm_stateonly_find);
1110
Fan Duc4549972014-01-03 11:18:32 +08001111struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1112 unsigned short family)
1113{
1114 struct xfrm_state *x;
1115 struct xfrm_state_walk *w;
1116
1117 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1118 list_for_each_entry(w, &net->xfrm.state_all, all) {
1119 x = container_of(w, struct xfrm_state, km);
1120 if (x->props.family != family ||
1121 x->id.spi != spi)
1122 continue;
1123
Fan Duc4549972014-01-03 11:18:32 +08001124 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001125 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001126 return x;
1127 }
1128 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1129 return NULL;
1130}
1131EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static void __xfrm_state_insert(struct xfrm_state *x)
1134{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001135 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001136 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001138 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001139
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001140 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001141 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001142 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001144 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001145 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001147 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001148 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001149 x->props.family);
1150
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001151 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001152 }
1153
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001154 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -07001155 if (x->replay_maxage)
1156 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001157
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001158 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001159
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001160 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Fan Du283bc9f2013-11-07 17:47:50 +08001163/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001164static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1165{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001166 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001167 unsigned short family = xnew->props.family;
1168 u32 reqid = xnew->props.reqid;
1169 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001170 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001171 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001172 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001173
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001174 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001175 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001176 if (x->props.family == family &&
1177 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001178 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001179 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001180 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1181 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001182 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001183 }
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186void xfrm_state_insert(struct xfrm_state *x)
1187{
Fan Du283bc9f2013-11-07 17:47:50 +08001188 struct net *net = xs_net(x);
1189
1190 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001191 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001193 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195EXPORT_SYMBOL(xfrm_state_insert);
1196
Fan Du283bc9f2013-11-07 17:47:50 +08001197/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001198static struct xfrm_state *__find_acq_core(struct net *net,
1199 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001200 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001201 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001202 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001203 const xfrm_address_t *saddr,
1204 int create)
David S. Miller27708342006-08-24 00:13:10 -07001205{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001206 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001207 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001208 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001209
Sasha Levinb67bfe02013-02-27 17:06:00 -08001210 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001211 if (x->props.reqid != reqid ||
1212 x->props.mode != mode ||
1213 x->props.family != family ||
1214 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001215 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001216 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001217 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001218 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1219 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001220 continue;
1221
David S. Miller27708342006-08-24 00:13:10 -07001222 xfrm_state_hold(x);
1223 return x;
1224 }
1225
1226 if (!create)
1227 return NULL;
1228
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001229 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001230 if (likely(x)) {
1231 switch (family) {
1232 case AF_INET:
1233 x->sel.daddr.a4 = daddr->a4;
1234 x->sel.saddr.a4 = saddr->a4;
1235 x->sel.prefixlen_d = 32;
1236 x->sel.prefixlen_s = 32;
1237 x->props.saddr.a4 = saddr->a4;
1238 x->id.daddr.a4 = daddr->a4;
1239 break;
1240
1241 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001242 x->sel.daddr.in6 = daddr->in6;
1243 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001244 x->sel.prefixlen_d = 128;
1245 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001246 x->props.saddr.in6 = saddr->in6;
1247 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001248 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001249 }
David S. Miller27708342006-08-24 00:13:10 -07001250
1251 x->km.state = XFRM_STATE_ACQ;
1252 x->id.proto = proto;
1253 x->props.family = family;
1254 x->props.mode = mode;
1255 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001256 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001257 x->mark.v = m->v;
1258 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001259 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001260 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001261 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001262 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001263 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001264 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001265 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001266
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001267 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001268
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001269 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001270 }
1271
1272 return x;
1273}
1274
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001275static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277int xfrm_state_add(struct xfrm_state *x)
1278{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001279 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001280 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int family;
1282 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001283 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001284 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
David S. Miller37b08e32008-09-02 20:14:15 -07001288 to_put = NULL;
1289
Fan Du283bc9f2013-11-07 17:47:50 +08001290 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
David S. Milleredcd5822006-08-24 00:42:45 -07001292 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001294 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 x1 = NULL;
1296 err = -EEXIST;
1297 goto out;
1298 }
1299
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001300 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001301 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001302 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001303 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001304 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 x1 = NULL;
1306 }
1307 }
1308
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001309 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001310 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001311 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001312 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
David S. Millerc7f5ea32006-08-24 03:29:04 -07001314 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 __xfrm_state_insert(x);
1316 err = 0;
1317
1318out:
Fan Du283bc9f2013-11-07 17:47:50 +08001319 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (x1) {
1322 xfrm_state_delete(x1);
1323 xfrm_state_put(x1);
1324 }
1325
David S. Miller37b08e32008-09-02 20:14:15 -07001326 if (to_put)
1327 xfrm_state_put(to_put);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return err;
1330}
1331EXPORT_SYMBOL(xfrm_state_add);
1332
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001333#ifdef CONFIG_XFRM_MIGRATE
Antony Antony4ab47d42017-06-06 12:12:13 +02001334static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1335 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001336{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001337 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001338 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001339 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001340 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001341
1342 memcpy(&x->id, &orig->id, sizeof(x->id));
1343 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1344 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1345 x->props.mode = orig->props.mode;
1346 x->props.replay_window = orig->props.replay_window;
1347 x->props.reqid = orig->props.reqid;
1348 x->props.family = orig->props.family;
1349 x->props.saddr = orig->props.saddr;
1350
1351 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001352 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001353 if (!x->aalg)
1354 goto error;
1355 }
1356 x->props.aalgo = orig->props.aalgo;
1357
Steffen Klassertee5c2312014-02-19 13:33:24 +01001358 if (orig->aead) {
1359 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001360 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001361 if (!x->aead)
1362 goto error;
1363 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001364 if (orig->ealg) {
1365 x->ealg = xfrm_algo_clone(orig->ealg);
1366 if (!x->ealg)
1367 goto error;
1368 }
1369 x->props.ealgo = orig->props.ealgo;
1370
1371 if (orig->calg) {
1372 x->calg = xfrm_algo_clone(orig->calg);
1373 if (!x->calg)
1374 goto error;
1375 }
1376 x->props.calgo = orig->props.calgo;
1377
Antony Antony4ab47d42017-06-06 12:12:13 +02001378 if (encap || orig->encap) {
1379 if (encap)
1380 x->encap = kmemdup(encap, sizeof(*x->encap),
1381 GFP_KERNEL);
1382 else
1383 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1384 GFP_KERNEL);
1385
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001386 if (!x->encap)
1387 goto error;
1388 }
1389
1390 if (orig->coaddr) {
1391 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1392 GFP_KERNEL);
1393 if (!x->coaddr)
1394 goto error;
1395 }
1396
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001397 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001398 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001399 goto error;
1400 }
1401
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001402 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1403
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001404 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001405 goto error;
1406
1407 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001408 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001409
Steffen Klassert7e652642018-06-12 14:07:07 +02001410 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001411 x->tfcpad = orig->tfcpad;
1412 x->replay_maxdiff = orig->replay_maxdiff;
1413 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001414 x->curlft.add_time = orig->curlft.add_time;
1415 x->km.state = orig->km.state;
1416 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001417 x->replay = orig->replay;
1418 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001419
1420 return x;
1421
1422 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001423 xfrm_state_put(x);
1424out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001425 return NULL;
1426}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001427
Fan Du283bc9f2013-11-07 17:47:50 +08001428struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001429{
1430 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001431 struct xfrm_state *x = NULL;
1432
1433 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001434
1435 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001436 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001437 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001438 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001439 if (x->props.mode != m->mode ||
1440 x->id.proto != m->proto)
1441 continue;
1442 if (m->reqid && x->props.reqid != m->reqid)
1443 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001444 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1445 m->old_family) ||
1446 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1447 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001448 continue;
1449 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001450 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001451 }
1452 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001453 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001454 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001455 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001456 if (x->props.mode != m->mode ||
1457 x->id.proto != m->proto)
1458 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001459 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1460 m->old_family) ||
1461 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1462 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001463 continue;
1464 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001465 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001466 }
1467 }
1468
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001469 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1470
1471 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001472}
1473EXPORT_SYMBOL(xfrm_migrate_state_find);
1474
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001475struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001476 struct xfrm_migrate *m,
1477 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001478{
1479 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001480
Antony Antony4ab47d42017-06-06 12:12:13 +02001481 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001482 if (!xc)
1483 return NULL;
1484
1485 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1486 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1487
1488 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001489 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001490 /* a care is needed when the destination address of the
1491 state is to be updated as it is a part of triplet */
1492 xfrm_state_insert(xc);
1493 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001494 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001495 goto error;
1496 }
1497
1498 return xc;
1499error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001500 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001501 return NULL;
1502}
1503EXPORT_SYMBOL(xfrm_state_migrate);
1504#endif
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506int xfrm_state_update(struct xfrm_state *x)
1507{
David S. Miller37b08e32008-09-02 20:14:15 -07001508 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001510 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001511 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
David S. Miller37b08e32008-09-02 20:14:15 -07001513 to_put = NULL;
1514
Fan Du283bc9f2013-11-07 17:47:50 +08001515 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001516 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 err = -ESRCH;
1519 if (!x1)
1520 goto out;
1521
1522 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001523 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 err = -EEXIST;
1525 goto out;
1526 }
1527
1528 if (x1->km.state == XFRM_STATE_ACQ) {
1529 __xfrm_state_insert(x);
1530 x = NULL;
1531 }
1532 err = 0;
1533
1534out:
Fan Du283bc9f2013-11-07 17:47:50 +08001535 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
David S. Miller37b08e32008-09-02 20:14:15 -07001537 if (to_put)
1538 xfrm_state_put(to_put);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (err)
1541 return err;
1542
1543 if (!x) {
1544 xfrm_state_delete(x1);
1545 xfrm_state_put(x1);
1546 return 0;
1547 }
1548
1549 err = -EINVAL;
1550 spin_lock_bh(&x1->lock);
1551 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001552 if (x->encap && x1->encap &&
1553 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001555 else if (x->encap || x1->encap)
1556 goto fail;
1557
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001558 if (x->coaddr && x1->coaddr) {
1559 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1560 }
1561 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1562 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1564 x1->km.dying = 0;
1565
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001566 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (x1->curlft.use_time)
1568 xfrm_state_check_expire(x1);
1569
Nathan Harold5baf4f92018-07-19 19:07:47 -07001570 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001571 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1572
Nathan Harold5baf4f92018-07-19 19:07:47 -07001573 if (x->props.smark.m || x->props.smark.v)
1574 x1->props.smark = x->props.smark;
1575
1576 if (x->if_id)
1577 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001578
1579 __xfrm_state_bump_genids(x1);
1580 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1581 }
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001584 x->km.state = XFRM_STATE_DEAD;
1585 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001587
1588fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_unlock_bh(&x1->lock);
1590
1591 xfrm_state_put(x1);
1592
1593 return err;
1594}
1595EXPORT_SYMBOL(xfrm_state_update);
1596
1597int xfrm_state_check_expire(struct xfrm_state *x)
1598{
1599 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001600 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1603 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001604 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001605 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return -EINVAL;
1607 }
1608
1609 if (!x->km.dying &&
1610 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001611 x->curlft.packets >= x->lft.soft_packet_limit)) {
1612 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001613 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616}
1617EXPORT_SYMBOL(xfrm_state_check_expire);
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001620xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001621 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Florian Westphalc2f672f2016-09-20 15:45:26 +02001625 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001626 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001627 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return x;
1629}
1630EXPORT_SYMBOL(xfrm_state_lookup);
1631
1632struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001633xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001634 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001635 u8 proto, unsigned short family)
1636{
1637 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001638
Fan Du283bc9f2013-11-07 17:47:50 +08001639 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001640 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001641 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001642 return x;
1643}
1644EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1645
1646struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001647xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001648 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001649 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Fan Du283bc9f2013-11-07 17:47:50 +08001653 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001654 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001655 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return x;
1658}
1659EXPORT_SYMBOL(xfrm_find_acq);
1660
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001661#ifdef CONFIG_XFRM_SUB_POLICY
1662int
1663xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001664 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001665{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001666 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001667 int err = 0;
1668 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1669 if (!afinfo)
1670 return -EAFNOSUPPORT;
1671
Fan Du283bc9f2013-11-07 17:47:50 +08001672 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001673 if (afinfo->tmpl_sort)
1674 err = afinfo->tmpl_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001675 else
1676 for (i = 0; i < n; i++)
1677 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001678 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001679 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001680 return err;
1681}
1682EXPORT_SYMBOL(xfrm_tmpl_sort);
1683
1684int
1685xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1686 unsigned short family)
1687{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001688 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001689 int err = 0;
1690 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001691 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001692
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001693 if (!afinfo)
1694 return -EAFNOSUPPORT;
1695
Fan Du283bc9f2013-11-07 17:47:50 +08001696 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001697 if (afinfo->state_sort)
1698 err = afinfo->state_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001699 else
1700 for (i = 0; i < n; i++)
1701 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001702 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001703 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001704 return err;
1705}
1706EXPORT_SYMBOL(xfrm_state_sort);
1707#endif
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/* Silly enough, but I'm lazy to build resolution list */
1710
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001711static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001715 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001716 struct xfrm_state *x;
1717
Sasha Levinb67bfe02013-02-27 17:06:00 -08001718 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001719 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001720 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001721 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 xfrm_state_hold(x);
1723 return x;
1724 }
1725 }
1726 }
1727 return NULL;
1728}
1729
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001730struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 struct xfrm_state *x;
1733
Fan Du283bc9f2013-11-07 17:47:50 +08001734 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001735 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001736 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return x;
1738}
1739EXPORT_SYMBOL(xfrm_find_acq_byseq);
1740
1741u32 xfrm_get_acqseq(void)
1742{
1743 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001744 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
jamal6836b9b2010-02-16 02:01:22 +00001746 do {
1747 res = atomic_inc_return(&acqseq);
1748 } while (!res);
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return res;
1751}
1752EXPORT_SYMBOL(xfrm_get_acqseq);
1753
Fan Du776e9dd2013-12-16 18:47:49 +08001754int verify_spi_info(u8 proto, u32 min, u32 max)
1755{
1756 switch (proto) {
1757 case IPPROTO_AH:
1758 case IPPROTO_ESP:
1759 break;
1760
1761 case IPPROTO_COMP:
1762 /* IPCOMP spi is 16-bits. */
1763 if (max >= 0x10000)
1764 return -EINVAL;
1765 break;
1766
1767 default:
1768 return -EINVAL;
1769 }
1770
1771 if (min > max)
1772 return -EINVAL;
1773
1774 return 0;
1775}
1776EXPORT_SYMBOL(verify_spi_info);
1777
Herbert Xu658b2192007-10-09 13:29:52 -07001778int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001780 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001781 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001783 int err = -ENOENT;
1784 __be32 minspi = htonl(low);
1785 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001786 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Herbert Xu658b2192007-10-09 13:29:52 -07001788 spin_lock_bh(&x->lock);
1789 if (x->km.state == XFRM_STATE_DEAD)
1790 goto unlock;
1791
1792 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001794 goto unlock;
1795
1796 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001799 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (x0) {
1801 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001802 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804 x->id.spi = minspi;
1805 } else {
1806 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001807 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001808 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001809 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (x0 == NULL) {
1811 x->id.spi = htonl(spi);
1812 break;
1813 }
1814 xfrm_state_put(x0);
1815 }
1816 }
1817 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001818 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001819 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001820 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001821 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001822
1823 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
Herbert Xu658b2192007-10-09 13:29:52 -07001825
1826unlock:
1827 spin_unlock_bh(&x->lock);
1828
1829 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831EXPORT_SYMBOL(xfrm_alloc_spi);
1832
Nicolas Dichteld3623092014-02-14 15:30:36 +01001833static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001834 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001835{
1836 if (filter) {
1837 if ((filter->family == AF_INET ||
1838 filter->family == AF_INET6) &&
1839 x->props.family != filter->family)
1840 return false;
1841
1842 return addr_match(&x->props.saddr, &filter->saddr,
1843 filter->splen) &&
1844 addr_match(&x->id.daddr, &filter->daddr,
1845 filter->dplen);
1846 }
1847 return true;
1848}
1849
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001850int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001851 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 void *data)
1853{
Herbert Xu12a169e2008-10-01 07:03:24 -07001854 struct xfrm_state *state;
1855 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int err = 0;
1857
Herbert Xu12a169e2008-10-01 07:03:24 -07001858 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001859 return 0;
1860
Fan Du283bc9f2013-11-07 17:47:50 +08001861 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001862 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001863 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001864 else
Li RongQing80077702015-04-22 17:09:54 +08001865 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001866 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001867 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001868 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001869 state = container_of(x, struct xfrm_state, km);
1870 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001871 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001872 if (!__xfrm_state_filter_match(state, walk->filter))
1873 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001874 err = func(state, walk->seq, data);
1875 if (err) {
1876 list_move_tail(&walk->all, &x->all);
1877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001879 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001881 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 err = -ENOENT;
1883 goto out;
1884 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001885 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886out:
Fan Du283bc9f2013-11-07 17:47:50 +08001887 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return err;
1889}
1890EXPORT_SYMBOL(xfrm_state_walk);
1891
Nicolas Dichteld3623092014-02-14 15:30:36 +01001892void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001893 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001894{
Herbert Xu12a169e2008-10-01 07:03:24 -07001895 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001896 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001897 walk->state = XFRM_STATE_DEAD;
1898 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001899 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001900}
1901EXPORT_SYMBOL(xfrm_state_walk_init);
1902
Fan Du283bc9f2013-11-07 17:47:50 +08001903void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001904{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001905 kfree(walk->filter);
1906
Herbert Xu12a169e2008-10-01 07:03:24 -07001907 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001908 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001909
Fan Du283bc9f2013-11-07 17:47:50 +08001910 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001911 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001912 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001913}
1914EXPORT_SYMBOL(xfrm_state_walk_done);
1915
Kees Cooke99e88a2017-10-16 14:43:17 -07001916static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001917{
Kees Cooke99e88a2017-10-16 14:43:17 -07001918 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001919
1920 spin_lock(&x->lock);
1921
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001922 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001923 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001924 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001925 else
1926 x->xflags |= XFRM_TIME_DEFER;
1927 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001928
1929 spin_unlock(&x->lock);
1930}
1931
Denis Chengdf018122007-12-07 00:51:11 -08001932static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
David S. Miller214e0052011-02-24 00:02:38 -05001934void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 struct xfrm_mgr *km;
1937
Cong Wang85168c02013-01-16 16:05:06 +08001938 rcu_read_lock();
1939 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001940 if (km->notify_policy)
1941 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001942 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001943}
1944
David S. Miller214e0052011-02-24 00:02:38 -05001945void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001946{
1947 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001948 rcu_read_lock();
1949 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001950 if (km->notify)
1951 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001952 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001953}
1954
1955EXPORT_SYMBOL(km_policy_notify);
1956EXPORT_SYMBOL(km_state_notify);
1957
Eric W. Biederman15e47302012-09-07 20:12:54 +00001958void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001959{
1960 struct km_event c;
1961
Herbert Xubf088672005-06-18 22:44:00 -07001962 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001963 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001964 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001965 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001968EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001969/*
1970 * We send to all registered managers regardless of failure
1971 * We are happy with one success
1972*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001973int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001975 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 struct xfrm_mgr *km;
1977
Cong Wang85168c02013-01-16 16:05:06 +08001978 rcu_read_lock();
1979 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001980 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001981 if (!acqret)
1982 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Cong Wang85168c02013-01-16 16:05:06 +08001984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return err;
1986}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001987EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Al Viro5d36b182006-11-08 00:24:06 -08001989int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int err = -EINVAL;
1992 struct xfrm_mgr *km;
1993
Cong Wang85168c02013-01-16 16:05:06 +08001994 rcu_read_lock();
1995 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (km->new_mapping)
1997 err = km->new_mapping(x, ipaddr, sport);
1998 if (!err)
1999 break;
2000 }
Cong Wang85168c02013-01-16 16:05:06 +08002001 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return err;
2003}
2004EXPORT_SYMBOL(km_new_mapping);
2005
Eric W. Biederman15e47302012-09-07 20:12:54 +00002006void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002008 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Herbert Xubf088672005-06-18 22:44:00 -07002010 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002011 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002012 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002013 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
David S. Millera70fcb02006-03-20 19:18:52 -08002015EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002017#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002018int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2019 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002020 const struct xfrm_kmaddress *k,
2021 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002022{
2023 int err = -EINVAL;
2024 int ret;
2025 struct xfrm_mgr *km;
2026
Cong Wang85168c02013-01-16 16:05:06 +08002027 rcu_read_lock();
2028 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002029 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002030 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2031 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002032 if (!ret)
2033 err = ret;
2034 }
2035 }
Cong Wang85168c02013-01-16 16:05:06 +08002036 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002037 return err;
2038}
2039EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002040#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002041
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002042int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002043{
2044 int err = -EINVAL;
2045 int ret;
2046 struct xfrm_mgr *km;
2047
Cong Wang85168c02013-01-16 16:05:06 +08002048 rcu_read_lock();
2049 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002050 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002051 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002052 if (!ret)
2053 err = ret;
2054 }
2055 }
Cong Wang85168c02013-01-16 16:05:06 +08002056 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002057 return err;
2058}
2059EXPORT_SYMBOL(km_report);
2060
Horia Geanta0f245582014-02-12 16:20:06 +02002061bool km_is_alive(const struct km_event *c)
2062{
2063 struct xfrm_mgr *km;
2064 bool is_alive = false;
2065
2066 rcu_read_lock();
2067 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2068 if (km->is_alive && km->is_alive(c)) {
2069 is_alive = true;
2070 break;
2071 }
2072 }
2073 rcu_read_unlock();
2074
2075 return is_alive;
2076}
2077EXPORT_SYMBOL(km_is_alive);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2080{
2081 int err;
2082 u8 *data;
2083 struct xfrm_mgr *km;
2084 struct xfrm_policy *pol = NULL;
2085
Steffen Klassert19d7df62018-02-01 08:49:23 +01002086 if (in_compat_syscall())
2087 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002088
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002089 if (!optval && !optlen) {
2090 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2091 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2092 __sk_dst_reset(sk);
2093 return 0;
2094 }
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (optlen <= 0 || optlen > PAGE_SIZE)
2097 return -EMSGSIZE;
2098
Geliang Tanga133d932017-05-06 23:42:21 +08002099 data = memdup_user(optval, optlen);
2100 if (IS_ERR(data))
2101 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002104 rcu_read_lock();
2105 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002106 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 optlen, &err);
2108 if (err >= 0)
2109 break;
2110 }
Cong Wang85168c02013-01-16 16:05:06 +08002111 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 if (err >= 0) {
2114 xfrm_sk_policy_insert(sk, err, pol);
2115 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002116 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 err = 0;
2118 }
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 kfree(data);
2121 return err;
2122}
2123EXPORT_SYMBOL(xfrm_user_policy);
2124
Cong Wang85168c02013-01-16 16:05:06 +08002125static DEFINE_SPINLOCK(xfrm_km_lock);
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127int xfrm_register_km(struct xfrm_mgr *km)
2128{
Cong Wang85168c02013-01-16 16:05:06 +08002129 spin_lock_bh(&xfrm_km_lock);
2130 list_add_tail_rcu(&km->list, &xfrm_km_list);
2131 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return 0;
2133}
2134EXPORT_SYMBOL(xfrm_register_km);
2135
2136int xfrm_unregister_km(struct xfrm_mgr *km)
2137{
Cong Wang85168c02013-01-16 16:05:06 +08002138 spin_lock_bh(&xfrm_km_lock);
2139 list_del_rcu(&km->list);
2140 spin_unlock_bh(&xfrm_km_lock);
2141 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143}
2144EXPORT_SYMBOL(xfrm_unregister_km);
2145
2146int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2147{
2148 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002149
2150 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002152
Cong Wang44abdc32013-01-16 16:05:05 +08002153 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002155 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002156 else
Cong Wang44abdc32013-01-16 16:05:05 +08002157 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2158 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return err;
2160}
2161EXPORT_SYMBOL(xfrm_state_register_afinfo);
2162
2163int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2164{
Florian Westphal423826a2017-01-09 14:20:46 +01002165 int err = 0, family = afinfo->family;
2166
2167 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002169
Cong Wang44abdc32013-01-16 16:05:05 +08002170 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002172 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002174 else
Cong Wang44abdc32013-01-16 16:05:05 +08002175 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Cong Wang44abdc32013-01-16 16:05:05 +08002177 spin_unlock_bh(&xfrm_state_afinfo_lock);
2178 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return err;
2180}
2181EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2182
Florian Westphal711059b2017-01-09 14:20:48 +01002183struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2184{
2185 if (unlikely(family >= NPROTO))
2186 return NULL;
2187
2188 return rcu_dereference(xfrm_state_afinfo[family]);
2189}
2190
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002191struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 struct xfrm_state_afinfo *afinfo;
2194 if (unlikely(family >= NPROTO))
2195 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002196 rcu_read_lock();
2197 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002198 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002199 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return afinfo;
2201}
2202
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002203void xfrm_flush_gc(void)
2204{
2205 flush_work(&xfrm_state_gc_work);
2206}
2207EXPORT_SYMBOL(xfrm_flush_gc);
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2210void xfrm_state_delete_tunnel(struct xfrm_state *x)
2211{
2212 if (x->tunnel) {
2213 struct xfrm_state *t = x->tunnel;
2214
2215 if (atomic_read(&t->tunnel_users) == 2)
2216 xfrm_state_delete(t);
2217 atomic_dec(&t->tunnel_users);
2218 xfrm_state_put(t);
2219 x->tunnel = NULL;
2220 }
2221}
2222EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2223
2224int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2225{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002226 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Patrick McHardyc5c25232007-04-09 11:47:18 -07002228 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002229 type && type->get_mtu)
2230 return type->get_mtu(x, mtu);
2231
2232 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Ilan Tayariffdb5212017-08-01 12:49:08 +03002235int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002236{
Herbert Xud094cd82005-06-20 13:19:41 -07002237 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002238 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002239 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002240 int err;
2241
Herbert Xud094cd82005-06-20 13:19:41 -07002242 err = -EAFNOSUPPORT;
2243 afinfo = xfrm_state_get_afinfo(family);
2244 if (!afinfo)
2245 goto error;
2246
2247 err = 0;
2248 if (afinfo->init_flags)
2249 err = afinfo->init_flags(x);
2250
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002251 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002252
2253 if (err)
2254 goto error;
2255
2256 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002257
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002258 if (x->sel.family != AF_UNSPEC) {
2259 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2260 if (inner_mode == NULL)
2261 goto error;
2262
2263 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2264 family != x->sel.family) {
2265 xfrm_put_mode(inner_mode);
2266 goto error;
2267 }
2268
2269 x->inner_mode = inner_mode;
2270 } else {
2271 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002272 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002273
Martin Willid81d2282008-12-03 15:38:07 -08002274 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002275 if (inner_mode == NULL)
2276 goto error;
2277
2278 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2279 xfrm_put_mode(inner_mode);
2280 goto error;
2281 }
Martin Willid81d2282008-12-03 15:38:07 -08002282 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002283
Martin Willid81d2282008-12-03 15:38:07 -08002284 if (x->props.family == AF_INET)
2285 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002286
Martin Willid81d2282008-12-03 15:38:07 -08002287 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2288 if (inner_mode_iaf) {
2289 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2290 x->inner_mode_iaf = inner_mode_iaf;
2291 else
2292 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002293 }
2294 }
Herbert Xu13996372007-10-17 21:35:51 -07002295
Herbert Xud094cd82005-06-20 13:19:41 -07002296 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002297 if (x->type == NULL)
2298 goto error;
2299
Ilan Tayariffdb5212017-08-01 12:49:08 +03002300 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002301
Herbert Xu72cb6962005-06-20 13:18:08 -07002302 err = x->type->init_state(x);
2303 if (err)
2304 goto error;
2305
Herbert Xu13996372007-10-17 21:35:51 -07002306 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002307 if (x->outer_mode == NULL) {
2308 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002309 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002310 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002311
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002312 if (init_replay) {
2313 err = xfrm_init_replay(x);
2314 if (err)
2315 goto error;
2316 }
2317
Herbert Xu72cb6962005-06-20 13:18:08 -07002318error:
2319 return err;
2320}
2321
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002322EXPORT_SYMBOL(__xfrm_init_state);
2323
2324int xfrm_init_state(struct xfrm_state *x)
2325{
Yossi Kupermancc015722018-01-17 15:52:41 +02002326 int err;
2327
2328 err = __xfrm_init_state(x, true, false);
2329 if (!err)
2330 x->km.state = XFRM_STATE_VALID;
2331
2332 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002333}
2334
Herbert Xu72cb6962005-06-20 13:18:08 -07002335EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002336
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002337int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
David S. Millerf034b5d2006-08-24 03:08:07 -07002339 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Mathias Krause565f0fa2018-05-03 10:55:07 +02002341 if (net_eq(net, &init_net))
2342 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2343 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2344
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002345 INIT_LIST_HEAD(&net->xfrm.state_all);
2346
David S. Millerf034b5d2006-08-24 03:08:07 -07002347 sz = sizeof(struct hlist_head) * 8;
2348
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002349 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2350 if (!net->xfrm.state_bydst)
2351 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002352 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2353 if (!net->xfrm.state_bysrc)
2354 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002355 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2356 if (!net->xfrm.state_byspi)
2357 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002358 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002359
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002360 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002361 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002362 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002363 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002364
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002365out_byspi:
2366 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002367out_bysrc:
2368 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002369out_bydst:
2370 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002371}
2372
2373void xfrm_state_fini(struct net *net)
2374{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002375 unsigned int sz;
2376
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002377 flush_work(&net->xfrm.state_hash_work);
Tetsuo Handa2e710292014-04-22 21:48:30 +09002378 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002379 flush_work(&xfrm_state_gc_work);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002380
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002381 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002382
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002383 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002384 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2385 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002386 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2387 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002388 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2389 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Joy Lattenab5f5e82007-09-17 11:51:22 -07002392#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002393static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2394 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002395{
Paul Moore68277ac2007-12-20 20:49:33 -08002396 struct xfrm_sec_ctx *ctx = x->security;
2397 u32 spi = ntohl(x->id.spi);
2398
2399 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002400 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002401 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002402
Weilong Chen9b7a7872013-12-24 09:43:46 +08002403 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002404 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002405 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2406 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002407 break;
2408 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002409 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002410 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002411 break;
2412 }
Paul Moore68277ac2007-12-20 20:49:33 -08002413
2414 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002415}
2416
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002417static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2418 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002419{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002420 const struct iphdr *iph4;
2421 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002422
2423 switch (family) {
2424 case AF_INET:
2425 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002426 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2427 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002428 break;
2429 case AF_INET6:
2430 iph6 = ipv6_hdr(skb);
2431 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002432 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002433 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002434 iph6->flow_lbl[0] & 0x0f,
2435 iph6->flow_lbl[1],
2436 iph6->flow_lbl[2]);
2437 break;
2438 }
2439}
2440
Tetsuo Handa2e710292014-04-22 21:48:30 +09002441void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002442{
2443 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002444
Paul Mooreafeb14b2007-12-21 14:58:11 -08002445 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002446 if (audit_buf == NULL)
2447 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002448 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002449 xfrm_audit_helper_sainfo(x, audit_buf);
2450 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002451 audit_log_end(audit_buf);
2452}
2453EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2454
Tetsuo Handa2e710292014-04-22 21:48:30 +09002455void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002456{
2457 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002458
Paul Mooreafeb14b2007-12-21 14:58:11 -08002459 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002460 if (audit_buf == NULL)
2461 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002462 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002463 xfrm_audit_helper_sainfo(x, audit_buf);
2464 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002465 audit_log_end(audit_buf);
2466}
2467EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002468
2469void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2470 struct sk_buff *skb)
2471{
2472 struct audit_buffer *audit_buf;
2473 u32 spi;
2474
2475 audit_buf = xfrm_audit_start("SA-replay-overflow");
2476 if (audit_buf == NULL)
2477 return;
2478 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2479 /* don't record the sequence number because it's inherent in this kind
2480 * of audit message */
2481 spi = ntohl(x->id.spi);
2482 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2483 audit_log_end(audit_buf);
2484}
2485EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2486
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002487void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002488 struct sk_buff *skb, __be32 net_seq)
2489{
2490 struct audit_buffer *audit_buf;
2491 u32 spi;
2492
2493 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2494 if (audit_buf == NULL)
2495 return;
2496 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2497 spi = ntohl(x->id.spi);
2498 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2499 spi, spi, ntohl(net_seq));
2500 audit_log_end(audit_buf);
2501}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002502EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002503
2504void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2505{
2506 struct audit_buffer *audit_buf;
2507
2508 audit_buf = xfrm_audit_start("SA-notfound");
2509 if (audit_buf == NULL)
2510 return;
2511 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2512 audit_log_end(audit_buf);
2513}
2514EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2515
2516void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2517 __be32 net_spi, __be32 net_seq)
2518{
2519 struct audit_buffer *audit_buf;
2520 u32 spi;
2521
2522 audit_buf = xfrm_audit_start("SA-notfound");
2523 if (audit_buf == NULL)
2524 return;
2525 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2526 spi = ntohl(net_spi);
2527 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2528 spi, spi, ntohl(net_seq));
2529 audit_log_end(audit_buf);
2530}
2531EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2532
2533void xfrm_audit_state_icvfail(struct xfrm_state *x,
2534 struct sk_buff *skb, u8 proto)
2535{
2536 struct audit_buffer *audit_buf;
2537 __be32 net_spi;
2538 __be32 net_seq;
2539
2540 audit_buf = xfrm_audit_start("SA-icv-failure");
2541 if (audit_buf == NULL)
2542 return;
2543 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2544 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2545 u32 spi = ntohl(net_spi);
2546 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2547 spi, spi, ntohl(net_seq));
2548 }
2549 audit_log_end(audit_buf);
2550}
2551EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002552#endif /* CONFIG_AUDITSYSCALL */