blob: ed25eb81aabe97c3c930be1714f935a2ef34c9e1 [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 Salim53bc6b4d2006-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);
Florian Westphalbb9cd072019-04-17 11:45:13 +0200176static bool 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
Florian Westphal4c145dc2019-03-29 21:16:31 +0100333static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
334 [XFRM_MODE_BEET] = {
335 .encap = XFRM_MODE_BEET,
336 .flags = XFRM_MODE_FLAG_TUNNEL,
337 .family = AF_INET,
338 },
339 [XFRM_MODE_TRANSPORT] = {
340 .encap = XFRM_MODE_TRANSPORT,
341 .family = AF_INET,
342 },
343 [XFRM_MODE_TUNNEL] = {
344 .encap = XFRM_MODE_TUNNEL,
345 .flags = XFRM_MODE_FLAG_TUNNEL,
346 .family = AF_INET,
347 },
348};
349
350static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
351 [XFRM_MODE_BEET] = {
352 .encap = XFRM_MODE_BEET,
353 .flags = XFRM_MODE_FLAG_TUNNEL,
354 .family = AF_INET6,
355 },
356 [XFRM_MODE_ROUTEOPTIMIZATION] = {
357 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
358 .family = AF_INET6,
359 },
360 [XFRM_MODE_TRANSPORT] = {
361 .encap = XFRM_MODE_TRANSPORT,
362 .family = AF_INET6,
363 },
364 [XFRM_MODE_TUNNEL] = {
365 .encap = XFRM_MODE_TUNNEL,
366 .flags = XFRM_MODE_FLAG_TUNNEL,
367 .family = AF_INET6,
368 },
369};
370
371static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700372{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100373 const struct xfrm_mode *mode;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700374
375 if (unlikely(encap >= XFRM_MODE_MAX))
376 return NULL;
377
Florian Westphal4c145dc2019-03-29 21:16:31 +0100378 switch (family) {
379 case AF_INET:
380 mode = &xfrm4_mode_map[encap];
381 if (mode->family == family)
382 return mode;
383 break;
384 case AF_INET6:
385 mode = &xfrm6_mode_map[encap];
386 if (mode->family == family)
387 return mode;
388 break;
389 default:
390 break;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700391 }
392
Florian Westphal4c145dc2019-03-29 21:16:31 +0100393 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700394}
395
Mathias Krause4a135e52018-11-21 21:09:23 +0100396void xfrm_state_free(struct xfrm_state *x)
397{
398 kmem_cache_free(xfrm_state_cache, x);
399}
400EXPORT_SYMBOL(xfrm_state_free);
401
Cong Wangf75a2802019-01-31 13:05:49 -0800402static void ___xfrm_state_destroy(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800404 tasklet_hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700405 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000406 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800407 kfree(x->aalg);
408 kfree(x->ealg);
409 kfree(x->calg);
410 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700411 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412 kfree(x->replay_esn);
413 kfree(x->preplay_esn);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200414 if (x->type_offload)
415 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (x->type) {
417 x->type->destructor(x);
418 xfrm_put_type(x->type);
419 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200420 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800421 security_xfrm_state_free(x);
Mathias Krause4a135e52018-11-21 21:09:23 +0100422 xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800425static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Herbert Xu12a169e2008-10-01 07:03:24 -0700427 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800428 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700429 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200432 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_unlock_bh(&xfrm_state_gc_lock);
434
Florian Westphaldf7274e2016-08-09 12:16:06 +0200435 synchronize_rcu();
436
Sasha Levinb67bfe02013-02-27 17:06:00 -0800437 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Cong Wangf75a2802019-01-31 13:05:49 -0800438 ___xfrm_state_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800441static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800443 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
444 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200445 time64_t now = ktime_get_real_seconds();
446 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600448 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 spin_lock(&x->lock);
451 if (x->km.state == XFRM_STATE_DEAD)
452 goto out;
453 if (x->km.state == XFRM_STATE_EXPIRED)
454 goto expired;
455 if (x->lft.hard_add_expires_seconds) {
456 long tmo = x->lft.hard_add_expires_seconds +
457 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000458 if (tmo <= 0) {
459 if (x->xflags & XFRM_SOFT_EXPIRE) {
460 /* enter hard expire without soft expire first?!
461 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100462 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000463 */
464 x->curlft.add_time = now - x->saved_tmo - 1;
465 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
466 } else
467 goto expired;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (tmo < next)
470 next = tmo;
471 }
472 if (x->lft.hard_use_expires_seconds) {
473 long tmo = x->lft.hard_use_expires_seconds +
474 (x->curlft.use_time ? : now) - now;
475 if (tmo <= 0)
476 goto expired;
477 if (tmo < next)
478 next = tmo;
479 }
480 if (x->km.dying)
481 goto resched;
482 if (x->lft.soft_add_expires_seconds) {
483 long tmo = x->lft.soft_add_expires_seconds +
484 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000485 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000487 x->xflags &= ~XFRM_SOFT_EXPIRE;
488 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000490 x->xflags |= XFRM_SOFT_EXPIRE;
491 x->saved_tmo = tmo;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 if (x->lft.soft_use_expires_seconds) {
495 long tmo = x->lft.soft_use_expires_seconds +
496 (x->curlft.use_time ? : now) - now;
497 if (tmo <= 0)
498 warn = 1;
499 else if (tmo < next)
500 next = tmo;
501 }
502
Herbert Xu4666faa2005-06-18 22:43:22 -0700503 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800505 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506resched:
Arnd Bergmann386c5682018-07-11 12:19:13 +0200507 if (next != TIME64_MAX) {
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800508 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
509 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto out;
512
513expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200514 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600516
517 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200518 if (!err)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800519 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Tetsuo Handa2e710292014-04-22 21:48:30 +0900521 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523out:
524 spin_unlock(&x->lock);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800525 return HRTIMER_NORESTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Kees Cooke99e88a2017-10-16 14:43:17 -0700528static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800529
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800530struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 struct xfrm_state *x;
533
Mathias Krause565f0fa2018-05-03 10:55:07 +0200534 x = kmem_cache_alloc(xfrm_state_cache, GFP_ATOMIC | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800537 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300538 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700540 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700541 INIT_HLIST_NODE(&x->bydst);
542 INIT_HLIST_NODE(&x->bysrc);
543 INIT_HLIST_NODE(&x->byspi);
Fan Du99565a6c2013-08-15 15:49:06 +0800544 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
545 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
Kees Cooke99e88a2017-10-16 14:43:17 -0700546 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200547 x->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 x->lft.soft_byte_limit = XFRM_INF;
549 x->lft.soft_packet_limit = XFRM_INF;
550 x->lft.hard_byte_limit = XFRM_INF;
551 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800552 x->replay_maxage = 0;
553 x->replay_maxdiff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 spin_lock_init(&x->lock);
555 }
556 return x;
557}
558EXPORT_SYMBOL(xfrm_state_alloc);
559
Cong Wangf75a2802019-01-31 13:05:49 -0800560void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700562 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Cong Wangf75a2802019-01-31 13:05:49 -0800564 if (sync) {
565 synchronize_rcu();
566 ___xfrm_state_destroy(x);
567 } else {
568 spin_lock_bh(&xfrm_state_gc_lock);
569 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
570 spin_unlock_bh(&xfrm_state_gc_lock);
571 schedule_work(&xfrm_state_gc_work);
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574EXPORT_SYMBOL(__xfrm_state_destroy);
575
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800576int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800578 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700579 int err = -ESRCH;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (x->km.state != XFRM_STATE_DEAD) {
582 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800583 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700584 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200585 hlist_del_rcu(&x->bydst);
586 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700587 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200588 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800589 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800590 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200592 xfrm_dev_state_delete(x);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* All xfrm_state objects are created by xfrm_state_alloc.
595 * The xfrm_state_alloc call gives a reference, and that
596 * is what we are dropping here.
597 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800598 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700599 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700601
602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800604EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700606int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608 int err;
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700611 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700613
614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616EXPORT_SYMBOL(xfrm_state_delete);
617
Joy Latten4aa2e622007-06-04 19:05:57 -0400618#ifdef CONFIG_SECURITY_NETWORK_XFRM
619static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900620xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Joy Latten4aa2e622007-06-04 19:05:57 -0400622 int i, err = 0;
623
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800624 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400625 struct xfrm_state *x;
626
Sasha Levinb67bfe02013-02-27 17:06:00 -0800627 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400628 if (xfrm_id_proto_match(x->id.proto, proto) &&
629 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900630 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400631 return err;
632 }
633 }
634 }
635
636 return err;
637}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200638
639static inline int
640xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
641{
642 int i, err = 0;
643
644 for (i = 0; i <= net->xfrm.state_hmask; i++) {
645 struct xfrm_state *x;
646 struct xfrm_state_offload *xso;
647
648 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
649 xso = &x->xso;
650
651 if (xso->dev == dev &&
652 (err = security_xfrm_state_delete(x)) != 0) {
653 xfrm_audit_state_delete(x, 0, task_valid);
654 return err;
655 }
656 }
657 }
658
659 return err;
660}
Joy Latten4aa2e622007-06-04 19:05:57 -0400661#else
662static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900663xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400664{
665 return 0;
666}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200667
668static inline int
669xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
670{
671 return 0;
672}
Joy Latten4aa2e622007-06-04 19:05:57 -0400673#endif
674
Cong Wangf75a2802019-01-31 13:05:49 -0800675int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
Joy Latten4aa2e622007-06-04 19:05:57 -0400676{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000677 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Fan Du283bc9f2013-11-07 17:47:50 +0800679 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900680 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400681 if (err)
682 goto out;
683
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000684 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800685 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700686 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800688 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700690 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800692 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Joy Latten161a09e2006-11-27 13:11:54 -0600694 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700695 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900696 task_valid);
Cong Wangf75a2802019-01-31 13:05:49 -0800697 if (sync)
698 xfrm_state_put_sync(x);
699 else
700 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000701 if (!err)
702 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Fan Du283bc9f2013-11-07 17:47:50 +0800704 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto restart;
706 }
707 }
708 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200709out:
710 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200711 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000712 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200713
Joy Latten4aa2e622007-06-04 19:05:57 -0400714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716EXPORT_SYMBOL(xfrm_state_flush);
717
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200718int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
719{
720 int i, err = 0, cnt = 0;
721
722 spin_lock_bh(&net->xfrm.xfrm_state_lock);
723 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
724 if (err)
725 goto out;
726
727 err = -ESRCH;
728 for (i = 0; i <= net->xfrm.state_hmask; i++) {
729 struct xfrm_state *x;
730 struct xfrm_state_offload *xso;
731restart:
732 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
733 xso = &x->xso;
734
735 if (!xfrm_state_kern(x) && xso->dev == dev) {
736 xfrm_state_hold(x);
737 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
738
739 err = xfrm_state_delete(x);
740 xfrm_audit_state_delete(x, err ? 0 : 1,
741 task_valid);
742 xfrm_state_put(x);
743 if (!err)
744 cnt++;
745
746 spin_lock_bh(&net->xfrm.xfrm_state_lock);
747 goto restart;
748 }
749 }
750 }
751 if (cnt)
752 err = 0;
753
754out:
755 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
756 return err;
757}
758EXPORT_SYMBOL(xfrm_dev_state_flush);
759
Alexey Dobriyane0710412010-01-23 13:37:10 +0000760void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700761{
Fan Du283bc9f2013-11-07 17:47:50 +0800762 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000763 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900764 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700765 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800766 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700767}
768EXPORT_SYMBOL(xfrm_sad_getinfo);
769
Florian Westphal711059b2017-01-09 14:20:48 +0100770static void
David S. Miller1a898592011-02-22 18:22:34 -0800771xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500772 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500773 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700774 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Florian Westphal711059b2017-01-09 14:20:48 +0100776 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
777
Florian Westphal3819a352017-01-13 14:55:14 +0100778 if (!afinfo)
779 return;
780
781 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700782
783 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100784 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700785 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100786 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700787 }
788 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
David S. Miller9aa60082011-02-24 01:51:36 -0500791static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
792 const xfrm_address_t *daddr,
793 __be32 spi, u8 proto,
794 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700795{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800796 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700797 struct xfrm_state *x;
798
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200799 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700800 if (x->props.family != family ||
801 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000802 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000803 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700804 continue;
805
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000806 if ((mark & x->mark.m) != x->mark.v)
807 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200808 if (!xfrm_state_hold_rcu(x))
809 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700810 return x;
811 }
812
813 return NULL;
814}
815
David S. Miller9aa60082011-02-24 01:51:36 -0500816static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
817 const xfrm_address_t *daddr,
818 const xfrm_address_t *saddr,
819 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700820{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800821 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700822 struct xfrm_state *x;
823
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200824 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700825 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000826 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000827 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
828 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700829 continue;
830
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000831 if ((mark & x->mark.m) != x->mark.v)
832 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200833 if (!xfrm_state_hold_rcu(x))
834 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700835 return x;
836 }
837
838 return NULL;
839}
840
841static inline struct xfrm_state *
842__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
843{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800844 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800845 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800846
David S. Milleredcd5822006-08-24 00:42:45 -0700847 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800848 return __xfrm_state_lookup(net, mark, &x->id.daddr,
849 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700850 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800851 return __xfrm_state_lookup_byaddr(net, mark,
852 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700853 &x->props.saddr,
854 x->id.proto, family);
855}
856
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800857static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700858{
859 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800860 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
861 net->xfrm.state_num > net->xfrm.state_hmask)
862 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700863}
864
David S. Miller08ec9af2009-03-13 14:22:40 -0700865static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800866 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700867 struct xfrm_state **best, int *acq_in_progress,
868 int *error)
869{
870 /* Resolution logic:
871 * 1. There is a valid state with matching selector. Done.
872 * 2. Valid state with inappropriate selector. Skip.
873 *
874 * Entering area of "sysdeps".
875 *
876 * 3. If state is not valid, selector is temporary, it selects
877 * only session which triggered previous resolution. Key
878 * manager will do something to install a state with proper
879 * selector.
880 */
881 if (x->km.state == XFRM_STATE_VALID) {
882 if ((x->sel.family &&
883 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
884 !security_xfrm_state_pol_flow_match(x, pol, fl))
885 return;
886
887 if (!*best ||
888 (*best)->km.dying > x->km.dying ||
889 ((*best)->km.dying == x->km.dying &&
890 (*best)->curlft.add_time < x->curlft.add_time))
891 *best = x;
892 } else if (x->km.state == XFRM_STATE_ACQ) {
893 *acq_in_progress = 1;
894 } else if (x->km.state == XFRM_STATE_ERROR ||
895 x->km.state == XFRM_STATE_EXPIRED) {
896 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
897 security_xfrm_state_pol_flow_match(x, pol, fl))
898 *error = -ESRCH;
899 }
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500903xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800904 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -0700906 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
David S. Miller08ec9af2009-03-13 14:22:40 -0700908 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800909 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700910 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700911 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int acquire_in_progress = 0;
913 int error = 0;
914 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800915 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700916 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200917 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200918 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900919
David S. Miller37b08e32008-09-02 20:14:15 -0700920 to_put = NULL;
921
Florian Westphalb65e3d72016-08-09 12:16:07 +0200922 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
923
Florian Westphald737a582016-08-09 12:16:09 +0200924 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700925 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200926 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700927 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000929 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200930 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700931 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700932 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 tmpl->mode == x->props.mode &&
934 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700935 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500936 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700937 &best, &acquire_in_progress, &error);
938 }
Fan Du6f115632013-09-23 17:18:25 +0800939 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700940 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Thomas Egerer8444cf72010-09-20 11:11:38 -0700942 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200943 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700944 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700945 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000946 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200947 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700948 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800949 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700950 tmpl->mode == x->props.mode &&
951 tmpl->id.proto == x->id.proto &&
952 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500953 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700954 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
David S. Miller08ec9af2009-03-13 14:22:40 -0700957found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 x = best;
959 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700960 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800961 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700962 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700963 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 error = -EEXIST;
965 goto out;
966 }
Horia Geanta0f245582014-02-12 16:20:06 +0200967
968 c.net = net;
969 /* If the KMs have no listeners (yet...), avoid allocating an SA
970 * for each and every packet - garbage collection might not
971 * handle the flood.
972 */
973 if (!km_is_alive(&c)) {
974 error = -ESRCH;
975 goto out;
976 }
977
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800978 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (x == NULL) {
980 error = -ENOMEM;
981 goto out;
982 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700983 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700985 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800986 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +0200987 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
David S. Miller1d28f422011-03-12 00:29:39 -0500989 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700990 if (error) {
991 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700992 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700993 x = NULL;
994 goto out;
995 }
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +0200998 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001000 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001001 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001002 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001003 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001005 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001006 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001008 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001009 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001010 net->xfrm.state_num++;
1011 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001012 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 } else {
1014 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001015 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 x = NULL;
1017 error = -ESRCH;
1018 }
1019 }
1020out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001021 if (x) {
1022 if (!xfrm_state_hold_rcu(x)) {
1023 *err = -EAGAIN;
1024 x = NULL;
1025 }
1026 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001028 }
Florian Westphald737a582016-08-09 12:16:09 +02001029 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001030 if (to_put)
1031 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001032
1033 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1034 *err = -EAGAIN;
1035 if (x) {
1036 xfrm_state_put(x);
1037 x = NULL;
1038 }
1039 }
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return x;
1042}
1043
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001044struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001045xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001046 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001047 unsigned short family, u8 mode, u8 proto, u32 reqid)
1048{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001049 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001050 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001051
Fan Du4ae770b2014-01-03 11:18:29 +08001052 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001053 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001054 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001055 if (x->props.family == family &&
1056 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001057 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001058 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001059 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1060 xfrm_state_addr_check(x, daddr, saddr, family) &&
1061 mode == x->props.mode &&
1062 proto == x->id.proto &&
1063 x->km.state == XFRM_STATE_VALID) {
1064 rx = x;
1065 break;
1066 }
1067 }
1068
1069 if (rx)
1070 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001071 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001072
1073
1074 return rx;
1075}
1076EXPORT_SYMBOL(xfrm_stateonly_find);
1077
Fan Duc4549972014-01-03 11:18:32 +08001078struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1079 unsigned short family)
1080{
1081 struct xfrm_state *x;
1082 struct xfrm_state_walk *w;
1083
1084 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1085 list_for_each_entry(w, &net->xfrm.state_all, all) {
1086 x = container_of(w, struct xfrm_state, km);
1087 if (x->props.family != family ||
1088 x->id.spi != spi)
1089 continue;
1090
Fan Duc4549972014-01-03 11:18:32 +08001091 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001092 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001093 return x;
1094 }
1095 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1096 return NULL;
1097}
1098EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100static void __xfrm_state_insert(struct xfrm_state *x)
1101{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001102 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001103 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001105 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001106
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001107 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001108 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001109 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001111 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001112 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001114 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001115 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001116 x->props.family);
1117
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001118 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001119 }
1120
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001121 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
David S. Millera47f0ce2006-08-24 03:54:22 -07001122 if (x->replay_maxage)
1123 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001124
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001125 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001126
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001127 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Fan Du283bc9f2013-11-07 17:47:50 +08001130/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001131static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1132{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001133 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001134 unsigned short family = xnew->props.family;
1135 u32 reqid = xnew->props.reqid;
1136 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001137 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001138 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001139 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001140
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001141 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001142 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001143 if (x->props.family == family &&
1144 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001145 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001146 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001147 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1148 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001149 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001150 }
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153void xfrm_state_insert(struct xfrm_state *x)
1154{
Fan Du283bc9f2013-11-07 17:47:50 +08001155 struct net *net = xs_net(x);
1156
1157 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001158 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001160 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162EXPORT_SYMBOL(xfrm_state_insert);
1163
Fan Du283bc9f2013-11-07 17:47:50 +08001164/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001165static struct xfrm_state *__find_acq_core(struct net *net,
1166 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001167 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001168 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001169 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001170 const xfrm_address_t *saddr,
1171 int create)
David S. Miller27708342006-08-24 00:13:10 -07001172{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001173 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001174 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001175 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001176
Sasha Levinb67bfe02013-02-27 17:06:00 -08001177 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001178 if (x->props.reqid != reqid ||
1179 x->props.mode != mode ||
1180 x->props.family != family ||
1181 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001182 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001183 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001184 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001185 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1186 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001187 continue;
1188
David S. Miller27708342006-08-24 00:13:10 -07001189 xfrm_state_hold(x);
1190 return x;
1191 }
1192
1193 if (!create)
1194 return NULL;
1195
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001196 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001197 if (likely(x)) {
1198 switch (family) {
1199 case AF_INET:
1200 x->sel.daddr.a4 = daddr->a4;
1201 x->sel.saddr.a4 = saddr->a4;
1202 x->sel.prefixlen_d = 32;
1203 x->sel.prefixlen_s = 32;
1204 x->props.saddr.a4 = saddr->a4;
1205 x->id.daddr.a4 = daddr->a4;
1206 break;
1207
1208 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001209 x->sel.daddr.in6 = daddr->in6;
1210 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001211 x->sel.prefixlen_d = 128;
1212 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001213 x->props.saddr.in6 = saddr->in6;
1214 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001215 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001216 }
David S. Miller27708342006-08-24 00:13:10 -07001217
1218 x->km.state = XFRM_STATE_ACQ;
1219 x->id.proto = proto;
1220 x->props.family = family;
1221 x->props.mode = mode;
1222 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001223 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001224 x->mark.v = m->v;
1225 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001226 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001227 xfrm_state_hold(x);
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001228 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001229 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001230 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001231 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001232 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001233
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001234 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001235
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001236 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001237 }
1238
1239 return x;
1240}
1241
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001242static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244int xfrm_state_add(struct xfrm_state *x)
1245{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001246 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001247 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int family;
1249 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001250 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001251 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
David S. Miller37b08e32008-09-02 20:14:15 -07001255 to_put = NULL;
1256
Fan Du283bc9f2013-11-07 17:47:50 +08001257 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
David S. Milleredcd5822006-08-24 00:42:45 -07001259 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001261 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 x1 = NULL;
1263 err = -EEXIST;
1264 goto out;
1265 }
1266
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001267 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001268 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001269 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001270 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001271 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 x1 = NULL;
1273 }
1274 }
1275
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001276 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001277 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001278 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001279 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
David S. Millerc7f5ea32006-08-24 03:29:04 -07001281 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 __xfrm_state_insert(x);
1283 err = 0;
1284
1285out:
Fan Du283bc9f2013-11-07 17:47:50 +08001286 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (x1) {
1289 xfrm_state_delete(x1);
1290 xfrm_state_put(x1);
1291 }
1292
David S. Miller37b08e32008-09-02 20:14:15 -07001293 if (to_put)
1294 xfrm_state_put(to_put);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return err;
1297}
1298EXPORT_SYMBOL(xfrm_state_add);
1299
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001300#ifdef CONFIG_XFRM_MIGRATE
Antony Antony4ab47d42017-06-06 12:12:13 +02001301static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1302 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001303{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001304 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001305 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001306 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001307 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001308
1309 memcpy(&x->id, &orig->id, sizeof(x->id));
1310 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1311 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1312 x->props.mode = orig->props.mode;
1313 x->props.replay_window = orig->props.replay_window;
1314 x->props.reqid = orig->props.reqid;
1315 x->props.family = orig->props.family;
1316 x->props.saddr = orig->props.saddr;
1317
1318 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001319 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001320 if (!x->aalg)
1321 goto error;
1322 }
1323 x->props.aalgo = orig->props.aalgo;
1324
Steffen Klassertee5c2312014-02-19 13:33:24 +01001325 if (orig->aead) {
1326 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001327 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001328 if (!x->aead)
1329 goto error;
1330 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001331 if (orig->ealg) {
1332 x->ealg = xfrm_algo_clone(orig->ealg);
1333 if (!x->ealg)
1334 goto error;
1335 }
1336 x->props.ealgo = orig->props.ealgo;
1337
1338 if (orig->calg) {
1339 x->calg = xfrm_algo_clone(orig->calg);
1340 if (!x->calg)
1341 goto error;
1342 }
1343 x->props.calgo = orig->props.calgo;
1344
Antony Antony4ab47d42017-06-06 12:12:13 +02001345 if (encap || orig->encap) {
1346 if (encap)
1347 x->encap = kmemdup(encap, sizeof(*x->encap),
1348 GFP_KERNEL);
1349 else
1350 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1351 GFP_KERNEL);
1352
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001353 if (!x->encap)
1354 goto error;
1355 }
1356
1357 if (orig->coaddr) {
1358 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1359 GFP_KERNEL);
1360 if (!x->coaddr)
1361 goto error;
1362 }
1363
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001364 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001365 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001366 goto error;
1367 }
1368
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001369 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1370
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001371 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001372 goto error;
1373
1374 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001375 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001376
Steffen Klassert7e652642018-06-12 14:07:07 +02001377 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001378 x->tfcpad = orig->tfcpad;
1379 x->replay_maxdiff = orig->replay_maxdiff;
1380 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001381 x->curlft.add_time = orig->curlft.add_time;
1382 x->km.state = orig->km.state;
1383 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001384 x->replay = orig->replay;
1385 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001386
1387 return x;
1388
1389 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001390 xfrm_state_put(x);
1391out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001392 return NULL;
1393}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001394
Fan Du283bc9f2013-11-07 17:47:50 +08001395struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001396{
1397 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001398 struct xfrm_state *x = NULL;
1399
1400 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001401
1402 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001403 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001404 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001405 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001406 if (x->props.mode != m->mode ||
1407 x->id.proto != m->proto)
1408 continue;
1409 if (m->reqid && x->props.reqid != m->reqid)
1410 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001411 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1412 m->old_family) ||
1413 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1414 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001415 continue;
1416 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001417 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001418 }
1419 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001420 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001421 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001422 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001423 if (x->props.mode != m->mode ||
1424 x->id.proto != m->proto)
1425 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001426 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1427 m->old_family) ||
1428 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1429 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001430 continue;
1431 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001432 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001433 }
1434 }
1435
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001436 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1437
1438 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001439}
1440EXPORT_SYMBOL(xfrm_migrate_state_find);
1441
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001442struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001443 struct xfrm_migrate *m,
1444 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001445{
1446 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001447
Antony Antony4ab47d42017-06-06 12:12:13 +02001448 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001449 if (!xc)
1450 return NULL;
1451
1452 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1453 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1454
1455 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001456 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001457 /* a care is needed when the destination address of the
1458 state is to be updated as it is a part of triplet */
1459 xfrm_state_insert(xc);
1460 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001461 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001462 goto error;
1463 }
1464
1465 return xc;
1466error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001467 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001468 return NULL;
1469}
1470EXPORT_SYMBOL(xfrm_state_migrate);
1471#endif
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473int xfrm_state_update(struct xfrm_state *x)
1474{
David S. Miller37b08e32008-09-02 20:14:15 -07001475 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001477 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001478 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
David S. Miller37b08e32008-09-02 20:14:15 -07001480 to_put = NULL;
1481
Fan Du283bc9f2013-11-07 17:47:50 +08001482 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001483 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 err = -ESRCH;
1486 if (!x1)
1487 goto out;
1488
1489 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001490 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 err = -EEXIST;
1492 goto out;
1493 }
1494
1495 if (x1->km.state == XFRM_STATE_ACQ) {
1496 __xfrm_state_insert(x);
1497 x = NULL;
1498 }
1499 err = 0;
1500
1501out:
Fan Du283bc9f2013-11-07 17:47:50 +08001502 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
David S. Miller37b08e32008-09-02 20:14:15 -07001504 if (to_put)
1505 xfrm_state_put(to_put);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (err)
1508 return err;
1509
1510 if (!x) {
1511 xfrm_state_delete(x1);
1512 xfrm_state_put(x1);
1513 return 0;
1514 }
1515
1516 err = -EINVAL;
1517 spin_lock_bh(&x1->lock);
1518 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001519 if (x->encap && x1->encap &&
1520 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001522 else if (x->encap || x1->encap)
1523 goto fail;
1524
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001525 if (x->coaddr && x1->coaddr) {
1526 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1527 }
1528 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1529 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1531 x1->km.dying = 0;
1532
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -08001533 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (x1->curlft.use_time)
1535 xfrm_state_check_expire(x1);
1536
Nathan Harold5baf4f92018-07-19 19:07:47 -07001537 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001538 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1539
Nathan Harold5baf4f92018-07-19 19:07:47 -07001540 if (x->props.smark.m || x->props.smark.v)
1541 x1->props.smark = x->props.smark;
1542
1543 if (x->if_id)
1544 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001545
1546 __xfrm_state_bump_genids(x1);
1547 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1548 }
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001551 x->km.state = XFRM_STATE_DEAD;
1552 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001554
1555fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 spin_unlock_bh(&x1->lock);
1557
1558 xfrm_state_put(x1);
1559
1560 return err;
1561}
1562EXPORT_SYMBOL(xfrm_state_update);
1563
1564int xfrm_state_check_expire(struct xfrm_state *x)
1565{
1566 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001567 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1570 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001571 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001572 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return -EINVAL;
1574 }
1575
1576 if (!x->km.dying &&
1577 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001578 x->curlft.packets >= x->lft.soft_packet_limit)) {
1579 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001580 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return 0;
1583}
1584EXPORT_SYMBOL(xfrm_state_check_expire);
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001587xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001588 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Florian Westphalc2f672f2016-09-20 15:45:26 +02001592 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001593 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001594 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return x;
1596}
1597EXPORT_SYMBOL(xfrm_state_lookup);
1598
1599struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001600xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001601 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001602 u8 proto, unsigned short family)
1603{
1604 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001605
Fan Du283bc9f2013-11-07 17:47:50 +08001606 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001607 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001608 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001609 return x;
1610}
1611EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1612
1613struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001614xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001615 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001616 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Fan Du283bc9f2013-11-07 17:47:50 +08001620 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001621 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001622 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return x;
1625}
1626EXPORT_SYMBOL(xfrm_find_acq);
1627
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001628#ifdef CONFIG_XFRM_SUB_POLICY
1629int
1630xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001631 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001632{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001633 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001634 int err = 0;
1635 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1636 if (!afinfo)
1637 return -EAFNOSUPPORT;
1638
Fan Du283bc9f2013-11-07 17:47:50 +08001639 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001640 if (afinfo->tmpl_sort)
1641 err = afinfo->tmpl_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001642 else
1643 for (i = 0; i < n; i++)
1644 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001645 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001646 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001647 return err;
1648}
1649EXPORT_SYMBOL(xfrm_tmpl_sort);
1650
1651int
1652xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1653 unsigned short family)
1654{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001655 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001656 int err = 0;
1657 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001658 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001659
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001660 if (!afinfo)
1661 return -EAFNOSUPPORT;
1662
Fan Du283bc9f2013-11-07 17:47:50 +08001663 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001664 if (afinfo->state_sort)
1665 err = afinfo->state_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001666 else
1667 for (i = 0; i < n; i++)
1668 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001669 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001670 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001671 return err;
1672}
1673EXPORT_SYMBOL(xfrm_state_sort);
1674#endif
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676/* Silly enough, but I'm lazy to build resolution list */
1677
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001678static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001682 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001683 struct xfrm_state *x;
1684
Sasha Levinb67bfe02013-02-27 17:06:00 -08001685 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001686 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001687 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001688 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 xfrm_state_hold(x);
1690 return x;
1691 }
1692 }
1693 }
1694 return NULL;
1695}
1696
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001697struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct xfrm_state *x;
1700
Fan Du283bc9f2013-11-07 17:47:50 +08001701 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001702 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001703 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return x;
1705}
1706EXPORT_SYMBOL(xfrm_find_acq_byseq);
1707
1708u32 xfrm_get_acqseq(void)
1709{
1710 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001711 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
jamal6836b9b2010-02-16 02:01:22 +00001713 do {
1714 res = atomic_inc_return(&acqseq);
1715 } while (!res);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return res;
1718}
1719EXPORT_SYMBOL(xfrm_get_acqseq);
1720
Fan Du776e9dd2013-12-16 18:47:49 +08001721int verify_spi_info(u8 proto, u32 min, u32 max)
1722{
1723 switch (proto) {
1724 case IPPROTO_AH:
1725 case IPPROTO_ESP:
1726 break;
1727
1728 case IPPROTO_COMP:
1729 /* IPCOMP spi is 16-bits. */
1730 if (max >= 0x10000)
1731 return -EINVAL;
1732 break;
1733
1734 default:
1735 return -EINVAL;
1736 }
1737
1738 if (min > max)
1739 return -EINVAL;
1740
1741 return 0;
1742}
1743EXPORT_SYMBOL(verify_spi_info);
1744
Herbert Xu658b2192007-10-09 13:29:52 -07001745int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001747 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001748 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001750 int err = -ENOENT;
1751 __be32 minspi = htonl(low);
1752 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001753 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Herbert Xu658b2192007-10-09 13:29:52 -07001755 spin_lock_bh(&x->lock);
1756 if (x->km.state == XFRM_STATE_DEAD)
1757 goto unlock;
1758
1759 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001761 goto unlock;
1762
1763 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001766 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (x0) {
1768 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001769 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 x->id.spi = minspi;
1772 } else {
1773 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001774 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001775 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001776 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (x0 == NULL) {
1778 x->id.spi = htonl(spi);
1779 break;
1780 }
1781 xfrm_state_put(x0);
1782 }
1783 }
1784 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001785 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001786 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001787 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001788 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001789
1790 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Herbert Xu658b2192007-10-09 13:29:52 -07001792
1793unlock:
1794 spin_unlock_bh(&x->lock);
1795
1796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798EXPORT_SYMBOL(xfrm_alloc_spi);
1799
Nicolas Dichteld3623092014-02-14 15:30:36 +01001800static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001801 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001802{
1803 if (filter) {
1804 if ((filter->family == AF_INET ||
1805 filter->family == AF_INET6) &&
1806 x->props.family != filter->family)
1807 return false;
1808
1809 return addr_match(&x->props.saddr, &filter->saddr,
1810 filter->splen) &&
1811 addr_match(&x->id.daddr, &filter->daddr,
1812 filter->dplen);
1813 }
1814 return true;
1815}
1816
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001817int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001818 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 void *data)
1820{
Herbert Xu12a169e2008-10-01 07:03:24 -07001821 struct xfrm_state *state;
1822 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int err = 0;
1824
Herbert Xu12a169e2008-10-01 07:03:24 -07001825 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001826 return 0;
1827
Fan Du283bc9f2013-11-07 17:47:50 +08001828 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001829 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001830 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001831 else
Li RongQing80077702015-04-22 17:09:54 +08001832 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001833 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001834 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001835 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001836 state = container_of(x, struct xfrm_state, km);
1837 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001838 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001839 if (!__xfrm_state_filter_match(state, walk->filter))
1840 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001841 err = func(state, walk->seq, data);
1842 if (err) {
1843 list_move_tail(&walk->all, &x->all);
1844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001846 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001848 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 err = -ENOENT;
1850 goto out;
1851 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001852 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853out:
Fan Du283bc9f2013-11-07 17:47:50 +08001854 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return err;
1856}
1857EXPORT_SYMBOL(xfrm_state_walk);
1858
Nicolas Dichteld3623092014-02-14 15:30:36 +01001859void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001860 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001861{
Herbert Xu12a169e2008-10-01 07:03:24 -07001862 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001863 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001864 walk->state = XFRM_STATE_DEAD;
1865 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001866 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001867}
1868EXPORT_SYMBOL(xfrm_state_walk_init);
1869
Fan Du283bc9f2013-11-07 17:47:50 +08001870void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001871{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001872 kfree(walk->filter);
1873
Herbert Xu12a169e2008-10-01 07:03:24 -07001874 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001875 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001876
Fan Du283bc9f2013-11-07 17:47:50 +08001877 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001878 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001879 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001880}
1881EXPORT_SYMBOL(xfrm_state_walk_done);
1882
Kees Cooke99e88a2017-10-16 14:43:17 -07001883static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001884{
Kees Cooke99e88a2017-10-16 14:43:17 -07001885 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001886
1887 spin_lock(&x->lock);
1888
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001889 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001890 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001891 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001892 else
1893 x->xflags |= XFRM_TIME_DEFER;
1894 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001895
1896 spin_unlock(&x->lock);
1897}
1898
Denis Chengdf018122007-12-07 00:51:11 -08001899static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
David S. Miller214e0052011-02-24 00:02:38 -05001901void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct xfrm_mgr *km;
1904
Cong Wang85168c02013-01-16 16:05:06 +08001905 rcu_read_lock();
1906 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001907 if (km->notify_policy)
1908 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001909 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001910}
1911
David S. Miller214e0052011-02-24 00:02:38 -05001912void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001913{
1914 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001915 rcu_read_lock();
1916 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001917 if (km->notify)
1918 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001919 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001920}
1921
1922EXPORT_SYMBOL(km_policy_notify);
1923EXPORT_SYMBOL(km_state_notify);
1924
Eric W. Biederman15e47302012-09-07 20:12:54 +00001925void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001926{
1927 struct km_event c;
1928
Herbert Xubf08867f92005-06-18 22:44:00 -07001929 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001930 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001931 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001932 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001935EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001936/*
1937 * We send to all registered managers regardless of failure
1938 * We are happy with one success
1939*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001940int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001942 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct xfrm_mgr *km;
1944
Cong Wang85168c02013-01-16 16:05:06 +08001945 rcu_read_lock();
1946 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001947 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001948 if (!acqret)
1949 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Cong Wang85168c02013-01-16 16:05:06 +08001951 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return err;
1953}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001954EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Al Viro5d36b182006-11-08 00:24:06 -08001956int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 int err = -EINVAL;
1959 struct xfrm_mgr *km;
1960
Cong Wang85168c02013-01-16 16:05:06 +08001961 rcu_read_lock();
1962 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (km->new_mapping)
1964 err = km->new_mapping(x, ipaddr, sport);
1965 if (!err)
1966 break;
1967 }
Cong Wang85168c02013-01-16 16:05:06 +08001968 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return err;
1970}
1971EXPORT_SYMBOL(km_new_mapping);
1972
Eric W. Biederman15e47302012-09-07 20:12:54 +00001973void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001975 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Herbert Xubf08867f92005-06-18 22:44:00 -07001977 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001978 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001979 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001980 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
David S. Millera70fcb02006-03-20 19:18:52 -08001982EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001984#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001985int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1986 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02001987 const struct xfrm_kmaddress *k,
1988 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001989{
1990 int err = -EINVAL;
1991 int ret;
1992 struct xfrm_mgr *km;
1993
Cong Wang85168c02013-01-16 16:05:06 +08001994 rcu_read_lock();
1995 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001996 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02001997 ret = km->migrate(sel, dir, type, m, num_migrate, k,
1998 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001999 if (!ret)
2000 err = ret;
2001 }
2002 }
Cong Wang85168c02013-01-16 16:05:06 +08002003 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002004 return err;
2005}
2006EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002007#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002008
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002009int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002010{
2011 int err = -EINVAL;
2012 int ret;
2013 struct xfrm_mgr *km;
2014
Cong Wang85168c02013-01-16 16:05:06 +08002015 rcu_read_lock();
2016 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002017 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002018 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002019 if (!ret)
2020 err = ret;
2021 }
2022 }
Cong Wang85168c02013-01-16 16:05:06 +08002023 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002024 return err;
2025}
2026EXPORT_SYMBOL(km_report);
2027
Florian Westphalbb9cd072019-04-17 11:45:13 +02002028static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002029{
2030 struct xfrm_mgr *km;
2031 bool is_alive = false;
2032
2033 rcu_read_lock();
2034 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2035 if (km->is_alive && km->is_alive(c)) {
2036 is_alive = true;
2037 break;
2038 }
2039 }
2040 rcu_read_unlock();
2041
2042 return is_alive;
2043}
Horia Geanta0f245582014-02-12 16:20:06 +02002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2046{
2047 int err;
2048 u8 *data;
2049 struct xfrm_mgr *km;
2050 struct xfrm_policy *pol = NULL;
2051
Steffen Klassert19d7df62018-02-01 08:49:23 +01002052 if (in_compat_syscall())
2053 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002054
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002055 if (!optval && !optlen) {
2056 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2057 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2058 __sk_dst_reset(sk);
2059 return 0;
2060 }
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (optlen <= 0 || optlen > PAGE_SIZE)
2063 return -EMSGSIZE;
2064
Geliang Tanga133d932017-05-06 23:42:21 +08002065 data = memdup_user(optval, optlen);
2066 if (IS_ERR(data))
2067 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002070 rcu_read_lock();
2071 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002072 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 optlen, &err);
2074 if (err >= 0)
2075 break;
2076 }
Cong Wang85168c02013-01-16 16:05:06 +08002077 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 if (err >= 0) {
2080 xfrm_sk_policy_insert(sk, err, pol);
2081 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002082 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 err = 0;
2084 }
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 kfree(data);
2087 return err;
2088}
2089EXPORT_SYMBOL(xfrm_user_policy);
2090
Cong Wang85168c02013-01-16 16:05:06 +08002091static DEFINE_SPINLOCK(xfrm_km_lock);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093int xfrm_register_km(struct xfrm_mgr *km)
2094{
Cong Wang85168c02013-01-16 16:05:06 +08002095 spin_lock_bh(&xfrm_km_lock);
2096 list_add_tail_rcu(&km->list, &xfrm_km_list);
2097 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return 0;
2099}
2100EXPORT_SYMBOL(xfrm_register_km);
2101
2102int xfrm_unregister_km(struct xfrm_mgr *km)
2103{
Cong Wang85168c02013-01-16 16:05:06 +08002104 spin_lock_bh(&xfrm_km_lock);
2105 list_del_rcu(&km->list);
2106 spin_unlock_bh(&xfrm_km_lock);
2107 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return 0;
2109}
2110EXPORT_SYMBOL(xfrm_unregister_km);
2111
2112int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2113{
2114 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002115
2116 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002118
Cong Wang44abdc32013-01-16 16:05:05 +08002119 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002121 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002122 else
Cong Wang44abdc32013-01-16 16:05:05 +08002123 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2124 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return err;
2126}
2127EXPORT_SYMBOL(xfrm_state_register_afinfo);
2128
2129int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2130{
Florian Westphal423826a2017-01-09 14:20:46 +01002131 int err = 0, family = afinfo->family;
2132
2133 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002135
Cong Wang44abdc32013-01-16 16:05:05 +08002136 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002138 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002140 else
Cong Wang44abdc32013-01-16 16:05:05 +08002141 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Cong Wang44abdc32013-01-16 16:05:05 +08002143 spin_unlock_bh(&xfrm_state_afinfo_lock);
2144 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return err;
2146}
2147EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2148
Florian Westphal711059b2017-01-09 14:20:48 +01002149struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2150{
2151 if (unlikely(family >= NPROTO))
2152 return NULL;
2153
2154 return rcu_dereference(xfrm_state_afinfo[family]);
2155}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002156EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002157
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002158struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 struct xfrm_state_afinfo *afinfo;
2161 if (unlikely(family >= NPROTO))
2162 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002163 rcu_read_lock();
2164 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002165 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002166 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return afinfo;
2168}
2169
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002170void xfrm_flush_gc(void)
2171{
2172 flush_work(&xfrm_state_gc_work);
2173}
2174EXPORT_SYMBOL(xfrm_flush_gc);
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2177void xfrm_state_delete_tunnel(struct xfrm_state *x)
2178{
2179 if (x->tunnel) {
2180 struct xfrm_state *t = x->tunnel;
2181
2182 if (atomic_read(&t->tunnel_users) == 2)
2183 xfrm_state_delete(t);
2184 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002185 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 x->tunnel = NULL;
2187 }
2188}
2189EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2190
2191int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2192{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002193 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Patrick McHardyc5c25232007-04-09 11:47:18 -07002195 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002196 type && type->get_mtu)
2197 return type->get_mtu(x, mtu);
2198
2199 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Ilan Tayariffdb5212017-08-01 12:49:08 +03002202int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002203{
Florian Westphal4c145dc2019-03-29 21:16:31 +01002204 const struct xfrm_state_afinfo *afinfo;
Florian Westphalc9500d72019-03-29 21:16:32 +01002205 const struct xfrm_mode *inner_mode;
2206 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002207 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002208 int err;
2209
Herbert Xud094cd82005-06-20 13:19:41 -07002210 err = -EAFNOSUPPORT;
2211 afinfo = xfrm_state_get_afinfo(family);
2212 if (!afinfo)
2213 goto error;
2214
2215 err = 0;
2216 if (afinfo->init_flags)
2217 err = afinfo->init_flags(x);
2218
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002219 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002220
2221 if (err)
2222 goto error;
2223
2224 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002225
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002226 if (x->sel.family != AF_UNSPEC) {
2227 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2228 if (inner_mode == NULL)
2229 goto error;
2230
2231 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002232 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002233 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002234
Florian Westphalc9500d72019-03-29 21:16:32 +01002235 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002236 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002237 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002238 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002239
Martin Willid81d2282008-12-03 15:38:07 -08002240 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002241 if (inner_mode == NULL)
2242 goto error;
2243
Florian Westphal4c145dc2019-03-29 21:16:31 +01002244 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002245 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002246
Florian Westphalc9500d72019-03-29 21:16:32 +01002247 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002248
Martin Willid81d2282008-12-03 15:38:07 -08002249 if (x->props.family == AF_INET)
2250 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002251
Martin Willid81d2282008-12-03 15:38:07 -08002252 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2253 if (inner_mode_iaf) {
2254 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002255 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002256 }
2257 }
Herbert Xu13996372007-10-17 21:35:51 -07002258
Herbert Xud094cd82005-06-20 13:19:41 -07002259 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002260 if (x->type == NULL)
2261 goto error;
2262
Ilan Tayariffdb5212017-08-01 12:49:08 +03002263 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002264
Herbert Xu72cb6962005-06-20 13:18:08 -07002265 err = x->type->init_state(x);
2266 if (err)
2267 goto error;
2268
Florian Westphalc9500d72019-03-29 21:16:32 +01002269 outer_mode = xfrm_get_mode(x->props.mode, family);
2270 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002271 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002272 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002273 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002274
Florian Westphalc9500d72019-03-29 21:16:32 +01002275 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002276 if (init_replay) {
2277 err = xfrm_init_replay(x);
2278 if (err)
2279 goto error;
2280 }
2281
Herbert Xu72cb6962005-06-20 13:18:08 -07002282error:
2283 return err;
2284}
2285
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002286EXPORT_SYMBOL(__xfrm_init_state);
2287
2288int xfrm_init_state(struct xfrm_state *x)
2289{
Yossi Kupermancc015722018-01-17 15:52:41 +02002290 int err;
2291
2292 err = __xfrm_init_state(x, true, false);
2293 if (!err)
2294 x->km.state = XFRM_STATE_VALID;
2295
2296 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002297}
2298
Herbert Xu72cb6962005-06-20 13:18:08 -07002299EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002300
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002301int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
David S. Millerf034b5d2006-08-24 03:08:07 -07002303 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Mathias Krause565f0fa2018-05-03 10:55:07 +02002305 if (net_eq(net, &init_net))
2306 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2307 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2308
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002309 INIT_LIST_HEAD(&net->xfrm.state_all);
2310
David S. Millerf034b5d2006-08-24 03:08:07 -07002311 sz = sizeof(struct hlist_head) * 8;
2312
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002313 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2314 if (!net->xfrm.state_bydst)
2315 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002316 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2317 if (!net->xfrm.state_bysrc)
2318 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002319 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2320 if (!net->xfrm.state_byspi)
2321 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002322 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002323
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002324 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002325 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002326 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002327 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002328
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002329out_byspi:
2330 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002331out_bysrc:
2332 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002333out_bydst:
2334 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002335}
2336
2337void xfrm_state_fini(struct net *net)
2338{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002339 unsigned int sz;
2340
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002341 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002342 flush_work(&xfrm_state_gc_work);
Cong Wangf75a2802019-01-31 13:05:49 -08002343 xfrm_state_flush(net, IPSEC_PROTO_ANY, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002344
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002345 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002346
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002347 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002348 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2349 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002350 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2351 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002352 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2353 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
Joy Lattenab5f5e82007-09-17 11:51:22 -07002356#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002357static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2358 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002359{
Paul Moore68277ac2007-12-20 20:49:33 -08002360 struct xfrm_sec_ctx *ctx = x->security;
2361 u32 spi = ntohl(x->id.spi);
2362
2363 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002364 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002365 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002366
Weilong Chen9b7a7872013-12-24 09:43:46 +08002367 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002368 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002369 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2370 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002371 break;
2372 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002373 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002374 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002375 break;
2376 }
Paul Moore68277ac2007-12-20 20:49:33 -08002377
2378 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002379}
2380
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002381static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2382 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002383{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002384 const struct iphdr *iph4;
2385 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002386
2387 switch (family) {
2388 case AF_INET:
2389 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002390 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2391 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002392 break;
2393 case AF_INET6:
2394 iph6 = ipv6_hdr(skb);
2395 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002396 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002397 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002398 iph6->flow_lbl[0] & 0x0f,
2399 iph6->flow_lbl[1],
2400 iph6->flow_lbl[2]);
2401 break;
2402 }
2403}
2404
Tetsuo Handa2e710292014-04-22 21:48:30 +09002405void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002406{
2407 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002408
Paul Mooreafeb14b2007-12-21 14:58:11 -08002409 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002410 if (audit_buf == NULL)
2411 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002412 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002413 xfrm_audit_helper_sainfo(x, audit_buf);
2414 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002415 audit_log_end(audit_buf);
2416}
2417EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2418
Tetsuo Handa2e710292014-04-22 21:48:30 +09002419void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002420{
2421 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002422
Paul Mooreafeb14b2007-12-21 14:58:11 -08002423 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002424 if (audit_buf == NULL)
2425 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002426 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002427 xfrm_audit_helper_sainfo(x, audit_buf);
2428 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002429 audit_log_end(audit_buf);
2430}
2431EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002432
2433void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2434 struct sk_buff *skb)
2435{
2436 struct audit_buffer *audit_buf;
2437 u32 spi;
2438
2439 audit_buf = xfrm_audit_start("SA-replay-overflow");
2440 if (audit_buf == NULL)
2441 return;
2442 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2443 /* don't record the sequence number because it's inherent in this kind
2444 * of audit message */
2445 spi = ntohl(x->id.spi);
2446 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2447 audit_log_end(audit_buf);
2448}
2449EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2450
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002451void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002452 struct sk_buff *skb, __be32 net_seq)
2453{
2454 struct audit_buffer *audit_buf;
2455 u32 spi;
2456
2457 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2458 if (audit_buf == NULL)
2459 return;
2460 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2461 spi = ntohl(x->id.spi);
2462 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2463 spi, spi, ntohl(net_seq));
2464 audit_log_end(audit_buf);
2465}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002466EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002467
2468void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2469{
2470 struct audit_buffer *audit_buf;
2471
2472 audit_buf = xfrm_audit_start("SA-notfound");
2473 if (audit_buf == NULL)
2474 return;
2475 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2476 audit_log_end(audit_buf);
2477}
2478EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2479
2480void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2481 __be32 net_spi, __be32 net_seq)
2482{
2483 struct audit_buffer *audit_buf;
2484 u32 spi;
2485
2486 audit_buf = xfrm_audit_start("SA-notfound");
2487 if (audit_buf == NULL)
2488 return;
2489 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2490 spi = ntohl(net_spi);
2491 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2492 spi, spi, ntohl(net_seq));
2493 audit_log_end(audit_buf);
2494}
2495EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2496
2497void xfrm_audit_state_icvfail(struct xfrm_state *x,
2498 struct sk_buff *skb, u8 proto)
2499{
2500 struct audit_buffer *audit_buf;
2501 __be32 net_spi;
2502 __be32 net_seq;
2503
2504 audit_buf = xfrm_audit_start("SA-icv-failure");
2505 if (audit_buf == NULL)
2506 return;
2507 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2508 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2509 u32 spi = ntohl(net_spi);
2510 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2511 spi, spi, ntohl(net_seq));
2512 }
2513 audit_log_end(audit_buf);
2514}
2515EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002516#endif /* CONFIG_AUDITSYSCALL */