blob: 50621d9829707341844ad684d2f639ac1e2779d5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm_state.c
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific functions
12 * Derek Atkins <derek@ihtfp.com>
13 * Add UDP Encapsulation
Trent Jaegerdf718372005-12-13 23:12:27 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#include <linux/workqueue.h>
18#include <net/xfrm.h>
19#include <linux/pfkeyv2.h>
20#include <linux/ipsec.h>
21#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070022#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080023#include <linux/audit.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080025#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080027#include <linux/interrupt.h>
28#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller44e36b42006-08-24 04:50:50 -070030#include "xfrm_hash.h"
31
Florian Westphalc8406992016-08-09 12:16:08 +020032#define xfrm_state_deref_prot(table, net) \
33 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
34
Florian Westphal35db57bb2016-08-23 16:00:12 +020035static void xfrm_state_gc_task(struct work_struct *work);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* Each xfrm_state may be linked to two tables:
38
39 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070040 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 destination/tunnel endpoint. (output)
42 */
43
David S. Millerf034b5d2006-08-24 03:08:07 -070044static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Florian Westphalb65e3d72016-08-09 12:16:07 +020045static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
Mathias Krause565f0fa2018-05-03 10:55:07 +020046static struct kmem_cache *xfrm_state_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Florian Westphal35db57bb2016-08-23 16:00:12 +020048static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
49static HLIST_HEAD(xfrm_state_gc_list);
50
Florian Westphal02efdff2016-08-09 12:16:05 +020051static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
52{
Reshetova, Elena88755e9c2017-07-04 15:53:21 +030053 return refcount_inc_not_zero(&x->refcnt);
Florian Westphal02efdff2016-08-09 12:16:05 +020054}
55
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080056static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050057 const xfrm_address_t *daddr,
58 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070059 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070060 unsigned short family)
61{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080062 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070063}
64
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080065static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050066 const xfrm_address_t *daddr,
67 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070068 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070069{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080070 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070071}
72
David S. Miller2575b652006-08-24 03:26:44 -070073static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050074xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
75 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070076{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080077 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070078}
79
David S. Millerf034b5d2006-08-24 03:08:07 -070080static void xfrm_hash_transfer(struct hlist_head *list,
81 struct hlist_head *ndsttable,
82 struct hlist_head *nsrctable,
83 struct hlist_head *nspitable,
84 unsigned int nhashmask)
85{
Sasha Levinb67bfe02013-02-27 17:06:00 -080086 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070087 struct xfrm_state *x;
88
Sasha Levinb67bfe02013-02-27 17:06:00 -080089 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070090 unsigned int h;
91
David S. Millerc1969f22006-08-24 04:00:03 -070092 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
93 x->props.reqid, x->props.family,
94 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020095 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070096
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070097 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
98 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -070099 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200100 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -0700101
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700102 if (x->id.spi) {
103 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
104 x->id.proto, x->props.family,
105 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200106 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700107 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700108 }
109}
110
Alexey Dobriyan63082732008-11-25 17:19:07 -0800111static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700112{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800113 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700114}
115
Alexey Dobriyan63082732008-11-25 17:19:07 -0800116static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700117{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800118 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700119 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
120 unsigned long nsize, osize;
121 unsigned int nhashmask, ohashmask;
122 int i;
123
Alexey Dobriyan63082732008-11-25 17:19:07 -0800124 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700125 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700126 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800127 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700128 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700129 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700130 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800131 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700132 }
David S. Miller44e36b42006-08-24 04:50:50 -0700133 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700134 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700135 xfrm_hash_free(ndst, nsize);
136 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800137 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700138 }
139
Fan Du283bc9f2013-11-07 17:47:50 +0800140 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200141 write_seqcount_begin(&xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700142
143 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200144 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800145 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200146 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700147
Florian Westphalc8406992016-08-09 12:16:08 +0200148 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
149 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800150 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700151
Florian Westphalc8406992016-08-09 12:16:08 +0200152 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
153 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
154 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800155 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700156
Florian Westphalb65e3d72016-08-09 12:16:07 +0200157 write_seqcount_end(&xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800158 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700159
160 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200161
162 synchronize_rcu();
163
David S. Miller44e36b42006-08-24 04:50:50 -0700164 xfrm_hash_free(odst, osize);
165 xfrm_hash_free(osrc, osize);
166 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700167}
168
Cong Wang44abdc32013-01-16 16:05:05 +0800169static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
170static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static DEFINE_SPINLOCK(xfrm_state_gc_lock);
173
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800174int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800176int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Florian Westphalbb9cd072019-04-17 11:45:13 +0200177static bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000178void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Cong Wang7a9885b2013-01-17 16:34:11 +0800180static DEFINE_SPINLOCK(xfrm_type_lock);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800181int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700182{
Cong Wang7a9885b2013-01-17 16:34:11 +0800183 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800184 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700185 int err = 0;
186
187 if (unlikely(afinfo == NULL))
188 return -EAFNOSUPPORT;
189 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800190 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700191
192 if (likely(typemap[type->proto] == NULL))
193 typemap[type->proto] = type;
194 else
195 err = -EEXIST;
Cong Wang7a9885b2013-01-17 16:34:11 +0800196 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100197 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700198 return err;
199}
200EXPORT_SYMBOL(xfrm_register_type);
201
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800202int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700203{
Cong Wang7a9885b2013-01-17 16:34:11 +0800204 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800205 const struct xfrm_type **typemap;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700206 int err = 0;
207
208 if (unlikely(afinfo == NULL))
209 return -EAFNOSUPPORT;
210 typemap = afinfo->type_map;
Cong Wang7a9885b2013-01-17 16:34:11 +0800211 spin_lock_bh(&xfrm_type_lock);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700212
213 if (unlikely(typemap[type->proto] != type))
214 err = -ENOENT;
215 else
216 typemap[type->proto] = NULL;
Cong Wang7a9885b2013-01-17 16:34:11 +0800217 spin_unlock_bh(&xfrm_type_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100218 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700219 return err;
220}
221EXPORT_SYMBOL(xfrm_unregister_type);
222
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800223static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700224{
225 struct xfrm_state_afinfo *afinfo;
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800226 const struct xfrm_type **typemap;
227 const struct xfrm_type *type;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700228 int modload_attempted = 0;
229
230retry:
231 afinfo = xfrm_state_get_afinfo(family);
232 if (unlikely(afinfo == NULL))
233 return NULL;
234 typemap = afinfo->type_map;
235
Florian Westphal75cda622017-01-09 14:20:49 +0100236 type = READ_ONCE(typemap[proto]);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700237 if (unlikely(type && !try_module_get(type->owner)))
238 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100239
240 rcu_read_unlock();
241
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700242 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700243 request_module("xfrm-type-%d-%d", family, proto);
244 modload_attempted = 1;
245 goto retry;
246 }
247
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700248 return type;
249}
250
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800251static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700252{
253 module_put(type->owner);
254}
255
Steffen Klassert9d389d72017-04-14 10:05:44 +0200256static DEFINE_SPINLOCK(xfrm_type_offload_lock);
257int xfrm_register_type_offload(const struct xfrm_type_offload *type,
258 unsigned short family)
259{
260 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
261 const struct xfrm_type_offload **typemap;
262 int err = 0;
263
264 if (unlikely(afinfo == NULL))
265 return -EAFNOSUPPORT;
266 typemap = afinfo->type_offload_map;
267 spin_lock_bh(&xfrm_type_offload_lock);
268
269 if (likely(typemap[type->proto] == NULL))
270 typemap[type->proto] = type;
271 else
272 err = -EEXIST;
273 spin_unlock_bh(&xfrm_type_offload_lock);
274 rcu_read_unlock();
275 return err;
276}
277EXPORT_SYMBOL(xfrm_register_type_offload);
278
279int xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
280 unsigned short family)
281{
282 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
283 const struct xfrm_type_offload **typemap;
284 int err = 0;
285
286 if (unlikely(afinfo == NULL))
287 return -EAFNOSUPPORT;
288 typemap = afinfo->type_offload_map;
289 spin_lock_bh(&xfrm_type_offload_lock);
290
291 if (unlikely(typemap[type->proto] != type))
292 err = -ENOENT;
293 else
294 typemap[type->proto] = NULL;
295 spin_unlock_bh(&xfrm_type_offload_lock);
296 rcu_read_unlock();
297 return err;
298}
299EXPORT_SYMBOL(xfrm_unregister_type_offload);
300
Ilan Tayariffdb5212017-08-01 12:49:08 +0300301static const struct xfrm_type_offload *
302xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200303{
304 struct xfrm_state_afinfo *afinfo;
305 const struct xfrm_type_offload **typemap;
306 const struct xfrm_type_offload *type;
307
Ilan Tayariffdb5212017-08-01 12:49:08 +0300308retry:
Steffen Klassert9d389d72017-04-14 10:05:44 +0200309 afinfo = xfrm_state_get_afinfo(family);
310 if (unlikely(afinfo == NULL))
311 return NULL;
312 typemap = afinfo->type_offload_map;
313
314 type = typemap[proto];
315 if ((type && !try_module_get(type->owner)))
316 type = NULL;
317
Sabrina Dubroca2f10a612017-12-31 16:18:56 +0100318 rcu_read_unlock();
319
Ilan Tayariffdb5212017-08-01 12:49:08 +0300320 if (!type && try_load) {
321 request_module("xfrm-offload-%d-%d", family, proto);
Gustavo A. R. Silva545d8ae2018-01-22 16:34:09 -0600322 try_load = false;
Ilan Tayariffdb5212017-08-01 12:49:08 +0300323 goto retry;
324 }
325
Steffen Klassert9d389d72017-04-14 10:05:44 +0200326 return type;
327}
328
329static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
330{
331 module_put(type->owner);
332}
333
Florian Westphal4c145dc2019-03-29 21:16:31 +0100334static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
335 [XFRM_MODE_BEET] = {
336 .encap = XFRM_MODE_BEET,
337 .flags = XFRM_MODE_FLAG_TUNNEL,
338 .family = AF_INET,
339 },
340 [XFRM_MODE_TRANSPORT] = {
341 .encap = XFRM_MODE_TRANSPORT,
342 .family = AF_INET,
343 },
344 [XFRM_MODE_TUNNEL] = {
345 .encap = XFRM_MODE_TUNNEL,
346 .flags = XFRM_MODE_FLAG_TUNNEL,
347 .family = AF_INET,
348 },
349};
350
351static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
352 [XFRM_MODE_BEET] = {
353 .encap = XFRM_MODE_BEET,
354 .flags = XFRM_MODE_FLAG_TUNNEL,
355 .family = AF_INET6,
356 },
357 [XFRM_MODE_ROUTEOPTIMIZATION] = {
358 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
359 .family = AF_INET6,
360 },
361 [XFRM_MODE_TRANSPORT] = {
362 .encap = XFRM_MODE_TRANSPORT,
363 .family = AF_INET6,
364 },
365 [XFRM_MODE_TUNNEL] = {
366 .encap = XFRM_MODE_TUNNEL,
367 .flags = XFRM_MODE_FLAG_TUNNEL,
368 .family = AF_INET6,
369 },
370};
371
372static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700373{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100374 const struct xfrm_mode *mode;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700375
376 if (unlikely(encap >= XFRM_MODE_MAX))
377 return NULL;
378
Florian Westphal4c145dc2019-03-29 21:16:31 +0100379 switch (family) {
380 case AF_INET:
381 mode = &xfrm4_mode_map[encap];
382 if (mode->family == family)
383 return mode;
384 break;
385 case AF_INET6:
386 mode = &xfrm6_mode_map[encap];
387 if (mode->family == family)
388 return mode;
389 break;
390 default:
391 break;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700392 }
393
Florian Westphal4c145dc2019-03-29 21:16:31 +0100394 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700395}
396
Mathias Krause4a135e52018-11-21 21:09:23 +0100397void xfrm_state_free(struct xfrm_state *x)
398{
399 kmem_cache_free(xfrm_state_cache, x);
400}
401EXPORT_SYMBOL(xfrm_state_free);
402
Cong Wangf75a2802019-01-31 13:05:49 -0800403static void ___xfrm_state_destroy(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100405 hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700406 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000407 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800408 kfree(x->aalg);
409 kfree(x->ealg);
410 kfree(x->calg);
411 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700412 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000413 kfree(x->replay_esn);
414 kfree(x->preplay_esn);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200415 if (x->type_offload)
416 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (x->type) {
418 x->type->destructor(x);
419 xfrm_put_type(x->type);
420 }
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200421 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800422 security_xfrm_state_free(x);
Mathias Krause4a135e52018-11-21 21:09:23 +0100423 xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800426static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Herbert Xu12a169e2008-10-01 07:03:24 -0700428 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800429 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700430 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200433 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock_bh(&xfrm_state_gc_lock);
435
Florian Westphaldf7274e2016-08-09 12:16:06 +0200436 synchronize_rcu();
437
Sasha Levinb67bfe02013-02-27 17:06:00 -0800438 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Cong Wangf75a2802019-01-31 13:05:49 -0800439 ___xfrm_state_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800442static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100444 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
445 enum hrtimer_restart ret = HRTIMER_NORESTART;
Arnd Bergmann386c5682018-07-11 12:19:13 +0200446 time64_t now = ktime_get_real_seconds();
447 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600449 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 spin_lock(&x->lock);
452 if (x->km.state == XFRM_STATE_DEAD)
453 goto out;
454 if (x->km.state == XFRM_STATE_EXPIRED)
455 goto expired;
456 if (x->lft.hard_add_expires_seconds) {
457 long tmo = x->lft.hard_add_expires_seconds +
458 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000459 if (tmo <= 0) {
460 if (x->xflags & XFRM_SOFT_EXPIRE) {
461 /* enter hard expire without soft expire first?!
462 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100463 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000464 */
465 x->curlft.add_time = now - x->saved_tmo - 1;
466 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
467 } else
468 goto expired;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (tmo < next)
471 next = tmo;
472 }
473 if (x->lft.hard_use_expires_seconds) {
474 long tmo = x->lft.hard_use_expires_seconds +
475 (x->curlft.use_time ? : now) - now;
476 if (tmo <= 0)
477 goto expired;
478 if (tmo < next)
479 next = tmo;
480 }
481 if (x->km.dying)
482 goto resched;
483 if (x->lft.soft_add_expires_seconds) {
484 long tmo = x->lft.soft_add_expires_seconds +
485 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000486 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000488 x->xflags &= ~XFRM_SOFT_EXPIRE;
489 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000491 x->xflags |= XFRM_SOFT_EXPIRE;
492 x->saved_tmo = tmo;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 if (x->lft.soft_use_expires_seconds) {
496 long tmo = x->lft.soft_use_expires_seconds +
497 (x->curlft.use_time ? : now) - now;
498 if (tmo <= 0)
499 warn = 1;
500 else if (tmo < next)
501 next = tmo;
502 }
503
Herbert Xu4666faa2005-06-18 22:43:22 -0700504 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (warn)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800506 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507resched:
Arnd Bergmann386c5682018-07-11 12:19:13 +0200508 if (next != TIME64_MAX) {
Thomas Gleixner671422b2019-03-01 23:48:20 +0100509 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
510 ret = HRTIMER_RESTART;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800511 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out;
514
515expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200516 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600518
519 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200520 if (!err)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800521 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Tetsuo Handa2e710292014-04-22 21:48:30 +0900523 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525out:
526 spin_unlock(&x->lock);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Kees Cooke99e88a2017-10-16 14:43:17 -0700530static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800531
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800532struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct xfrm_state *x;
535
Mathias Krause565f0fa2018-05-03 10:55:07 +0200536 x = kmem_cache_alloc(xfrm_state_cache, GFP_ATOMIC | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800539 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300540 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700542 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700543 INIT_HLIST_NODE(&x->bydst);
544 INIT_HLIST_NODE(&x->bysrc);
545 INIT_HLIST_NODE(&x->byspi);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100546 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
547 x->mtimer.function = xfrm_timer_handler;
Kees Cooke99e88a2017-10-16 14:43:17 -0700548 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200549 x->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 x->lft.soft_byte_limit = XFRM_INF;
551 x->lft.soft_packet_limit = XFRM_INF;
552 x->lft.hard_byte_limit = XFRM_INF;
553 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800554 x->replay_maxage = 0;
555 x->replay_maxdiff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_lock_init(&x->lock);
557 }
558 return x;
559}
560EXPORT_SYMBOL(xfrm_state_alloc);
561
Cong Wangf75a2802019-01-31 13:05:49 -0800562void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700564 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Cong Wangf75a2802019-01-31 13:05:49 -0800566 if (sync) {
567 synchronize_rcu();
568 ___xfrm_state_destroy(x);
569 } else {
570 spin_lock_bh(&xfrm_state_gc_lock);
571 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
572 spin_unlock_bh(&xfrm_state_gc_lock);
573 schedule_work(&xfrm_state_gc_work);
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576EXPORT_SYMBOL(__xfrm_state_destroy);
577
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800578int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800580 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700581 int err = -ESRCH;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (x->km.state != XFRM_STATE_DEAD) {
584 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800585 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700586 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200587 hlist_del_rcu(&x->bydst);
588 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700589 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200590 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800591 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800592 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200594 xfrm_dev_state_delete(x);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* All xfrm_state objects are created by xfrm_state_alloc.
597 * The xfrm_state_alloc call gives a reference, and that
598 * is what we are dropping here.
599 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800600 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700601 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700603
604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -0800606EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700610 int err;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700613 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700615
616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618EXPORT_SYMBOL(xfrm_state_delete);
619
Joy Latten4aa2e622007-06-04 19:05:57 -0400620#ifdef CONFIG_SECURITY_NETWORK_XFRM
621static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900622xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Joy Latten4aa2e622007-06-04 19:05:57 -0400624 int i, err = 0;
625
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800626 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400627 struct xfrm_state *x;
628
Sasha Levinb67bfe02013-02-27 17:06:00 -0800629 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400630 if (xfrm_id_proto_match(x->id.proto, proto) &&
631 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900632 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400633 return err;
634 }
635 }
636 }
637
638 return err;
639}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200640
641static inline int
642xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
643{
644 int i, err = 0;
645
646 for (i = 0; i <= net->xfrm.state_hmask; i++) {
647 struct xfrm_state *x;
648 struct xfrm_state_offload *xso;
649
650 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
651 xso = &x->xso;
652
653 if (xso->dev == dev &&
654 (err = security_xfrm_state_delete(x)) != 0) {
655 xfrm_audit_state_delete(x, 0, task_valid);
656 return err;
657 }
658 }
659 }
660
661 return err;
662}
Joy Latten4aa2e622007-06-04 19:05:57 -0400663#else
664static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900665xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400666{
667 return 0;
668}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200669
670static inline int
671xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
672{
673 return 0;
674}
Joy Latten4aa2e622007-06-04 19:05:57 -0400675#endif
676
Cong Wangf75a2802019-01-31 13:05:49 -0800677int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
Joy Latten4aa2e622007-06-04 19:05:57 -0400678{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000679 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Fan Du283bc9f2013-11-07 17:47:50 +0800681 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900682 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400683 if (err)
684 goto out;
685
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000686 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800687 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700688 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800690 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700692 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800694 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Joy Latten161a09e2006-11-27 13:11:54 -0600696 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700697 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900698 task_valid);
Cong Wangf75a2802019-01-31 13:05:49 -0800699 if (sync)
700 xfrm_state_put_sync(x);
701 else
702 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000703 if (!err)
704 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Fan Du283bc9f2013-11-07 17:47:50 +0800706 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto restart;
708 }
709 }
710 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200711out:
712 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200713 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000714 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200715
Joy Latten4aa2e622007-06-04 19:05:57 -0400716 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718EXPORT_SYMBOL(xfrm_state_flush);
719
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200720int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
721{
722 int i, err = 0, cnt = 0;
723
724 spin_lock_bh(&net->xfrm.xfrm_state_lock);
725 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
726 if (err)
727 goto out;
728
729 err = -ESRCH;
730 for (i = 0; i <= net->xfrm.state_hmask; i++) {
731 struct xfrm_state *x;
732 struct xfrm_state_offload *xso;
733restart:
734 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
735 xso = &x->xso;
736
737 if (!xfrm_state_kern(x) && xso->dev == dev) {
738 xfrm_state_hold(x);
739 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
740
741 err = xfrm_state_delete(x);
742 xfrm_audit_state_delete(x, err ? 0 : 1,
743 task_valid);
744 xfrm_state_put(x);
745 if (!err)
746 cnt++;
747
748 spin_lock_bh(&net->xfrm.xfrm_state_lock);
749 goto restart;
750 }
751 }
752 }
753 if (cnt)
754 err = 0;
755
756out:
757 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
758 return err;
759}
760EXPORT_SYMBOL(xfrm_dev_state_flush);
761
Alexey Dobriyane0710412010-01-23 13:37:10 +0000762void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700763{
Fan Du283bc9f2013-11-07 17:47:50 +0800764 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000765 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900766 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700767 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800768 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700769}
770EXPORT_SYMBOL(xfrm_sad_getinfo);
771
Florian Westphal711059b2017-01-09 14:20:48 +0100772static void
David S. Miller1a898592011-02-22 18:22:34 -0800773xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500774 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500775 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700776 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Florian Westphal711059b2017-01-09 14:20:48 +0100778 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
779
Florian Westphal3819a352017-01-13 14:55:14 +0100780 if (!afinfo)
781 return;
782
783 afinfo->init_tempsel(&x->sel, fl);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700784
785 if (family != tmpl->encap_family) {
Florian Westphal711059b2017-01-09 14:20:48 +0100786 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
Thomas Egerer8444cf72010-09-20 11:11:38 -0700787 if (!afinfo)
Florian Westphal711059b2017-01-09 14:20:48 +0100788 return;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700789 }
790 afinfo->init_temprop(x, tmpl, daddr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
David S. Miller9aa60082011-02-24 01:51:36 -0500793static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
794 const xfrm_address_t *daddr,
795 __be32 spi, u8 proto,
796 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700797{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800798 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700799 struct xfrm_state *x;
800
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200801 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700802 if (x->props.family != family ||
803 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000804 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000805 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700806 continue;
807
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000808 if ((mark & x->mark.m) != x->mark.v)
809 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200810 if (!xfrm_state_hold_rcu(x))
811 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700812 return x;
813 }
814
815 return NULL;
816}
817
David S. Miller9aa60082011-02-24 01:51:36 -0500818static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
819 const xfrm_address_t *daddr,
820 const xfrm_address_t *saddr,
821 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700822{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800823 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700824 struct xfrm_state *x;
825
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200826 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700827 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000828 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000829 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
830 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700831 continue;
832
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000833 if ((mark & x->mark.m) != x->mark.v)
834 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200835 if (!xfrm_state_hold_rcu(x))
836 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700837 return x;
838 }
839
840 return NULL;
841}
842
843static inline struct xfrm_state *
844__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
845{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800846 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800847 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800848
David S. Milleredcd5822006-08-24 00:42:45 -0700849 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800850 return __xfrm_state_lookup(net, mark, &x->id.daddr,
851 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700852 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800853 return __xfrm_state_lookup_byaddr(net, mark,
854 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700855 &x->props.saddr,
856 x->id.proto, family);
857}
858
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800859static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700860{
861 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800862 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
863 net->xfrm.state_num > net->xfrm.state_hmask)
864 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700865}
866
David S. Miller08ec9af2009-03-13 14:22:40 -0700867static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -0800868 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700869 struct xfrm_state **best, int *acq_in_progress,
870 int *error)
871{
872 /* Resolution logic:
873 * 1. There is a valid state with matching selector. Done.
874 * 2. Valid state with inappropriate selector. Skip.
875 *
876 * Entering area of "sysdeps".
877 *
878 * 3. If state is not valid, selector is temporary, it selects
879 * only session which triggered previous resolution. Key
880 * manager will do something to install a state with proper
881 * selector.
882 */
883 if (x->km.state == XFRM_STATE_VALID) {
884 if ((x->sel.family &&
885 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
886 !security_xfrm_state_pol_flow_match(x, pol, fl))
887 return;
888
889 if (!*best ||
890 (*best)->km.dying > x->km.dying ||
891 ((*best)->km.dying == x->km.dying &&
892 (*best)->curlft.add_time < x->curlft.add_time))
893 *best = x;
894 } else if (x->km.state == XFRM_STATE_ACQ) {
895 *acq_in_progress = 1;
896 } else if (x->km.state == XFRM_STATE_ERROR ||
897 x->km.state == XFRM_STATE_EXPIRED) {
898 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
899 security_xfrm_state_pol_flow_match(x, pol, fl))
900 *error = -ESRCH;
901 }
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -0500905xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -0800906 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -0700908 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
David S. Miller08ec9af2009-03-13 14:22:40 -0700910 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800911 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -0700912 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -0700913 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int acquire_in_progress = 0;
915 int error = 0;
916 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800917 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -0700918 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +0200919 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +0200920 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900921
David S. Miller37b08e32008-09-02 20:14:15 -0700922 to_put = NULL;
923
Florian Westphalb65e3d72016-08-09 12:16:07 +0200924 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
925
Florian Westphald737a582016-08-09 12:16:09 +0200926 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -0700927 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200928 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700929 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000931 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200932 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700933 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -0700934 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 tmpl->mode == x->props.mode &&
936 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700937 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500938 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700939 &best, &acquire_in_progress, &error);
940 }
Fan Du6f115632013-09-23 17:18:25 +0800941 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -0700942 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Thomas Egerer8444cf72010-09-20 11:11:38 -0700944 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200945 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -0700946 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700947 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000948 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +0200949 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700950 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +0800951 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -0700952 tmpl->mode == x->props.mode &&
953 tmpl->id.proto == x->id.proto &&
954 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -0500955 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -0700956 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
David S. Miller08ec9af2009-03-13 14:22:40 -0700959found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 x = best;
961 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -0700962 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800963 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700964 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -0700965 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 error = -EEXIST;
967 goto out;
968 }
Horia Geanta0f245582014-02-12 16:20:06 +0200969
970 c.net = net;
971 /* If the KMs have no listeners (yet...), avoid allocating an SA
972 * for each and every packet - garbage collection might not
973 * handle the flood.
974 */
975 if (!km_is_alive(&c)) {
976 error = -ESRCH;
977 goto out;
978 }
979
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -0800980 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (x == NULL) {
982 error = -ENOMEM;
983 goto out;
984 }
Thomas Egerer8444cf72010-09-20 11:11:38 -0700985 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -0700987 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800988 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +0200989 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
David S. Miller1d28f422011-03-12 00:29:39 -0500991 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700992 if (error) {
993 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -0700994 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -0700995 x = NULL;
996 goto out;
997 }
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001000 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001002 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001003 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001004 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001005 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001007 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001008 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001010 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001011 hrtimer_start(&x->mtimer,
1012 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1013 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001014 net->xfrm.state_num++;
1015 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001016 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else {
1018 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001019 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 x = NULL;
1021 error = -ESRCH;
1022 }
1023 }
1024out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001025 if (x) {
1026 if (!xfrm_state_hold_rcu(x)) {
1027 *err = -EAGAIN;
1028 x = NULL;
1029 }
1030 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001032 }
Florian Westphald737a582016-08-09 12:16:09 +02001033 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001034 if (to_put)
1035 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001036
1037 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1038 *err = -EAGAIN;
1039 if (x) {
1040 xfrm_state_put(x);
1041 x = NULL;
1042 }
1043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return x;
1046}
1047
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001048struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001049xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001050 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001051 unsigned short family, u8 mode, u8 proto, u32 reqid)
1052{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001053 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001054 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001055
Fan Du4ae770b2014-01-03 11:18:29 +08001056 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001057 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001058 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001059 if (x->props.family == family &&
1060 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001061 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001062 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001063 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1064 xfrm_state_addr_check(x, daddr, saddr, family) &&
1065 mode == x->props.mode &&
1066 proto == x->id.proto &&
1067 x->km.state == XFRM_STATE_VALID) {
1068 rx = x;
1069 break;
1070 }
1071 }
1072
1073 if (rx)
1074 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001075 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001076
1077
1078 return rx;
1079}
1080EXPORT_SYMBOL(xfrm_stateonly_find);
1081
Fan Duc4549972014-01-03 11:18:32 +08001082struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1083 unsigned short family)
1084{
1085 struct xfrm_state *x;
1086 struct xfrm_state_walk *w;
1087
1088 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1089 list_for_each_entry(w, &net->xfrm.state_all, all) {
1090 x = container_of(w, struct xfrm_state, km);
1091 if (x->props.family != family ||
1092 x->id.spi != spi)
1093 continue;
1094
Fan Duc4549972014-01-03 11:18:32 +08001095 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001096 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001097 return x;
1098 }
1099 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1100 return NULL;
1101}
1102EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104static void __xfrm_state_insert(struct xfrm_state *x)
1105{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001106 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001107 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001109 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001110
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001111 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001112 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001113 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001115 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001116 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001118 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001119 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001120 x->props.family);
1121
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001122 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001123 }
1124
Thomas Gleixner671422b2019-03-01 23:48:20 +01001125 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
David S. Millera47f0ce2006-08-24 03:54:22 -07001126 if (x->replay_maxage)
1127 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001128
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001129 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001130
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001131 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Fan Du283bc9f2013-11-07 17:47:50 +08001134/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001135static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1136{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001138 unsigned short family = xnew->props.family;
1139 u32 reqid = xnew->props.reqid;
1140 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001141 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001142 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001143 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001144
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001145 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001146 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001147 if (x->props.family == family &&
1148 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001149 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001150 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001151 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1152 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001153 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001154 }
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157void xfrm_state_insert(struct xfrm_state *x)
1158{
Fan Du283bc9f2013-11-07 17:47:50 +08001159 struct net *net = xs_net(x);
1160
1161 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001162 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001164 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166EXPORT_SYMBOL(xfrm_state_insert);
1167
Fan Du283bc9f2013-11-07 17:47:50 +08001168/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001169static struct xfrm_state *__find_acq_core(struct net *net,
1170 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001171 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001172 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001173 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001174 const xfrm_address_t *saddr,
1175 int create)
David S. Miller27708342006-08-24 00:13:10 -07001176{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001177 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001178 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001179 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001180
Sasha Levinb67bfe02013-02-27 17:06:00 -08001181 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001182 if (x->props.reqid != reqid ||
1183 x->props.mode != mode ||
1184 x->props.family != family ||
1185 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001186 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001187 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001188 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001189 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1190 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001191 continue;
1192
David S. Miller27708342006-08-24 00:13:10 -07001193 xfrm_state_hold(x);
1194 return x;
1195 }
1196
1197 if (!create)
1198 return NULL;
1199
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001200 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001201 if (likely(x)) {
1202 switch (family) {
1203 case AF_INET:
1204 x->sel.daddr.a4 = daddr->a4;
1205 x->sel.saddr.a4 = saddr->a4;
1206 x->sel.prefixlen_d = 32;
1207 x->sel.prefixlen_s = 32;
1208 x->props.saddr.a4 = saddr->a4;
1209 x->id.daddr.a4 = daddr->a4;
1210 break;
1211
1212 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001213 x->sel.daddr.in6 = daddr->in6;
1214 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001215 x->sel.prefixlen_d = 128;
1216 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001217 x->props.saddr.in6 = saddr->in6;
1218 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001219 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001220 }
David S. Miller27708342006-08-24 00:13:10 -07001221
1222 x->km.state = XFRM_STATE_ACQ;
1223 x->id.proto = proto;
1224 x->props.family = family;
1225 x->props.mode = mode;
1226 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001227 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001228 x->mark.v = m->v;
1229 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001230 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001231 xfrm_state_hold(x);
Thomas Gleixner671422b2019-03-01 23:48:20 +01001232 hrtimer_start(&x->mtimer,
1233 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1234 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001235 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001236 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001237 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001238 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001239
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001240 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001241
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001242 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001243 }
1244
1245 return x;
1246}
1247
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001248static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250int xfrm_state_add(struct xfrm_state *x)
1251{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001252 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001253 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int family;
1255 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001256 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001257 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
David S. Miller37b08e32008-09-02 20:14:15 -07001261 to_put = NULL;
1262
Fan Du283bc9f2013-11-07 17:47:50 +08001263 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
David S. Milleredcd5822006-08-24 00:42:45 -07001265 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001267 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 x1 = NULL;
1269 err = -EEXIST;
1270 goto out;
1271 }
1272
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001273 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001274 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001275 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001276 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001277 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 x1 = NULL;
1279 }
1280 }
1281
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001282 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001283 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001284 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001285 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
David S. Millerc7f5ea32006-08-24 03:29:04 -07001287 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 __xfrm_state_insert(x);
1289 err = 0;
1290
1291out:
Fan Du283bc9f2013-11-07 17:47:50 +08001292 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (x1) {
1295 xfrm_state_delete(x1);
1296 xfrm_state_put(x1);
1297 }
1298
David S. Miller37b08e32008-09-02 20:14:15 -07001299 if (to_put)
1300 xfrm_state_put(to_put);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return err;
1303}
1304EXPORT_SYMBOL(xfrm_state_add);
1305
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001306#ifdef CONFIG_XFRM_MIGRATE
Antony Antony4ab47d42017-06-06 12:12:13 +02001307static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1308 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001309{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001310 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001311 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001312 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001313 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001314
1315 memcpy(&x->id, &orig->id, sizeof(x->id));
1316 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1317 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1318 x->props.mode = orig->props.mode;
1319 x->props.replay_window = orig->props.replay_window;
1320 x->props.reqid = orig->props.reqid;
1321 x->props.family = orig->props.family;
1322 x->props.saddr = orig->props.saddr;
1323
1324 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001325 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001326 if (!x->aalg)
1327 goto error;
1328 }
1329 x->props.aalgo = orig->props.aalgo;
1330
Steffen Klassertee5c2312014-02-19 13:33:24 +01001331 if (orig->aead) {
1332 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001333 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001334 if (!x->aead)
1335 goto error;
1336 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001337 if (orig->ealg) {
1338 x->ealg = xfrm_algo_clone(orig->ealg);
1339 if (!x->ealg)
1340 goto error;
1341 }
1342 x->props.ealgo = orig->props.ealgo;
1343
1344 if (orig->calg) {
1345 x->calg = xfrm_algo_clone(orig->calg);
1346 if (!x->calg)
1347 goto error;
1348 }
1349 x->props.calgo = orig->props.calgo;
1350
Antony Antony4ab47d42017-06-06 12:12:13 +02001351 if (encap || orig->encap) {
1352 if (encap)
1353 x->encap = kmemdup(encap, sizeof(*x->encap),
1354 GFP_KERNEL);
1355 else
1356 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1357 GFP_KERNEL);
1358
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001359 if (!x->encap)
1360 goto error;
1361 }
1362
1363 if (orig->coaddr) {
1364 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1365 GFP_KERNEL);
1366 if (!x->coaddr)
1367 goto error;
1368 }
1369
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001370 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001371 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001372 goto error;
1373 }
1374
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001375 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1376
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001377 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001378 goto error;
1379
1380 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001381 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001382
Steffen Klassert7e652642018-06-12 14:07:07 +02001383 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001384 x->tfcpad = orig->tfcpad;
1385 x->replay_maxdiff = orig->replay_maxdiff;
1386 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001387 x->curlft.add_time = orig->curlft.add_time;
1388 x->km.state = orig->km.state;
1389 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001390 x->replay = orig->replay;
1391 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001392
1393 return x;
1394
1395 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001396 xfrm_state_put(x);
1397out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001398 return NULL;
1399}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001400
Fan Du283bc9f2013-11-07 17:47:50 +08001401struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001402{
1403 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001404 struct xfrm_state *x = NULL;
1405
1406 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001407
1408 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001409 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001410 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001411 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001412 if (x->props.mode != m->mode ||
1413 x->id.proto != m->proto)
1414 continue;
1415 if (m->reqid && x->props.reqid != m->reqid)
1416 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001417 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1418 m->old_family) ||
1419 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1420 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001421 continue;
1422 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001423 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001424 }
1425 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001426 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001427 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001428 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001429 if (x->props.mode != m->mode ||
1430 x->id.proto != m->proto)
1431 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001432 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1433 m->old_family) ||
1434 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1435 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001436 continue;
1437 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001438 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001439 }
1440 }
1441
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001442 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1443
1444 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001445}
1446EXPORT_SYMBOL(xfrm_migrate_state_find);
1447
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001448struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001449 struct xfrm_migrate *m,
1450 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001451{
1452 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001453
Antony Antony4ab47d42017-06-06 12:12:13 +02001454 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001455 if (!xc)
1456 return NULL;
1457
1458 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1459 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1460
1461 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001462 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001463 /* a care is needed when the destination address of the
1464 state is to be updated as it is a part of triplet */
1465 xfrm_state_insert(xc);
1466 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001467 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001468 goto error;
1469 }
1470
1471 return xc;
1472error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001473 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001474 return NULL;
1475}
1476EXPORT_SYMBOL(xfrm_state_migrate);
1477#endif
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479int xfrm_state_update(struct xfrm_state *x)
1480{
David S. Miller37b08e32008-09-02 20:14:15 -07001481 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001483 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001484 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
David S. Miller37b08e32008-09-02 20:14:15 -07001486 to_put = NULL;
1487
Fan Du283bc9f2013-11-07 17:47:50 +08001488 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001489 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 err = -ESRCH;
1492 if (!x1)
1493 goto out;
1494
1495 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001496 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 err = -EEXIST;
1498 goto out;
1499 }
1500
1501 if (x1->km.state == XFRM_STATE_ACQ) {
1502 __xfrm_state_insert(x);
1503 x = NULL;
1504 }
1505 err = 0;
1506
1507out:
Fan Du283bc9f2013-11-07 17:47:50 +08001508 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
David S. Miller37b08e32008-09-02 20:14:15 -07001510 if (to_put)
1511 xfrm_state_put(to_put);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (err)
1514 return err;
1515
1516 if (!x) {
1517 xfrm_state_delete(x1);
1518 xfrm_state_put(x1);
1519 return 0;
1520 }
1521
1522 err = -EINVAL;
1523 spin_lock_bh(&x1->lock);
1524 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001525 if (x->encap && x1->encap &&
1526 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001528 else if (x->encap || x1->encap)
1529 goto fail;
1530
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001531 if (x->coaddr && x1->coaddr) {
1532 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1533 }
1534 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1535 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1537 x1->km.dying = 0;
1538
Thomas Gleixner671422b2019-03-01 23:48:20 +01001539 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1540 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (x1->curlft.use_time)
1542 xfrm_state_check_expire(x1);
1543
Nathan Harold5baf4f92018-07-19 19:07:47 -07001544 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001545 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1546
Nathan Harold5baf4f92018-07-19 19:07:47 -07001547 if (x->props.smark.m || x->props.smark.v)
1548 x1->props.smark = x->props.smark;
1549
1550 if (x->if_id)
1551 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001552
1553 __xfrm_state_bump_genids(x1);
1554 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1555 }
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001558 x->km.state = XFRM_STATE_DEAD;
1559 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001561
1562fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 spin_unlock_bh(&x1->lock);
1564
1565 xfrm_state_put(x1);
1566
1567 return err;
1568}
1569EXPORT_SYMBOL(xfrm_state_update);
1570
1571int xfrm_state_check_expire(struct xfrm_state *x)
1572{
1573 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001574 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1577 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001578 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001579 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return -EINVAL;
1581 }
1582
1583 if (!x->km.dying &&
1584 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001585 x->curlft.packets >= x->lft.soft_packet_limit)) {
1586 x->km.dying = 1;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001587 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
1590}
1591EXPORT_SYMBOL(xfrm_state_check_expire);
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001594xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001595 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Florian Westphalc2f672f2016-09-20 15:45:26 +02001599 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001600 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001601 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return x;
1603}
1604EXPORT_SYMBOL(xfrm_state_lookup);
1605
1606struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001607xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001608 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001609 u8 proto, unsigned short family)
1610{
1611 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001612
Fan Du283bc9f2013-11-07 17:47:50 +08001613 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001614 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001615 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001616 return x;
1617}
1618EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1619
1620struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001621xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001622 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001623 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
1625 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Fan Du283bc9f2013-11-07 17:47:50 +08001627 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001628 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001629 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return x;
1632}
1633EXPORT_SYMBOL(xfrm_find_acq);
1634
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001635#ifdef CONFIG_XFRM_SUB_POLICY
1636int
1637xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
Fan Du283bc9f2013-11-07 17:47:50 +08001638 unsigned short family, struct net *net)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001639{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001640 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001641 int err = 0;
1642 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1643 if (!afinfo)
1644 return -EAFNOSUPPORT;
1645
Fan Du283bc9f2013-11-07 17:47:50 +08001646 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001647 if (afinfo->tmpl_sort)
1648 err = afinfo->tmpl_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001649 else
1650 for (i = 0; i < n; i++)
1651 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001652 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001653 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001654 return err;
1655}
1656EXPORT_SYMBOL(xfrm_tmpl_sort);
1657
1658int
1659xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1660 unsigned short family)
1661{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001662 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001663 int err = 0;
1664 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert35ea790d2014-02-19 13:33:23 +01001665 struct net *net = xs_net(*src);
Fan Du283bc9f2013-11-07 17:47:50 +08001666
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001667 if (!afinfo)
1668 return -EAFNOSUPPORT;
1669
Fan Du283bc9f2013-11-07 17:47:50 +08001670 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001671 if (afinfo->state_sort)
1672 err = afinfo->state_sort(dst, src, n);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001673 else
1674 for (i = 0; i < n; i++)
1675 dst[i] = src[i];
Fan Du283bc9f2013-11-07 17:47:50 +08001676 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalaf5d27c2017-01-09 14:20:47 +01001677 rcu_read_unlock();
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001678 return err;
1679}
1680EXPORT_SYMBOL(xfrm_state_sort);
1681#endif
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683/* Silly enough, but I'm lazy to build resolution list */
1684
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001685static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001689 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001690 struct xfrm_state *x;
1691
Sasha Levinb67bfe02013-02-27 17:06:00 -08001692 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001693 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001694 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001695 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 xfrm_state_hold(x);
1697 return x;
1698 }
1699 }
1700 }
1701 return NULL;
1702}
1703
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001704struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct xfrm_state *x;
1707
Fan Du283bc9f2013-11-07 17:47:50 +08001708 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001709 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001710 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return x;
1712}
1713EXPORT_SYMBOL(xfrm_find_acq_byseq);
1714
1715u32 xfrm_get_acqseq(void)
1716{
1717 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001718 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
jamal6836b9b2010-02-16 02:01:22 +00001720 do {
1721 res = atomic_inc_return(&acqseq);
1722 } while (!res);
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return res;
1725}
1726EXPORT_SYMBOL(xfrm_get_acqseq);
1727
Fan Du776e9dd2013-12-16 18:47:49 +08001728int verify_spi_info(u8 proto, u32 min, u32 max)
1729{
1730 switch (proto) {
1731 case IPPROTO_AH:
1732 case IPPROTO_ESP:
1733 break;
1734
1735 case IPPROTO_COMP:
1736 /* IPCOMP spi is 16-bits. */
1737 if (max >= 0x10000)
1738 return -EINVAL;
1739 break;
1740
1741 default:
1742 return -EINVAL;
1743 }
1744
1745 if (min > max)
1746 return -EINVAL;
1747
1748 return 0;
1749}
1750EXPORT_SYMBOL(verify_spi_info);
1751
Herbert Xu658b2192007-10-09 13:29:52 -07001752int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001754 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001755 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001757 int err = -ENOENT;
1758 __be32 minspi = htonl(low);
1759 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001760 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Herbert Xu658b2192007-10-09 13:29:52 -07001762 spin_lock_bh(&x->lock);
1763 if (x->km.state == XFRM_STATE_DEAD)
1764 goto unlock;
1765
1766 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001768 goto unlock;
1769
1770 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001773 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (x0) {
1775 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001776 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 x->id.spi = minspi;
1779 } else {
1780 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001781 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001782 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001783 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (x0 == NULL) {
1785 x->id.spi = htonl(spi);
1786 break;
1787 }
1788 xfrm_state_put(x0);
1789 }
1790 }
1791 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08001792 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08001793 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001794 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08001795 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07001796
1797 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
Herbert Xu658b2192007-10-09 13:29:52 -07001799
1800unlock:
1801 spin_unlock_bh(&x->lock);
1802
1803 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805EXPORT_SYMBOL(xfrm_alloc_spi);
1806
Nicolas Dichteld3623092014-02-14 15:30:36 +01001807static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001808 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01001809{
1810 if (filter) {
1811 if ((filter->family == AF_INET ||
1812 filter->family == AF_INET6) &&
1813 x->props.family != filter->family)
1814 return false;
1815
1816 return addr_match(&x->props.saddr, &filter->saddr,
1817 filter->splen) &&
1818 addr_match(&x->id.daddr, &filter->daddr,
1819 filter->dplen);
1820 }
1821 return true;
1822}
1823
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001824int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001825 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 void *data)
1827{
Herbert Xu12a169e2008-10-01 07:03:24 -07001828 struct xfrm_state *state;
1829 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 int err = 0;
1831
Herbert Xu12a169e2008-10-01 07:03:24 -07001832 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08001833 return 0;
1834
Fan Du283bc9f2013-11-07 17:47:50 +08001835 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001836 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001837 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001838 else
Li RongQing80077702015-04-22 17:09:54 +08001839 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08001840 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001841 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08001842 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001843 state = container_of(x, struct xfrm_state, km);
1844 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08001845 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001846 if (!__xfrm_state_filter_match(state, walk->filter))
1847 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001848 err = func(state, walk->seq, data);
1849 if (err) {
1850 list_move_tail(&walk->all, &x->all);
1851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001853 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001855 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 err = -ENOENT;
1857 goto out;
1858 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001859 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860out:
Fan Du283bc9f2013-11-07 17:47:50 +08001861 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return err;
1863}
1864EXPORT_SYMBOL(xfrm_state_walk);
1865
Nicolas Dichteld3623092014-02-14 15:30:36 +01001866void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01001867 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07001868{
Herbert Xu12a169e2008-10-01 07:03:24 -07001869 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07001870 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07001871 walk->state = XFRM_STATE_DEAD;
1872 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01001873 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07001874}
1875EXPORT_SYMBOL(xfrm_state_walk_init);
1876
Fan Du283bc9f2013-11-07 17:47:50 +08001877void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07001878{
Nicolas Dichteld3623092014-02-14 15:30:36 +01001879 kfree(walk->filter);
1880
Herbert Xu12a169e2008-10-01 07:03:24 -07001881 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07001882 return;
Herbert Xu5c182452008-09-22 19:48:19 -07001883
Fan Du283bc9f2013-11-07 17:47:50 +08001884 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001885 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08001886 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07001887}
1888EXPORT_SYMBOL(xfrm_state_walk_done);
1889
Kees Cooke99e88a2017-10-16 14:43:17 -07001890static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001891{
Kees Cooke99e88a2017-10-16 14:43:17 -07001892 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001893
1894 spin_lock(&x->lock);
1895
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001896 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001897 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00001898 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07001899 else
1900 x->xflags |= XFRM_TIME_DEFER;
1901 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001902
1903 spin_unlock(&x->lock);
1904}
1905
Denis Chengdf018122007-12-07 00:51:11 -08001906static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
David S. Miller214e0052011-02-24 00:02:38 -05001908void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 struct xfrm_mgr *km;
1911
Cong Wang85168c02013-01-16 16:05:06 +08001912 rcu_read_lock();
1913 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001914 if (km->notify_policy)
1915 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08001916 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001917}
1918
David S. Miller214e0052011-02-24 00:02:38 -05001919void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001920{
1921 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08001922 rcu_read_lock();
1923 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001924 if (km->notify)
1925 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08001926 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001927}
1928
1929EXPORT_SYMBOL(km_policy_notify);
1930EXPORT_SYMBOL(km_state_notify);
1931
Eric W. Biederman15e47302012-09-07 20:12:54 +00001932void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001933{
1934 struct km_event c;
1935
Herbert Xubf08867f92005-06-18 22:44:00 -07001936 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001937 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001938 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001939 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001942EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001943/*
1944 * We send to all registered managers regardless of failure
1945 * We are happy with one success
1946*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001947int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001949 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct xfrm_mgr *km;
1951
Cong Wang85168c02013-01-16 16:05:06 +08001952 rcu_read_lock();
1953 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08001954 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001955 if (!acqret)
1956 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Cong Wang85168c02013-01-16 16:05:06 +08001958 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return err;
1960}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001961EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Al Viro5d36b182006-11-08 00:24:06 -08001963int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int err = -EINVAL;
1966 struct xfrm_mgr *km;
1967
Cong Wang85168c02013-01-16 16:05:06 +08001968 rcu_read_lock();
1969 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (km->new_mapping)
1971 err = km->new_mapping(x, ipaddr, sport);
1972 if (!err)
1973 break;
1974 }
Cong Wang85168c02013-01-16 16:05:06 +08001975 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return err;
1977}
1978EXPORT_SYMBOL(km_new_mapping);
1979
Eric W. Biederman15e47302012-09-07 20:12:54 +00001980void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001982 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Herbert Xubf08867f92005-06-18 22:44:00 -07001984 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001985 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001986 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001987 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
David S. Millera70fcb02006-03-20 19:18:52 -08001989EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Eric Dumazet2d60abc2008-01-03 20:43:21 -08001991#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001992int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1993 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02001994 const struct xfrm_kmaddress *k,
1995 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001996{
1997 int err = -EINVAL;
1998 int ret;
1999 struct xfrm_mgr *km;
2000
Cong Wang85168c02013-01-16 16:05:06 +08002001 rcu_read_lock();
2002 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002003 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002004 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2005 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002006 if (!ret)
2007 err = ret;
2008 }
2009 }
Cong Wang85168c02013-01-16 16:05:06 +08002010 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002011 return err;
2012}
2013EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002014#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002015
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002016int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002017{
2018 int err = -EINVAL;
2019 int ret;
2020 struct xfrm_mgr *km;
2021
Cong Wang85168c02013-01-16 16:05:06 +08002022 rcu_read_lock();
2023 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002024 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002025 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002026 if (!ret)
2027 err = ret;
2028 }
2029 }
Cong Wang85168c02013-01-16 16:05:06 +08002030 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002031 return err;
2032}
2033EXPORT_SYMBOL(km_report);
2034
Florian Westphalbb9cd072019-04-17 11:45:13 +02002035static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002036{
2037 struct xfrm_mgr *km;
2038 bool is_alive = false;
2039
2040 rcu_read_lock();
2041 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2042 if (km->is_alive && km->is_alive(c)) {
2043 is_alive = true;
2044 break;
2045 }
2046 }
2047 rcu_read_unlock();
2048
2049 return is_alive;
2050}
Horia Geanta0f245582014-02-12 16:20:06 +02002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
2053{
2054 int err;
2055 u8 *data;
2056 struct xfrm_mgr *km;
2057 struct xfrm_policy *pol = NULL;
2058
Steffen Klassert19d7df62018-02-01 08:49:23 +01002059 if (in_compat_syscall())
2060 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002061
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002062 if (!optval && !optlen) {
2063 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2064 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2065 __sk_dst_reset(sk);
2066 return 0;
2067 }
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (optlen <= 0 || optlen > PAGE_SIZE)
2070 return -EMSGSIZE;
2071
Geliang Tanga133d932017-05-06 23:42:21 +08002072 data = memdup_user(optval, optlen);
2073 if (IS_ERR(data))
2074 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002077 rcu_read_lock();
2078 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002079 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 optlen, &err);
2081 if (err >= 0)
2082 break;
2083 }
Cong Wang85168c02013-01-16 16:05:06 +08002084 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 if (err >= 0) {
2087 xfrm_sk_policy_insert(sk, err, pol);
2088 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002089 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 err = 0;
2091 }
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 kfree(data);
2094 return err;
2095}
2096EXPORT_SYMBOL(xfrm_user_policy);
2097
Cong Wang85168c02013-01-16 16:05:06 +08002098static DEFINE_SPINLOCK(xfrm_km_lock);
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100int xfrm_register_km(struct xfrm_mgr *km)
2101{
Cong Wang85168c02013-01-16 16:05:06 +08002102 spin_lock_bh(&xfrm_km_lock);
2103 list_add_tail_rcu(&km->list, &xfrm_km_list);
2104 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
2106}
2107EXPORT_SYMBOL(xfrm_register_km);
2108
2109int xfrm_unregister_km(struct xfrm_mgr *km)
2110{
Cong Wang85168c02013-01-16 16:05:06 +08002111 spin_lock_bh(&xfrm_km_lock);
2112 list_del_rcu(&km->list);
2113 spin_unlock_bh(&xfrm_km_lock);
2114 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
2116}
2117EXPORT_SYMBOL(xfrm_unregister_km);
2118
2119int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2120{
2121 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002122
2123 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002125
Cong Wang44abdc32013-01-16 16:05:05 +08002126 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002128 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002129 else
Cong Wang44abdc32013-01-16 16:05:05 +08002130 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2131 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return err;
2133}
2134EXPORT_SYMBOL(xfrm_state_register_afinfo);
2135
2136int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2137{
Florian Westphal423826a2017-01-09 14:20:46 +01002138 int err = 0, family = afinfo->family;
2139
2140 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002142
Cong Wang44abdc32013-01-16 16:05:05 +08002143 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002145 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002147 else
Cong Wang44abdc32013-01-16 16:05:05 +08002148 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Cong Wang44abdc32013-01-16 16:05:05 +08002150 spin_unlock_bh(&xfrm_state_afinfo_lock);
2151 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return err;
2153}
2154EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2155
Florian Westphal711059b2017-01-09 14:20:48 +01002156struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2157{
2158 if (unlikely(family >= NPROTO))
2159 return NULL;
2160
2161 return rcu_dereference(xfrm_state_afinfo[family]);
2162}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002163EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002164
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002165struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct xfrm_state_afinfo *afinfo;
2168 if (unlikely(family >= NPROTO))
2169 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002170 rcu_read_lock();
2171 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002172 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002173 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return afinfo;
2175}
2176
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002177void xfrm_flush_gc(void)
2178{
2179 flush_work(&xfrm_state_gc_work);
2180}
2181EXPORT_SYMBOL(xfrm_flush_gc);
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2184void xfrm_state_delete_tunnel(struct xfrm_state *x)
2185{
2186 if (x->tunnel) {
2187 struct xfrm_state *t = x->tunnel;
2188
2189 if (atomic_read(&t->tunnel_users) == 2)
2190 xfrm_state_delete(t);
2191 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002192 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 x->tunnel = NULL;
2194 }
2195}
2196EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2197
2198int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2199{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002200 const struct xfrm_type *type = READ_ONCE(x->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Patrick McHardyc5c25232007-04-09 11:47:18 -07002202 if (x->km.state == XFRM_STATE_VALID &&
Florian Westphalb3b73b82017-01-05 13:23:58 +01002203 type && type->get_mtu)
2204 return type->get_mtu(x, mtu);
2205
2206 return mtu - x->props.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Ilan Tayariffdb5212017-08-01 12:49:08 +03002209int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002210{
Florian Westphal4c145dc2019-03-29 21:16:31 +01002211 const struct xfrm_state_afinfo *afinfo;
Florian Westphalc9500d72019-03-29 21:16:32 +01002212 const struct xfrm_mode *inner_mode;
2213 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002214 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002215 int err;
2216
Herbert Xud094cd82005-06-20 13:19:41 -07002217 err = -EAFNOSUPPORT;
2218 afinfo = xfrm_state_get_afinfo(family);
2219 if (!afinfo)
2220 goto error;
2221
2222 err = 0;
2223 if (afinfo->init_flags)
2224 err = afinfo->init_flags(x);
2225
Florian Westphalaf5d27c2017-01-09 14:20:47 +01002226 rcu_read_unlock();
Herbert Xud094cd82005-06-20 13:19:41 -07002227
2228 if (err)
2229 goto error;
2230
2231 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002232
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002233 if (x->sel.family != AF_UNSPEC) {
2234 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2235 if (inner_mode == NULL)
2236 goto error;
2237
2238 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002239 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002240 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002241
Florian Westphalc9500d72019-03-29 21:16:32 +01002242 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002243 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002244 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002245 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002246
Martin Willid81d2282008-12-03 15:38:07 -08002247 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002248 if (inner_mode == NULL)
2249 goto error;
2250
Florian Westphal4c145dc2019-03-29 21:16:31 +01002251 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002252 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002253
Florian Westphalc9500d72019-03-29 21:16:32 +01002254 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002255
Martin Willid81d2282008-12-03 15:38:07 -08002256 if (x->props.family == AF_INET)
2257 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002258
Martin Willid81d2282008-12-03 15:38:07 -08002259 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2260 if (inner_mode_iaf) {
2261 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002262 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002263 }
2264 }
Herbert Xu13996372007-10-17 21:35:51 -07002265
Herbert Xud094cd82005-06-20 13:19:41 -07002266 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002267 if (x->type == NULL)
2268 goto error;
2269
Ilan Tayariffdb5212017-08-01 12:49:08 +03002270 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002271
Herbert Xu72cb6962005-06-20 13:18:08 -07002272 err = x->type->init_state(x);
2273 if (err)
2274 goto error;
2275
Florian Westphalc9500d72019-03-29 21:16:32 +01002276 outer_mode = xfrm_get_mode(x->props.mode, family);
2277 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002278 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002279 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002280 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002281
Florian Westphalc9500d72019-03-29 21:16:32 +01002282 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002283 if (init_replay) {
2284 err = xfrm_init_replay(x);
2285 if (err)
2286 goto error;
2287 }
2288
Herbert Xu72cb6962005-06-20 13:18:08 -07002289error:
2290 return err;
2291}
2292
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002293EXPORT_SYMBOL(__xfrm_init_state);
2294
2295int xfrm_init_state(struct xfrm_state *x)
2296{
Yossi Kupermancc015722018-01-17 15:52:41 +02002297 int err;
2298
2299 err = __xfrm_init_state(x, true, false);
2300 if (!err)
2301 x->km.state = XFRM_STATE_VALID;
2302
2303 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002304}
2305
Herbert Xu72cb6962005-06-20 13:18:08 -07002306EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002307
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002308int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
David S. Millerf034b5d2006-08-24 03:08:07 -07002310 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Mathias Krause565f0fa2018-05-03 10:55:07 +02002312 if (net_eq(net, &init_net))
2313 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2314 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2315
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002316 INIT_LIST_HEAD(&net->xfrm.state_all);
2317
David S. Millerf034b5d2006-08-24 03:08:07 -07002318 sz = sizeof(struct hlist_head) * 8;
2319
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002320 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2321 if (!net->xfrm.state_bydst)
2322 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002323 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2324 if (!net->xfrm.state_bysrc)
2325 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002326 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2327 if (!net->xfrm.state_byspi)
2328 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002329 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002330
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002331 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002332 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002333 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002334 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002335
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002336out_byspi:
2337 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002338out_bysrc:
2339 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002340out_bydst:
2341 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002342}
2343
2344void xfrm_state_fini(struct net *net)
2345{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002346 unsigned int sz;
2347
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002348 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002349 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002350 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002351
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002352 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002353
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002354 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002355 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2356 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002357 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2358 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002359 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2360 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
Joy Lattenab5f5e82007-09-17 11:51:22 -07002363#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002364static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2365 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002366{
Paul Moore68277ac2007-12-20 20:49:33 -08002367 struct xfrm_sec_ctx *ctx = x->security;
2368 u32 spi = ntohl(x->id.spi);
2369
2370 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002371 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002372 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002373
Weilong Chen9b7a7872013-12-24 09:43:46 +08002374 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002375 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002376 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2377 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002378 break;
2379 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002380 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002381 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002382 break;
2383 }
Paul Moore68277ac2007-12-20 20:49:33 -08002384
2385 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002386}
2387
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002388static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2389 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002390{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002391 const struct iphdr *iph4;
2392 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002393
2394 switch (family) {
2395 case AF_INET:
2396 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002397 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2398 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002399 break;
2400 case AF_INET6:
2401 iph6 = ipv6_hdr(skb);
2402 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002403 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002404 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002405 iph6->flow_lbl[0] & 0x0f,
2406 iph6->flow_lbl[1],
2407 iph6->flow_lbl[2]);
2408 break;
2409 }
2410}
2411
Tetsuo Handa2e710292014-04-22 21:48:30 +09002412void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002413{
2414 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002415
Paul Mooreafeb14b2007-12-21 14:58:11 -08002416 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002417 if (audit_buf == NULL)
2418 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002419 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002420 xfrm_audit_helper_sainfo(x, audit_buf);
2421 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002422 audit_log_end(audit_buf);
2423}
2424EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2425
Tetsuo Handa2e710292014-04-22 21:48:30 +09002426void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002427{
2428 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002429
Paul Mooreafeb14b2007-12-21 14:58:11 -08002430 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002431 if (audit_buf == NULL)
2432 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002433 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002434 xfrm_audit_helper_sainfo(x, audit_buf);
2435 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002436 audit_log_end(audit_buf);
2437}
2438EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002439
2440void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2441 struct sk_buff *skb)
2442{
2443 struct audit_buffer *audit_buf;
2444 u32 spi;
2445
2446 audit_buf = xfrm_audit_start("SA-replay-overflow");
2447 if (audit_buf == NULL)
2448 return;
2449 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2450 /* don't record the sequence number because it's inherent in this kind
2451 * of audit message */
2452 spi = ntohl(x->id.spi);
2453 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2454 audit_log_end(audit_buf);
2455}
2456EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2457
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002458void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002459 struct sk_buff *skb, __be32 net_seq)
2460{
2461 struct audit_buffer *audit_buf;
2462 u32 spi;
2463
2464 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2465 if (audit_buf == NULL)
2466 return;
2467 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2468 spi = ntohl(x->id.spi);
2469 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2470 spi, spi, ntohl(net_seq));
2471 audit_log_end(audit_buf);
2472}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002473EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002474
2475void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2476{
2477 struct audit_buffer *audit_buf;
2478
2479 audit_buf = xfrm_audit_start("SA-notfound");
2480 if (audit_buf == NULL)
2481 return;
2482 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2483 audit_log_end(audit_buf);
2484}
2485EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2486
2487void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2488 __be32 net_spi, __be32 net_seq)
2489{
2490 struct audit_buffer *audit_buf;
2491 u32 spi;
2492
2493 audit_buf = xfrm_audit_start("SA-notfound");
2494 if (audit_buf == NULL)
2495 return;
2496 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2497 spi = ntohl(net_spi);
2498 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2499 spi, spi, ntohl(net_seq));
2500 audit_log_end(audit_buf);
2501}
2502EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2503
2504void xfrm_audit_state_icvfail(struct xfrm_state *x,
2505 struct sk_buff *skb, u8 proto)
2506{
2507 struct audit_buffer *audit_buf;
2508 __be32 net_spi;
2509 __be32 net_seq;
2510
2511 audit_buf = xfrm_audit_start("SA-icv-failure");
2512 if (audit_buf == NULL)
2513 return;
2514 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2515 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2516 u32 spi = ntohl(net_spi);
2517 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2518 spi, spi, ntohl(net_seq));
2519 }
2520 audit_log_end(audit_buf);
2521}
2522EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002523#endif /* CONFIG_AUDITSYSCALL */