blob: 1befc6db723b0ef286bd662f6b2e666e1b4a0197 [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
Florian Westphalc7b37c72019-06-24 22:04:48 +020030#include <crypto/aead.h>
31
David S. Miller44e36b42006-08-24 04:50:50 -070032#include "xfrm_hash.h"
33
Florian Westphalc8406992016-08-09 12:16:08 +020034#define xfrm_state_deref_prot(table, net) \
35 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
36
Florian Westphal35db57bb2016-08-23 16:00:12 +020037static void xfrm_state_gc_task(struct work_struct *work);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* Each xfrm_state may be linked to two tables:
40
41 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
David S. Millera624c102006-08-24 03:24:33 -070042 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 destination/tunnel endpoint. (output)
44 */
45
David S. Millerf034b5d2006-08-24 03:08:07 -070046static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Mathias Krause565f0fa2018-05-03 10:55:07 +020047static struct kmem_cache *xfrm_state_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Florian Westphal35db57bb2016-08-23 16:00:12 +020049static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
50static HLIST_HEAD(xfrm_state_gc_list);
51
Florian Westphal02efdff2016-08-09 12:16:05 +020052static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
53{
Reshetova, Elena88755e9c2017-07-04 15:53:21 +030054 return refcount_inc_not_zero(&x->refcnt);
Florian Westphal02efdff2016-08-09 12:16:05 +020055}
56
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080057static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050058 const xfrm_address_t *daddr,
59 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070060 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070061 unsigned short family)
62{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080063 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070064}
65
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080066static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050067 const xfrm_address_t *daddr,
68 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070069 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070070{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080071 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070072}
73
David S. Miller2575b652006-08-24 03:26:44 -070074static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050075xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
76 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070077{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080078 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070079}
80
David S. Millerf034b5d2006-08-24 03:08:07 -070081static void xfrm_hash_transfer(struct hlist_head *list,
82 struct hlist_head *ndsttable,
83 struct hlist_head *nsrctable,
84 struct hlist_head *nspitable,
85 unsigned int nhashmask)
86{
Sasha Levinb67bfe02013-02-27 17:06:00 -080087 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070088 struct xfrm_state *x;
89
Sasha Levinb67bfe02013-02-27 17:06:00 -080090 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070091 unsigned int h;
92
David S. Millerc1969f22006-08-24 04:00:03 -070093 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
94 x->props.reqid, x->props.family,
95 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020096 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070097
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070098 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
99 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -0700100 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200101 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -0700102
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700103 if (x->id.spi) {
104 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
105 x->id.proto, x->props.family,
106 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200107 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700108 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700109 }
110}
111
Alexey Dobriyan63082732008-11-25 17:19:07 -0800112static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700113{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800114 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700115}
116
Alexey Dobriyan63082732008-11-25 17:19:07 -0800117static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700118{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800119 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700120 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
121 unsigned long nsize, osize;
122 unsigned int nhashmask, ohashmask;
123 int i;
124
Alexey Dobriyan63082732008-11-25 17:19:07 -0800125 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700126 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700127 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800128 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700129 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700130 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700131 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800132 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700133 }
David S. Miller44e36b42006-08-24 04:50:50 -0700134 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700135 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700136 xfrm_hash_free(ndst, nsize);
137 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800138 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700139 }
140
Fan Du283bc9f2013-11-07 17:47:50 +0800141 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Ahmed S. Darwish02244322021-03-16 11:56:29 +0100142 write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700143
144 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200145 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800146 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200147 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700148
Florian Westphalc8406992016-08-09 12:16:08 +0200149 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
150 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800151 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700152
Florian Westphalc8406992016-08-09 12:16:08 +0200153 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
154 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
155 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800156 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700157
Ahmed S. Darwish02244322021-03-16 11:56:29 +0100158 write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800159 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700160
161 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200162
163 synchronize_rcu();
164
David S. Miller44e36b42006-08-24 04:50:50 -0700165 xfrm_hash_free(odst, osize);
166 xfrm_hash_free(osrc, osize);
167 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700168}
169
Cong Wang44abdc32013-01-16 16:05:05 +0800170static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
171static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static DEFINE_SPINLOCK(xfrm_state_gc_lock);
174
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800175int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800177int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Florian Westphalbb9cd072019-04-17 11:45:13 +0200178static bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000179void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
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);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700184 int err = 0;
185
Florian Westphal4f518e82019-05-03 17:46:19 +0200186 if (!afinfo)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700187 return -EAFNOSUPPORT;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700188
Florian Westphal4f518e82019-05-03 17:46:19 +0200189#define X(afi, T, name) do { \
190 WARN_ON((afi)->type_ ## name); \
191 (afi)->type_ ## name = (T); \
192 } while (0)
193
194 switch (type->proto) {
195 case IPPROTO_COMP:
196 X(afinfo, type, comp);
197 break;
198 case IPPROTO_AH:
199 X(afinfo, type, ah);
200 break;
201 case IPPROTO_ESP:
202 X(afinfo, type, esp);
203 break;
204 case IPPROTO_IPIP:
205 X(afinfo, type, ipip);
206 break;
207 case IPPROTO_DSTOPTS:
208 X(afinfo, type, dstopts);
209 break;
210 case IPPROTO_ROUTING:
211 X(afinfo, type, routing);
212 break;
213 case IPPROTO_IPV6:
214 X(afinfo, type, ipip6);
215 break;
216 default:
217 WARN_ON(1);
218 err = -EPROTONOSUPPORT;
219 break;
220 }
221#undef X
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100222 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700223 return err;
224}
225EXPORT_SYMBOL(xfrm_register_type);
226
Florian Westphal4f518e82019-05-03 17:46:19 +0200227void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700228{
Cong Wang7a9885b2013-01-17 16:34:11 +0800229 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700230
231 if (unlikely(afinfo == NULL))
Florian Westphal4f518e82019-05-03 17:46:19 +0200232 return;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700233
Florian Westphal4f518e82019-05-03 17:46:19 +0200234#define X(afi, T, name) do { \
235 WARN_ON((afi)->type_ ## name != (T)); \
236 (afi)->type_ ## name = NULL; \
237 } while (0)
238
239 switch (type->proto) {
240 case IPPROTO_COMP:
241 X(afinfo, type, comp);
242 break;
243 case IPPROTO_AH:
244 X(afinfo, type, ah);
245 break;
246 case IPPROTO_ESP:
247 X(afinfo, type, esp);
248 break;
249 case IPPROTO_IPIP:
250 X(afinfo, type, ipip);
251 break;
252 case IPPROTO_DSTOPTS:
253 X(afinfo, type, dstopts);
254 break;
255 case IPPROTO_ROUTING:
256 X(afinfo, type, routing);
257 break;
258 case IPPROTO_IPV6:
259 X(afinfo, type, ipip6);
260 break;
261 default:
262 WARN_ON(1);
263 break;
264 }
265#undef X
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100266 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700267}
268EXPORT_SYMBOL(xfrm_unregister_type);
269
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800270static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700271{
Florian Westphal4f518e82019-05-03 17:46:19 +0200272 const struct xfrm_type *type = NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700273 struct xfrm_state_afinfo *afinfo;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700274 int modload_attempted = 0;
275
276retry:
277 afinfo = xfrm_state_get_afinfo(family);
278 if (unlikely(afinfo == NULL))
279 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700280
Florian Westphal4f518e82019-05-03 17:46:19 +0200281 switch (proto) {
282 case IPPROTO_COMP:
283 type = afinfo->type_comp;
284 break;
285 case IPPROTO_AH:
286 type = afinfo->type_ah;
287 break;
288 case IPPROTO_ESP:
289 type = afinfo->type_esp;
290 break;
291 case IPPROTO_IPIP:
292 type = afinfo->type_ipip;
293 break;
294 case IPPROTO_DSTOPTS:
295 type = afinfo->type_dstopts;
296 break;
297 case IPPROTO_ROUTING:
298 type = afinfo->type_routing;
299 break;
300 case IPPROTO_IPV6:
301 type = afinfo->type_ipip6;
302 break;
303 default:
304 break;
305 }
306
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700307 if (unlikely(type && !try_module_get(type->owner)))
308 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100309
310 rcu_read_unlock();
311
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700312 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700313 request_module("xfrm-type-%d-%d", family, proto);
314 modload_attempted = 1;
315 goto retry;
316 }
317
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700318 return type;
319}
320
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800321static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700322{
323 module_put(type->owner);
324}
325
Steffen Klassert9d389d72017-04-14 10:05:44 +0200326int xfrm_register_type_offload(const struct xfrm_type_offload *type,
327 unsigned short family)
328{
329 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200330 int err = 0;
331
332 if (unlikely(afinfo == NULL))
333 return -EAFNOSUPPORT;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200334
Florian Westphal4f518e82019-05-03 17:46:19 +0200335 switch (type->proto) {
336 case IPPROTO_ESP:
337 WARN_ON(afinfo->type_offload_esp);
338 afinfo->type_offload_esp = type;
339 break;
340 default:
341 WARN_ON(1);
342 err = -EPROTONOSUPPORT;
343 break;
344 }
345
Steffen Klassert9d389d72017-04-14 10:05:44 +0200346 rcu_read_unlock();
347 return err;
348}
349EXPORT_SYMBOL(xfrm_register_type_offload);
350
Florian Westphal4f518e82019-05-03 17:46:19 +0200351void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
352 unsigned short family)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200353{
354 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200355
356 if (unlikely(afinfo == NULL))
Florian Westphal4f518e82019-05-03 17:46:19 +0200357 return;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200358
Florian Westphal4f518e82019-05-03 17:46:19 +0200359 switch (type->proto) {
360 case IPPROTO_ESP:
361 WARN_ON(afinfo->type_offload_esp != type);
362 afinfo->type_offload_esp = NULL;
363 break;
364 default:
365 WARN_ON(1);
366 break;
367 }
Steffen Klassert9d389d72017-04-14 10:05:44 +0200368 rcu_read_unlock();
Steffen Klassert9d389d72017-04-14 10:05:44 +0200369}
370EXPORT_SYMBOL(xfrm_unregister_type_offload);
371
Ilan Tayariffdb5212017-08-01 12:49:08 +0300372static const struct xfrm_type_offload *
373xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200374{
Florian Westphal4f518e82019-05-03 17:46:19 +0200375 const struct xfrm_type_offload *type = NULL;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200376 struct xfrm_state_afinfo *afinfo;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200377
Ilan Tayariffdb5212017-08-01 12:49:08 +0300378retry:
Steffen Klassert9d389d72017-04-14 10:05:44 +0200379 afinfo = xfrm_state_get_afinfo(family);
380 if (unlikely(afinfo == NULL))
381 return NULL;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200382
Florian Westphal4f518e82019-05-03 17:46:19 +0200383 switch (proto) {
384 case IPPROTO_ESP:
385 type = afinfo->type_offload_esp;
386 break;
387 default:
388 break;
389 }
390
Steffen Klassert9d389d72017-04-14 10:05:44 +0200391 if ((type && !try_module_get(type->owner)))
392 type = NULL;
393
Sabrina Dubroca2f10a612017-12-31 16:18:56 +0100394 rcu_read_unlock();
395
Ilan Tayariffdb5212017-08-01 12:49:08 +0300396 if (!type && try_load) {
397 request_module("xfrm-offload-%d-%d", family, proto);
Gustavo A. R. Silva545d8ae2018-01-22 16:34:09 -0600398 try_load = false;
Ilan Tayariffdb5212017-08-01 12:49:08 +0300399 goto retry;
400 }
401
Steffen Klassert9d389d72017-04-14 10:05:44 +0200402 return type;
403}
404
405static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
406{
407 module_put(type->owner);
408}
409
Florian Westphal4c145dc2019-03-29 21:16:31 +0100410static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
411 [XFRM_MODE_BEET] = {
412 .encap = XFRM_MODE_BEET,
413 .flags = XFRM_MODE_FLAG_TUNNEL,
414 .family = AF_INET,
415 },
416 [XFRM_MODE_TRANSPORT] = {
417 .encap = XFRM_MODE_TRANSPORT,
418 .family = AF_INET,
419 },
420 [XFRM_MODE_TUNNEL] = {
421 .encap = XFRM_MODE_TUNNEL,
422 .flags = XFRM_MODE_FLAG_TUNNEL,
423 .family = AF_INET,
424 },
425};
426
427static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
428 [XFRM_MODE_BEET] = {
429 .encap = XFRM_MODE_BEET,
430 .flags = XFRM_MODE_FLAG_TUNNEL,
431 .family = AF_INET6,
432 },
433 [XFRM_MODE_ROUTEOPTIMIZATION] = {
434 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
435 .family = AF_INET6,
436 },
437 [XFRM_MODE_TRANSPORT] = {
438 .encap = XFRM_MODE_TRANSPORT,
439 .family = AF_INET6,
440 },
441 [XFRM_MODE_TUNNEL] = {
442 .encap = XFRM_MODE_TUNNEL,
443 .flags = XFRM_MODE_FLAG_TUNNEL,
444 .family = AF_INET6,
445 },
446};
447
448static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700449{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100450 const struct xfrm_mode *mode;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700451
452 if (unlikely(encap >= XFRM_MODE_MAX))
453 return NULL;
454
Florian Westphal4c145dc2019-03-29 21:16:31 +0100455 switch (family) {
456 case AF_INET:
457 mode = &xfrm4_mode_map[encap];
458 if (mode->family == family)
459 return mode;
460 break;
461 case AF_INET6:
462 mode = &xfrm6_mode_map[encap];
463 if (mode->family == family)
464 return mode;
465 break;
466 default:
467 break;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700468 }
469
Florian Westphal4c145dc2019-03-29 21:16:31 +0100470 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700471}
472
Mathias Krause4a135e52018-11-21 21:09:23 +0100473void xfrm_state_free(struct xfrm_state *x)
474{
475 kmem_cache_free(xfrm_state_cache, x);
476}
477EXPORT_SYMBOL(xfrm_state_free);
478
Cong Wangf75a2802019-01-31 13:05:49 -0800479static void ___xfrm_state_destroy(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100481 hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700482 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000483 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800484 kfree(x->aalg);
485 kfree(x->ealg);
486 kfree(x->calg);
487 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700488 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000489 kfree(x->replay_esn);
490 kfree(x->preplay_esn);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200491 if (x->type_offload)
492 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (x->type) {
494 x->type->destructor(x);
495 xfrm_put_type(x->type);
496 }
Steffen Klassert86c67392019-11-06 08:13:49 +0100497 if (x->xfrag.page)
498 put_page(x->xfrag.page);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200499 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800500 security_xfrm_state_free(x);
Mathias Krause4a135e52018-11-21 21:09:23 +0100501 xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800504static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Herbert Xu12a169e2008-10-01 07:03:24 -0700506 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800507 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700508 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200511 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 spin_unlock_bh(&xfrm_state_gc_lock);
513
Florian Westphaldf7274e2016-08-09 12:16:06 +0200514 synchronize_rcu();
515
Sasha Levinb67bfe02013-02-27 17:06:00 -0800516 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Cong Wangf75a2802019-01-31 13:05:49 -0800517 ___xfrm_state_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800520static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100522 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
523 enum hrtimer_restart ret = HRTIMER_NORESTART;
Arnd Bergmann386c5682018-07-11 12:19:13 +0200524 time64_t now = ktime_get_real_seconds();
525 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600527 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 spin_lock(&x->lock);
530 if (x->km.state == XFRM_STATE_DEAD)
531 goto out;
532 if (x->km.state == XFRM_STATE_EXPIRED)
533 goto expired;
534 if (x->lft.hard_add_expires_seconds) {
535 long tmo = x->lft.hard_add_expires_seconds +
536 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000537 if (tmo <= 0) {
538 if (x->xflags & XFRM_SOFT_EXPIRE) {
539 /* enter hard expire without soft expire first?!
540 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100541 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000542 */
543 x->curlft.add_time = now - x->saved_tmo - 1;
544 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
545 } else
546 goto expired;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (tmo < next)
549 next = tmo;
550 }
551 if (x->lft.hard_use_expires_seconds) {
552 long tmo = x->lft.hard_use_expires_seconds +
553 (x->curlft.use_time ? : now) - now;
554 if (tmo <= 0)
555 goto expired;
556 if (tmo < next)
557 next = tmo;
558 }
559 if (x->km.dying)
560 goto resched;
561 if (x->lft.soft_add_expires_seconds) {
562 long tmo = x->lft.soft_add_expires_seconds +
563 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000564 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000566 x->xflags &= ~XFRM_SOFT_EXPIRE;
567 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000569 x->xflags |= XFRM_SOFT_EXPIRE;
570 x->saved_tmo = tmo;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 if (x->lft.soft_use_expires_seconds) {
574 long tmo = x->lft.soft_use_expires_seconds +
575 (x->curlft.use_time ? : now) - now;
576 if (tmo <= 0)
577 warn = 1;
578 else if (tmo < next)
579 next = tmo;
580 }
581
Herbert Xu4666faa2005-06-18 22:43:22 -0700582 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800584 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585resched:
Arnd Bergmann386c5682018-07-11 12:19:13 +0200586 if (next != TIME64_MAX) {
Thomas Gleixner671422b2019-03-01 23:48:20 +0100587 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
588 ret = HRTIMER_RESTART;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800589 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto out;
592
593expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200594 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600596
597 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200598 if (!err)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800599 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Tetsuo Handa2e710292014-04-22 21:48:30 +0900601 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603out:
604 spin_unlock(&x->lock);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100605 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Kees Cooke99e88a2017-10-16 14:43:17 -0700608static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800609
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800610struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct xfrm_state *x;
613
Huang Zijianga4c278d2020-02-12 17:54:36 +0800614 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800617 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300618 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700620 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700621 INIT_HLIST_NODE(&x->bydst);
622 INIT_HLIST_NODE(&x->bysrc);
623 INIT_HLIST_NODE(&x->byspi);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100624 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
625 x->mtimer.function = xfrm_timer_handler;
Kees Cooke99e88a2017-10-16 14:43:17 -0700626 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200627 x->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 x->lft.soft_byte_limit = XFRM_INF;
629 x->lft.soft_packet_limit = XFRM_INF;
630 x->lft.hard_byte_limit = XFRM_INF;
631 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800632 x->replay_maxage = 0;
633 x->replay_maxdiff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 spin_lock_init(&x->lock);
635 }
636 return x;
637}
638EXPORT_SYMBOL(xfrm_state_alloc);
639
Cong Wangf75a2802019-01-31 13:05:49 -0800640void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700642 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Cong Wangf75a2802019-01-31 13:05:49 -0800644 if (sync) {
645 synchronize_rcu();
646 ___xfrm_state_destroy(x);
647 } else {
648 spin_lock_bh(&xfrm_state_gc_lock);
649 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
650 spin_unlock_bh(&xfrm_state_gc_lock);
651 schedule_work(&xfrm_state_gc_work);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654EXPORT_SYMBOL(__xfrm_state_destroy);
655
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800656int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800658 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700659 int err = -ESRCH;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (x->km.state != XFRM_STATE_DEAD) {
662 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800663 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700664 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200665 hlist_del_rcu(&x->bydst);
666 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700667 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200668 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800669 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800670 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Sabrina Dubrocae27cca92019-11-25 14:49:02 +0100672 if (x->encap_sk)
673 sock_put(rcu_dereference_raw(x->encap_sk));
674
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200675 xfrm_dev_state_delete(x);
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* All xfrm_state objects are created by xfrm_state_alloc.
678 * The xfrm_state_alloc call gives a reference, and that
679 * is what we are dropping here.
680 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800681 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700682 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700684
685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800687EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700689int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700691 int err;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700694 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700696
697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699EXPORT_SYMBOL(xfrm_state_delete);
700
Joy Latten4aa2e622007-06-04 19:05:57 -0400701#ifdef CONFIG_SECURITY_NETWORK_XFRM
702static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900703xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Joy Latten4aa2e622007-06-04 19:05:57 -0400705 int i, err = 0;
706
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800707 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400708 struct xfrm_state *x;
709
Sasha Levinb67bfe02013-02-27 17:06:00 -0800710 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400711 if (xfrm_id_proto_match(x->id.proto, proto) &&
712 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900713 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400714 return err;
715 }
716 }
717 }
718
719 return err;
720}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200721
722static inline int
723xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
724{
725 int i, err = 0;
726
727 for (i = 0; i <= net->xfrm.state_hmask; i++) {
728 struct xfrm_state *x;
729 struct xfrm_state_offload *xso;
730
731 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
732 xso = &x->xso;
733
734 if (xso->dev == dev &&
735 (err = security_xfrm_state_delete(x)) != 0) {
736 xfrm_audit_state_delete(x, 0, task_valid);
737 return err;
738 }
739 }
740 }
741
742 return err;
743}
Joy Latten4aa2e622007-06-04 19:05:57 -0400744#else
745static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900746xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400747{
748 return 0;
749}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200750
751static inline int
752xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
753{
754 return 0;
755}
Joy Latten4aa2e622007-06-04 19:05:57 -0400756#endif
757
Cong Wangf75a2802019-01-31 13:05:49 -0800758int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
Joy Latten4aa2e622007-06-04 19:05:57 -0400759{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000760 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Fan Du283bc9f2013-11-07 17:47:50 +0800762 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900763 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400764 if (err)
765 goto out;
766
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000767 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800768 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700769 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800771 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700773 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800775 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Joy Latten161a09e2006-11-27 13:11:54 -0600777 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700778 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900779 task_valid);
Cong Wangf75a2802019-01-31 13:05:49 -0800780 if (sync)
781 xfrm_state_put_sync(x);
782 else
783 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000784 if (!err)
785 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Fan Du283bc9f2013-11-07 17:47:50 +0800787 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto restart;
789 }
790 }
791 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200792out:
793 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200794 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000795 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200796
Joy Latten4aa2e622007-06-04 19:05:57 -0400797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799EXPORT_SYMBOL(xfrm_state_flush);
800
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200801int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
802{
803 int i, err = 0, cnt = 0;
804
805 spin_lock_bh(&net->xfrm.xfrm_state_lock);
806 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
807 if (err)
808 goto out;
809
810 err = -ESRCH;
811 for (i = 0; i <= net->xfrm.state_hmask; i++) {
812 struct xfrm_state *x;
813 struct xfrm_state_offload *xso;
814restart:
815 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
816 xso = &x->xso;
817
818 if (!xfrm_state_kern(x) && xso->dev == dev) {
819 xfrm_state_hold(x);
820 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
821
822 err = xfrm_state_delete(x);
823 xfrm_audit_state_delete(x, err ? 0 : 1,
824 task_valid);
825 xfrm_state_put(x);
826 if (!err)
827 cnt++;
828
829 spin_lock_bh(&net->xfrm.xfrm_state_lock);
830 goto restart;
831 }
832 }
833 }
834 if (cnt)
835 err = 0;
836
837out:
838 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
839 return err;
840}
841EXPORT_SYMBOL(xfrm_dev_state_flush);
842
Alexey Dobriyane0710412010-01-23 13:37:10 +0000843void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700844{
Fan Du283bc9f2013-11-07 17:47:50 +0800845 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000846 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900847 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700848 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800849 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700850}
851EXPORT_SYMBOL(xfrm_sad_getinfo);
852
Florian Westphal711059b2017-01-09 14:20:48 +0100853static void
Florian Westphalbac95932019-05-03 17:46:14 +0200854__xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
855{
856 const struct flowi4 *fl4 = &fl->u.ip4;
857
858 sel->daddr.a4 = fl4->daddr;
859 sel->saddr.a4 = fl4->saddr;
860 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
861 sel->dport_mask = htons(0xffff);
862 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
863 sel->sport_mask = htons(0xffff);
864 sel->family = AF_INET;
865 sel->prefixlen_d = 32;
866 sel->prefixlen_s = 32;
867 sel->proto = fl4->flowi4_proto;
868 sel->ifindex = fl4->flowi4_oif;
869}
870
871static void
872__xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
873{
874 const struct flowi6 *fl6 = &fl->u.ip6;
875
876 /* Initialize temporary selector matching only to current session. */
877 *(struct in6_addr *)&sel->daddr = fl6->daddr;
878 *(struct in6_addr *)&sel->saddr = fl6->saddr;
879 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
880 sel->dport_mask = htons(0xffff);
881 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
882 sel->sport_mask = htons(0xffff);
883 sel->family = AF_INET6;
884 sel->prefixlen_d = 128;
885 sel->prefixlen_s = 128;
886 sel->proto = fl6->flowi6_proto;
887 sel->ifindex = fl6->flowi6_oif;
888}
889
890static void
David S. Miller1a898592011-02-22 18:22:34 -0800891xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500892 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500893 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700894 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Florian Westphalbac95932019-05-03 17:46:14 +0200896 switch (family) {
897 case AF_INET:
898 __xfrm4_init_tempsel(&x->sel, fl);
899 break;
900 case AF_INET6:
901 __xfrm6_init_tempsel(&x->sel, fl);
902 break;
903 }
904
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200905 x->id = tmpl->id;
Florian Westphalbac95932019-05-03 17:46:14 +0200906
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200907 switch (tmpl->encap_family) {
908 case AF_INET:
909 if (x->id.daddr.a4 == 0)
910 x->id.daddr.a4 = daddr->a4;
911 x->props.saddr = tmpl->saddr;
912 if (x->props.saddr.a4 == 0)
913 x->props.saddr.a4 = saddr->a4;
914 break;
915 case AF_INET6:
916 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
917 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
918 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
919 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
920 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
921 break;
922 }
Florian Westphal3819a352017-01-13 14:55:14 +0100923
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200924 x->props.mode = tmpl->mode;
925 x->props.reqid = tmpl->reqid;
926 x->props.family = tmpl->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
David S. Miller9aa60082011-02-24 01:51:36 -0500929static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
930 const xfrm_address_t *daddr,
931 __be32 spi, u8 proto,
932 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700933{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800934 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700935 struct xfrm_state *x;
936
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200937 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700938 if (x->props.family != family ||
939 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000940 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000941 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700942 continue;
943
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000944 if ((mark & x->mark.m) != x->mark.v)
945 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200946 if (!xfrm_state_hold_rcu(x))
947 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700948 return x;
949 }
950
951 return NULL;
952}
953
David S. Miller9aa60082011-02-24 01:51:36 -0500954static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
955 const xfrm_address_t *daddr,
956 const xfrm_address_t *saddr,
957 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700958{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800959 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700960 struct xfrm_state *x;
961
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200962 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700963 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000964 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000965 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
966 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700967 continue;
968
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000969 if ((mark & x->mark.m) != x->mark.v)
970 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200971 if (!xfrm_state_hold_rcu(x))
972 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700973 return x;
974 }
975
976 return NULL;
977}
978
979static inline struct xfrm_state *
980__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
981{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800982 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800983 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800984
David S. Milleredcd5822006-08-24 00:42:45 -0700985 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800986 return __xfrm_state_lookup(net, mark, &x->id.daddr,
987 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700988 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800989 return __xfrm_state_lookup_byaddr(net, mark,
990 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700991 &x->props.saddr,
992 x->id.proto, family);
993}
994
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800995static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700996{
997 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800998 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
999 net->xfrm.state_num > net->xfrm.state_hmask)
1000 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -07001001}
1002
David S. Miller08ec9af2009-03-13 14:22:40 -07001003static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -08001004 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001005 struct xfrm_state **best, int *acq_in_progress,
1006 int *error)
1007{
1008 /* Resolution logic:
1009 * 1. There is a valid state with matching selector. Done.
1010 * 2. Valid state with inappropriate selector. Skip.
1011 *
1012 * Entering area of "sysdeps".
1013 *
1014 * 3. If state is not valid, selector is temporary, it selects
1015 * only session which triggered previous resolution. Key
1016 * manager will do something to install a state with proper
1017 * selector.
1018 */
1019 if (x->km.state == XFRM_STATE_VALID) {
1020 if ((x->sel.family &&
Herbert Xue94ee172020-09-25 14:42:56 +10001021 (x->sel.family != family ||
1022 !xfrm_selector_match(&x->sel, fl, family))) ||
David S. Miller08ec9af2009-03-13 14:22:40 -07001023 !security_xfrm_state_pol_flow_match(x, pol, fl))
1024 return;
1025
1026 if (!*best ||
1027 (*best)->km.dying > x->km.dying ||
1028 ((*best)->km.dying == x->km.dying &&
1029 (*best)->curlft.add_time < x->curlft.add_time))
1030 *best = x;
1031 } else if (x->km.state == XFRM_STATE_ACQ) {
1032 *acq_in_progress = 1;
1033 } else if (x->km.state == XFRM_STATE_ERROR ||
1034 x->km.state == XFRM_STATE_EXPIRED) {
Herbert Xue94ee172020-09-25 14:42:56 +10001035 if ((!x->sel.family ||
1036 (x->sel.family == family &&
1037 xfrm_selector_match(&x->sel, fl, family))) &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001038 security_xfrm_state_pol_flow_match(x, pol, fl))
1039 *error = -ESRCH;
1040 }
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -05001044xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -08001045 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -07001047 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
David S. Miller08ec9af2009-03-13 14:22:40 -07001049 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001050 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -07001051 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -07001052 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int acquire_in_progress = 0;
1054 int error = 0;
1055 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001056 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001057 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +02001058 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +02001059 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001060
David S. Miller37b08e32008-09-02 20:14:15 -07001061 to_put = NULL;
1062
Ahmed S. Darwish02244322021-03-16 11:56:29 +01001063 sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001064
Florian Westphald737a582016-08-09 12:16:09 +02001065 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -07001066 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001067 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001068 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001070 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001071 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -07001072 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -07001073 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 tmpl->mode == x->props.mode &&
1075 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001076 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Herbert Xue94ee172020-09-25 14:42:56 +10001077 xfrm_state_look_at(pol, x, fl, family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001078 &best, &acquire_in_progress, &error);
1079 }
Fan Du6f115632013-09-23 17:18:25 +08001080 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -07001081 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Thomas Egerer8444cf72010-09-20 11:11:38 -07001083 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001084 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001085 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001086 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001087 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001088 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001089 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +08001090 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001091 tmpl->mode == x->props.mode &&
1092 tmpl->id.proto == x->id.proto &&
1093 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Herbert Xue94ee172020-09-25 14:42:56 +10001094 xfrm_state_look_at(pol, x, fl, family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001095 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
David S. Miller08ec9af2009-03-13 14:22:40 -07001098found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 x = best;
1100 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -07001101 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001102 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -07001103 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -07001104 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 error = -EEXIST;
1106 goto out;
1107 }
Horia Geanta0f245582014-02-12 16:20:06 +02001108
1109 c.net = net;
1110 /* If the KMs have no listeners (yet...), avoid allocating an SA
1111 * for each and every packet - garbage collection might not
1112 * handle the flood.
1113 */
1114 if (!km_is_alive(&c)) {
1115 error = -ESRCH;
1116 goto out;
1117 }
1118
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001119 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (x == NULL) {
1121 error = -ENOMEM;
1122 goto out;
1123 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001124 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001126 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001127 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +02001128 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
David S. Miller1d28f422011-03-12 00:29:39 -05001130 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001131 if (error) {
1132 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001133 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001134 x = NULL;
1135 goto out;
1136 }
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001139 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001141 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001142 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001143 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001144 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001146 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001147 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001149 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001150 hrtimer_start(&x->mtimer,
1151 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1152 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001153 net->xfrm.state_num++;
1154 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001155 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else {
1157 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001158 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 x = NULL;
1160 error = -ESRCH;
1161 }
1162 }
1163out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001164 if (x) {
1165 if (!xfrm_state_hold_rcu(x)) {
1166 *err = -EAGAIN;
1167 x = NULL;
1168 }
1169 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001171 }
Florian Westphald737a582016-08-09 12:16:09 +02001172 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001173 if (to_put)
1174 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001175
Ahmed S. Darwish02244322021-03-16 11:56:29 +01001176 if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
Florian Westphalb65e3d72016-08-09 12:16:07 +02001177 *err = -EAGAIN;
1178 if (x) {
1179 xfrm_state_put(x);
1180 x = NULL;
1181 }
1182 }
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return x;
1185}
1186
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001187struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001188xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001189 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001190 unsigned short family, u8 mode, u8 proto, u32 reqid)
1191{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001192 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001193 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001194
Fan Du4ae770b2014-01-03 11:18:29 +08001195 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001196 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001198 if (x->props.family == family &&
1199 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001200 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001201 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001202 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1203 xfrm_state_addr_check(x, daddr, saddr, family) &&
1204 mode == x->props.mode &&
1205 proto == x->id.proto &&
1206 x->km.state == XFRM_STATE_VALID) {
1207 rx = x;
1208 break;
1209 }
1210 }
1211
1212 if (rx)
1213 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001214 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001215
1216
1217 return rx;
1218}
1219EXPORT_SYMBOL(xfrm_stateonly_find);
1220
Fan Duc4549972014-01-03 11:18:32 +08001221struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1222 unsigned short family)
1223{
1224 struct xfrm_state *x;
1225 struct xfrm_state_walk *w;
1226
1227 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1228 list_for_each_entry(w, &net->xfrm.state_all, all) {
1229 x = container_of(w, struct xfrm_state, km);
1230 if (x->props.family != family ||
1231 x->id.spi != spi)
1232 continue;
1233
Fan Duc4549972014-01-03 11:18:32 +08001234 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001235 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001236 return x;
1237 }
1238 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1239 return NULL;
1240}
1241EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243static void __xfrm_state_insert(struct xfrm_state *x)
1244{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001245 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001246 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001248 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001249
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001250 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001251 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001252 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001254 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001255 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001257 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001258 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001259 x->props.family);
1260
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001261 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001262 }
1263
Thomas Gleixner671422b2019-03-01 23:48:20 +01001264 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
David S. Millera47f0ce2006-08-24 03:54:22 -07001265 if (x->replay_maxage)
1266 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001267
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001268 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001269
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001270 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Fan Du283bc9f2013-11-07 17:47:50 +08001273/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001274static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1275{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001276 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001277 unsigned short family = xnew->props.family;
1278 u32 reqid = xnew->props.reqid;
1279 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001280 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001281 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001282 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001283
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001284 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001285 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001286 if (x->props.family == family &&
1287 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001288 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001289 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001290 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1291 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001292 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001293 }
1294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296void xfrm_state_insert(struct xfrm_state *x)
1297{
Fan Du283bc9f2013-11-07 17:47:50 +08001298 struct net *net = xs_net(x);
1299
1300 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001301 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001303 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305EXPORT_SYMBOL(xfrm_state_insert);
1306
Fan Du283bc9f2013-11-07 17:47:50 +08001307/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001308static struct xfrm_state *__find_acq_core(struct net *net,
1309 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001310 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001311 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001312 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001313 const xfrm_address_t *saddr,
1314 int create)
David S. Miller27708342006-08-24 00:13:10 -07001315{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001316 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001317 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001318 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001319
Sasha Levinb67bfe02013-02-27 17:06:00 -08001320 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001321 if (x->props.reqid != reqid ||
1322 x->props.mode != mode ||
1323 x->props.family != family ||
1324 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001325 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001326 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001327 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001328 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1329 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001330 continue;
1331
David S. Miller27708342006-08-24 00:13:10 -07001332 xfrm_state_hold(x);
1333 return x;
1334 }
1335
1336 if (!create)
1337 return NULL;
1338
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001339 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001340 if (likely(x)) {
1341 switch (family) {
1342 case AF_INET:
1343 x->sel.daddr.a4 = daddr->a4;
1344 x->sel.saddr.a4 = saddr->a4;
1345 x->sel.prefixlen_d = 32;
1346 x->sel.prefixlen_s = 32;
1347 x->props.saddr.a4 = saddr->a4;
1348 x->id.daddr.a4 = daddr->a4;
1349 break;
1350
1351 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001352 x->sel.daddr.in6 = daddr->in6;
1353 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001354 x->sel.prefixlen_d = 128;
1355 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001356 x->props.saddr.in6 = saddr->in6;
1357 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001358 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001359 }
David S. Miller27708342006-08-24 00:13:10 -07001360
1361 x->km.state = XFRM_STATE_ACQ;
1362 x->id.proto = proto;
1363 x->props.family = family;
1364 x->props.mode = mode;
1365 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001366 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001367 x->mark.v = m->v;
1368 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001369 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001370 xfrm_state_hold(x);
Thomas Gleixner671422b2019-03-01 23:48:20 +01001371 hrtimer_start(&x->mtimer,
1372 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1373 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001374 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001375 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001376 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001377 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001378
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001379 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001380
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001381 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001382 }
1383
1384 return x;
1385}
1386
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001387static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389int xfrm_state_add(struct xfrm_state *x)
1390{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001391 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001392 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int family;
1394 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001395 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001396 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
David S. Miller37b08e32008-09-02 20:14:15 -07001400 to_put = NULL;
1401
Fan Du283bc9f2013-11-07 17:47:50 +08001402 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
David S. Milleredcd5822006-08-24 00:42:45 -07001404 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001406 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 x1 = NULL;
1408 err = -EEXIST;
1409 goto out;
1410 }
1411
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001412 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001413 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001414 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001415 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001416 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 x1 = NULL;
1418 }
1419 }
1420
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001421 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001422 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001423 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001424 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
David S. Millerc7f5ea32006-08-24 03:29:04 -07001426 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 __xfrm_state_insert(x);
1428 err = 0;
1429
1430out:
Fan Du283bc9f2013-11-07 17:47:50 +08001431 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (x1) {
1434 xfrm_state_delete(x1);
1435 xfrm_state_put(x1);
1436 }
1437
David S. Miller37b08e32008-09-02 20:14:15 -07001438 if (to_put)
1439 xfrm_state_put(to_put);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return err;
1442}
1443EXPORT_SYMBOL(xfrm_state_add);
1444
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001445#ifdef CONFIG_XFRM_MIGRATE
Antony Antony7aa05d32020-09-04 08:50:11 +02001446static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1447{
1448 struct xfrm_user_sec_ctx *uctx;
1449 int size = sizeof(*uctx) + security->ctx_len;
1450 int err;
1451
1452 uctx = kmalloc(size, GFP_KERNEL);
1453 if (!uctx)
1454 return -ENOMEM;
1455
1456 uctx->exttype = XFRMA_SEC_CTX;
1457 uctx->len = size;
1458 uctx->ctx_doi = security->ctx_doi;
1459 uctx->ctx_alg = security->ctx_alg;
1460 uctx->ctx_len = security->ctx_len;
1461 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1462 err = security_xfrm_state_alloc(x, uctx);
1463 kfree(uctx);
1464 if (err)
1465 return err;
1466
1467 return 0;
1468}
1469
Antony Antony4ab47d42017-06-06 12:12:13 +02001470static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1471 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001472{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001473 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001474 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001475 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001476 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001477
1478 memcpy(&x->id, &orig->id, sizeof(x->id));
1479 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1480 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1481 x->props.mode = orig->props.mode;
1482 x->props.replay_window = orig->props.replay_window;
1483 x->props.reqid = orig->props.reqid;
1484 x->props.family = orig->props.family;
1485 x->props.saddr = orig->props.saddr;
1486
1487 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001488 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001489 if (!x->aalg)
1490 goto error;
1491 }
1492 x->props.aalgo = orig->props.aalgo;
1493
Steffen Klassertee5c2312014-02-19 13:33:24 +01001494 if (orig->aead) {
1495 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001496 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001497 if (!x->aead)
1498 goto error;
1499 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001500 if (orig->ealg) {
1501 x->ealg = xfrm_algo_clone(orig->ealg);
1502 if (!x->ealg)
1503 goto error;
1504 }
1505 x->props.ealgo = orig->props.ealgo;
1506
1507 if (orig->calg) {
1508 x->calg = xfrm_algo_clone(orig->calg);
1509 if (!x->calg)
1510 goto error;
1511 }
1512 x->props.calgo = orig->props.calgo;
1513
Antony Antony4ab47d42017-06-06 12:12:13 +02001514 if (encap || orig->encap) {
1515 if (encap)
1516 x->encap = kmemdup(encap, sizeof(*x->encap),
1517 GFP_KERNEL);
1518 else
1519 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1520 GFP_KERNEL);
1521
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001522 if (!x->encap)
1523 goto error;
1524 }
1525
Antony Antony7aa05d32020-09-04 08:50:11 +02001526 if (orig->security)
1527 if (clone_security(x, orig->security))
1528 goto error;
1529
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001530 if (orig->coaddr) {
1531 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1532 GFP_KERNEL);
1533 if (!x->coaddr)
1534 goto error;
1535 }
1536
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001537 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001538 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001539 goto error;
1540 }
1541
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001542 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
Antony Antony545e5c52020-09-04 08:49:38 +02001543 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001544
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001545 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001546 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001547
Steffen Klassert7e652642018-06-12 14:07:07 +02001548 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001549 x->tfcpad = orig->tfcpad;
1550 x->replay_maxdiff = orig->replay_maxdiff;
1551 x->replay_maxage = orig->replay_maxage;
Antony Antony83666852020-09-04 08:50:29 +02001552 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001553 x->km.state = orig->km.state;
1554 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001555 x->replay = orig->replay;
1556 x->preplay = orig->preplay;
Antony Antonya0b13332021-12-22 14:11:18 +01001557 x->mapping_maxage = orig->mapping_maxage;
1558 x->new_mapping = 0;
1559 x->new_mapping_sport = 0;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001560
1561 return x;
1562
1563 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001564 xfrm_state_put(x);
1565out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001566 return NULL;
1567}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001568
Yan Yand8889a42022-01-18 16:00:13 -08001569struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
1570 u32 if_id)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001571{
1572 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001573 struct xfrm_state *x = NULL;
1574
1575 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001576
1577 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001578 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001579 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001580 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001581 if (x->props.mode != m->mode ||
1582 x->id.proto != m->proto)
1583 continue;
1584 if (m->reqid && x->props.reqid != m->reqid)
1585 continue;
Yan Yand8889a42022-01-18 16:00:13 -08001586 if (if_id != 0 && x->if_id != if_id)
1587 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001588 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1589 m->old_family) ||
1590 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1591 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001592 continue;
1593 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001594 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001595 }
1596 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001597 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001598 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001599 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001600 if (x->props.mode != m->mode ||
1601 x->id.proto != m->proto)
1602 continue;
Yan Yand8889a42022-01-18 16:00:13 -08001603 if (if_id != 0 && x->if_id != if_id)
1604 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001605 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1606 m->old_family) ||
1607 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1608 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001609 continue;
1610 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001611 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001612 }
1613 }
1614
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001615 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1616
1617 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001618}
1619EXPORT_SYMBOL(xfrm_migrate_state_find);
1620
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001621struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001622 struct xfrm_migrate *m,
1623 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001624{
1625 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001626
Antony Antony4ab47d42017-06-06 12:12:13 +02001627 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001628 if (!xc)
1629 return NULL;
1630
Yan Yanca142032022-01-18 16:00:14 -08001631 xc->props.family = m->new_family;
1632
1633 if (xfrm_init_state(xc) < 0)
1634 goto error;
1635
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001636 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1637 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1638
1639 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001640 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001641 /* a care is needed when the destination address of the
1642 state is to be updated as it is a part of triplet */
1643 xfrm_state_insert(xc);
1644 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001645 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001646 goto error;
1647 }
1648
1649 return xc;
1650error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001651 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001652 return NULL;
1653}
1654EXPORT_SYMBOL(xfrm_state_migrate);
1655#endif
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657int xfrm_state_update(struct xfrm_state *x)
1658{
David S. Miller37b08e32008-09-02 20:14:15 -07001659 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001661 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001662 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
David S. Miller37b08e32008-09-02 20:14:15 -07001664 to_put = NULL;
1665
Fan Du283bc9f2013-11-07 17:47:50 +08001666 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001667 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 err = -ESRCH;
1670 if (!x1)
1671 goto out;
1672
1673 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001674 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 err = -EEXIST;
1676 goto out;
1677 }
1678
1679 if (x1->km.state == XFRM_STATE_ACQ) {
1680 __xfrm_state_insert(x);
1681 x = NULL;
1682 }
1683 err = 0;
1684
1685out:
Fan Du283bc9f2013-11-07 17:47:50 +08001686 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
David S. Miller37b08e32008-09-02 20:14:15 -07001688 if (to_put)
1689 xfrm_state_put(to_put);
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (err)
1692 return err;
1693
1694 if (!x) {
1695 xfrm_state_delete(x1);
1696 xfrm_state_put(x1);
1697 return 0;
1698 }
1699
1700 err = -EINVAL;
1701 spin_lock_bh(&x1->lock);
1702 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001703 if (x->encap && x1->encap &&
1704 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001706 else if (x->encap || x1->encap)
1707 goto fail;
1708
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001709 if (x->coaddr && x1->coaddr) {
1710 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1711 }
1712 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1713 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1715 x1->km.dying = 0;
1716
Thomas Gleixner671422b2019-03-01 23:48:20 +01001717 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1718 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (x1->curlft.use_time)
1720 xfrm_state_check_expire(x1);
1721
Nathan Harold5baf4f92018-07-19 19:07:47 -07001722 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001723 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1724
Nathan Harold5baf4f92018-07-19 19:07:47 -07001725 if (x->props.smark.m || x->props.smark.v)
1726 x1->props.smark = x->props.smark;
1727
1728 if (x->if_id)
1729 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001730
1731 __xfrm_state_bump_genids(x1);
1732 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1733 }
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001736 x->km.state = XFRM_STATE_DEAD;
1737 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001739
1740fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 spin_unlock_bh(&x1->lock);
1742
1743 xfrm_state_put(x1);
1744
1745 return err;
1746}
1747EXPORT_SYMBOL(xfrm_state_update);
1748
1749int xfrm_state_check_expire(struct xfrm_state *x)
1750{
1751 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001752 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1755 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001756 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001757 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return -EINVAL;
1759 }
1760
1761 if (!x->km.dying &&
1762 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001763 x->curlft.packets >= x->lft.soft_packet_limit)) {
1764 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001765 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
1769EXPORT_SYMBOL(xfrm_state_check_expire);
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001772xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001773 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Florian Westphalc2f672f2016-09-20 15:45:26 +02001777 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001778 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001779 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return x;
1781}
1782EXPORT_SYMBOL(xfrm_state_lookup);
1783
1784struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001785xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001786 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001787 u8 proto, unsigned short family)
1788{
1789 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001790
Fan Du283bc9f2013-11-07 17:47:50 +08001791 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001792 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001793 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001794 return x;
1795}
1796EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1797
1798struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001799xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001800 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001801 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Fan Du283bc9f2013-11-07 17:47:50 +08001805 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001806 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001807 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return x;
1810}
1811EXPORT_SYMBOL(xfrm_find_acq);
1812
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001813#ifdef CONFIG_XFRM_SUB_POLICY
Florian Westphal3aaf3912019-05-03 17:46:17 +02001814#if IS_ENABLED(CONFIG_IPV6)
1815/* distribution counting sort function for xfrm_state and xfrm_tmpl */
1816static void
1817__xfrm6_sort(void **dst, void **src, int n,
1818 int (*cmp)(const void *p), int maxclass)
1819{
1820 int count[XFRM_MAX_DEPTH] = { };
1821 int class[XFRM_MAX_DEPTH];
1822 int i;
1823
1824 for (i = 0; i < n; i++) {
1825 int c = cmp(src[i]);
1826
1827 class[i] = c;
1828 count[c]++;
1829 }
1830
1831 for (i = 2; i < maxclass; i++)
1832 count[i] += count[i - 1];
1833
1834 for (i = 0; i < n; i++) {
1835 dst[count[class[i] - 1]++] = src[i];
1836 src[i] = NULL;
1837 }
1838}
1839
1840/* Rule for xfrm_state:
1841 *
1842 * rule 1: select IPsec transport except AH
1843 * rule 2: select MIPv6 RO or inbound trigger
1844 * rule 3: select IPsec transport AH
1845 * rule 4: select IPsec tunnel
1846 * rule 5: others
1847 */
1848static int __xfrm6_state_sort_cmp(const void *p)
1849{
1850 const struct xfrm_state *v = p;
1851
1852 switch (v->props.mode) {
1853 case XFRM_MODE_TRANSPORT:
1854 if (v->id.proto != IPPROTO_AH)
1855 return 1;
1856 else
1857 return 3;
1858#if IS_ENABLED(CONFIG_IPV6_MIP6)
1859 case XFRM_MODE_ROUTEOPTIMIZATION:
1860 case XFRM_MODE_IN_TRIGGER:
1861 return 2;
1862#endif
1863 case XFRM_MODE_TUNNEL:
1864 case XFRM_MODE_BEET:
1865 return 4;
1866 }
1867 return 5;
1868}
1869
1870/* Rule for xfrm_tmpl:
1871 *
1872 * rule 1: select IPsec transport
1873 * rule 2: select MIPv6 RO or inbound trigger
1874 * rule 3: select IPsec tunnel
1875 * rule 4: others
1876 */
1877static int __xfrm6_tmpl_sort_cmp(const void *p)
1878{
1879 const struct xfrm_tmpl *v = p;
1880
1881 switch (v->mode) {
1882 case XFRM_MODE_TRANSPORT:
1883 return 1;
1884#if IS_ENABLED(CONFIG_IPV6_MIP6)
1885 case XFRM_MODE_ROUTEOPTIMIZATION:
1886 case XFRM_MODE_IN_TRIGGER:
1887 return 2;
1888#endif
1889 case XFRM_MODE_TUNNEL:
1890 case XFRM_MODE_BEET:
1891 return 3;
1892 }
1893 return 4;
1894}
1895#else
1896static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1897static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1898
1899static inline void
1900__xfrm6_sort(void **dst, void **src, int n,
1901 int (*cmp)(const void *p), int maxclass)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001902{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001903 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001904
Florian Westphal3aaf3912019-05-03 17:46:17 +02001905 for (i = 0; i < n; i++)
1906 dst[i] = src[i];
1907}
1908#endif /* CONFIG_IPV6 */
1909
1910void
1911xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1912 unsigned short family)
1913{
1914 int i;
1915
1916 if (family == AF_INET6)
1917 __xfrm6_sort((void **)dst, (void **)src, n,
1918 __xfrm6_tmpl_sort_cmp, 5);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001919 else
1920 for (i = 0; i < n; i++)
1921 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001922}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001923
Florian Westphal3aaf3912019-05-03 17:46:17 +02001924void
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001925xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1926 unsigned short family)
1927{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001928 int i;
Fan Du283bc9f2013-11-07 17:47:50 +08001929
Florian Westphal3aaf3912019-05-03 17:46:17 +02001930 if (family == AF_INET6)
1931 __xfrm6_sort((void **)dst, (void **)src, n,
1932 __xfrm6_state_sort_cmp, 6);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001933 else
1934 for (i = 0; i < n; i++)
1935 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001936}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001937#endif
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/* Silly enough, but I'm lazy to build resolution list */
1940
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001941static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001945 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001946 struct xfrm_state *x;
1947
Sasha Levinb67bfe02013-02-27 17:06:00 -08001948 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001949 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001950 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001951 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 xfrm_state_hold(x);
1953 return x;
1954 }
1955 }
1956 }
1957 return NULL;
1958}
1959
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001960struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 struct xfrm_state *x;
1963
Fan Du283bc9f2013-11-07 17:47:50 +08001964 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001965 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001966 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return x;
1968}
1969EXPORT_SYMBOL(xfrm_find_acq_byseq);
1970
1971u32 xfrm_get_acqseq(void)
1972{
1973 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001974 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
jamal6836b9b2010-02-16 02:01:22 +00001976 do {
1977 res = atomic_inc_return(&acqseq);
1978 } while (!res);
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return res;
1981}
1982EXPORT_SYMBOL(xfrm_get_acqseq);
1983
Fan Du776e9dd2013-12-16 18:47:49 +08001984int verify_spi_info(u8 proto, u32 min, u32 max)
1985{
1986 switch (proto) {
1987 case IPPROTO_AH:
1988 case IPPROTO_ESP:
1989 break;
1990
1991 case IPPROTO_COMP:
1992 /* IPCOMP spi is 16-bits. */
1993 if (max >= 0x10000)
1994 return -EINVAL;
1995 break;
1996
1997 default:
1998 return -EINVAL;
1999 }
2000
2001 if (min > max)
2002 return -EINVAL;
2003
2004 return 0;
2005}
2006EXPORT_SYMBOL(verify_spi_info);
2007
Herbert Xu658b2192007-10-09 13:29:52 -07002008int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08002010 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07002011 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07002013 int err = -ENOENT;
2014 __be32 minspi = htonl(low);
2015 __be32 maxspi = htonl(high);
zhuoliang zhanga779d912020-10-23 09:05:35 +02002016 __be32 newspi = 0;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002017 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Herbert Xu658b2192007-10-09 13:29:52 -07002019 spin_lock_bh(&x->lock);
2020 if (x->km.state == XFRM_STATE_DEAD)
2021 goto unlock;
2022
2023 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07002025 goto unlock;
2026
2027 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002030 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (x0) {
2032 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07002033 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
zhuoliang zhanga779d912020-10-23 09:05:35 +02002035 newspi = minspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 } else {
2037 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08002038 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002039 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002040 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (x0 == NULL) {
zhuoliang zhanga779d912020-10-23 09:05:35 +02002042 newspi = htonl(spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 break;
2044 }
2045 xfrm_state_put(x0);
2046 }
2047 }
zhuoliang zhanga779d912020-10-23 09:05:35 +02002048 if (newspi) {
Fan Du283bc9f2013-11-07 17:47:50 +08002049 spin_lock_bh(&net->xfrm.xfrm_state_lock);
zhuoliang zhanga779d912020-10-23 09:05:35 +02002050 x->id.spi = newspi;
Alexey Dobriyan12604d82008-11-25 17:31:18 -08002051 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02002052 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08002053 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07002054
2055 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Herbert Xu658b2192007-10-09 13:29:52 -07002057
2058unlock:
2059 spin_unlock_bh(&x->lock);
2060
2061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063EXPORT_SYMBOL(xfrm_alloc_spi);
2064
Nicolas Dichteld3623092014-02-14 15:30:36 +01002065static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002066 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01002067{
2068 if (filter) {
2069 if ((filter->family == AF_INET ||
2070 filter->family == AF_INET6) &&
2071 x->props.family != filter->family)
2072 return false;
2073
2074 return addr_match(&x->props.saddr, &filter->saddr,
2075 filter->splen) &&
2076 addr_match(&x->id.daddr, &filter->daddr,
2077 filter->dplen);
2078 }
2079 return true;
2080}
2081
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002082int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08002083 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 void *data)
2085{
Herbert Xu12a169e2008-10-01 07:03:24 -07002086 struct xfrm_state *state;
2087 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 int err = 0;
2089
Herbert Xu12a169e2008-10-01 07:03:24 -07002090 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08002091 return 0;
2092
Fan Du283bc9f2013-11-07 17:47:50 +08002093 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002094 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002095 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07002096 else
Li RongQing80077702015-04-22 17:09:54 +08002097 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002098 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07002099 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08002100 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002101 state = container_of(x, struct xfrm_state, km);
2102 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08002103 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002104 if (!__xfrm_state_filter_match(state, walk->filter))
2105 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002106 err = func(state, walk->seq, data);
2107 if (err) {
2108 list_move_tail(&walk->all, &x->all);
2109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002111 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002113 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 err = -ENOENT;
2115 goto out;
2116 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002117 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118out:
Fan Du283bc9f2013-11-07 17:47:50 +08002119 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return err;
2121}
2122EXPORT_SYMBOL(xfrm_state_walk);
2123
Nicolas Dichteld3623092014-02-14 15:30:36 +01002124void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002125 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07002126{
Herbert Xu12a169e2008-10-01 07:03:24 -07002127 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07002128 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07002129 walk->state = XFRM_STATE_DEAD;
2130 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002131 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07002132}
2133EXPORT_SYMBOL(xfrm_state_walk_init);
2134
Fan Du283bc9f2013-11-07 17:47:50 +08002135void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07002136{
Nicolas Dichteld3623092014-02-14 15:30:36 +01002137 kfree(walk->filter);
2138
Herbert Xu12a169e2008-10-01 07:03:24 -07002139 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07002140 return;
Herbert Xu5c182452008-09-22 19:48:19 -07002141
Fan Du283bc9f2013-11-07 17:47:50 +08002142 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002143 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08002144 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07002145}
2146EXPORT_SYMBOL(xfrm_state_walk_done);
2147
Kees Cooke99e88a2017-10-16 14:43:17 -07002148static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002149{
Kees Cooke99e88a2017-10-16 14:43:17 -07002150 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002151
2152 spin_lock(&x->lock);
2153
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002154 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002155 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002156 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002157 else
2158 x->xflags |= XFRM_TIME_DEFER;
2159 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002160
2161 spin_unlock(&x->lock);
2162}
2163
Denis Chengdf018122007-12-07 00:51:11 -08002164static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
David S. Miller214e0052011-02-24 00:02:38 -05002166void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 struct xfrm_mgr *km;
2169
Cong Wang85168c02013-01-16 16:05:06 +08002170 rcu_read_lock();
2171 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002172 if (km->notify_policy)
2173 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08002174 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002175}
2176
David S. Miller214e0052011-02-24 00:02:38 -05002177void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002178{
2179 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08002180 rcu_read_lock();
2181 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002182 if (km->notify)
2183 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08002184 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002185}
2186
2187EXPORT_SYMBOL(km_policy_notify);
2188EXPORT_SYMBOL(km_state_notify);
2189
Eric W. Biederman15e47302012-09-07 20:12:54 +00002190void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002191{
2192 struct km_event c;
2193
Herbert Xubf08867f92005-06-18 22:44:00 -07002194 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002195 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002196 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002197 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002200EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002201/*
2202 * We send to all registered managers regardless of failure
2203 * We are happy with one success
2204*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002205int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002207 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 struct xfrm_mgr *km;
2209
Cong Wang85168c02013-01-16 16:05:06 +08002210 rcu_read_lock();
2211 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08002212 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002213 if (!acqret)
2214 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
Cong Wang85168c02013-01-16 16:05:06 +08002216 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return err;
2218}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002219EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Antony Antonya0b13332021-12-22 14:11:18 +01002221static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 int err = -EINVAL;
2224 struct xfrm_mgr *km;
2225
Cong Wang85168c02013-01-16 16:05:06 +08002226 rcu_read_lock();
2227 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (km->new_mapping)
2229 err = km->new_mapping(x, ipaddr, sport);
2230 if (!err)
2231 break;
2232 }
Cong Wang85168c02013-01-16 16:05:06 +08002233 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return err;
2235}
Antony Antonya0b13332021-12-22 14:11:18 +01002236
2237int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2238{
2239 int ret = 0;
2240
2241 if (x->mapping_maxage) {
2242 if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
2243 x->new_mapping_sport != sport) {
2244 x->new_mapping_sport = sport;
2245 x->new_mapping = jiffies / HZ;
2246 ret = __km_new_mapping(x, ipaddr, sport);
2247 }
2248 } else {
2249 ret = __km_new_mapping(x, ipaddr, sport);
2250 }
2251
2252 return ret;
2253}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254EXPORT_SYMBOL(km_new_mapping);
2255
Eric W. Biederman15e47302012-09-07 20:12:54 +00002256void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Herbert Xubf08867f92005-06-18 22:44:00 -07002260 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002261 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002262 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002263 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
David S. Millera70fcb02006-03-20 19:18:52 -08002265EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002267#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002268int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2269 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002270 const struct xfrm_kmaddress *k,
2271 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002272{
2273 int err = -EINVAL;
2274 int ret;
2275 struct xfrm_mgr *km;
2276
Cong Wang85168c02013-01-16 16:05:06 +08002277 rcu_read_lock();
2278 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002279 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002280 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2281 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002282 if (!ret)
2283 err = ret;
2284 }
2285 }
Cong Wang85168c02013-01-16 16:05:06 +08002286 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002287 return err;
2288}
2289EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002290#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002291
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002292int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002293{
2294 int err = -EINVAL;
2295 int ret;
2296 struct xfrm_mgr *km;
2297
Cong Wang85168c02013-01-16 16:05:06 +08002298 rcu_read_lock();
2299 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002300 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002301 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002302 if (!ret)
2303 err = ret;
2304 }
2305 }
Cong Wang85168c02013-01-16 16:05:06 +08002306 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002307 return err;
2308}
2309EXPORT_SYMBOL(km_report);
2310
Florian Westphalbb9cd072019-04-17 11:45:13 +02002311static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002312{
2313 struct xfrm_mgr *km;
2314 bool is_alive = false;
2315
2316 rcu_read_lock();
2317 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2318 if (km->is_alive && km->is_alive(c)) {
2319 is_alive = true;
2320 break;
2321 }
2322 }
2323 rcu_read_unlock();
2324
2325 return is_alive;
2326}
Horia Geanta0f245582014-02-12 16:20:06 +02002327
Dmitry Safonovc9e7c762020-09-21 15:36:51 +01002328#if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2329static DEFINE_SPINLOCK(xfrm_translator_lock);
2330static struct xfrm_translator __rcu *xfrm_translator;
2331
2332struct xfrm_translator *xfrm_get_translator(void)
2333{
2334 struct xfrm_translator *xtr;
2335
2336 rcu_read_lock();
2337 xtr = rcu_dereference(xfrm_translator);
2338 if (unlikely(!xtr))
2339 goto out;
2340 if (!try_module_get(xtr->owner))
2341 xtr = NULL;
2342out:
2343 rcu_read_unlock();
2344 return xtr;
2345}
2346EXPORT_SYMBOL_GPL(xfrm_get_translator);
2347
2348void xfrm_put_translator(struct xfrm_translator *xtr)
2349{
2350 module_put(xtr->owner);
2351}
2352EXPORT_SYMBOL_GPL(xfrm_put_translator);
2353
2354int xfrm_register_translator(struct xfrm_translator *xtr)
2355{
2356 int err = 0;
2357
2358 spin_lock_bh(&xfrm_translator_lock);
2359 if (unlikely(xfrm_translator != NULL))
2360 err = -EEXIST;
2361 else
2362 rcu_assign_pointer(xfrm_translator, xtr);
2363 spin_unlock_bh(&xfrm_translator_lock);
2364
2365 return err;
2366}
2367EXPORT_SYMBOL_GPL(xfrm_register_translator);
2368
2369int xfrm_unregister_translator(struct xfrm_translator *xtr)
2370{
2371 int err = 0;
2372
2373 spin_lock_bh(&xfrm_translator_lock);
2374 if (likely(xfrm_translator != NULL)) {
2375 if (rcu_access_pointer(xfrm_translator) != xtr)
2376 err = -EINVAL;
2377 else
2378 RCU_INIT_POINTER(xfrm_translator, NULL);
2379 }
2380 spin_unlock_bh(&xfrm_translator_lock);
2381 synchronize_rcu();
2382
2383 return err;
2384}
2385EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2386#endif
2387
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002388int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
2390 int err;
2391 u8 *data;
2392 struct xfrm_mgr *km;
2393 struct xfrm_policy *pol = NULL;
2394
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002395 if (sockptr_is_null(optval) && !optlen) {
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002396 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2397 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2398 __sk_dst_reset(sk);
2399 return 0;
2400 }
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (optlen <= 0 || optlen > PAGE_SIZE)
2403 return -EMSGSIZE;
2404
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002405 data = memdup_sockptr(optval, optlen);
Geliang Tanga133d932017-05-06 23:42:21 +08002406 if (IS_ERR(data))
2407 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Dmitry Safonov96392ee2020-09-21 15:36:56 +01002409 if (in_compat_syscall()) {
2410 struct xfrm_translator *xtr = xfrm_get_translator();
2411
Yu Kuai48f486e2020-11-10 09:14:43 +08002412 if (!xtr) {
2413 kfree(data);
Dmitry Safonov96392ee2020-09-21 15:36:56 +01002414 return -EOPNOTSUPP;
Yu Kuai48f486e2020-11-10 09:14:43 +08002415 }
Dmitry Safonov96392ee2020-09-21 15:36:56 +01002416
2417 err = xtr->xlate_user_policy_sockptr(&data, optlen);
2418 xfrm_put_translator(xtr);
2419 if (err) {
2420 kfree(data);
2421 return err;
2422 }
2423 }
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002426 rcu_read_lock();
2427 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002428 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 optlen, &err);
2430 if (err >= 0)
2431 break;
2432 }
Cong Wang85168c02013-01-16 16:05:06 +08002433 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 if (err >= 0) {
2436 xfrm_sk_policy_insert(sk, err, pol);
2437 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002438 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 err = 0;
2440 }
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 kfree(data);
2443 return err;
2444}
2445EXPORT_SYMBOL(xfrm_user_policy);
2446
Cong Wang85168c02013-01-16 16:05:06 +08002447static DEFINE_SPINLOCK(xfrm_km_lock);
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449int xfrm_register_km(struct xfrm_mgr *km)
2450{
Cong Wang85168c02013-01-16 16:05:06 +08002451 spin_lock_bh(&xfrm_km_lock);
2452 list_add_tail_rcu(&km->list, &xfrm_km_list);
2453 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 0;
2455}
2456EXPORT_SYMBOL(xfrm_register_km);
2457
2458int xfrm_unregister_km(struct xfrm_mgr *km)
2459{
Cong Wang85168c02013-01-16 16:05:06 +08002460 spin_lock_bh(&xfrm_km_lock);
2461 list_del_rcu(&km->list);
2462 spin_unlock_bh(&xfrm_km_lock);
2463 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return 0;
2465}
2466EXPORT_SYMBOL(xfrm_unregister_km);
2467
2468int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2469{
2470 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002471
2472 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002474
Cong Wang44abdc32013-01-16 16:05:05 +08002475 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002477 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002478 else
Cong Wang44abdc32013-01-16 16:05:05 +08002479 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2480 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return err;
2482}
2483EXPORT_SYMBOL(xfrm_state_register_afinfo);
2484
2485int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2486{
Florian Westphal423826a2017-01-09 14:20:46 +01002487 int err = 0, family = afinfo->family;
2488
2489 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002491
Cong Wang44abdc32013-01-16 16:05:05 +08002492 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002494 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002496 else
Cong Wang44abdc32013-01-16 16:05:05 +08002497 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
Cong Wang44abdc32013-01-16 16:05:05 +08002499 spin_unlock_bh(&xfrm_state_afinfo_lock);
2500 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return err;
2502}
2503EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2504
Florian Westphal711059b2017-01-09 14:20:48 +01002505struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2506{
2507 if (unlikely(family >= NPROTO))
2508 return NULL;
2509
2510 return rcu_dereference(xfrm_state_afinfo[family]);
2511}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002512EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002513
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002514struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 struct xfrm_state_afinfo *afinfo;
2517 if (unlikely(family >= NPROTO))
2518 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002519 rcu_read_lock();
2520 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002521 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002522 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return afinfo;
2524}
2525
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002526void xfrm_flush_gc(void)
2527{
2528 flush_work(&xfrm_state_gc_work);
2529}
2530EXPORT_SYMBOL(xfrm_flush_gc);
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2533void xfrm_state_delete_tunnel(struct xfrm_state *x)
2534{
2535 if (x->tunnel) {
2536 struct xfrm_state *t = x->tunnel;
2537
2538 if (atomic_read(&t->tunnel_users) == 2)
2539 xfrm_state_delete(t);
2540 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002541 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 x->tunnel = NULL;
2543 }
2544}
2545EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2546
Jiri Bohacdcd03ef2022-01-26 16:00:18 +01002547u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002549 const struct xfrm_type *type = READ_ONCE(x->type);
Florian Westphalc7b37c72019-06-24 22:04:48 +02002550 struct crypto_aead *aead;
2551 u32 blksize, net_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Florian Westphalc7b37c72019-06-24 22:04:48 +02002553 if (x->km.state != XFRM_STATE_VALID ||
2554 !type || type->proto != IPPROTO_ESP)
2555 return mtu - x->props.header_len;
Florian Westphalb3b73b82017-01-05 13:23:58 +01002556
Florian Westphalc7b37c72019-06-24 22:04:48 +02002557 aead = x->data;
2558 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2559
2560 switch (x->props.mode) {
2561 case XFRM_MODE_TRANSPORT:
2562 case XFRM_MODE_BEET:
2563 if (x->props.family == AF_INET)
2564 net_adj = sizeof(struct iphdr);
2565 else if (x->props.family == AF_INET6)
2566 net_adj = sizeof(struct ipv6hdr);
2567 break;
2568 case XFRM_MODE_TUNNEL:
2569 break;
2570 default:
2571 WARN_ON_ONCE(1);
2572 break;
2573 }
2574
2575 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2576 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
Jiri Bohacdcd03ef2022-01-26 16:00:18 +01002578EXPORT_SYMBOL_GPL(xfrm_state_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Ilan Tayariffdb5212017-08-01 12:49:08 +03002580int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002581{
Florian Westphalc9500d72019-03-29 21:16:32 +01002582 const struct xfrm_mode *inner_mode;
2583 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002584 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002585 int err;
2586
Florian Westphale4681742019-05-03 17:46:16 +02002587 if (family == AF_INET &&
2588 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2589 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Herbert Xud094cd82005-06-20 13:19:41 -07002590
2591 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002592
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002593 if (x->sel.family != AF_UNSPEC) {
2594 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2595 if (inner_mode == NULL)
2596 goto error;
2597
2598 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002599 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002600 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002601
Florian Westphalc9500d72019-03-29 21:16:32 +01002602 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002603 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002604 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002605 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002606
Martin Willid81d2282008-12-03 15:38:07 -08002607 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002608 if (inner_mode == NULL)
2609 goto error;
2610
Florian Westphal4c145dc2019-03-29 21:16:31 +01002611 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002612 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002613
Florian Westphalc9500d72019-03-29 21:16:32 +01002614 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002615
Martin Willid81d2282008-12-03 15:38:07 -08002616 if (x->props.family == AF_INET)
2617 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002618
Martin Willid81d2282008-12-03 15:38:07 -08002619 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2620 if (inner_mode_iaf) {
2621 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002622 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002623 }
2624 }
Herbert Xu13996372007-10-17 21:35:51 -07002625
Herbert Xud094cd82005-06-20 13:19:41 -07002626 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002627 if (x->type == NULL)
2628 goto error;
2629
Ilan Tayariffdb5212017-08-01 12:49:08 +03002630 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002631
Herbert Xu72cb6962005-06-20 13:18:08 -07002632 err = x->type->init_state(x);
2633 if (err)
2634 goto error;
2635
Florian Westphalc9500d72019-03-29 21:16:32 +01002636 outer_mode = xfrm_get_mode(x->props.mode, family);
2637 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002638 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002639 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002640 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002641
Florian Westphalc9500d72019-03-29 21:16:32 +01002642 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002643 if (init_replay) {
2644 err = xfrm_init_replay(x);
2645 if (err)
2646 goto error;
2647 }
2648
Herbert Xu72cb6962005-06-20 13:18:08 -07002649error:
2650 return err;
2651}
2652
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002653EXPORT_SYMBOL(__xfrm_init_state);
2654
2655int xfrm_init_state(struct xfrm_state *x)
2656{
Yossi Kupermancc015722018-01-17 15:52:41 +02002657 int err;
2658
2659 err = __xfrm_init_state(x, true, false);
2660 if (!err)
2661 x->km.state = XFRM_STATE_VALID;
2662
2663 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002664}
2665
Herbert Xu72cb6962005-06-20 13:18:08 -07002666EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002667
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002668int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
David S. Millerf034b5d2006-08-24 03:08:07 -07002670 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Mathias Krause565f0fa2018-05-03 10:55:07 +02002672 if (net_eq(net, &init_net))
2673 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2674 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2675
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002676 INIT_LIST_HEAD(&net->xfrm.state_all);
2677
David S. Millerf034b5d2006-08-24 03:08:07 -07002678 sz = sizeof(struct hlist_head) * 8;
2679
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002680 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2681 if (!net->xfrm.state_bydst)
2682 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002683 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2684 if (!net->xfrm.state_bysrc)
2685 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002686 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2687 if (!net->xfrm.state_byspi)
2688 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002689 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002690
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002691 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002692 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002693 spin_lock_init(&net->xfrm.xfrm_state_lock);
Ahmed S. Darwish02244322021-03-16 11:56:29 +01002694 seqcount_init(&net->xfrm.xfrm_state_hash_generation);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002695 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002696
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002697out_byspi:
2698 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002699out_bysrc:
2700 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002701out_bydst:
2702 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002703}
2704
2705void xfrm_state_fini(struct net *net)
2706{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002707 unsigned int sz;
2708
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002709 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002710 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002711 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002712
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002713 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002714
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002715 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002716 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2717 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002718 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2719 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002720 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2721 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Joy Lattenab5f5e82007-09-17 11:51:22 -07002724#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002725static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2726 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002727{
Paul Moore68277ac2007-12-20 20:49:33 -08002728 struct xfrm_sec_ctx *ctx = x->security;
2729 u32 spi = ntohl(x->id.spi);
2730
2731 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002732 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002733 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002734
Weilong Chen9b7a7872013-12-24 09:43:46 +08002735 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002736 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002737 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2738 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002739 break;
2740 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002741 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002742 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002743 break;
2744 }
Paul Moore68277ac2007-12-20 20:49:33 -08002745
2746 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002747}
2748
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002749static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2750 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002751{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002752 const struct iphdr *iph4;
2753 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002754
2755 switch (family) {
2756 case AF_INET:
2757 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002758 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2759 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002760 break;
2761 case AF_INET6:
2762 iph6 = ipv6_hdr(skb);
2763 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002764 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002765 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002766 iph6->flow_lbl[0] & 0x0f,
2767 iph6->flow_lbl[1],
2768 iph6->flow_lbl[2]);
2769 break;
2770 }
2771}
2772
Tetsuo Handa2e710292014-04-22 21:48:30 +09002773void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002774{
2775 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002776
Paul Mooreafeb14b2007-12-21 14:58:11 -08002777 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002778 if (audit_buf == NULL)
2779 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002780 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002781 xfrm_audit_helper_sainfo(x, audit_buf);
2782 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002783 audit_log_end(audit_buf);
2784}
2785EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2786
Tetsuo Handa2e710292014-04-22 21:48:30 +09002787void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002788{
2789 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002790
Paul Mooreafeb14b2007-12-21 14:58:11 -08002791 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002792 if (audit_buf == NULL)
2793 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002794 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002795 xfrm_audit_helper_sainfo(x, audit_buf);
2796 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002797 audit_log_end(audit_buf);
2798}
2799EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002800
2801void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2802 struct sk_buff *skb)
2803{
2804 struct audit_buffer *audit_buf;
2805 u32 spi;
2806
2807 audit_buf = xfrm_audit_start("SA-replay-overflow");
2808 if (audit_buf == NULL)
2809 return;
2810 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2811 /* don't record the sequence number because it's inherent in this kind
2812 * of audit message */
2813 spi = ntohl(x->id.spi);
2814 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2815 audit_log_end(audit_buf);
2816}
2817EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2818
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002819void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002820 struct sk_buff *skb, __be32 net_seq)
2821{
2822 struct audit_buffer *audit_buf;
2823 u32 spi;
2824
2825 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2826 if (audit_buf == NULL)
2827 return;
2828 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2829 spi = ntohl(x->id.spi);
2830 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2831 spi, spi, ntohl(net_seq));
2832 audit_log_end(audit_buf);
2833}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002834EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002835
2836void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2837{
2838 struct audit_buffer *audit_buf;
2839
2840 audit_buf = xfrm_audit_start("SA-notfound");
2841 if (audit_buf == NULL)
2842 return;
2843 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2844 audit_log_end(audit_buf);
2845}
2846EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2847
2848void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2849 __be32 net_spi, __be32 net_seq)
2850{
2851 struct audit_buffer *audit_buf;
2852 u32 spi;
2853
2854 audit_buf = xfrm_audit_start("SA-notfound");
2855 if (audit_buf == NULL)
2856 return;
2857 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2858 spi = ntohl(net_spi);
2859 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2860 spi, spi, ntohl(net_seq));
2861 audit_log_end(audit_buf);
2862}
2863EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2864
2865void xfrm_audit_state_icvfail(struct xfrm_state *x,
2866 struct sk_buff *skb, u8 proto)
2867{
2868 struct audit_buffer *audit_buf;
2869 __be32 net_spi;
2870 __be32 net_seq;
2871
2872 audit_buf = xfrm_audit_start("SA-icv-failure");
2873 if (audit_buf == NULL)
2874 return;
2875 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2876 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2877 u32 spi = ntohl(net_spi);
2878 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2879 spi, spi, ntohl(net_seq));
2880 }
2881 audit_log_end(audit_buf);
2882}
2883EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002884#endif /* CONFIG_AUDITSYSCALL */