blob: 1bb971f46fc6f9096f59c99740d6e832276c3b34 [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
Cong Wangf75a2802019-01-31 13:05:49 -0800435static void ___xfrm_state_destroy(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
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)
Cong Wangf75a2802019-01-31 13:05:49 -0800477 ___xfrm_state_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
Cong Wangf75a2802019-01-31 13:05:49 -0800601void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
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
Cong Wangf75a2802019-01-31 13:05:49 -0800605 if (sync) {
606 synchronize_rcu();
607 ___xfrm_state_destroy(x);
608 } else {
609 spin_lock_bh(&xfrm_state_gc_lock);
610 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
611 spin_unlock_bh(&xfrm_state_gc_lock);
612 schedule_work(&xfrm_state_gc_work);
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615EXPORT_SYMBOL(__xfrm_state_destroy);
616
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800617int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800619 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700620 int err = -ESRCH;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (x->km.state != XFRM_STATE_DEAD) {
623 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800624 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700625 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200626 hlist_del_rcu(&x->bydst);
627 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700628 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200629 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800630 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800631 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200633 xfrm_dev_state_delete(x);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* All xfrm_state objects are created by xfrm_state_alloc.
636 * The xfrm_state_alloc call gives a reference, and that
637 * is what we are dropping here.
638 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800639 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700640 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700642
643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800645EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700647int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700649 int err;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700652 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700654
655 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657EXPORT_SYMBOL(xfrm_state_delete);
658
Joy Latten4aa2e622007-06-04 19:05:57 -0400659#ifdef CONFIG_SECURITY_NETWORK_XFRM
660static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900661xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Joy Latten4aa2e622007-06-04 19:05:57 -0400663 int i, err = 0;
664
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800665 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400666 struct xfrm_state *x;
667
Sasha Levinb67bfe02013-02-27 17:06:00 -0800668 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400669 if (xfrm_id_proto_match(x->id.proto, proto) &&
670 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900671 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400672 return err;
673 }
674 }
675 }
676
677 return err;
678}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200679
680static inline int
681xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
682{
683 int i, err = 0;
684
685 for (i = 0; i <= net->xfrm.state_hmask; i++) {
686 struct xfrm_state *x;
687 struct xfrm_state_offload *xso;
688
689 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
690 xso = &x->xso;
691
692 if (xso->dev == dev &&
693 (err = security_xfrm_state_delete(x)) != 0) {
694 xfrm_audit_state_delete(x, 0, task_valid);
695 return err;
696 }
697 }
698 }
699
700 return err;
701}
Joy Latten4aa2e622007-06-04 19:05:57 -0400702#else
703static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900704xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400705{
706 return 0;
707}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200708
709static inline int
710xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
711{
712 return 0;
713}
Joy Latten4aa2e622007-06-04 19:05:57 -0400714#endif
715
Cong Wangf75a2802019-01-31 13:05:49 -0800716int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
Joy Latten4aa2e622007-06-04 19:05:57 -0400717{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000718 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Fan Du283bc9f2013-11-07 17:47:50 +0800720 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900721 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400722 if (err)
723 goto out;
724
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000725 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800726 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700727 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800729 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700731 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800733 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Joy Latten161a09e2006-11-27 13:11:54 -0600735 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700736 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900737 task_valid);
Cong Wangf75a2802019-01-31 13:05:49 -0800738 if (sync)
739 xfrm_state_put_sync(x);
740 else
741 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000742 if (!err)
743 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Fan Du283bc9f2013-11-07 17:47:50 +0800745 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto restart;
747 }
748 }
749 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200750out:
751 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200752 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000753 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200754
Joy Latten4aa2e622007-06-04 19:05:57 -0400755 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757EXPORT_SYMBOL(xfrm_state_flush);
758
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200759int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
760{
761 int i, err = 0, cnt = 0;
762
763 spin_lock_bh(&net->xfrm.xfrm_state_lock);
764 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
765 if (err)
766 goto out;
767
768 err = -ESRCH;
769 for (i = 0; i <= net->xfrm.state_hmask; i++) {
770 struct xfrm_state *x;
771 struct xfrm_state_offload *xso;
772restart:
773 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
774 xso = &x->xso;
775
776 if (!xfrm_state_kern(x) && xso->dev == dev) {
777 xfrm_state_hold(x);
778 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
779
780 err = xfrm_state_delete(x);
781 xfrm_audit_state_delete(x, err ? 0 : 1,
782 task_valid);
783 xfrm_state_put(x);
784 if (!err)
785 cnt++;
786
787 spin_lock_bh(&net->xfrm.xfrm_state_lock);
788 goto restart;
789 }
790 }
791 }
792 if (cnt)
793 err = 0;
794
795out:
796 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
797 return err;
798}
799EXPORT_SYMBOL(xfrm_dev_state_flush);
800
Alexey Dobriyane0710412010-01-23 13:37:10 +0000801void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700802{
Fan Du283bc9f2013-11-07 17:47:50 +0800803 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000804 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900805 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700806 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800807 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700808}
809EXPORT_SYMBOL(xfrm_sad_getinfo);
810
Florian Westphal711059b2017-01-09 14:20:48 +0100811static void
David S. Miller1a898592011-02-22 18:22:34 -0800812xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500813 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500814 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700815 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Florian Westphal711059b2017-01-09 14:20:48 +0100817 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
818
Florian Westphal3819a352017-01-13 14:55:14 +0100819 if (!afinfo)
820 return;
821
822 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700823
824 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100825 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700826 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100827 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700828 }
829 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
David S. Miller9aa60082011-02-24 01:51:36 -0500832static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
833 const xfrm_address_t *daddr,
834 __be32 spi, u8 proto,
835 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700836{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800837 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700838 struct xfrm_state *x;
839
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200840 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700841 if (x->props.family != family ||
842 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000843 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000844 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700845 continue;
846
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000847 if ((mark & x->mark.m) != x->mark.v)
848 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200849 if (!xfrm_state_hold_rcu(x))
850 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700851 return x;
852 }
853
854 return NULL;
855}
856
David S. Miller9aa60082011-02-24 01:51:36 -0500857static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
858 const xfrm_address_t *daddr,
859 const xfrm_address_t *saddr,
860 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700861{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800862 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700863 struct xfrm_state *x;
864
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200865 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700866 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000867 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000868 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
869 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700870 continue;
871
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000872 if ((mark & x->mark.m) != x->mark.v)
873 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200874 if (!xfrm_state_hold_rcu(x))
875 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700876 return x;
877 }
878
879 return NULL;
880}
881
882static inline struct xfrm_state *
883__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
884{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800885 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800886 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800887
David S. Milleredcd5822006-08-24 00:42:45 -0700888 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800889 return __xfrm_state_lookup(net, mark, &x->id.daddr,
890 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700891 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800892 return __xfrm_state_lookup_byaddr(net, mark,
893 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700894 &x->props.saddr,
895 x->id.proto, family);
896}
897
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800898static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700899{
900 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800901 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
902 net->xfrm.state_num > net->xfrm.state_hmask)
903 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700904}
905
David S. Miller08ec9af2009-03-13 14:22:40 -0700906static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800907 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700908 struct xfrm_state **best, int *acq_in_progress,
909 int *error)
910{
911 /* Resolution logic:
912 * 1. There is a valid state with matching selector. Done.
913 * 2. Valid state with inappropriate selector. Skip.
914 *
915 * Entering area of "sysdeps".
916 *
917 * 3. If state is not valid, selector is temporary, it selects
918 * only session which triggered previous resolution. Key
919 * manager will do something to install a state with proper
920 * selector.
921 */
922 if (x->km.state == XFRM_STATE_VALID) {
923 if ((x->sel.family &&
924 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
925 !security_xfrm_state_pol_flow_match(x, pol, fl))
926 return;
927
928 if (!*best ||
929 (*best)->km.dying > x->km.dying ||
930 ((*best)->km.dying == x->km.dying &&
931 (*best)->curlft.add_time < x->curlft.add_time))
932 *best = x;
933 } else if (x->km.state == XFRM_STATE_ACQ) {
934 *acq_in_progress = 1;
935 } else if (x->km.state == XFRM_STATE_ERROR ||
936 x->km.state == XFRM_STATE_EXPIRED) {
937 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
938 security_xfrm_state_pol_flow_match(x, pol, fl))
939 *error = -ESRCH;
940 }
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500944xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800945 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -0700947 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
David S. Miller08ec9af2009-03-13 14:22:40 -0700949 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800950 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700951 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700952 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int acquire_in_progress = 0;
954 int error = 0;
955 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800956 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700957 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200958 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200959 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900960
David S. Miller37b08e32008-09-02 20:14:15 -0700961 to_put = NULL;
962
Florian Westphalb65e3d72016-08-09 12:16:07 +0200963 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
964
Florian Westphald737a582016-08-09 12:16:09 +0200965 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700966 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200967 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700968 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000970 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200971 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700972 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700973 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 tmpl->mode == x->props.mode &&
975 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700976 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500977 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700978 &best, &acquire_in_progress, &error);
979 }
Fan Du6f115632013-09-23 17:18:25 +0800980 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700981 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Thomas Egerer8444cf72010-09-20 11:11:38 -0700983 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200984 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700985 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700986 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000987 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200988 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700989 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800990 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700991 tmpl->mode == x->props.mode &&
992 tmpl->id.proto == x->id.proto &&
993 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500994 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700995 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997
David S. Miller08ec9af2009-03-13 14:22:40 -0700998found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 x = best;
1000 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -07001001 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001002 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -07001003 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -07001004 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 error = -EEXIST;
1006 goto out;
1007 }
Horia Geanta0f245582014-02-12 16:20:06 +02001008
1009 c.net = net;
1010 /* If the KMs have no listeners (yet...), avoid allocating an SA
1011 * for each and every packet - garbage collection might not
1012 * handle the flood.
1013 */
1014 if (!km_is_alive(&c)) {
1015 error = -ESRCH;
1016 goto out;
1017 }
1018
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001019 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (x == NULL) {
1021 error = -ENOMEM;
1022 goto out;
1023 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001024 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001026 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001027 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +02001028 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
David S. Miller1d28f422011-03-12 00:29:39 -05001030 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001031 if (error) {
1032 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001033 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001034 x = NULL;
1035 goto out;
1036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001039 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001041 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001042 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001043 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001044 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001046 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001047 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001049 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001050 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001051 net->xfrm.state_num++;
1052 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001053 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 } else {
1055 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001056 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 x = NULL;
1058 error = -ESRCH;
1059 }
1060 }
1061out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001062 if (x) {
1063 if (!xfrm_state_hold_rcu(x)) {
1064 *err = -EAGAIN;
1065 x = NULL;
1066 }
1067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001069 }
Florian Westphald737a582016-08-09 12:16:09 +02001070 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001071 if (to_put)
1072 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001073
1074 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1075 *err = -EAGAIN;
1076 if (x) {
1077 xfrm_state_put(x);
1078 x = NULL;
1079 }
1080 }
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return x;
1083}
1084
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001085struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001086xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001087 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001088 unsigned short family, u8 mode, u8 proto, u32 reqid)
1089{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001090 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001091 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001092
Fan Du4ae770b2014-01-03 11:18:29 +08001093 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001094 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001095 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001096 if (x->props.family == family &&
1097 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001098 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001099 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001100 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1101 xfrm_state_addr_check(x, daddr, saddr, family) &&
1102 mode == x->props.mode &&
1103 proto == x->id.proto &&
1104 x->km.state == XFRM_STATE_VALID) {
1105 rx = x;
1106 break;
1107 }
1108 }
1109
1110 if (rx)
1111 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001112 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001113
1114
1115 return rx;
1116}
1117EXPORT_SYMBOL(xfrm_stateonly_find);
1118
Fan Duc4549972014-01-03 11:18:32 +08001119struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1120 unsigned short family)
1121{
1122 struct xfrm_state *x;
1123 struct xfrm_state_walk *w;
1124
1125 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1126 list_for_each_entry(w, &net->xfrm.state_all, all) {
1127 x = container_of(w, struct xfrm_state, km);
1128 if (x->props.family != family ||
1129 x->id.spi != spi)
1130 continue;
1131
Fan Duc4549972014-01-03 11:18:32 +08001132 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001133 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001134 return x;
1135 }
1136 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1137 return NULL;
1138}
1139EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static void __xfrm_state_insert(struct xfrm_state *x)
1142{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001143 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001144 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001146 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001147
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001148 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001149 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001150 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001152 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001153 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001155 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001156 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001157 x->props.family);
1158
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001159 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001160 }
1161
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001162 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -07001163 if (x->replay_maxage)
1164 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001165
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001166 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001167
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001168 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Fan Du283bc9f2013-11-07 17:47:50 +08001171/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001172static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1173{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001174 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001175 unsigned short family = xnew->props.family;
1176 u32 reqid = xnew->props.reqid;
1177 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001178 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001179 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001180 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001181
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001182 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001183 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001184 if (x->props.family == family &&
1185 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001186 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001187 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001188 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1189 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001190 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001191 }
1192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194void xfrm_state_insert(struct xfrm_state *x)
1195{
Fan Du283bc9f2013-11-07 17:47:50 +08001196 struct net *net = xs_net(x);
1197
1198 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001199 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001201 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203EXPORT_SYMBOL(xfrm_state_insert);
1204
Fan Du283bc9f2013-11-07 17:47:50 +08001205/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001206static struct xfrm_state *__find_acq_core(struct net *net,
1207 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001208 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001209 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001210 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001211 const xfrm_address_t *saddr,
1212 int create)
David S. Miller27708342006-08-24 00:13:10 -07001213{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001214 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001215 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001216 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001217
Sasha Levinb67bfe02013-02-27 17:06:00 -08001218 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001219 if (x->props.reqid != reqid ||
1220 x->props.mode != mode ||
1221 x->props.family != family ||
1222 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001223 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001224 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001225 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001226 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1227 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001228 continue;
1229
David S. Miller27708342006-08-24 00:13:10 -07001230 xfrm_state_hold(x);
1231 return x;
1232 }
1233
1234 if (!create)
1235 return NULL;
1236
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001237 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001238 if (likely(x)) {
1239 switch (family) {
1240 case AF_INET:
1241 x->sel.daddr.a4 = daddr->a4;
1242 x->sel.saddr.a4 = saddr->a4;
1243 x->sel.prefixlen_d = 32;
1244 x->sel.prefixlen_s = 32;
1245 x->props.saddr.a4 = saddr->a4;
1246 x->id.daddr.a4 = daddr->a4;
1247 break;
1248
1249 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001250 x->sel.daddr.in6 = daddr->in6;
1251 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001252 x->sel.prefixlen_d = 128;
1253 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001254 x->props.saddr.in6 = saddr->in6;
1255 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001256 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001257 }
David S. Miller27708342006-08-24 00:13:10 -07001258
1259 x->km.state = XFRM_STATE_ACQ;
1260 x->id.proto = proto;
1261 x->props.family = family;
1262 x->props.mode = mode;
1263 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001264 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001265 x->mark.v = m->v;
1266 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001267 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001268 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001269 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001270 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001271 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001272 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001273 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001274
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001275 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001276
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001277 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001278 }
1279
1280 return x;
1281}
1282
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001283static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285int xfrm_state_add(struct xfrm_state *x)
1286{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001287 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001288 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int family;
1290 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001291 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001292 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
David S. Miller37b08e32008-09-02 20:14:15 -07001296 to_put = NULL;
1297
Fan Du283bc9f2013-11-07 17:47:50 +08001298 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
David S. Milleredcd5822006-08-24 00:42:45 -07001300 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001302 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 x1 = NULL;
1304 err = -EEXIST;
1305 goto out;
1306 }
1307
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001308 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001309 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001310 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001311 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001312 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 x1 = NULL;
1314 }
1315 }
1316
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001317 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001318 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001319 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001320 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
David S. Millerc7f5ea32006-08-24 03:29:04 -07001322 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 __xfrm_state_insert(x);
1324 err = 0;
1325
1326out:
Fan Du283bc9f2013-11-07 17:47:50 +08001327 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (x1) {
1330 xfrm_state_delete(x1);
1331 xfrm_state_put(x1);
1332 }
1333
David S. Miller37b08e32008-09-02 20:14:15 -07001334 if (to_put)
1335 xfrm_state_put(to_put);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return err;
1338}
1339EXPORT_SYMBOL(xfrm_state_add);
1340
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001341#ifdef CONFIG_XFRM_MIGRATE
Antony Antony4ab47d42017-06-06 12:12:13 +02001342static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1343 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001344{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001345 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001346 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001347 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001348 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001349
1350 memcpy(&x->id, &orig->id, sizeof(x->id));
1351 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1352 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1353 x->props.mode = orig->props.mode;
1354 x->props.replay_window = orig->props.replay_window;
1355 x->props.reqid = orig->props.reqid;
1356 x->props.family = orig->props.family;
1357 x->props.saddr = orig->props.saddr;
1358
1359 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001360 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001361 if (!x->aalg)
1362 goto error;
1363 }
1364 x->props.aalgo = orig->props.aalgo;
1365
Steffen Klassertee5c2312014-02-19 13:33:24 +01001366 if (orig->aead) {
1367 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001368 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001369 if (!x->aead)
1370 goto error;
1371 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001372 if (orig->ealg) {
1373 x->ealg = xfrm_algo_clone(orig->ealg);
1374 if (!x->ealg)
1375 goto error;
1376 }
1377 x->props.ealgo = orig->props.ealgo;
1378
1379 if (orig->calg) {
1380 x->calg = xfrm_algo_clone(orig->calg);
1381 if (!x->calg)
1382 goto error;
1383 }
1384 x->props.calgo = orig->props.calgo;
1385
Antony Antony4ab47d42017-06-06 12:12:13 +02001386 if (encap || orig->encap) {
1387 if (encap)
1388 x->encap = kmemdup(encap, sizeof(*x->encap),
1389 GFP_KERNEL);
1390 else
1391 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1392 GFP_KERNEL);
1393
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001394 if (!x->encap)
1395 goto error;
1396 }
1397
1398 if (orig->coaddr) {
1399 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1400 GFP_KERNEL);
1401 if (!x->coaddr)
1402 goto error;
1403 }
1404
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001405 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001406 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001407 goto error;
1408 }
1409
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001410 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1411
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001412 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001413 goto error;
1414
1415 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001416 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001417
Steffen Klassert7e652642018-06-12 14:07:07 +02001418 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001419 x->tfcpad = orig->tfcpad;
1420 x->replay_maxdiff = orig->replay_maxdiff;
1421 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001422 x->curlft.add_time = orig->curlft.add_time;
1423 x->km.state = orig->km.state;
1424 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001425 x->replay = orig->replay;
1426 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001427
1428 return x;
1429
1430 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001431 xfrm_state_put(x);
1432out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001433 return NULL;
1434}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001435
Fan Du283bc9f2013-11-07 17:47:50 +08001436struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001437{
1438 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001439 struct xfrm_state *x = NULL;
1440
1441 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001442
1443 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001444 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001445 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001446 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001447 if (x->props.mode != m->mode ||
1448 x->id.proto != m->proto)
1449 continue;
1450 if (m->reqid && x->props.reqid != m->reqid)
1451 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001452 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1453 m->old_family) ||
1454 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1455 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001456 continue;
1457 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001458 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001459 }
1460 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001461 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001462 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001463 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001464 if (x->props.mode != m->mode ||
1465 x->id.proto != m->proto)
1466 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001467 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1468 m->old_family) ||
1469 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1470 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001471 continue;
1472 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001473 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001474 }
1475 }
1476
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001477 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1478
1479 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001480}
1481EXPORT_SYMBOL(xfrm_migrate_state_find);
1482
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001483struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001484 struct xfrm_migrate *m,
1485 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001486{
1487 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001488
Antony Antony4ab47d42017-06-06 12:12:13 +02001489 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001490 if (!xc)
1491 return NULL;
1492
1493 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1494 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1495
1496 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001497 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001498 /* a care is needed when the destination address of the
1499 state is to be updated as it is a part of triplet */
1500 xfrm_state_insert(xc);
1501 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001502 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001503 goto error;
1504 }
1505
1506 return xc;
1507error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001508 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001509 return NULL;
1510}
1511EXPORT_SYMBOL(xfrm_state_migrate);
1512#endif
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514int xfrm_state_update(struct xfrm_state *x)
1515{
David S. Miller37b08e32008-09-02 20:14:15 -07001516 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001518 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001519 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
David S. Miller37b08e32008-09-02 20:14:15 -07001521 to_put = NULL;
1522
Fan Du283bc9f2013-11-07 17:47:50 +08001523 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001524 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 err = -ESRCH;
1527 if (!x1)
1528 goto out;
1529
1530 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001531 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 err = -EEXIST;
1533 goto out;
1534 }
1535
1536 if (x1->km.state == XFRM_STATE_ACQ) {
1537 __xfrm_state_insert(x);
1538 x = NULL;
1539 }
1540 err = 0;
1541
1542out:
Fan Du283bc9f2013-11-07 17:47:50 +08001543 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
David S. Miller37b08e32008-09-02 20:14:15 -07001545 if (to_put)
1546 xfrm_state_put(to_put);
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (err)
1549 return err;
1550
1551 if (!x) {
1552 xfrm_state_delete(x1);
1553 xfrm_state_put(x1);
1554 return 0;
1555 }
1556
1557 err = -EINVAL;
1558 spin_lock_bh(&x1->lock);
1559 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001560 if (x->encap && x1->encap &&
1561 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001563 else if (x->encap || x1->encap)
1564 goto fail;
1565
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001566 if (x->coaddr && x1->coaddr) {
1567 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1568 }
1569 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1570 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1572 x1->km.dying = 0;
1573
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001574 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (x1->curlft.use_time)
1576 xfrm_state_check_expire(x1);
1577
Nathan Harold5baf4f92018-07-19 19:07:47 -07001578 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001579 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1580
Nathan Harold5baf4f92018-07-19 19:07:47 -07001581 if (x->props.smark.m || x->props.smark.v)
1582 x1->props.smark = x->props.smark;
1583
1584 if (x->if_id)
1585 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001586
1587 __xfrm_state_bump_genids(x1);
1588 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1589 }
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001592 x->km.state = XFRM_STATE_DEAD;
1593 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001595
1596fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 spin_unlock_bh(&x1->lock);
1598
1599 xfrm_state_put(x1);
1600
1601 return err;
1602}
1603EXPORT_SYMBOL(xfrm_state_update);
1604
1605int xfrm_state_check_expire(struct xfrm_state *x)
1606{
1607 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001608 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1611 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001612 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001613 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return -EINVAL;
1615 }
1616
1617 if (!x->km.dying &&
1618 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001619 x->curlft.packets >= x->lft.soft_packet_limit)) {
1620 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001621 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return 0;
1624}
1625EXPORT_SYMBOL(xfrm_state_check_expire);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001628xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001629 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
1631 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Florian Westphalc2f672f2016-09-20 15:45:26 +02001633 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001634 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return x;
1637}
1638EXPORT_SYMBOL(xfrm_state_lookup);
1639
1640struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001641xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001642 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001643 u8 proto, unsigned short family)
1644{
1645 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001646
Fan Du283bc9f2013-11-07 17:47:50 +08001647 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001648 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001649 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001650 return x;
1651}
1652EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1653
1654struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001655xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001656 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001657 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Fan Du283bc9f2013-11-07 17:47:50 +08001661 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001662 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001663 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return x;
1666}
1667EXPORT_SYMBOL(xfrm_find_acq);
1668
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001669#ifdef CONFIG_XFRM_SUB_POLICY
1670int
1671xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001672 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001673{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001674 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001675 int err = 0;
1676 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1677 if (!afinfo)
1678 return -EAFNOSUPPORT;
1679
Fan Du283bc9f2013-11-07 17:47:50 +08001680 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001681 if (afinfo->tmpl_sort)
1682 err = afinfo->tmpl_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001683 else
1684 for (i = 0; i < n; i++)
1685 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001686 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001687 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001688 return err;
1689}
1690EXPORT_SYMBOL(xfrm_tmpl_sort);
1691
1692int
1693xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1694 unsigned short family)
1695{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001696 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001697 int err = 0;
1698 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001699 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001700
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001701 if (!afinfo)
1702 return -EAFNOSUPPORT;
1703
Fan Du283bc9f2013-11-07 17:47:50 +08001704 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001705 if (afinfo->state_sort)
1706 err = afinfo->state_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001707 else
1708 for (i = 0; i < n; i++)
1709 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001710 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001711 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001712 return err;
1713}
1714EXPORT_SYMBOL(xfrm_state_sort);
1715#endif
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/* Silly enough, but I'm lazy to build resolution list */
1718
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001719static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001723 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001724 struct xfrm_state *x;
1725
Sasha Levinb67bfe02013-02-27 17:06:00 -08001726 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001727 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001728 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001729 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 xfrm_state_hold(x);
1731 return x;
1732 }
1733 }
1734 }
1735 return NULL;
1736}
1737
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001738struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 struct xfrm_state *x;
1741
Fan Du283bc9f2013-11-07 17:47:50 +08001742 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001743 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001744 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return x;
1746}
1747EXPORT_SYMBOL(xfrm_find_acq_byseq);
1748
1749u32 xfrm_get_acqseq(void)
1750{
1751 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001752 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
jamal6836b9b2010-02-16 02:01:22 +00001754 do {
1755 res = atomic_inc_return(&acqseq);
1756 } while (!res);
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return res;
1759}
1760EXPORT_SYMBOL(xfrm_get_acqseq);
1761
Fan Du776e9dd2013-12-16 18:47:49 +08001762int verify_spi_info(u8 proto, u32 min, u32 max)
1763{
1764 switch (proto) {
1765 case IPPROTO_AH:
1766 case IPPROTO_ESP:
1767 break;
1768
1769 case IPPROTO_COMP:
1770 /* IPCOMP spi is 16-bits. */
1771 if (max >= 0x10000)
1772 return -EINVAL;
1773 break;
1774
1775 default:
1776 return -EINVAL;
1777 }
1778
1779 if (min > max)
1780 return -EINVAL;
1781
1782 return 0;
1783}
1784EXPORT_SYMBOL(verify_spi_info);
1785
Herbert Xu658b2192007-10-09 13:29:52 -07001786int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001788 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001789 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001791 int err = -ENOENT;
1792 __be32 minspi = htonl(low);
1793 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001794 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Herbert Xu658b2192007-10-09 13:29:52 -07001796 spin_lock_bh(&x->lock);
1797 if (x->km.state == XFRM_STATE_DEAD)
1798 goto unlock;
1799
1800 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001802 goto unlock;
1803
1804 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001807 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (x0) {
1809 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001810 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 x->id.spi = minspi;
1813 } else {
1814 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001815 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001816 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001817 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (x0 == NULL) {
1819 x->id.spi = htonl(spi);
1820 break;
1821 }
1822 xfrm_state_put(x0);
1823 }
1824 }
1825 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001826 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001827 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001828 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001829 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001830
1831 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Herbert Xu658b2192007-10-09 13:29:52 -07001833
1834unlock:
1835 spin_unlock_bh(&x->lock);
1836
1837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839EXPORT_SYMBOL(xfrm_alloc_spi);
1840
Nicolas Dichteld3623092014-02-14 15:30:36 +01001841static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001842 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001843{
1844 if (filter) {
1845 if ((filter->family == AF_INET ||
1846 filter->family == AF_INET6) &&
1847 x->props.family != filter->family)
1848 return false;
1849
1850 return addr_match(&x->props.saddr, &filter->saddr,
1851 filter->splen) &&
1852 addr_match(&x->id.daddr, &filter->daddr,
1853 filter->dplen);
1854 }
1855 return true;
1856}
1857
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001858int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001859 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 void *data)
1861{
Herbert Xu12a169e2008-10-01 07:03:24 -07001862 struct xfrm_state *state;
1863 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int err = 0;
1865
Herbert Xu12a169e2008-10-01 07:03:24 -07001866 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001867 return 0;
1868
Fan Du283bc9f2013-11-07 17:47:50 +08001869 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001870 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001871 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001872 else
Li RongQing80077702015-04-22 17:09:54 +08001873 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001874 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001875 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001876 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001877 state = container_of(x, struct xfrm_state, km);
1878 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001879 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001880 if (!__xfrm_state_filter_match(state, walk->filter))
1881 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001882 err = func(state, walk->seq, data);
1883 if (err) {
1884 list_move_tail(&walk->all, &x->all);
1885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001887 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001889 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 err = -ENOENT;
1891 goto out;
1892 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001893 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894out:
Fan Du283bc9f2013-11-07 17:47:50 +08001895 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return err;
1897}
1898EXPORT_SYMBOL(xfrm_state_walk);
1899
Nicolas Dichteld3623092014-02-14 15:30:36 +01001900void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001901 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001902{
Herbert Xu12a169e2008-10-01 07:03:24 -07001903 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001904 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001905 walk->state = XFRM_STATE_DEAD;
1906 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001907 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001908}
1909EXPORT_SYMBOL(xfrm_state_walk_init);
1910
Fan Du283bc9f2013-11-07 17:47:50 +08001911void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001912{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001913 kfree(walk->filter);
1914
Herbert Xu12a169e2008-10-01 07:03:24 -07001915 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001916 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001917
Fan Du283bc9f2013-11-07 17:47:50 +08001918 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001919 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001920 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001921}
1922EXPORT_SYMBOL(xfrm_state_walk_done);
1923
Kees Cooke99e88a2017-10-16 14:43:17 -07001924static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001925{
Kees Cooke99e88a2017-10-16 14:43:17 -07001926 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001927
1928 spin_lock(&x->lock);
1929
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001930 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001931 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001932 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001933 else
1934 x->xflags |= XFRM_TIME_DEFER;
1935 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001936
1937 spin_unlock(&x->lock);
1938}
1939
Denis Chengdf018122007-12-07 00:51:11 -08001940static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
David S. Miller214e0052011-02-24 00:02:38 -05001942void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 struct xfrm_mgr *km;
1945
Cong Wang85168c02013-01-16 16:05:06 +08001946 rcu_read_lock();
1947 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001948 if (km->notify_policy)
1949 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001950 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001951}
1952
David S. Miller214e0052011-02-24 00:02:38 -05001953void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001954{
1955 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001956 rcu_read_lock();
1957 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001958 if (km->notify)
1959 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001960 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001961}
1962
1963EXPORT_SYMBOL(km_policy_notify);
1964EXPORT_SYMBOL(km_state_notify);
1965
Eric W. Biederman15e47302012-09-07 20:12:54 +00001966void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001967{
1968 struct km_event c;
1969
Herbert Xubf08867f92005-06-18 22:44:00 -07001970 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001971 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001972 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001973 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001976EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001977/*
1978 * We send to all registered managers regardless of failure
1979 * We are happy with one success
1980*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001981int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001983 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct xfrm_mgr *km;
1985
Cong Wang85168c02013-01-16 16:05:06 +08001986 rcu_read_lock();
1987 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001988 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001989 if (!acqret)
1990 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Cong Wang85168c02013-01-16 16:05:06 +08001992 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return err;
1994}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001995EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Al Viro5d36b182006-11-08 00:24:06 -08001997int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 int err = -EINVAL;
2000 struct xfrm_mgr *km;
2001
Cong Wang85168c02013-01-16 16:05:06 +08002002 rcu_read_lock();
2003 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (km->new_mapping)
2005 err = km->new_mapping(x, ipaddr, sport);
2006 if (!err)
2007 break;
2008 }
Cong Wang85168c02013-01-16 16:05:06 +08002009 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return err;
2011}
2012EXPORT_SYMBOL(km_new_mapping);
2013
Eric W. Biederman15e47302012-09-07 20:12:54 +00002014void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002016 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Herbert Xubf08867f92005-06-18 22:44:00 -07002018 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002019 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002020 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002021 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
David S. Millera70fcb02006-03-20 19:18:52 -08002023EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002025#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002026int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2027 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002028 const struct xfrm_kmaddress *k,
2029 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002030{
2031 int err = -EINVAL;
2032 int ret;
2033 struct xfrm_mgr *km;
2034
Cong Wang85168c02013-01-16 16:05:06 +08002035 rcu_read_lock();
2036 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002037 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002038 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2039 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002040 if (!ret)
2041 err = ret;
2042 }
2043 }
Cong Wang85168c02013-01-16 16:05:06 +08002044 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002045 return err;
2046}
2047EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002048#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002049
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002050int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002051{
2052 int err = -EINVAL;
2053 int ret;
2054 struct xfrm_mgr *km;
2055
Cong Wang85168c02013-01-16 16:05:06 +08002056 rcu_read_lock();
2057 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002058 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002059 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002060 if (!ret)
2061 err = ret;
2062 }
2063 }
Cong Wang85168c02013-01-16 16:05:06 +08002064 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002065 return err;
2066}
2067EXPORT_SYMBOL(km_report);
2068
Horia Geanta0f245582014-02-12 16:20:06 +02002069bool km_is_alive(const struct km_event *c)
2070{
2071 struct xfrm_mgr *km;
2072 bool is_alive = false;
2073
2074 rcu_read_lock();
2075 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2076 if (km->is_alive && km->is_alive(c)) {
2077 is_alive = true;
2078 break;
2079 }
2080 }
2081 rcu_read_unlock();
2082
2083 return is_alive;
2084}
2085EXPORT_SYMBOL(km_is_alive);
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2088{
2089 int err;
2090 u8 *data;
2091 struct xfrm_mgr *km;
2092 struct xfrm_policy *pol = NULL;
2093
Steffen Klassert19d7df62018-02-01 08:49:23 +01002094 if (in_compat_syscall())
2095 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002096
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002097 if (!optval && !optlen) {
2098 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2099 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2100 __sk_dst_reset(sk);
2101 return 0;
2102 }
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (optlen <= 0 || optlen > PAGE_SIZE)
2105 return -EMSGSIZE;
2106
Geliang Tanga133d932017-05-06 23:42:21 +08002107 data = memdup_user(optval, optlen);
2108 if (IS_ERR(data))
2109 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002112 rcu_read_lock();
2113 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002114 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 optlen, &err);
2116 if (err >= 0)
2117 break;
2118 }
Cong Wang85168c02013-01-16 16:05:06 +08002119 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 if (err >= 0) {
2122 xfrm_sk_policy_insert(sk, err, pol);
2123 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002124 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 err = 0;
2126 }
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 kfree(data);
2129 return err;
2130}
2131EXPORT_SYMBOL(xfrm_user_policy);
2132
Cong Wang85168c02013-01-16 16:05:06 +08002133static DEFINE_SPINLOCK(xfrm_km_lock);
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135int xfrm_register_km(struct xfrm_mgr *km)
2136{
Cong Wang85168c02013-01-16 16:05:06 +08002137 spin_lock_bh(&xfrm_km_lock);
2138 list_add_tail_rcu(&km->list, &xfrm_km_list);
2139 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return 0;
2141}
2142EXPORT_SYMBOL(xfrm_register_km);
2143
2144int xfrm_unregister_km(struct xfrm_mgr *km)
2145{
Cong Wang85168c02013-01-16 16:05:06 +08002146 spin_lock_bh(&xfrm_km_lock);
2147 list_del_rcu(&km->list);
2148 spin_unlock_bh(&xfrm_km_lock);
2149 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return 0;
2151}
2152EXPORT_SYMBOL(xfrm_unregister_km);
2153
2154int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2155{
2156 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002157
2158 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002160
Cong Wang44abdc32013-01-16 16:05:05 +08002161 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002163 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002164 else
Cong Wang44abdc32013-01-16 16:05:05 +08002165 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2166 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return err;
2168}
2169EXPORT_SYMBOL(xfrm_state_register_afinfo);
2170
2171int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2172{
Florian Westphal423826a2017-01-09 14:20:46 +01002173 int err = 0, family = afinfo->family;
2174
2175 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002177
Cong Wang44abdc32013-01-16 16:05:05 +08002178 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002180 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002182 else
Cong Wang44abdc32013-01-16 16:05:05 +08002183 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Cong Wang44abdc32013-01-16 16:05:05 +08002185 spin_unlock_bh(&xfrm_state_afinfo_lock);
2186 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return err;
2188}
2189EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2190
Florian Westphal711059b2017-01-09 14:20:48 +01002191struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2192{
2193 if (unlikely(family >= NPROTO))
2194 return NULL;
2195
2196 return rcu_dereference(xfrm_state_afinfo[family]);
2197}
2198
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002199struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 struct xfrm_state_afinfo *afinfo;
2202 if (unlikely(family >= NPROTO))
2203 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002204 rcu_read_lock();
2205 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002206 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return afinfo;
2209}
2210
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002211void xfrm_flush_gc(void)
2212{
2213 flush_work(&xfrm_state_gc_work);
2214}
2215EXPORT_SYMBOL(xfrm_flush_gc);
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2218void xfrm_state_delete_tunnel(struct xfrm_state *x)
2219{
2220 if (x->tunnel) {
2221 struct xfrm_state *t = x->tunnel;
2222
2223 if (atomic_read(&t->tunnel_users) == 2)
2224 xfrm_state_delete(t);
2225 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002226 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 x->tunnel = NULL;
2228 }
2229}
2230EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2231
2232int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2233{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002234 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Patrick McHardyc5c25232007-04-09 11:47:18 -07002236 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002237 type && type->get_mtu)
2238 return type->get_mtu(x, mtu);
2239
2240 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Ilan Tayariffdb5212017-08-01 12:49:08 +03002243int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002244{
Herbert Xud094cd82005-06-20 13:19:41 -07002245 struct xfrm_state_afinfo *afinfo;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002246 struct xfrm_mode *inner_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002247 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002248 int err;
2249
Herbert Xud094cd82005-06-20 13:19:41 -07002250 err = -EAFNOSUPPORT;
2251 afinfo = xfrm_state_get_afinfo(family);
2252 if (!afinfo)
2253 goto error;
2254
2255 err = 0;
2256 if (afinfo->init_flags)
2257 err = afinfo->init_flags(x);
2258
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002259 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002260
2261 if (err)
2262 goto error;
2263
2264 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002265
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002266 if (x->sel.family != AF_UNSPEC) {
2267 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2268 if (inner_mode == NULL)
2269 goto error;
2270
2271 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2272 family != x->sel.family) {
2273 xfrm_put_mode(inner_mode);
2274 goto error;
2275 }
2276
2277 x->inner_mode = inner_mode;
2278 } else {
2279 struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002280 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002281
Martin Willid81d2282008-12-03 15:38:07 -08002282 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002283 if (inner_mode == NULL)
2284 goto error;
2285
2286 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2287 xfrm_put_mode(inner_mode);
2288 goto error;
2289 }
Martin Willid81d2282008-12-03 15:38:07 -08002290 x->inner_mode = inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002291
Martin Willid81d2282008-12-03 15:38:07 -08002292 if (x->props.family == AF_INET)
2293 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002294
Martin Willid81d2282008-12-03 15:38:07 -08002295 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2296 if (inner_mode_iaf) {
2297 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2298 x->inner_mode_iaf = inner_mode_iaf;
2299 else
2300 xfrm_put_mode(inner_mode_iaf);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002301 }
2302 }
Herbert Xu13996372007-10-17 21:35:51 -07002303
Herbert Xud094cd82005-06-20 13:19:41 -07002304 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002305 if (x->type == NULL)
2306 goto error;
2307
Ilan Tayariffdb5212017-08-01 12:49:08 +03002308 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002309
Herbert Xu72cb6962005-06-20 13:18:08 -07002310 err = x->type->init_state(x);
2311 if (err)
2312 goto error;
2313
Herbert Xu13996372007-10-17 21:35:51 -07002314 x->outer_mode = xfrm_get_mode(x->props.mode, family);
Julia Lawall599901c2012-08-29 06:49:15 +00002315 if (x->outer_mode == NULL) {
2316 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002317 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002318 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002319
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002320 if (init_replay) {
2321 err = xfrm_init_replay(x);
2322 if (err)
2323 goto error;
2324 }
2325
Herbert Xu72cb6962005-06-20 13:18:08 -07002326error:
2327 return err;
2328}
2329
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002330EXPORT_SYMBOL(__xfrm_init_state);
2331
2332int xfrm_init_state(struct xfrm_state *x)
2333{
Yossi Kupermancc015722018-01-17 15:52:41 +02002334 int err;
2335
2336 err = __xfrm_init_state(x, true, false);
2337 if (!err)
2338 x->km.state = XFRM_STATE_VALID;
2339
2340 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002341}
2342
Herbert Xu72cb6962005-06-20 13:18:08 -07002343EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002344
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002345int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
David S. Millerf034b5d2006-08-24 03:08:07 -07002347 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Mathias Krause565f0fa2018-05-03 10:55:07 +02002349 if (net_eq(net, &init_net))
2350 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2351 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2352
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002353 INIT_LIST_HEAD(&net->xfrm.state_all);
2354
David S. Millerf034b5d2006-08-24 03:08:07 -07002355 sz = sizeof(struct hlist_head) * 8;
2356
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002357 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2358 if (!net->xfrm.state_bydst)
2359 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002360 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2361 if (!net->xfrm.state_bysrc)
2362 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002363 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2364 if (!net->xfrm.state_byspi)
2365 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002366 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002367
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002368 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002369 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002370 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002371 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002372
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002373out_byspi:
2374 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002375out_bysrc:
2376 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002377out_bydst:
2378 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002379}
2380
2381void xfrm_state_fini(struct net *net)
2382{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002383 unsigned int sz;
2384
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002385 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002386 flush_work(&xfrm_state_gc_work);
Cong Wangf75a2802019-01-31 13:05:49 -08002387 xfrm_state_flush(net, IPSEC_PROTO_ANY, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002388
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002389 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002390
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002391 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002392 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2393 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002394 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2395 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002396 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2397 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Joy Lattenab5f5e82007-09-17 11:51:22 -07002400#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002401static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2402 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002403{
Paul Moore68277ac2007-12-20 20:49:33 -08002404 struct xfrm_sec_ctx *ctx = x->security;
2405 u32 spi = ntohl(x->id.spi);
2406
2407 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002408 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002409 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002410
Weilong Chen9b7a7872013-12-24 09:43:46 +08002411 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002412 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002413 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2414 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002415 break;
2416 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002417 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002418 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002419 break;
2420 }
Paul Moore68277ac2007-12-20 20:49:33 -08002421
2422 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002423}
2424
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002425static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2426 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002427{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002428 const struct iphdr *iph4;
2429 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002430
2431 switch (family) {
2432 case AF_INET:
2433 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002434 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2435 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002436 break;
2437 case AF_INET6:
2438 iph6 = ipv6_hdr(skb);
2439 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002440 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002441 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002442 iph6->flow_lbl[0] & 0x0f,
2443 iph6->flow_lbl[1],
2444 iph6->flow_lbl[2]);
2445 break;
2446 }
2447}
2448
Tetsuo Handa2e710292014-04-22 21:48:30 +09002449void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002450{
2451 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002452
Paul Mooreafeb14b2007-12-21 14:58:11 -08002453 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002454 if (audit_buf == NULL)
2455 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002456 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002457 xfrm_audit_helper_sainfo(x, audit_buf);
2458 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002459 audit_log_end(audit_buf);
2460}
2461EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2462
Tetsuo Handa2e710292014-04-22 21:48:30 +09002463void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002464{
2465 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002466
Paul Mooreafeb14b2007-12-21 14:58:11 -08002467 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002468 if (audit_buf == NULL)
2469 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002470 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002471 xfrm_audit_helper_sainfo(x, audit_buf);
2472 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002473 audit_log_end(audit_buf);
2474}
2475EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002476
2477void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2478 struct sk_buff *skb)
2479{
2480 struct audit_buffer *audit_buf;
2481 u32 spi;
2482
2483 audit_buf = xfrm_audit_start("SA-replay-overflow");
2484 if (audit_buf == NULL)
2485 return;
2486 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2487 /* don't record the sequence number because it's inherent in this kind
2488 * of audit message */
2489 spi = ntohl(x->id.spi);
2490 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2491 audit_log_end(audit_buf);
2492}
2493EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2494
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002495void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002496 struct sk_buff *skb, __be32 net_seq)
2497{
2498 struct audit_buffer *audit_buf;
2499 u32 spi;
2500
2501 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2502 if (audit_buf == NULL)
2503 return;
2504 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2505 spi = ntohl(x->id.spi);
2506 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2507 spi, spi, ntohl(net_seq));
2508 audit_log_end(audit_buf);
2509}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002510EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002511
2512void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2513{
2514 struct audit_buffer *audit_buf;
2515
2516 audit_buf = xfrm_audit_start("SA-notfound");
2517 if (audit_buf == NULL)
2518 return;
2519 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2520 audit_log_end(audit_buf);
2521}
2522EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2523
2524void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2525 __be32 net_spi, __be32 net_seq)
2526{
2527 struct audit_buffer *audit_buf;
2528 u32 spi;
2529
2530 audit_buf = xfrm_audit_start("SA-notfound");
2531 if (audit_buf == NULL)
2532 return;
2533 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2534 spi = ntohl(net_spi);
2535 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2536 spi, spi, ntohl(net_seq));
2537 audit_log_end(audit_buf);
2538}
2539EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2540
2541void xfrm_audit_state_icvfail(struct xfrm_state *x,
2542 struct sk_buff *skb, u8 proto)
2543{
2544 struct audit_buffer *audit_buf;
2545 __be32 net_spi;
2546 __be32 net_seq;
2547
2548 audit_buf = xfrm_audit_start("SA-icv-failure");
2549 if (audit_buf == NULL)
2550 return;
2551 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2552 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2553 u32 spi = ntohl(net_spi);
2554 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2555 spi, spi, ntohl(net_seq));
2556 }
2557 audit_log_end(audit_buf);
2558}
2559EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002560#endif /* CONFIG_AUDITSYSCALL */