blob: 5e5ed81084989d55d85dcd6509bc09b04f048415 [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;
Florian Westphalb65e3d72016-08-09 12:16:07 +020047static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
Mathias Krause565f0fa2018-05-03 10:55:07 +020048static struct kmem_cache *xfrm_state_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Florian Westphal35db57bb2016-08-23 16:00:12 +020050static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
51static HLIST_HEAD(xfrm_state_gc_list);
52
Florian Westphal02efdff2016-08-09 12:16:05 +020053static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
54{
Reshetova, Elena88755e9c2017-07-04 15:53:21 +030055 return refcount_inc_not_zero(&x->refcnt);
Florian Westphal02efdff2016-08-09 12:16:05 +020056}
57
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080058static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050059 const xfrm_address_t *daddr,
60 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070061 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070062 unsigned short family)
63{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080064 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070065}
66
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080067static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050068 const xfrm_address_t *daddr,
69 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070070 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070071{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080072 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070073}
74
David S. Miller2575b652006-08-24 03:26:44 -070075static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050076xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
77 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070078{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080079 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070080}
81
David S. Millerf034b5d2006-08-24 03:08:07 -070082static void xfrm_hash_transfer(struct hlist_head *list,
83 struct hlist_head *ndsttable,
84 struct hlist_head *nsrctable,
85 struct hlist_head *nspitable,
86 unsigned int nhashmask)
87{
Sasha Levinb67bfe02013-02-27 17:06:00 -080088 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -070089 struct xfrm_state *x;
90
Sasha Levinb67bfe02013-02-27 17:06:00 -080091 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -070092 unsigned int h;
93
David S. Millerc1969f22006-08-24 04:00:03 -070094 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
95 x->props.reqid, x->props.family,
96 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +020097 hlist_add_head_rcu(&x->bydst, ndsttable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -070098
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -070099 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
100 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -0700101 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200102 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
David S. Millerf034b5d2006-08-24 03:08:07 -0700103
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700104 if (x->id.spi) {
105 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
106 x->id.proto, x->props.family,
107 nhashmask);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200108 hlist_add_head_rcu(&x->byspi, nspitable + h);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700109 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700110 }
111}
112
Alexey Dobriyan63082732008-11-25 17:19:07 -0800113static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700114{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800115 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700116}
117
Alexey Dobriyan63082732008-11-25 17:19:07 -0800118static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700119{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800120 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
David S. Millerf034b5d2006-08-24 03:08:07 -0700121 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
122 unsigned long nsize, osize;
123 unsigned int nhashmask, ohashmask;
124 int i;
125
Alexey Dobriyan63082732008-11-25 17:19:07 -0800126 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700127 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700128 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800129 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700130 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700131 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700132 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800133 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700134 }
David S. Miller44e36b42006-08-24 04:50:50 -0700135 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700136 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700137 xfrm_hash_free(ndst, nsize);
138 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800139 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700140 }
141
Fan Du283bc9f2013-11-07 17:47:50 +0800142 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphalb65e3d72016-08-09 12:16:07 +0200143 write_seqcount_begin(&xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700144
145 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200146 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800147 for (i = net->xfrm.state_hmask; i >= 0; i--)
Florian Westphalc8406992016-08-09 12:16:08 +0200148 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700149
Florian Westphalc8406992016-08-09 12:16:08 +0200150 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
151 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800152 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700153
Florian Westphalc8406992016-08-09 12:16:08 +0200154 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
155 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
156 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800157 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700158
Florian Westphalb65e3d72016-08-09 12:16:07 +0200159 write_seqcount_end(&xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800160 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700161
162 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200163
164 synchronize_rcu();
165
David S. Miller44e36b42006-08-24 04:50:50 -0700166 xfrm_hash_free(odst, osize);
167 xfrm_hash_free(osrc, osize);
168 xfrm_hash_free(ospi, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700169}
170
Cong Wang44abdc32013-01-16 16:05:05 +0800171static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
172static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static DEFINE_SPINLOCK(xfrm_state_gc_lock);
175
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800176int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800178int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Florian Westphalbb9cd072019-04-17 11:45:13 +0200179static bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000180void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800182int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700183{
Cong Wang7a9885b2013-01-17 16:34:11 +0800184 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700185 int err = 0;
186
Florian Westphal4f518e82019-05-03 17:46:19 +0200187 if (!afinfo)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700188 return -EAFNOSUPPORT;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700189
Florian Westphal4f518e82019-05-03 17:46:19 +0200190#define X(afi, T, name) do { \
191 WARN_ON((afi)->type_ ## name); \
192 (afi)->type_ ## name = (T); \
193 } while (0)
194
195 switch (type->proto) {
196 case IPPROTO_COMP:
197 X(afinfo, type, comp);
198 break;
199 case IPPROTO_AH:
200 X(afinfo, type, ah);
201 break;
202 case IPPROTO_ESP:
203 X(afinfo, type, esp);
204 break;
205 case IPPROTO_IPIP:
206 X(afinfo, type, ipip);
207 break;
208 case IPPROTO_DSTOPTS:
209 X(afinfo, type, dstopts);
210 break;
211 case IPPROTO_ROUTING:
212 X(afinfo, type, routing);
213 break;
214 case IPPROTO_IPV6:
215 X(afinfo, type, ipip6);
216 break;
217 default:
218 WARN_ON(1);
219 err = -EPROTONOSUPPORT;
220 break;
221 }
222#undef X
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100223 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700224 return err;
225}
226EXPORT_SYMBOL(xfrm_register_type);
227
Florian Westphal4f518e82019-05-03 17:46:19 +0200228void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700229{
Cong Wang7a9885b2013-01-17 16:34:11 +0800230 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700231
232 if (unlikely(afinfo == NULL))
Florian Westphal4f518e82019-05-03 17:46:19 +0200233 return;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700234
Florian Westphal4f518e82019-05-03 17:46:19 +0200235#define X(afi, T, name) do { \
236 WARN_ON((afi)->type_ ## name != (T)); \
237 (afi)->type_ ## name = NULL; \
238 } while (0)
239
240 switch (type->proto) {
241 case IPPROTO_COMP:
242 X(afinfo, type, comp);
243 break;
244 case IPPROTO_AH:
245 X(afinfo, type, ah);
246 break;
247 case IPPROTO_ESP:
248 X(afinfo, type, esp);
249 break;
250 case IPPROTO_IPIP:
251 X(afinfo, type, ipip);
252 break;
253 case IPPROTO_DSTOPTS:
254 X(afinfo, type, dstopts);
255 break;
256 case IPPROTO_ROUTING:
257 X(afinfo, type, routing);
258 break;
259 case IPPROTO_IPV6:
260 X(afinfo, type, ipip6);
261 break;
262 default:
263 WARN_ON(1);
264 break;
265 }
266#undef X
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100267 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700268}
269EXPORT_SYMBOL(xfrm_unregister_type);
270
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800271static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700272{
Florian Westphal4f518e82019-05-03 17:46:19 +0200273 const struct xfrm_type *type = NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700274 struct xfrm_state_afinfo *afinfo;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700275 int modload_attempted = 0;
276
277retry:
278 afinfo = xfrm_state_get_afinfo(family);
279 if (unlikely(afinfo == NULL))
280 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700281
Florian Westphal4f518e82019-05-03 17:46:19 +0200282 switch (proto) {
283 case IPPROTO_COMP:
284 type = afinfo->type_comp;
285 break;
286 case IPPROTO_AH:
287 type = afinfo->type_ah;
288 break;
289 case IPPROTO_ESP:
290 type = afinfo->type_esp;
291 break;
292 case IPPROTO_IPIP:
293 type = afinfo->type_ipip;
294 break;
295 case IPPROTO_DSTOPTS:
296 type = afinfo->type_dstopts;
297 break;
298 case IPPROTO_ROUTING:
299 type = afinfo->type_routing;
300 break;
301 case IPPROTO_IPV6:
302 type = afinfo->type_ipip6;
303 break;
304 default:
305 break;
306 }
307
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700308 if (unlikely(type && !try_module_get(type->owner)))
309 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100310
311 rcu_read_unlock();
312
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700313 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700314 request_module("xfrm-type-%d-%d", family, proto);
315 modload_attempted = 1;
316 goto retry;
317 }
318
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700319 return type;
320}
321
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800322static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700323{
324 module_put(type->owner);
325}
326
Steffen Klassert9d389d72017-04-14 10:05:44 +0200327int xfrm_register_type_offload(const struct xfrm_type_offload *type,
328 unsigned short family)
329{
330 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200331 int err = 0;
332
333 if (unlikely(afinfo == NULL))
334 return -EAFNOSUPPORT;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200335
Florian Westphal4f518e82019-05-03 17:46:19 +0200336 switch (type->proto) {
337 case IPPROTO_ESP:
338 WARN_ON(afinfo->type_offload_esp);
339 afinfo->type_offload_esp = type;
340 break;
341 default:
342 WARN_ON(1);
343 err = -EPROTONOSUPPORT;
344 break;
345 }
346
Steffen Klassert9d389d72017-04-14 10:05:44 +0200347 rcu_read_unlock();
348 return err;
349}
350EXPORT_SYMBOL(xfrm_register_type_offload);
351
Florian Westphal4f518e82019-05-03 17:46:19 +0200352void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
353 unsigned short family)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200354{
355 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200356
357 if (unlikely(afinfo == NULL))
Florian Westphal4f518e82019-05-03 17:46:19 +0200358 return;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200359
Florian Westphal4f518e82019-05-03 17:46:19 +0200360 switch (type->proto) {
361 case IPPROTO_ESP:
362 WARN_ON(afinfo->type_offload_esp != type);
363 afinfo->type_offload_esp = NULL;
364 break;
365 default:
366 WARN_ON(1);
367 break;
368 }
Steffen Klassert9d389d72017-04-14 10:05:44 +0200369 rcu_read_unlock();
Steffen Klassert9d389d72017-04-14 10:05:44 +0200370}
371EXPORT_SYMBOL(xfrm_unregister_type_offload);
372
Ilan Tayariffdb5212017-08-01 12:49:08 +0300373static const struct xfrm_type_offload *
374xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200375{
Florian Westphal4f518e82019-05-03 17:46:19 +0200376 const struct xfrm_type_offload *type = NULL;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200377 struct xfrm_state_afinfo *afinfo;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200378
Ilan Tayariffdb5212017-08-01 12:49:08 +0300379retry:
Steffen Klassert9d389d72017-04-14 10:05:44 +0200380 afinfo = xfrm_state_get_afinfo(family);
381 if (unlikely(afinfo == NULL))
382 return NULL;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200383
Florian Westphal4f518e82019-05-03 17:46:19 +0200384 switch (proto) {
385 case IPPROTO_ESP:
386 type = afinfo->type_offload_esp;
387 break;
388 default:
389 break;
390 }
391
Steffen Klassert9d389d72017-04-14 10:05:44 +0200392 if ((type && !try_module_get(type->owner)))
393 type = NULL;
394
Sabrina Dubroca2f10a612017-12-31 16:18:56 +0100395 rcu_read_unlock();
396
Ilan Tayariffdb5212017-08-01 12:49:08 +0300397 if (!type && try_load) {
398 request_module("xfrm-offload-%d-%d", family, proto);
Gustavo A. R. Silva545d8ae2018-01-22 16:34:09 -0600399 try_load = false;
Ilan Tayariffdb5212017-08-01 12:49:08 +0300400 goto retry;
401 }
402
Steffen Klassert9d389d72017-04-14 10:05:44 +0200403 return type;
404}
405
406static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
407{
408 module_put(type->owner);
409}
410
Florian Westphal4c145dc2019-03-29 21:16:31 +0100411static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
412 [XFRM_MODE_BEET] = {
413 .encap = XFRM_MODE_BEET,
414 .flags = XFRM_MODE_FLAG_TUNNEL,
415 .family = AF_INET,
416 },
417 [XFRM_MODE_TRANSPORT] = {
418 .encap = XFRM_MODE_TRANSPORT,
419 .family = AF_INET,
420 },
421 [XFRM_MODE_TUNNEL] = {
422 .encap = XFRM_MODE_TUNNEL,
423 .flags = XFRM_MODE_FLAG_TUNNEL,
424 .family = AF_INET,
425 },
426};
427
428static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
429 [XFRM_MODE_BEET] = {
430 .encap = XFRM_MODE_BEET,
431 .flags = XFRM_MODE_FLAG_TUNNEL,
432 .family = AF_INET6,
433 },
434 [XFRM_MODE_ROUTEOPTIMIZATION] = {
435 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
436 .family = AF_INET6,
437 },
438 [XFRM_MODE_TRANSPORT] = {
439 .encap = XFRM_MODE_TRANSPORT,
440 .family = AF_INET6,
441 },
442 [XFRM_MODE_TUNNEL] = {
443 .encap = XFRM_MODE_TUNNEL,
444 .flags = XFRM_MODE_FLAG_TUNNEL,
445 .family = AF_INET6,
446 },
447};
448
449static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700450{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100451 const struct xfrm_mode *mode;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700452
453 if (unlikely(encap >= XFRM_MODE_MAX))
454 return NULL;
455
Florian Westphal4c145dc2019-03-29 21:16:31 +0100456 switch (family) {
457 case AF_INET:
458 mode = &xfrm4_mode_map[encap];
459 if (mode->family == family)
460 return mode;
461 break;
462 case AF_INET6:
463 mode = &xfrm6_mode_map[encap];
464 if (mode->family == family)
465 return mode;
466 break;
467 default:
468 break;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700469 }
470
Florian Westphal4c145dc2019-03-29 21:16:31 +0100471 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700472}
473
Mathias Krause4a135e52018-11-21 21:09:23 +0100474void xfrm_state_free(struct xfrm_state *x)
475{
476 kmem_cache_free(xfrm_state_cache, x);
477}
478EXPORT_SYMBOL(xfrm_state_free);
479
Cong Wangf75a2802019-01-31 13:05:49 -0800480static void ___xfrm_state_destroy(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100482 hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700483 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000484 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800485 kfree(x->aalg);
486 kfree(x->ealg);
487 kfree(x->calg);
488 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700489 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000490 kfree(x->replay_esn);
491 kfree(x->preplay_esn);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200492 if (x->type_offload)
493 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (x->type) {
495 x->type->destructor(x);
496 xfrm_put_type(x->type);
497 }
Steffen Klassert86c67392019-11-06 08:13:49 +0100498 if (x->xfrag.page)
499 put_page(x->xfrag.page);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200500 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800501 security_xfrm_state_free(x);
Mathias Krause4a135e52018-11-21 21:09:23 +0100502 xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800505static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Herbert Xu12a169e2008-10-01 07:03:24 -0700507 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800508 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700509 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200512 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 spin_unlock_bh(&xfrm_state_gc_lock);
514
Florian Westphaldf7274e2016-08-09 12:16:06 +0200515 synchronize_rcu();
516
Sasha Levinb67bfe02013-02-27 17:06:00 -0800517 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Cong Wangf75a2802019-01-31 13:05:49 -0800518 ___xfrm_state_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800521static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100523 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
524 enum hrtimer_restart ret = HRTIMER_NORESTART;
Arnd Bergmann386c5682018-07-11 12:19:13 +0200525 time64_t now = ktime_get_real_seconds();
526 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600528 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 spin_lock(&x->lock);
531 if (x->km.state == XFRM_STATE_DEAD)
532 goto out;
533 if (x->km.state == XFRM_STATE_EXPIRED)
534 goto expired;
535 if (x->lft.hard_add_expires_seconds) {
536 long tmo = x->lft.hard_add_expires_seconds +
537 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000538 if (tmo <= 0) {
539 if (x->xflags & XFRM_SOFT_EXPIRE) {
540 /* enter hard expire without soft expire first?!
541 * setting a new date could trigger this.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100542 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000543 */
544 x->curlft.add_time = now - x->saved_tmo - 1;
545 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
546 } else
547 goto expired;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (tmo < next)
550 next = tmo;
551 }
552 if (x->lft.hard_use_expires_seconds) {
553 long tmo = x->lft.hard_use_expires_seconds +
554 (x->curlft.use_time ? : now) - now;
555 if (tmo <= 0)
556 goto expired;
557 if (tmo < next)
558 next = tmo;
559 }
560 if (x->km.dying)
561 goto resched;
562 if (x->lft.soft_add_expires_seconds) {
563 long tmo = x->lft.soft_add_expires_seconds +
564 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000565 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000567 x->xflags &= ~XFRM_SOFT_EXPIRE;
568 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000570 x->xflags |= XFRM_SOFT_EXPIRE;
571 x->saved_tmo = tmo;
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 if (x->lft.soft_use_expires_seconds) {
575 long tmo = x->lft.soft_use_expires_seconds +
576 (x->curlft.use_time ? : now) - now;
577 if (tmo <= 0)
578 warn = 1;
579 else if (tmo < next)
580 next = tmo;
581 }
582
Herbert Xu4666faa2005-06-18 22:43:22 -0700583 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800585 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586resched:
Arnd Bergmann386c5682018-07-11 12:19:13 +0200587 if (next != TIME64_MAX) {
Thomas Gleixner671422b2019-03-01 23:48:20 +0100588 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
589 ret = HRTIMER_RESTART;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800590 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto out;
593
594expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200595 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600597
598 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200599 if (!err)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800600 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Tetsuo Handa2e710292014-04-22 21:48:30 +0900602 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604out:
605 spin_unlock(&x->lock);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Kees Cooke99e88a2017-10-16 14:43:17 -0700609static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800610
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800611struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct xfrm_state *x;
614
Huang Zijianga4c278d2020-02-12 17:54:36 +0800615 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (x) {
Alexey Dobriyan673c09b2008-11-25 17:15:16 -0800618 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300619 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700621 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700622 INIT_HLIST_NODE(&x->bydst);
623 INIT_HLIST_NODE(&x->bysrc);
624 INIT_HLIST_NODE(&x->byspi);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100625 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
626 x->mtimer.function = xfrm_timer_handler;
Kees Cooke99e88a2017-10-16 14:43:17 -0700627 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200628 x->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 x->lft.soft_byte_limit = XFRM_INF;
630 x->lft.soft_packet_limit = XFRM_INF;
631 x->lft.hard_byte_limit = XFRM_INF;
632 x->lft.hard_packet_limit = XFRM_INF;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800633 x->replay_maxage = 0;
634 x->replay_maxdiff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 spin_lock_init(&x->lock);
636 }
637 return x;
638}
639EXPORT_SYMBOL(xfrm_state_alloc);
640
Cong Wangf75a2802019-01-31 13:05:49 -0800641void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700643 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Cong Wangf75a2802019-01-31 13:05:49 -0800645 if (sync) {
646 synchronize_rcu();
647 ___xfrm_state_destroy(x);
648 } else {
649 spin_lock_bh(&xfrm_state_gc_lock);
650 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
651 spin_unlock_bh(&xfrm_state_gc_lock);
652 schedule_work(&xfrm_state_gc_work);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655EXPORT_SYMBOL(__xfrm_state_destroy);
656
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800657int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800659 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700660 int err = -ESRCH;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (x->km.state != XFRM_STATE_DEAD) {
663 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800664 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700665 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200666 hlist_del_rcu(&x->bydst);
667 hlist_del_rcu(&x->bysrc);
David S. Millera47f0ce2006-08-24 03:54:22 -0700668 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200669 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800670 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800671 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Sabrina Dubrocae27cca92019-11-25 14:49:02 +0100673 if (x->encap_sk)
674 sock_put(rcu_dereference_raw(x->encap_sk));
675
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200676 xfrm_dev_state_delete(x);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* All xfrm_state objects are created by xfrm_state_alloc.
679 * The xfrm_state_alloc call gives a reference, and that
680 * is what we are dropping here.
681 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800682 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700683 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700685
686 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800688EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700690int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700692 int err;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700695 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700697
698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700EXPORT_SYMBOL(xfrm_state_delete);
701
Joy Latten4aa2e622007-06-04 19:05:57 -0400702#ifdef CONFIG_SECURITY_NETWORK_XFRM
703static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900704xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Joy Latten4aa2e622007-06-04 19:05:57 -0400706 int i, err = 0;
707
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800708 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400709 struct xfrm_state *x;
710
Sasha Levinb67bfe02013-02-27 17:06:00 -0800711 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400712 if (xfrm_id_proto_match(x->id.proto, proto) &&
713 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900714 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400715 return err;
716 }
717 }
718 }
719
720 return err;
721}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200722
723static inline int
724xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
725{
726 int i, err = 0;
727
728 for (i = 0; i <= net->xfrm.state_hmask; i++) {
729 struct xfrm_state *x;
730 struct xfrm_state_offload *xso;
731
732 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
733 xso = &x->xso;
734
735 if (xso->dev == dev &&
736 (err = security_xfrm_state_delete(x)) != 0) {
737 xfrm_audit_state_delete(x, 0, task_valid);
738 return err;
739 }
740 }
741 }
742
743 return err;
744}
Joy Latten4aa2e622007-06-04 19:05:57 -0400745#else
746static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900747xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400748{
749 return 0;
750}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200751
752static inline int
753xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
754{
755 return 0;
756}
Joy Latten4aa2e622007-06-04 19:05:57 -0400757#endif
758
Cong Wangf75a2802019-01-31 13:05:49 -0800759int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
Joy Latten4aa2e622007-06-04 19:05:57 -0400760{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000761 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Fan Du283bc9f2013-11-07 17:47:50 +0800763 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900764 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400765 if (err)
766 goto out;
767
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000768 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800769 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700770 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800772 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700774 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800776 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Joy Latten161a09e2006-11-27 13:11:54 -0600778 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700779 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900780 task_valid);
Cong Wangf75a2802019-01-31 13:05:49 -0800781 if (sync)
782 xfrm_state_put_sync(x);
783 else
784 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000785 if (!err)
786 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Fan Du283bc9f2013-11-07 17:47:50 +0800788 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto restart;
790 }
791 }
792 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200793out:
794 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200795 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000796 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200797
Joy Latten4aa2e622007-06-04 19:05:57 -0400798 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800EXPORT_SYMBOL(xfrm_state_flush);
801
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200802int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
803{
804 int i, err = 0, cnt = 0;
805
806 spin_lock_bh(&net->xfrm.xfrm_state_lock);
807 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
808 if (err)
809 goto out;
810
811 err = -ESRCH;
812 for (i = 0; i <= net->xfrm.state_hmask; i++) {
813 struct xfrm_state *x;
814 struct xfrm_state_offload *xso;
815restart:
816 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
817 xso = &x->xso;
818
819 if (!xfrm_state_kern(x) && xso->dev == dev) {
820 xfrm_state_hold(x);
821 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
822
823 err = xfrm_state_delete(x);
824 xfrm_audit_state_delete(x, err ? 0 : 1,
825 task_valid);
826 xfrm_state_put(x);
827 if (!err)
828 cnt++;
829
830 spin_lock_bh(&net->xfrm.xfrm_state_lock);
831 goto restart;
832 }
833 }
834 }
835 if (cnt)
836 err = 0;
837
838out:
839 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
840 return err;
841}
842EXPORT_SYMBOL(xfrm_dev_state_flush);
843
Alexey Dobriyane0710412010-01-23 13:37:10 +0000844void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700845{
Fan Du283bc9f2013-11-07 17:47:50 +0800846 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000847 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900848 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700849 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800850 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700851}
852EXPORT_SYMBOL(xfrm_sad_getinfo);
853
Florian Westphal711059b2017-01-09 14:20:48 +0100854static void
Florian Westphalbac95932019-05-03 17:46:14 +0200855__xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
856{
857 const struct flowi4 *fl4 = &fl->u.ip4;
858
859 sel->daddr.a4 = fl4->daddr;
860 sel->saddr.a4 = fl4->saddr;
861 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
862 sel->dport_mask = htons(0xffff);
863 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
864 sel->sport_mask = htons(0xffff);
865 sel->family = AF_INET;
866 sel->prefixlen_d = 32;
867 sel->prefixlen_s = 32;
868 sel->proto = fl4->flowi4_proto;
869 sel->ifindex = fl4->flowi4_oif;
870}
871
872static void
873__xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
874{
875 const struct flowi6 *fl6 = &fl->u.ip6;
876
877 /* Initialize temporary selector matching only to current session. */
878 *(struct in6_addr *)&sel->daddr = fl6->daddr;
879 *(struct in6_addr *)&sel->saddr = fl6->saddr;
880 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
881 sel->dport_mask = htons(0xffff);
882 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
883 sel->sport_mask = htons(0xffff);
884 sel->family = AF_INET6;
885 sel->prefixlen_d = 128;
886 sel->prefixlen_s = 128;
887 sel->proto = fl6->flowi6_proto;
888 sel->ifindex = fl6->flowi6_oif;
889}
890
891static void
David S. Miller1a898592011-02-22 18:22:34 -0800892xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500893 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500894 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700895 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Florian Westphalbac95932019-05-03 17:46:14 +0200897 switch (family) {
898 case AF_INET:
899 __xfrm4_init_tempsel(&x->sel, fl);
900 break;
901 case AF_INET6:
902 __xfrm6_init_tempsel(&x->sel, fl);
903 break;
904 }
905
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200906 x->id = tmpl->id;
Florian Westphalbac95932019-05-03 17:46:14 +0200907
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200908 switch (tmpl->encap_family) {
909 case AF_INET:
910 if (x->id.daddr.a4 == 0)
911 x->id.daddr.a4 = daddr->a4;
912 x->props.saddr = tmpl->saddr;
913 if (x->props.saddr.a4 == 0)
914 x->props.saddr.a4 = saddr->a4;
915 break;
916 case AF_INET6:
917 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
918 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
919 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
920 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
921 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
922 break;
923 }
Florian Westphal3819a352017-01-13 14:55:14 +0100924
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200925 x->props.mode = tmpl->mode;
926 x->props.reqid = tmpl->reqid;
927 x->props.family = tmpl->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
David S. Miller9aa60082011-02-24 01:51:36 -0500930static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
931 const xfrm_address_t *daddr,
932 __be32 spi, u8 proto,
933 unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700934{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800935 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700936 struct xfrm_state *x;
937
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200938 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -0700939 if (x->props.family != family ||
940 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +0000941 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000942 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700943 continue;
944
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000945 if ((mark & x->mark.m) != x->mark.v)
946 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200947 if (!xfrm_state_hold_rcu(x))
948 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700949 return x;
950 }
951
952 return NULL;
953}
954
David S. Miller9aa60082011-02-24 01:51:36 -0500955static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
956 const xfrm_address_t *daddr,
957 const xfrm_address_t *saddr,
958 u8 proto, unsigned short family)
David S. Milleredcd5822006-08-24 00:42:45 -0700959{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800960 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700961 struct xfrm_state *x;
962
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200963 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -0700964 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +0000965 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +0000966 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
967 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -0700968 continue;
969
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +0000970 if ((mark & x->mark.m) != x->mark.v)
971 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +0200972 if (!xfrm_state_hold_rcu(x))
973 continue;
David S. Milleredcd5822006-08-24 00:42:45 -0700974 return x;
975 }
976
977 return NULL;
978}
979
980static inline struct xfrm_state *
981__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
982{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800983 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800984 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800985
David S. Milleredcd5822006-08-24 00:42:45 -0700986 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800987 return __xfrm_state_lookup(net, mark, &x->id.daddr,
988 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -0700989 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800990 return __xfrm_state_lookup_byaddr(net, mark,
991 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -0700992 &x->props.saddr,
993 x->id.proto, family);
994}
995
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800996static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -0700997{
998 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800999 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
1000 net->xfrm.state_num > net->xfrm.state_hmask)
1001 schedule_work(&net->xfrm.state_hash_work);
Patrick McHardy2fab22f2006-10-24 15:34:00 -07001002}
1003
David S. Miller08ec9af2009-03-13 14:22:40 -07001004static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -08001005 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001006 struct xfrm_state **best, int *acq_in_progress,
1007 int *error)
1008{
1009 /* Resolution logic:
1010 * 1. There is a valid state with matching selector. Done.
1011 * 2. Valid state with inappropriate selector. Skip.
1012 *
1013 * Entering area of "sysdeps".
1014 *
1015 * 3. If state is not valid, selector is temporary, it selects
1016 * only session which triggered previous resolution. Key
1017 * manager will do something to install a state with proper
1018 * selector.
1019 */
1020 if (x->km.state == XFRM_STATE_VALID) {
1021 if ((x->sel.family &&
1022 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
1023 !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) {
1035 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
1036 security_xfrm_state_pol_flow_match(x, pol, fl))
1037 *error = -ESRCH;
1038 }
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -05001042xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -08001043 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -07001045 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
David S. Miller08ec9af2009-03-13 14:22:40 -07001047 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001048 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -07001049 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -07001050 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int acquire_in_progress = 0;
1052 int error = 0;
1053 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001054 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001055 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +02001056 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +02001057 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001058
David S. Miller37b08e32008-09-02 20:14:15 -07001059 to_put = NULL;
1060
Florian Westphalb65e3d72016-08-09 12:16:07 +02001061 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
1062
Florian Westphald737a582016-08-09 12:16:09 +02001063 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -07001064 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001065 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001066 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001068 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001069 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -07001070 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -07001071 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 tmpl->mode == x->props.mode &&
1073 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001074 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -05001075 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001076 &best, &acquire_in_progress, &error);
1077 }
Fan Du6f115632013-09-23 17:18:25 +08001078 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -07001079 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Thomas Egerer8444cf72010-09-20 11:11:38 -07001081 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001082 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001083 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001084 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001085 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001086 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001087 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +08001088 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001089 tmpl->mode == x->props.mode &&
1090 tmpl->id.proto == x->id.proto &&
1091 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
David S. Miller1f673c52011-02-24 01:53:13 -05001092 xfrm_state_look_at(pol, x, fl, encap_family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001093 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
David S. Miller08ec9af2009-03-13 14:22:40 -07001096found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 x = best;
1098 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -07001099 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001100 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -07001101 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -07001102 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 error = -EEXIST;
1104 goto out;
1105 }
Horia Geanta0f245582014-02-12 16:20:06 +02001106
1107 c.net = net;
1108 /* If the KMs have no listeners (yet...), avoid allocating an SA
1109 * for each and every packet - garbage collection might not
1110 * handle the flood.
1111 */
1112 if (!km_is_alive(&c)) {
1113 error = -ESRCH;
1114 goto out;
1115 }
1116
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001117 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (x == NULL) {
1119 error = -ENOMEM;
1120 goto out;
1121 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001122 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001124 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001125 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +02001126 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
David S. Miller1d28f422011-03-12 00:29:39 -05001128 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001129 if (error) {
1130 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001131 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001132 x = NULL;
1133 goto out;
1134 }
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001137 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001139 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001140 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001141 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001142 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001144 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001145 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001147 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001148 hrtimer_start(&x->mtimer,
1149 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1150 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001151 net->xfrm.state_num++;
1152 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001153 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 } else {
1155 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001156 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 x = NULL;
1158 error = -ESRCH;
1159 }
1160 }
1161out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001162 if (x) {
1163 if (!xfrm_state_hold_rcu(x)) {
1164 *err = -EAGAIN;
1165 x = NULL;
1166 }
1167 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001169 }
Florian Westphald737a582016-08-09 12:16:09 +02001170 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001171 if (to_put)
1172 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001173
1174 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1175 *err = -EAGAIN;
1176 if (x) {
1177 xfrm_state_put(x);
1178 x = NULL;
1179 }
1180 }
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return x;
1183}
1184
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001185struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001186xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001187 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001188 unsigned short family, u8 mode, u8 proto, u32 reqid)
1189{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001190 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001191 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001192
Fan Du4ae770b2014-01-03 11:18:29 +08001193 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001194 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001195 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001196 if (x->props.family == family &&
1197 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001198 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001199 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001200 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1201 xfrm_state_addr_check(x, daddr, saddr, family) &&
1202 mode == x->props.mode &&
1203 proto == x->id.proto &&
1204 x->km.state == XFRM_STATE_VALID) {
1205 rx = x;
1206 break;
1207 }
1208 }
1209
1210 if (rx)
1211 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001212 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001213
1214
1215 return rx;
1216}
1217EXPORT_SYMBOL(xfrm_stateonly_find);
1218
Fan Duc4549972014-01-03 11:18:32 +08001219struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1220 unsigned short family)
1221{
1222 struct xfrm_state *x;
1223 struct xfrm_state_walk *w;
1224
1225 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1226 list_for_each_entry(w, &net->xfrm.state_all, all) {
1227 x = container_of(w, struct xfrm_state, km);
1228 if (x->props.family != family ||
1229 x->id.spi != spi)
1230 continue;
1231
Fan Duc4549972014-01-03 11:18:32 +08001232 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001233 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001234 return x;
1235 }
1236 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1237 return NULL;
1238}
1239EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241static void __xfrm_state_insert(struct xfrm_state *x)
1242{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001243 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001244 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001246 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001247
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001248 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001249 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001250 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001252 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001253 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001255 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001256 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001257 x->props.family);
1258
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001259 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001260 }
1261
Thomas Gleixner671422b2019-03-01 23:48:20 +01001262 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
David S. Millera47f0ce2006-08-24 03:54:22 -07001263 if (x->replay_maxage)
1264 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001265
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001266 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001267
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001268 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Fan Du283bc9f2013-11-07 17:47:50 +08001271/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001272static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1273{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001274 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001275 unsigned short family = xnew->props.family;
1276 u32 reqid = xnew->props.reqid;
1277 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001278 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001279 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001280 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001281
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001282 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001283 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001284 if (x->props.family == family &&
1285 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001286 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001287 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001288 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1289 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001290 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001291 }
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294void xfrm_state_insert(struct xfrm_state *x)
1295{
Fan Du283bc9f2013-11-07 17:47:50 +08001296 struct net *net = xs_net(x);
1297
1298 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001299 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001301 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303EXPORT_SYMBOL(xfrm_state_insert);
1304
Fan Du283bc9f2013-11-07 17:47:50 +08001305/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001306static struct xfrm_state *__find_acq_core(struct net *net,
1307 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001308 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001309 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001310 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001311 const xfrm_address_t *saddr,
1312 int create)
David S. Miller27708342006-08-24 00:13:10 -07001313{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001314 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001315 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001316 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001317
Sasha Levinb67bfe02013-02-27 17:06:00 -08001318 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001319 if (x->props.reqid != reqid ||
1320 x->props.mode != mode ||
1321 x->props.family != family ||
1322 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001323 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001324 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001325 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001326 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1327 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001328 continue;
1329
David S. Miller27708342006-08-24 00:13:10 -07001330 xfrm_state_hold(x);
1331 return x;
1332 }
1333
1334 if (!create)
1335 return NULL;
1336
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001337 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001338 if (likely(x)) {
1339 switch (family) {
1340 case AF_INET:
1341 x->sel.daddr.a4 = daddr->a4;
1342 x->sel.saddr.a4 = saddr->a4;
1343 x->sel.prefixlen_d = 32;
1344 x->sel.prefixlen_s = 32;
1345 x->props.saddr.a4 = saddr->a4;
1346 x->id.daddr.a4 = daddr->a4;
1347 break;
1348
1349 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001350 x->sel.daddr.in6 = daddr->in6;
1351 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001352 x->sel.prefixlen_d = 128;
1353 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001354 x->props.saddr.in6 = saddr->in6;
1355 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001356 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001357 }
David S. Miller27708342006-08-24 00:13:10 -07001358
1359 x->km.state = XFRM_STATE_ACQ;
1360 x->id.proto = proto;
1361 x->props.family = family;
1362 x->props.mode = mode;
1363 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001364 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001365 x->mark.v = m->v;
1366 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001367 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001368 xfrm_state_hold(x);
Thomas Gleixner671422b2019-03-01 23:48:20 +01001369 hrtimer_start(&x->mtimer,
1370 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1371 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001372 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001373 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001374 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001375 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001376
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001377 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001378
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001379 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001380 }
1381
1382 return x;
1383}
1384
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001385static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387int xfrm_state_add(struct xfrm_state *x)
1388{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001389 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001390 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int family;
1392 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001393 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001394 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
David S. Miller37b08e32008-09-02 20:14:15 -07001398 to_put = NULL;
1399
Fan Du283bc9f2013-11-07 17:47:50 +08001400 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
David S. Milleredcd5822006-08-24 00:42:45 -07001402 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001404 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 x1 = NULL;
1406 err = -EEXIST;
1407 goto out;
1408 }
1409
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001410 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001411 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001412 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001413 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001414 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 x1 = NULL;
1416 }
1417 }
1418
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001419 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001420 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001421 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001422 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
David S. Millerc7f5ea32006-08-24 03:29:04 -07001424 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 __xfrm_state_insert(x);
1426 err = 0;
1427
1428out:
Fan Du283bc9f2013-11-07 17:47:50 +08001429 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (x1) {
1432 xfrm_state_delete(x1);
1433 xfrm_state_put(x1);
1434 }
1435
David S. Miller37b08e32008-09-02 20:14:15 -07001436 if (to_put)
1437 xfrm_state_put(to_put);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return err;
1440}
1441EXPORT_SYMBOL(xfrm_state_add);
1442
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001443#ifdef CONFIG_XFRM_MIGRATE
Antony Antony7aa05d32020-09-04 08:50:11 +02001444static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1445{
1446 struct xfrm_user_sec_ctx *uctx;
1447 int size = sizeof(*uctx) + security->ctx_len;
1448 int err;
1449
1450 uctx = kmalloc(size, GFP_KERNEL);
1451 if (!uctx)
1452 return -ENOMEM;
1453
1454 uctx->exttype = XFRMA_SEC_CTX;
1455 uctx->len = size;
1456 uctx->ctx_doi = security->ctx_doi;
1457 uctx->ctx_alg = security->ctx_alg;
1458 uctx->ctx_len = security->ctx_len;
1459 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1460 err = security_xfrm_state_alloc(x, uctx);
1461 kfree(uctx);
1462 if (err)
1463 return err;
1464
1465 return 0;
1466}
1467
Antony Antony4ab47d42017-06-06 12:12:13 +02001468static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1469 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001470{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001471 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001472 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001473 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001474 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001475
1476 memcpy(&x->id, &orig->id, sizeof(x->id));
1477 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1478 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1479 x->props.mode = orig->props.mode;
1480 x->props.replay_window = orig->props.replay_window;
1481 x->props.reqid = orig->props.reqid;
1482 x->props.family = orig->props.family;
1483 x->props.saddr = orig->props.saddr;
1484
1485 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001486 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001487 if (!x->aalg)
1488 goto error;
1489 }
1490 x->props.aalgo = orig->props.aalgo;
1491
Steffen Klassertee5c2312014-02-19 13:33:24 +01001492 if (orig->aead) {
1493 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001494 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001495 if (!x->aead)
1496 goto error;
1497 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001498 if (orig->ealg) {
1499 x->ealg = xfrm_algo_clone(orig->ealg);
1500 if (!x->ealg)
1501 goto error;
1502 }
1503 x->props.ealgo = orig->props.ealgo;
1504
1505 if (orig->calg) {
1506 x->calg = xfrm_algo_clone(orig->calg);
1507 if (!x->calg)
1508 goto error;
1509 }
1510 x->props.calgo = orig->props.calgo;
1511
Antony Antony4ab47d42017-06-06 12:12:13 +02001512 if (encap || orig->encap) {
1513 if (encap)
1514 x->encap = kmemdup(encap, sizeof(*x->encap),
1515 GFP_KERNEL);
1516 else
1517 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1518 GFP_KERNEL);
1519
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001520 if (!x->encap)
1521 goto error;
1522 }
1523
Antony Antony7aa05d32020-09-04 08:50:11 +02001524 if (orig->security)
1525 if (clone_security(x, orig->security))
1526 goto error;
1527
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001528 if (orig->coaddr) {
1529 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1530 GFP_KERNEL);
1531 if (!x->coaddr)
1532 goto error;
1533 }
1534
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001535 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001536 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001537 goto error;
1538 }
1539
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001540 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
Antony Antony545e5c52020-09-04 08:49:38 +02001541 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001542
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001543 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001544 goto error;
1545
1546 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001547 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001548
Steffen Klassert7e652642018-06-12 14:07:07 +02001549 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001550 x->tfcpad = orig->tfcpad;
1551 x->replay_maxdiff = orig->replay_maxdiff;
1552 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001553 x->curlft.add_time = orig->curlft.add_time;
1554 x->km.state = orig->km.state;
1555 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001556 x->replay = orig->replay;
1557 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001558
1559 return x;
1560
1561 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001562 xfrm_state_put(x);
1563out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001564 return NULL;
1565}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001566
Fan Du283bc9f2013-11-07 17:47:50 +08001567struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001568{
1569 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001570 struct xfrm_state *x = NULL;
1571
1572 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001573
1574 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001575 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001576 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001577 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001578 if (x->props.mode != m->mode ||
1579 x->id.proto != m->proto)
1580 continue;
1581 if (m->reqid && x->props.reqid != m->reqid)
1582 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001583 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1584 m->old_family) ||
1585 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1586 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001587 continue;
1588 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001589 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001590 }
1591 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001592 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001593 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001594 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001595 if (x->props.mode != m->mode ||
1596 x->id.proto != m->proto)
1597 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001598 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1599 m->old_family) ||
1600 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1601 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001602 continue;
1603 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001604 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001605 }
1606 }
1607
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001608 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1609
1610 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001611}
1612EXPORT_SYMBOL(xfrm_migrate_state_find);
1613
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001614struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001615 struct xfrm_migrate *m,
1616 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001617{
1618 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001619
Antony Antony4ab47d42017-06-06 12:12:13 +02001620 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001621 if (!xc)
1622 return NULL;
1623
1624 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1625 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1626
1627 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001628 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001629 /* a care is needed when the destination address of the
1630 state is to be updated as it is a part of triplet */
1631 xfrm_state_insert(xc);
1632 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001633 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001634 goto error;
1635 }
1636
1637 return xc;
1638error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001639 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001640 return NULL;
1641}
1642EXPORT_SYMBOL(xfrm_state_migrate);
1643#endif
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645int xfrm_state_update(struct xfrm_state *x)
1646{
David S. Miller37b08e32008-09-02 20:14:15 -07001647 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001649 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001650 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
David S. Miller37b08e32008-09-02 20:14:15 -07001652 to_put = NULL;
1653
Fan Du283bc9f2013-11-07 17:47:50 +08001654 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001655 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 err = -ESRCH;
1658 if (!x1)
1659 goto out;
1660
1661 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001662 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 err = -EEXIST;
1664 goto out;
1665 }
1666
1667 if (x1->km.state == XFRM_STATE_ACQ) {
1668 __xfrm_state_insert(x);
1669 x = NULL;
1670 }
1671 err = 0;
1672
1673out:
Fan Du283bc9f2013-11-07 17:47:50 +08001674 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
David S. Miller37b08e32008-09-02 20:14:15 -07001676 if (to_put)
1677 xfrm_state_put(to_put);
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (err)
1680 return err;
1681
1682 if (!x) {
1683 xfrm_state_delete(x1);
1684 xfrm_state_put(x1);
1685 return 0;
1686 }
1687
1688 err = -EINVAL;
1689 spin_lock_bh(&x1->lock);
1690 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001691 if (x->encap && x1->encap &&
1692 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001694 else if (x->encap || x1->encap)
1695 goto fail;
1696
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001697 if (x->coaddr && x1->coaddr) {
1698 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1699 }
1700 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1701 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1703 x1->km.dying = 0;
1704
Thomas Gleixner671422b2019-03-01 23:48:20 +01001705 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1706 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (x1->curlft.use_time)
1708 xfrm_state_check_expire(x1);
1709
Nathan Harold5baf4f92018-07-19 19:07:47 -07001710 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001711 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1712
Nathan Harold5baf4f92018-07-19 19:07:47 -07001713 if (x->props.smark.m || x->props.smark.v)
1714 x1->props.smark = x->props.smark;
1715
1716 if (x->if_id)
1717 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001718
1719 __xfrm_state_bump_genids(x1);
1720 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1721 }
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001724 x->km.state = XFRM_STATE_DEAD;
1725 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001727
1728fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 spin_unlock_bh(&x1->lock);
1730
1731 xfrm_state_put(x1);
1732
1733 return err;
1734}
1735EXPORT_SYMBOL(xfrm_state_update);
1736
1737int xfrm_state_check_expire(struct xfrm_state *x)
1738{
1739 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001740 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1743 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001744 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001745 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return -EINVAL;
1747 }
1748
1749 if (!x->km.dying &&
1750 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001751 x->curlft.packets >= x->lft.soft_packet_limit)) {
1752 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001753 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return 0;
1756}
1757EXPORT_SYMBOL(xfrm_state_check_expire);
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001760xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001761 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Florian Westphalc2f672f2016-09-20 15:45:26 +02001765 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001766 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001767 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return x;
1769}
1770EXPORT_SYMBOL(xfrm_state_lookup);
1771
1772struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001773xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001774 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001775 u8 proto, unsigned short family)
1776{
1777 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001778
Fan Du283bc9f2013-11-07 17:47:50 +08001779 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001780 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001781 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001782 return x;
1783}
1784EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1785
1786struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001787xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001788 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001789 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Fan Du283bc9f2013-11-07 17:47:50 +08001793 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001794 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001795 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return x;
1798}
1799EXPORT_SYMBOL(xfrm_find_acq);
1800
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001801#ifdef CONFIG_XFRM_SUB_POLICY
Florian Westphal3aaf3912019-05-03 17:46:17 +02001802#if IS_ENABLED(CONFIG_IPV6)
1803/* distribution counting sort function for xfrm_state and xfrm_tmpl */
1804static void
1805__xfrm6_sort(void **dst, void **src, int n,
1806 int (*cmp)(const void *p), int maxclass)
1807{
1808 int count[XFRM_MAX_DEPTH] = { };
1809 int class[XFRM_MAX_DEPTH];
1810 int i;
1811
1812 for (i = 0; i < n; i++) {
1813 int c = cmp(src[i]);
1814
1815 class[i] = c;
1816 count[c]++;
1817 }
1818
1819 for (i = 2; i < maxclass; i++)
1820 count[i] += count[i - 1];
1821
1822 for (i = 0; i < n; i++) {
1823 dst[count[class[i] - 1]++] = src[i];
1824 src[i] = NULL;
1825 }
1826}
1827
1828/* Rule for xfrm_state:
1829 *
1830 * rule 1: select IPsec transport except AH
1831 * rule 2: select MIPv6 RO or inbound trigger
1832 * rule 3: select IPsec transport AH
1833 * rule 4: select IPsec tunnel
1834 * rule 5: others
1835 */
1836static int __xfrm6_state_sort_cmp(const void *p)
1837{
1838 const struct xfrm_state *v = p;
1839
1840 switch (v->props.mode) {
1841 case XFRM_MODE_TRANSPORT:
1842 if (v->id.proto != IPPROTO_AH)
1843 return 1;
1844 else
1845 return 3;
1846#if IS_ENABLED(CONFIG_IPV6_MIP6)
1847 case XFRM_MODE_ROUTEOPTIMIZATION:
1848 case XFRM_MODE_IN_TRIGGER:
1849 return 2;
1850#endif
1851 case XFRM_MODE_TUNNEL:
1852 case XFRM_MODE_BEET:
1853 return 4;
1854 }
1855 return 5;
1856}
1857
1858/* Rule for xfrm_tmpl:
1859 *
1860 * rule 1: select IPsec transport
1861 * rule 2: select MIPv6 RO or inbound trigger
1862 * rule 3: select IPsec tunnel
1863 * rule 4: others
1864 */
1865static int __xfrm6_tmpl_sort_cmp(const void *p)
1866{
1867 const struct xfrm_tmpl *v = p;
1868
1869 switch (v->mode) {
1870 case XFRM_MODE_TRANSPORT:
1871 return 1;
1872#if IS_ENABLED(CONFIG_IPV6_MIP6)
1873 case XFRM_MODE_ROUTEOPTIMIZATION:
1874 case XFRM_MODE_IN_TRIGGER:
1875 return 2;
1876#endif
1877 case XFRM_MODE_TUNNEL:
1878 case XFRM_MODE_BEET:
1879 return 3;
1880 }
1881 return 4;
1882}
1883#else
1884static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1885static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1886
1887static inline void
1888__xfrm6_sort(void **dst, void **src, int n,
1889 int (*cmp)(const void *p), int maxclass)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001890{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001891 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001892
Florian Westphal3aaf3912019-05-03 17:46:17 +02001893 for (i = 0; i < n; i++)
1894 dst[i] = src[i];
1895}
1896#endif /* CONFIG_IPV6 */
1897
1898void
1899xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1900 unsigned short family)
1901{
1902 int i;
1903
1904 if (family == AF_INET6)
1905 __xfrm6_sort((void **)dst, (void **)src, n,
1906 __xfrm6_tmpl_sort_cmp, 5);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001907 else
1908 for (i = 0; i < n; i++)
1909 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001910}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001911
Florian Westphal3aaf3912019-05-03 17:46:17 +02001912void
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001913xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1914 unsigned short family)
1915{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001916 int i;
Fan Du283bc9f2013-11-07 17:47:50 +08001917
Florian Westphal3aaf3912019-05-03 17:46:17 +02001918 if (family == AF_INET6)
1919 __xfrm6_sort((void **)dst, (void **)src, n,
1920 __xfrm6_state_sort_cmp, 6);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001921 else
1922 for (i = 0; i < n; i++)
1923 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001924}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001925#endif
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/* Silly enough, but I'm lazy to build resolution list */
1928
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001929static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001933 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001934 struct xfrm_state *x;
1935
Sasha Levinb67bfe02013-02-27 17:06:00 -08001936 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001937 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001938 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001939 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 xfrm_state_hold(x);
1941 return x;
1942 }
1943 }
1944 }
1945 return NULL;
1946}
1947
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001948struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 struct xfrm_state *x;
1951
Fan Du283bc9f2013-11-07 17:47:50 +08001952 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001953 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001954 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return x;
1956}
1957EXPORT_SYMBOL(xfrm_find_acq_byseq);
1958
1959u32 xfrm_get_acqseq(void)
1960{
1961 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001962 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
jamal6836b9b2010-02-16 02:01:22 +00001964 do {
1965 res = atomic_inc_return(&acqseq);
1966 } while (!res);
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return res;
1969}
1970EXPORT_SYMBOL(xfrm_get_acqseq);
1971
Fan Du776e9dd2013-12-16 18:47:49 +08001972int verify_spi_info(u8 proto, u32 min, u32 max)
1973{
1974 switch (proto) {
1975 case IPPROTO_AH:
1976 case IPPROTO_ESP:
1977 break;
1978
1979 case IPPROTO_COMP:
1980 /* IPCOMP spi is 16-bits. */
1981 if (max >= 0x10000)
1982 return -EINVAL;
1983 break;
1984
1985 default:
1986 return -EINVAL;
1987 }
1988
1989 if (min > max)
1990 return -EINVAL;
1991
1992 return 0;
1993}
1994EXPORT_SYMBOL(verify_spi_info);
1995
Herbert Xu658b2192007-10-09 13:29:52 -07001996int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001998 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001999 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07002001 int err = -ENOENT;
2002 __be32 minspi = htonl(low);
2003 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002004 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Herbert Xu658b2192007-10-09 13:29:52 -07002006 spin_lock_bh(&x->lock);
2007 if (x->km.state == XFRM_STATE_DEAD)
2008 goto unlock;
2009
2010 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07002012 goto unlock;
2013
2014 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002017 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (x0) {
2019 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07002020 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 x->id.spi = minspi;
2023 } else {
2024 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08002025 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002026 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002027 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (x0 == NULL) {
2029 x->id.spi = htonl(spi);
2030 break;
2031 }
2032 xfrm_state_put(x0);
2033 }
2034 }
2035 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08002036 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08002037 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02002038 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08002039 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07002040
2041 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
Herbert Xu658b2192007-10-09 13:29:52 -07002043
2044unlock:
2045 spin_unlock_bh(&x->lock);
2046
2047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049EXPORT_SYMBOL(xfrm_alloc_spi);
2050
Nicolas Dichteld3623092014-02-14 15:30:36 +01002051static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002052 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01002053{
2054 if (filter) {
2055 if ((filter->family == AF_INET ||
2056 filter->family == AF_INET6) &&
2057 x->props.family != filter->family)
2058 return false;
2059
2060 return addr_match(&x->props.saddr, &filter->saddr,
2061 filter->splen) &&
2062 addr_match(&x->id.daddr, &filter->daddr,
2063 filter->dplen);
2064 }
2065 return true;
2066}
2067
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002068int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08002069 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 void *data)
2071{
Herbert Xu12a169e2008-10-01 07:03:24 -07002072 struct xfrm_state *state;
2073 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 int err = 0;
2075
Herbert Xu12a169e2008-10-01 07:03:24 -07002076 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08002077 return 0;
2078
Fan Du283bc9f2013-11-07 17:47:50 +08002079 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002080 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002081 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07002082 else
Li RongQing80077702015-04-22 17:09:54 +08002083 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002084 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07002085 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08002086 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002087 state = container_of(x, struct xfrm_state, km);
2088 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08002089 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002090 if (!__xfrm_state_filter_match(state, walk->filter))
2091 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002092 err = func(state, walk->seq, data);
2093 if (err) {
2094 list_move_tail(&walk->all, &x->all);
2095 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002097 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002099 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 err = -ENOENT;
2101 goto out;
2102 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002103 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104out:
Fan Du283bc9f2013-11-07 17:47:50 +08002105 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
2107}
2108EXPORT_SYMBOL(xfrm_state_walk);
2109
Nicolas Dichteld3623092014-02-14 15:30:36 +01002110void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002111 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07002112{
Herbert Xu12a169e2008-10-01 07:03:24 -07002113 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07002114 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07002115 walk->state = XFRM_STATE_DEAD;
2116 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002117 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07002118}
2119EXPORT_SYMBOL(xfrm_state_walk_init);
2120
Fan Du283bc9f2013-11-07 17:47:50 +08002121void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07002122{
Nicolas Dichteld3623092014-02-14 15:30:36 +01002123 kfree(walk->filter);
2124
Herbert Xu12a169e2008-10-01 07:03:24 -07002125 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07002126 return;
Herbert Xu5c182452008-09-22 19:48:19 -07002127
Fan Du283bc9f2013-11-07 17:47:50 +08002128 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002129 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08002130 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07002131}
2132EXPORT_SYMBOL(xfrm_state_walk_done);
2133
Kees Cooke99e88a2017-10-16 14:43:17 -07002134static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002135{
Kees Cooke99e88a2017-10-16 14:43:17 -07002136 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002137
2138 spin_lock(&x->lock);
2139
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002140 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002141 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002142 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002143 else
2144 x->xflags |= XFRM_TIME_DEFER;
2145 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002146
2147 spin_unlock(&x->lock);
2148}
2149
Denis Chengdf018122007-12-07 00:51:11 -08002150static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
David S. Miller214e0052011-02-24 00:02:38 -05002152void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct xfrm_mgr *km;
2155
Cong Wang85168c02013-01-16 16:05:06 +08002156 rcu_read_lock();
2157 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002158 if (km->notify_policy)
2159 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08002160 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002161}
2162
David S. Miller214e0052011-02-24 00:02:38 -05002163void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002164{
2165 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08002166 rcu_read_lock();
2167 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002168 if (km->notify)
2169 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08002170 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002171}
2172
2173EXPORT_SYMBOL(km_policy_notify);
2174EXPORT_SYMBOL(km_state_notify);
2175
Eric W. Biederman15e47302012-09-07 20:12:54 +00002176void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002177{
2178 struct km_event c;
2179
Herbert Xubf08867f92005-06-18 22:44:00 -07002180 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002181 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002182 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002183 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002186EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002187/*
2188 * We send to all registered managers regardless of failure
2189 * We are happy with one success
2190*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002191int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002193 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct xfrm_mgr *km;
2195
Cong Wang85168c02013-01-16 16:05:06 +08002196 rcu_read_lock();
2197 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08002198 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002199 if (!acqret)
2200 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
Cong Wang85168c02013-01-16 16:05:06 +08002202 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return err;
2204}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002205EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Al Viro5d36b182006-11-08 00:24:06 -08002207int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 int err = -EINVAL;
2210 struct xfrm_mgr *km;
2211
Cong Wang85168c02013-01-16 16:05:06 +08002212 rcu_read_lock();
2213 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (km->new_mapping)
2215 err = km->new_mapping(x, ipaddr, sport);
2216 if (!err)
2217 break;
2218 }
Cong Wang85168c02013-01-16 16:05:06 +08002219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return err;
2221}
2222EXPORT_SYMBOL(km_new_mapping);
2223
Eric W. Biederman15e47302012-09-07 20:12:54 +00002224void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002226 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Herbert Xubf08867f92005-06-18 22:44:00 -07002228 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002229 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002230 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002231 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
David S. Millera70fcb02006-03-20 19:18:52 -08002233EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002235#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002236int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2237 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002238 const struct xfrm_kmaddress *k,
2239 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002240{
2241 int err = -EINVAL;
2242 int ret;
2243 struct xfrm_mgr *km;
2244
Cong Wang85168c02013-01-16 16:05:06 +08002245 rcu_read_lock();
2246 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002247 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002248 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2249 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002250 if (!ret)
2251 err = ret;
2252 }
2253 }
Cong Wang85168c02013-01-16 16:05:06 +08002254 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002255 return err;
2256}
2257EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002258#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002259
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002260int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002261{
2262 int err = -EINVAL;
2263 int ret;
2264 struct xfrm_mgr *km;
2265
Cong Wang85168c02013-01-16 16:05:06 +08002266 rcu_read_lock();
2267 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002268 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002269 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002270 if (!ret)
2271 err = ret;
2272 }
2273 }
Cong Wang85168c02013-01-16 16:05:06 +08002274 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002275 return err;
2276}
2277EXPORT_SYMBOL(km_report);
2278
Florian Westphalbb9cd072019-04-17 11:45:13 +02002279static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002280{
2281 struct xfrm_mgr *km;
2282 bool is_alive = false;
2283
2284 rcu_read_lock();
2285 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2286 if (km->is_alive && km->is_alive(c)) {
2287 is_alive = true;
2288 break;
2289 }
2290 }
2291 rcu_read_unlock();
2292
2293 return is_alive;
2294}
Horia Geanta0f245582014-02-12 16:20:06 +02002295
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002296int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 int err;
2299 u8 *data;
2300 struct xfrm_mgr *km;
2301 struct xfrm_policy *pol = NULL;
2302
Steffen Klassert19d7df62018-02-01 08:49:23 +01002303 if (in_compat_syscall())
2304 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002305
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002306 if (sockptr_is_null(optval) && !optlen) {
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002307 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2308 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2309 __sk_dst_reset(sk);
2310 return 0;
2311 }
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (optlen <= 0 || optlen > PAGE_SIZE)
2314 return -EMSGSIZE;
2315
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002316 data = memdup_sockptr(optval, optlen);
Geliang Tanga133d932017-05-06 23:42:21 +08002317 if (IS_ERR(data))
2318 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002321 rcu_read_lock();
2322 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002323 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 optlen, &err);
2325 if (err >= 0)
2326 break;
2327 }
Cong Wang85168c02013-01-16 16:05:06 +08002328 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 if (err >= 0) {
2331 xfrm_sk_policy_insert(sk, err, pol);
2332 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002333 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 err = 0;
2335 }
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 kfree(data);
2338 return err;
2339}
2340EXPORT_SYMBOL(xfrm_user_policy);
2341
Cong Wang85168c02013-01-16 16:05:06 +08002342static DEFINE_SPINLOCK(xfrm_km_lock);
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344int xfrm_register_km(struct xfrm_mgr *km)
2345{
Cong Wang85168c02013-01-16 16:05:06 +08002346 spin_lock_bh(&xfrm_km_lock);
2347 list_add_tail_rcu(&km->list, &xfrm_km_list);
2348 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return 0;
2350}
2351EXPORT_SYMBOL(xfrm_register_km);
2352
2353int xfrm_unregister_km(struct xfrm_mgr *km)
2354{
Cong Wang85168c02013-01-16 16:05:06 +08002355 spin_lock_bh(&xfrm_km_lock);
2356 list_del_rcu(&km->list);
2357 spin_unlock_bh(&xfrm_km_lock);
2358 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 return 0;
2360}
2361EXPORT_SYMBOL(xfrm_unregister_km);
2362
2363int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2364{
2365 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002366
2367 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002369
Cong Wang44abdc32013-01-16 16:05:05 +08002370 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002372 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002373 else
Cong Wang44abdc32013-01-16 16:05:05 +08002374 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2375 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return err;
2377}
2378EXPORT_SYMBOL(xfrm_state_register_afinfo);
2379
2380int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2381{
Florian Westphal423826a2017-01-09 14:20:46 +01002382 int err = 0, family = afinfo->family;
2383
2384 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002386
Cong Wang44abdc32013-01-16 16:05:05 +08002387 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002389 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002391 else
Cong Wang44abdc32013-01-16 16:05:05 +08002392 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
Cong Wang44abdc32013-01-16 16:05:05 +08002394 spin_unlock_bh(&xfrm_state_afinfo_lock);
2395 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return err;
2397}
2398EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2399
Florian Westphal711059b2017-01-09 14:20:48 +01002400struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2401{
2402 if (unlikely(family >= NPROTO))
2403 return NULL;
2404
2405 return rcu_dereference(xfrm_state_afinfo[family]);
2406}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002407EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002408
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002409struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 struct xfrm_state_afinfo *afinfo;
2412 if (unlikely(family >= NPROTO))
2413 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002414 rcu_read_lock();
2415 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002416 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002417 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return afinfo;
2419}
2420
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002421void xfrm_flush_gc(void)
2422{
2423 flush_work(&xfrm_state_gc_work);
2424}
2425EXPORT_SYMBOL(xfrm_flush_gc);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2428void xfrm_state_delete_tunnel(struct xfrm_state *x)
2429{
2430 if (x->tunnel) {
2431 struct xfrm_state *t = x->tunnel;
2432
2433 if (atomic_read(&t->tunnel_users) == 2)
2434 xfrm_state_delete(t);
2435 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002436 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 x->tunnel = NULL;
2438 }
2439}
2440EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2441
Florian Westphalc7b37c72019-06-24 22:04:48 +02002442u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002444 const struct xfrm_type *type = READ_ONCE(x->type);
Florian Westphalc7b37c72019-06-24 22:04:48 +02002445 struct crypto_aead *aead;
2446 u32 blksize, net_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Florian Westphalc7b37c72019-06-24 22:04:48 +02002448 if (x->km.state != XFRM_STATE_VALID ||
2449 !type || type->proto != IPPROTO_ESP)
2450 return mtu - x->props.header_len;
Florian Westphalb3b73b82017-01-05 13:23:58 +01002451
Florian Westphalc7b37c72019-06-24 22:04:48 +02002452 aead = x->data;
2453 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2454
2455 switch (x->props.mode) {
2456 case XFRM_MODE_TRANSPORT:
2457 case XFRM_MODE_BEET:
2458 if (x->props.family == AF_INET)
2459 net_adj = sizeof(struct iphdr);
2460 else if (x->props.family == AF_INET6)
2461 net_adj = sizeof(struct ipv6hdr);
2462 break;
2463 case XFRM_MODE_TUNNEL:
2464 break;
2465 default:
2466 WARN_ON_ONCE(1);
2467 break;
2468 }
2469
2470 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2471 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
Florian Westphalc7b37c72019-06-24 22:04:48 +02002473EXPORT_SYMBOL_GPL(xfrm_state_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Ilan Tayariffdb5212017-08-01 12:49:08 +03002475int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002476{
Florian Westphalc9500d72019-03-29 21:16:32 +01002477 const struct xfrm_mode *inner_mode;
2478 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002479 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002480 int err;
2481
Florian Westphale4681742019-05-03 17:46:16 +02002482 if (family == AF_INET &&
2483 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2484 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Herbert Xud094cd82005-06-20 13:19:41 -07002485
2486 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002487
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002488 if (x->sel.family != AF_UNSPEC) {
2489 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2490 if (inner_mode == NULL)
2491 goto error;
2492
2493 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002494 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002495 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002496
Florian Westphalc9500d72019-03-29 21:16:32 +01002497 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002498 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002499 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002500 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002501
Martin Willid81d2282008-12-03 15:38:07 -08002502 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002503 if (inner_mode == NULL)
2504 goto error;
2505
Florian Westphal4c145dc2019-03-29 21:16:31 +01002506 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002507 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002508
Florian Westphalc9500d72019-03-29 21:16:32 +01002509 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002510
Martin Willid81d2282008-12-03 15:38:07 -08002511 if (x->props.family == AF_INET)
2512 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002513
Martin Willid81d2282008-12-03 15:38:07 -08002514 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2515 if (inner_mode_iaf) {
2516 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002517 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002518 }
2519 }
Herbert Xu13996372007-10-17 21:35:51 -07002520
Herbert Xud094cd82005-06-20 13:19:41 -07002521 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002522 if (x->type == NULL)
2523 goto error;
2524
Ilan Tayariffdb5212017-08-01 12:49:08 +03002525 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002526
Herbert Xu72cb6962005-06-20 13:18:08 -07002527 err = x->type->init_state(x);
2528 if (err)
2529 goto error;
2530
Florian Westphalc9500d72019-03-29 21:16:32 +01002531 outer_mode = xfrm_get_mode(x->props.mode, family);
2532 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002533 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002534 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002535 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002536
Florian Westphalc9500d72019-03-29 21:16:32 +01002537 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002538 if (init_replay) {
2539 err = xfrm_init_replay(x);
2540 if (err)
2541 goto error;
2542 }
2543
Herbert Xu72cb6962005-06-20 13:18:08 -07002544error:
2545 return err;
2546}
2547
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002548EXPORT_SYMBOL(__xfrm_init_state);
2549
2550int xfrm_init_state(struct xfrm_state *x)
2551{
Yossi Kupermancc015722018-01-17 15:52:41 +02002552 int err;
2553
2554 err = __xfrm_init_state(x, true, false);
2555 if (!err)
2556 x->km.state = XFRM_STATE_VALID;
2557
2558 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002559}
2560
Herbert Xu72cb6962005-06-20 13:18:08 -07002561EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002562
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002563int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
David S. Millerf034b5d2006-08-24 03:08:07 -07002565 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Mathias Krause565f0fa2018-05-03 10:55:07 +02002567 if (net_eq(net, &init_net))
2568 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2569 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2570
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002571 INIT_LIST_HEAD(&net->xfrm.state_all);
2572
David S. Millerf034b5d2006-08-24 03:08:07 -07002573 sz = sizeof(struct hlist_head) * 8;
2574
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002575 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2576 if (!net->xfrm.state_bydst)
2577 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002578 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2579 if (!net->xfrm.state_bysrc)
2580 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002581 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2582 if (!net->xfrm.state_byspi)
2583 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002584 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002585
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002586 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002587 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002588 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002589 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002590
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002591out_byspi:
2592 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002593out_bysrc:
2594 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002595out_bydst:
2596 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002597}
2598
2599void xfrm_state_fini(struct net *net)
2600{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002601 unsigned int sz;
2602
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002603 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002604 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002605 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002606
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002607 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002608
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002609 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002610 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2611 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002612 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2613 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002614 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2615 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Joy Lattenab5f5e82007-09-17 11:51:22 -07002618#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002619static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2620 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002621{
Paul Moore68277ac2007-12-20 20:49:33 -08002622 struct xfrm_sec_ctx *ctx = x->security;
2623 u32 spi = ntohl(x->id.spi);
2624
2625 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002626 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002627 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002628
Weilong Chen9b7a7872013-12-24 09:43:46 +08002629 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002630 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002631 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2632 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002633 break;
2634 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002635 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002636 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002637 break;
2638 }
Paul Moore68277ac2007-12-20 20:49:33 -08002639
2640 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002641}
2642
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002643static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2644 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002645{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002646 const struct iphdr *iph4;
2647 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002648
2649 switch (family) {
2650 case AF_INET:
2651 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002652 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2653 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002654 break;
2655 case AF_INET6:
2656 iph6 = ipv6_hdr(skb);
2657 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002658 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002659 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002660 iph6->flow_lbl[0] & 0x0f,
2661 iph6->flow_lbl[1],
2662 iph6->flow_lbl[2]);
2663 break;
2664 }
2665}
2666
Tetsuo Handa2e710292014-04-22 21:48:30 +09002667void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002668{
2669 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002670
Paul Mooreafeb14b2007-12-21 14:58:11 -08002671 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002672 if (audit_buf == NULL)
2673 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002674 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002675 xfrm_audit_helper_sainfo(x, audit_buf);
2676 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002677 audit_log_end(audit_buf);
2678}
2679EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2680
Tetsuo Handa2e710292014-04-22 21:48:30 +09002681void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002682{
2683 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002684
Paul Mooreafeb14b2007-12-21 14:58:11 -08002685 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002686 if (audit_buf == NULL)
2687 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002688 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002689 xfrm_audit_helper_sainfo(x, audit_buf);
2690 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002691 audit_log_end(audit_buf);
2692}
2693EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002694
2695void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2696 struct sk_buff *skb)
2697{
2698 struct audit_buffer *audit_buf;
2699 u32 spi;
2700
2701 audit_buf = xfrm_audit_start("SA-replay-overflow");
2702 if (audit_buf == NULL)
2703 return;
2704 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2705 /* don't record the sequence number because it's inherent in this kind
2706 * of audit message */
2707 spi = ntohl(x->id.spi);
2708 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2709 audit_log_end(audit_buf);
2710}
2711EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2712
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002713void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002714 struct sk_buff *skb, __be32 net_seq)
2715{
2716 struct audit_buffer *audit_buf;
2717 u32 spi;
2718
2719 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2720 if (audit_buf == NULL)
2721 return;
2722 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2723 spi = ntohl(x->id.spi);
2724 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2725 spi, spi, ntohl(net_seq));
2726 audit_log_end(audit_buf);
2727}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002728EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002729
2730void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2731{
2732 struct audit_buffer *audit_buf;
2733
2734 audit_buf = xfrm_audit_start("SA-notfound");
2735 if (audit_buf == NULL)
2736 return;
2737 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2738 audit_log_end(audit_buf);
2739}
2740EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2741
2742void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2743 __be32 net_spi, __be32 net_seq)
2744{
2745 struct audit_buffer *audit_buf;
2746 u32 spi;
2747
2748 audit_buf = xfrm_audit_start("SA-notfound");
2749 if (audit_buf == NULL)
2750 return;
2751 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2752 spi = ntohl(net_spi);
2753 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2754 spi, spi, ntohl(net_seq));
2755 audit_log_end(audit_buf);
2756}
2757EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2758
2759void xfrm_audit_state_icvfail(struct xfrm_state *x,
2760 struct sk_buff *skb, u8 proto)
2761{
2762 struct audit_buffer *audit_buf;
2763 __be32 net_spi;
2764 __be32 net_seq;
2765
2766 audit_buf = xfrm_audit_start("SA-icv-failure");
2767 if (audit_buf == NULL)
2768 return;
2769 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2770 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2771 u32 spi = ntohl(net_spi);
2772 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2773 spi, spi, ntohl(net_seq));
2774 }
2775 audit_log_end(audit_buf);
2776}
2777EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002778#endif /* CONFIG_AUDITSYSCALL */