blob: 3a000f289dcd35d8d7113ad264cdaf6d9528744c [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 Antony4ab47d42017-06-06 12:12:13 +02001444static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1445 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001446{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001447 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001448 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001449 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001450 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001451
1452 memcpy(&x->id, &orig->id, sizeof(x->id));
1453 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1454 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1455 x->props.mode = orig->props.mode;
1456 x->props.replay_window = orig->props.replay_window;
1457 x->props.reqid = orig->props.reqid;
1458 x->props.family = orig->props.family;
1459 x->props.saddr = orig->props.saddr;
1460
1461 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001462 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001463 if (!x->aalg)
1464 goto error;
1465 }
1466 x->props.aalgo = orig->props.aalgo;
1467
Steffen Klassertee5c2312014-02-19 13:33:24 +01001468 if (orig->aead) {
1469 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001470 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001471 if (!x->aead)
1472 goto error;
1473 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001474 if (orig->ealg) {
1475 x->ealg = xfrm_algo_clone(orig->ealg);
1476 if (!x->ealg)
1477 goto error;
1478 }
1479 x->props.ealgo = orig->props.ealgo;
1480
1481 if (orig->calg) {
1482 x->calg = xfrm_algo_clone(orig->calg);
1483 if (!x->calg)
1484 goto error;
1485 }
1486 x->props.calgo = orig->props.calgo;
1487
Antony Antony4ab47d42017-06-06 12:12:13 +02001488 if (encap || orig->encap) {
1489 if (encap)
1490 x->encap = kmemdup(encap, sizeof(*x->encap),
1491 GFP_KERNEL);
1492 else
1493 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1494 GFP_KERNEL);
1495
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001496 if (!x->encap)
1497 goto error;
1498 }
1499
1500 if (orig->coaddr) {
1501 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1502 GFP_KERNEL);
1503 if (!x->coaddr)
1504 goto error;
1505 }
1506
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001507 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001508 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001509 goto error;
1510 }
1511
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001512 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
Antony Antony545e5c52020-09-04 08:49:38 +02001513 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001514
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001515 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001516 goto error;
1517
1518 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001519 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001520
Steffen Klassert7e652642018-06-12 14:07:07 +02001521 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001522 x->tfcpad = orig->tfcpad;
1523 x->replay_maxdiff = orig->replay_maxdiff;
1524 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001525 x->curlft.add_time = orig->curlft.add_time;
1526 x->km.state = orig->km.state;
1527 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001528 x->replay = orig->replay;
1529 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001530
1531 return x;
1532
1533 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001534 xfrm_state_put(x);
1535out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001536 return NULL;
1537}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001538
Fan Du283bc9f2013-11-07 17:47:50 +08001539struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001540{
1541 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001542 struct xfrm_state *x = NULL;
1543
1544 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001545
1546 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001547 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001548 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001549 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001550 if (x->props.mode != m->mode ||
1551 x->id.proto != m->proto)
1552 continue;
1553 if (m->reqid && x->props.reqid != m->reqid)
1554 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001555 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1556 m->old_family) ||
1557 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1558 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001559 continue;
1560 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001561 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001562 }
1563 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001564 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001565 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001566 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001567 if (x->props.mode != m->mode ||
1568 x->id.proto != m->proto)
1569 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001570 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1571 m->old_family) ||
1572 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1573 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001574 continue;
1575 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001576 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001577 }
1578 }
1579
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001580 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1581
1582 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001583}
1584EXPORT_SYMBOL(xfrm_migrate_state_find);
1585
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001586struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001587 struct xfrm_migrate *m,
1588 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001589{
1590 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001591
Antony Antony4ab47d42017-06-06 12:12:13 +02001592 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001593 if (!xc)
1594 return NULL;
1595
1596 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1597 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1598
1599 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001600 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001601 /* a care is needed when the destination address of the
1602 state is to be updated as it is a part of triplet */
1603 xfrm_state_insert(xc);
1604 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001605 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001606 goto error;
1607 }
1608
1609 return xc;
1610error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001611 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001612 return NULL;
1613}
1614EXPORT_SYMBOL(xfrm_state_migrate);
1615#endif
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617int xfrm_state_update(struct xfrm_state *x)
1618{
David S. Miller37b08e32008-09-02 20:14:15 -07001619 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001621 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001622 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
David S. Miller37b08e32008-09-02 20:14:15 -07001624 to_put = NULL;
1625
Fan Du283bc9f2013-11-07 17:47:50 +08001626 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001627 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 err = -ESRCH;
1630 if (!x1)
1631 goto out;
1632
1633 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001634 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 err = -EEXIST;
1636 goto out;
1637 }
1638
1639 if (x1->km.state == XFRM_STATE_ACQ) {
1640 __xfrm_state_insert(x);
1641 x = NULL;
1642 }
1643 err = 0;
1644
1645out:
Fan Du283bc9f2013-11-07 17:47:50 +08001646 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
David S. Miller37b08e32008-09-02 20:14:15 -07001648 if (to_put)
1649 xfrm_state_put(to_put);
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (err)
1652 return err;
1653
1654 if (!x) {
1655 xfrm_state_delete(x1);
1656 xfrm_state_put(x1);
1657 return 0;
1658 }
1659
1660 err = -EINVAL;
1661 spin_lock_bh(&x1->lock);
1662 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001663 if (x->encap && x1->encap &&
1664 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001666 else if (x->encap || x1->encap)
1667 goto fail;
1668
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001669 if (x->coaddr && x1->coaddr) {
1670 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1671 }
1672 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1673 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1675 x1->km.dying = 0;
1676
Thomas Gleixner671422b2019-03-01 23:48:20 +01001677 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1678 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (x1->curlft.use_time)
1680 xfrm_state_check_expire(x1);
1681
Nathan Harold5baf4f92018-07-19 19:07:47 -07001682 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001683 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1684
Nathan Harold5baf4f92018-07-19 19:07:47 -07001685 if (x->props.smark.m || x->props.smark.v)
1686 x1->props.smark = x->props.smark;
1687
1688 if (x->if_id)
1689 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001690
1691 __xfrm_state_bump_genids(x1);
1692 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1693 }
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001696 x->km.state = XFRM_STATE_DEAD;
1697 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001699
1700fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 spin_unlock_bh(&x1->lock);
1702
1703 xfrm_state_put(x1);
1704
1705 return err;
1706}
1707EXPORT_SYMBOL(xfrm_state_update);
1708
1709int xfrm_state_check_expire(struct xfrm_state *x)
1710{
1711 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001712 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1715 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001716 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001717 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return -EINVAL;
1719 }
1720
1721 if (!x->km.dying &&
1722 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001723 x->curlft.packets >= x->lft.soft_packet_limit)) {
1724 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001725 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return 0;
1728}
1729EXPORT_SYMBOL(xfrm_state_check_expire);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001732xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001733 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Florian Westphalc2f672f2016-09-20 15:45:26 +02001737 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001738 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001739 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return x;
1741}
1742EXPORT_SYMBOL(xfrm_state_lookup);
1743
1744struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001745xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001746 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001747 u8 proto, unsigned short family)
1748{
1749 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001750
Fan Du283bc9f2013-11-07 17:47:50 +08001751 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001752 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001753 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001754 return x;
1755}
1756EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1757
1758struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001759xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001760 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001761 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
1763 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Fan Du283bc9f2013-11-07 17:47:50 +08001765 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001766 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001767 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return x;
1770}
1771EXPORT_SYMBOL(xfrm_find_acq);
1772
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001773#ifdef CONFIG_XFRM_SUB_POLICY
Florian Westphal3aaf3912019-05-03 17:46:17 +02001774#if IS_ENABLED(CONFIG_IPV6)
1775/* distribution counting sort function for xfrm_state and xfrm_tmpl */
1776static void
1777__xfrm6_sort(void **dst, void **src, int n,
1778 int (*cmp)(const void *p), int maxclass)
1779{
1780 int count[XFRM_MAX_DEPTH] = { };
1781 int class[XFRM_MAX_DEPTH];
1782 int i;
1783
1784 for (i = 0; i < n; i++) {
1785 int c = cmp(src[i]);
1786
1787 class[i] = c;
1788 count[c]++;
1789 }
1790
1791 for (i = 2; i < maxclass; i++)
1792 count[i] += count[i - 1];
1793
1794 for (i = 0; i < n; i++) {
1795 dst[count[class[i] - 1]++] = src[i];
1796 src[i] = NULL;
1797 }
1798}
1799
1800/* Rule for xfrm_state:
1801 *
1802 * rule 1: select IPsec transport except AH
1803 * rule 2: select MIPv6 RO or inbound trigger
1804 * rule 3: select IPsec transport AH
1805 * rule 4: select IPsec tunnel
1806 * rule 5: others
1807 */
1808static int __xfrm6_state_sort_cmp(const void *p)
1809{
1810 const struct xfrm_state *v = p;
1811
1812 switch (v->props.mode) {
1813 case XFRM_MODE_TRANSPORT:
1814 if (v->id.proto != IPPROTO_AH)
1815 return 1;
1816 else
1817 return 3;
1818#if IS_ENABLED(CONFIG_IPV6_MIP6)
1819 case XFRM_MODE_ROUTEOPTIMIZATION:
1820 case XFRM_MODE_IN_TRIGGER:
1821 return 2;
1822#endif
1823 case XFRM_MODE_TUNNEL:
1824 case XFRM_MODE_BEET:
1825 return 4;
1826 }
1827 return 5;
1828}
1829
1830/* Rule for xfrm_tmpl:
1831 *
1832 * rule 1: select IPsec transport
1833 * rule 2: select MIPv6 RO or inbound trigger
1834 * rule 3: select IPsec tunnel
1835 * rule 4: others
1836 */
1837static int __xfrm6_tmpl_sort_cmp(const void *p)
1838{
1839 const struct xfrm_tmpl *v = p;
1840
1841 switch (v->mode) {
1842 case XFRM_MODE_TRANSPORT:
1843 return 1;
1844#if IS_ENABLED(CONFIG_IPV6_MIP6)
1845 case XFRM_MODE_ROUTEOPTIMIZATION:
1846 case XFRM_MODE_IN_TRIGGER:
1847 return 2;
1848#endif
1849 case XFRM_MODE_TUNNEL:
1850 case XFRM_MODE_BEET:
1851 return 3;
1852 }
1853 return 4;
1854}
1855#else
1856static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1857static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1858
1859static inline void
1860__xfrm6_sort(void **dst, void **src, int n,
1861 int (*cmp)(const void *p), int maxclass)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001862{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001863 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001864
Florian Westphal3aaf3912019-05-03 17:46:17 +02001865 for (i = 0; i < n; i++)
1866 dst[i] = src[i];
1867}
1868#endif /* CONFIG_IPV6 */
1869
1870void
1871xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1872 unsigned short family)
1873{
1874 int i;
1875
1876 if (family == AF_INET6)
1877 __xfrm6_sort((void **)dst, (void **)src, n,
1878 __xfrm6_tmpl_sort_cmp, 5);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001879 else
1880 for (i = 0; i < n; i++)
1881 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001882}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001883
Florian Westphal3aaf3912019-05-03 17:46:17 +02001884void
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001885xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1886 unsigned short family)
1887{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001888 int i;
Fan Du283bc9f2013-11-07 17:47:50 +08001889
Florian Westphal3aaf3912019-05-03 17:46:17 +02001890 if (family == AF_INET6)
1891 __xfrm6_sort((void **)dst, (void **)src, n,
1892 __xfrm6_state_sort_cmp, 6);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001893 else
1894 for (i = 0; i < n; i++)
1895 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001896}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001897#endif
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899/* Silly enough, but I'm lazy to build resolution list */
1900
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001901static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001905 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001906 struct xfrm_state *x;
1907
Sasha Levinb67bfe02013-02-27 17:06:00 -08001908 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001909 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001910 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001911 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 xfrm_state_hold(x);
1913 return x;
1914 }
1915 }
1916 }
1917 return NULL;
1918}
1919
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001920struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 struct xfrm_state *x;
1923
Fan Du283bc9f2013-11-07 17:47:50 +08001924 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001925 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001926 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return x;
1928}
1929EXPORT_SYMBOL(xfrm_find_acq_byseq);
1930
1931u32 xfrm_get_acqseq(void)
1932{
1933 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001934 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
jamal6836b9b2010-02-16 02:01:22 +00001936 do {
1937 res = atomic_inc_return(&acqseq);
1938 } while (!res);
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return res;
1941}
1942EXPORT_SYMBOL(xfrm_get_acqseq);
1943
Fan Du776e9dd2013-12-16 18:47:49 +08001944int verify_spi_info(u8 proto, u32 min, u32 max)
1945{
1946 switch (proto) {
1947 case IPPROTO_AH:
1948 case IPPROTO_ESP:
1949 break;
1950
1951 case IPPROTO_COMP:
1952 /* IPCOMP spi is 16-bits. */
1953 if (max >= 0x10000)
1954 return -EINVAL;
1955 break;
1956
1957 default:
1958 return -EINVAL;
1959 }
1960
1961 if (min > max)
1962 return -EINVAL;
1963
1964 return 0;
1965}
1966EXPORT_SYMBOL(verify_spi_info);
1967
Herbert Xu658b2192007-10-09 13:29:52 -07001968int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001970 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001971 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001973 int err = -ENOENT;
1974 __be32 minspi = htonl(low);
1975 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001976 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Herbert Xu658b2192007-10-09 13:29:52 -07001978 spin_lock_bh(&x->lock);
1979 if (x->km.state == XFRM_STATE_DEAD)
1980 goto unlock;
1981
1982 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001984 goto unlock;
1985
1986 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001989 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (x0) {
1991 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001992 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 x->id.spi = minspi;
1995 } else {
1996 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001997 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001998 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001999 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (x0 == NULL) {
2001 x->id.spi = htonl(spi);
2002 break;
2003 }
2004 xfrm_state_put(x0);
2005 }
2006 }
2007 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08002008 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08002009 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02002010 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08002011 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07002012
2013 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Herbert Xu658b2192007-10-09 13:29:52 -07002015
2016unlock:
2017 spin_unlock_bh(&x->lock);
2018
2019 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021EXPORT_SYMBOL(xfrm_alloc_spi);
2022
Nicolas Dichteld3623092014-02-14 15:30:36 +01002023static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002024 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01002025{
2026 if (filter) {
2027 if ((filter->family == AF_INET ||
2028 filter->family == AF_INET6) &&
2029 x->props.family != filter->family)
2030 return false;
2031
2032 return addr_match(&x->props.saddr, &filter->saddr,
2033 filter->splen) &&
2034 addr_match(&x->id.daddr, &filter->daddr,
2035 filter->dplen);
2036 }
2037 return true;
2038}
2039
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002040int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08002041 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 void *data)
2043{
Herbert Xu12a169e2008-10-01 07:03:24 -07002044 struct xfrm_state *state;
2045 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int err = 0;
2047
Herbert Xu12a169e2008-10-01 07:03:24 -07002048 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08002049 return 0;
2050
Fan Du283bc9f2013-11-07 17:47:50 +08002051 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002052 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002053 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07002054 else
Li RongQing80077702015-04-22 17:09:54 +08002055 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002056 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07002057 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08002058 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002059 state = container_of(x, struct xfrm_state, km);
2060 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08002061 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002062 if (!__xfrm_state_filter_match(state, walk->filter))
2063 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002064 err = func(state, walk->seq, data);
2065 if (err) {
2066 list_move_tail(&walk->all, &x->all);
2067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002069 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002071 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 err = -ENOENT;
2073 goto out;
2074 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002075 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076out:
Fan Du283bc9f2013-11-07 17:47:50 +08002077 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return err;
2079}
2080EXPORT_SYMBOL(xfrm_state_walk);
2081
Nicolas Dichteld3623092014-02-14 15:30:36 +01002082void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002083 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07002084{
Herbert Xu12a169e2008-10-01 07:03:24 -07002085 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07002086 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07002087 walk->state = XFRM_STATE_DEAD;
2088 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002089 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07002090}
2091EXPORT_SYMBOL(xfrm_state_walk_init);
2092
Fan Du283bc9f2013-11-07 17:47:50 +08002093void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07002094{
Nicolas Dichteld3623092014-02-14 15:30:36 +01002095 kfree(walk->filter);
2096
Herbert Xu12a169e2008-10-01 07:03:24 -07002097 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07002098 return;
Herbert Xu5c182452008-09-22 19:48:19 -07002099
Fan Du283bc9f2013-11-07 17:47:50 +08002100 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002101 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08002102 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07002103}
2104EXPORT_SYMBOL(xfrm_state_walk_done);
2105
Kees Cooke99e88a2017-10-16 14:43:17 -07002106static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002107{
Kees Cooke99e88a2017-10-16 14:43:17 -07002108 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002109
2110 spin_lock(&x->lock);
2111
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002112 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002113 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002114 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002115 else
2116 x->xflags |= XFRM_TIME_DEFER;
2117 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002118
2119 spin_unlock(&x->lock);
2120}
2121
Denis Chengdf018122007-12-07 00:51:11 -08002122static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
David S. Miller214e0052011-02-24 00:02:38 -05002124void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 struct xfrm_mgr *km;
2127
Cong Wang85168c02013-01-16 16:05:06 +08002128 rcu_read_lock();
2129 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002130 if (km->notify_policy)
2131 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08002132 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002133}
2134
David S. Miller214e0052011-02-24 00:02:38 -05002135void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002136{
2137 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08002138 rcu_read_lock();
2139 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002140 if (km->notify)
2141 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08002142 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002143}
2144
2145EXPORT_SYMBOL(km_policy_notify);
2146EXPORT_SYMBOL(km_state_notify);
2147
Eric W. Biederman15e47302012-09-07 20:12:54 +00002148void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002149{
2150 struct km_event c;
2151
Herbert Xubf08867f92005-06-18 22:44:00 -07002152 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002153 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002154 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002155 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002158EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002159/*
2160 * We send to all registered managers regardless of failure
2161 * We are happy with one success
2162*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002163int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002165 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct xfrm_mgr *km;
2167
Cong Wang85168c02013-01-16 16:05:06 +08002168 rcu_read_lock();
2169 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08002170 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002171 if (!acqret)
2172 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Cong Wang85168c02013-01-16 16:05:06 +08002174 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return err;
2176}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002177EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Al Viro5d36b182006-11-08 00:24:06 -08002179int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 int err = -EINVAL;
2182 struct xfrm_mgr *km;
2183
Cong Wang85168c02013-01-16 16:05:06 +08002184 rcu_read_lock();
2185 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (km->new_mapping)
2187 err = km->new_mapping(x, ipaddr, sport);
2188 if (!err)
2189 break;
2190 }
Cong Wang85168c02013-01-16 16:05:06 +08002191 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return err;
2193}
2194EXPORT_SYMBOL(km_new_mapping);
2195
Eric W. Biederman15e47302012-09-07 20:12:54 +00002196void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002198 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Herbert Xubf08867f92005-06-18 22:44:00 -07002200 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002201 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002202 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002203 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
David S. Millera70fcb02006-03-20 19:18:52 -08002205EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002207#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002208int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2209 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002210 const struct xfrm_kmaddress *k,
2211 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002212{
2213 int err = -EINVAL;
2214 int ret;
2215 struct xfrm_mgr *km;
2216
Cong Wang85168c02013-01-16 16:05:06 +08002217 rcu_read_lock();
2218 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002219 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002220 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2221 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002222 if (!ret)
2223 err = ret;
2224 }
2225 }
Cong Wang85168c02013-01-16 16:05:06 +08002226 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002227 return err;
2228}
2229EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002230#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002231
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002232int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002233{
2234 int err = -EINVAL;
2235 int ret;
2236 struct xfrm_mgr *km;
2237
Cong Wang85168c02013-01-16 16:05:06 +08002238 rcu_read_lock();
2239 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002240 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002241 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002242 if (!ret)
2243 err = ret;
2244 }
2245 }
Cong Wang85168c02013-01-16 16:05:06 +08002246 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002247 return err;
2248}
2249EXPORT_SYMBOL(km_report);
2250
Florian Westphalbb9cd072019-04-17 11:45:13 +02002251static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002252{
2253 struct xfrm_mgr *km;
2254 bool is_alive = false;
2255
2256 rcu_read_lock();
2257 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2258 if (km->is_alive && km->is_alive(c)) {
2259 is_alive = true;
2260 break;
2261 }
2262 }
2263 rcu_read_unlock();
2264
2265 return is_alive;
2266}
Horia Geanta0f245582014-02-12 16:20:06 +02002267
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002268int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 int err;
2271 u8 *data;
2272 struct xfrm_mgr *km;
2273 struct xfrm_policy *pol = NULL;
2274
Steffen Klassert19d7df62018-02-01 08:49:23 +01002275 if (in_compat_syscall())
2276 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002277
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002278 if (sockptr_is_null(optval) && !optlen) {
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002279 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2280 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2281 __sk_dst_reset(sk);
2282 return 0;
2283 }
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (optlen <= 0 || optlen > PAGE_SIZE)
2286 return -EMSGSIZE;
2287
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002288 data = memdup_sockptr(optval, optlen);
Geliang Tanga133d932017-05-06 23:42:21 +08002289 if (IS_ERR(data))
2290 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002293 rcu_read_lock();
2294 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002295 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 optlen, &err);
2297 if (err >= 0)
2298 break;
2299 }
Cong Wang85168c02013-01-16 16:05:06 +08002300 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 if (err >= 0) {
2303 xfrm_sk_policy_insert(sk, err, pol);
2304 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002305 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 err = 0;
2307 }
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 kfree(data);
2310 return err;
2311}
2312EXPORT_SYMBOL(xfrm_user_policy);
2313
Cong Wang85168c02013-01-16 16:05:06 +08002314static DEFINE_SPINLOCK(xfrm_km_lock);
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316int xfrm_register_km(struct xfrm_mgr *km)
2317{
Cong Wang85168c02013-01-16 16:05:06 +08002318 spin_lock_bh(&xfrm_km_lock);
2319 list_add_tail_rcu(&km->list, &xfrm_km_list);
2320 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return 0;
2322}
2323EXPORT_SYMBOL(xfrm_register_km);
2324
2325int xfrm_unregister_km(struct xfrm_mgr *km)
2326{
Cong Wang85168c02013-01-16 16:05:06 +08002327 spin_lock_bh(&xfrm_km_lock);
2328 list_del_rcu(&km->list);
2329 spin_unlock_bh(&xfrm_km_lock);
2330 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
2332}
2333EXPORT_SYMBOL(xfrm_unregister_km);
2334
2335int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2336{
2337 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002338
2339 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002341
Cong Wang44abdc32013-01-16 16:05:05 +08002342 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002344 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002345 else
Cong Wang44abdc32013-01-16 16:05:05 +08002346 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2347 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return err;
2349}
2350EXPORT_SYMBOL(xfrm_state_register_afinfo);
2351
2352int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2353{
Florian Westphal423826a2017-01-09 14:20:46 +01002354 int err = 0, family = afinfo->family;
2355
2356 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002358
Cong Wang44abdc32013-01-16 16:05:05 +08002359 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002361 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002363 else
Cong Wang44abdc32013-01-16 16:05:05 +08002364 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
Cong Wang44abdc32013-01-16 16:05:05 +08002366 spin_unlock_bh(&xfrm_state_afinfo_lock);
2367 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return err;
2369}
2370EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2371
Florian Westphal711059b2017-01-09 14:20:48 +01002372struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2373{
2374 if (unlikely(family >= NPROTO))
2375 return NULL;
2376
2377 return rcu_dereference(xfrm_state_afinfo[family]);
2378}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002379EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002380
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002381struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 struct xfrm_state_afinfo *afinfo;
2384 if (unlikely(family >= NPROTO))
2385 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002386 rcu_read_lock();
2387 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002388 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002389 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return afinfo;
2391}
2392
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002393void xfrm_flush_gc(void)
2394{
2395 flush_work(&xfrm_state_gc_work);
2396}
2397EXPORT_SYMBOL(xfrm_flush_gc);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2400void xfrm_state_delete_tunnel(struct xfrm_state *x)
2401{
2402 if (x->tunnel) {
2403 struct xfrm_state *t = x->tunnel;
2404
2405 if (atomic_read(&t->tunnel_users) == 2)
2406 xfrm_state_delete(t);
2407 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002408 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 x->tunnel = NULL;
2410 }
2411}
2412EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2413
Florian Westphalc7b37c72019-06-24 22:04:48 +02002414u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002416 const struct xfrm_type *type = READ_ONCE(x->type);
Florian Westphalc7b37c72019-06-24 22:04:48 +02002417 struct crypto_aead *aead;
2418 u32 blksize, net_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Florian Westphalc7b37c72019-06-24 22:04:48 +02002420 if (x->km.state != XFRM_STATE_VALID ||
2421 !type || type->proto != IPPROTO_ESP)
2422 return mtu - x->props.header_len;
Florian Westphalb3b73b82017-01-05 13:23:58 +01002423
Florian Westphalc7b37c72019-06-24 22:04:48 +02002424 aead = x->data;
2425 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2426
2427 switch (x->props.mode) {
2428 case XFRM_MODE_TRANSPORT:
2429 case XFRM_MODE_BEET:
2430 if (x->props.family == AF_INET)
2431 net_adj = sizeof(struct iphdr);
2432 else if (x->props.family == AF_INET6)
2433 net_adj = sizeof(struct ipv6hdr);
2434 break;
2435 case XFRM_MODE_TUNNEL:
2436 break;
2437 default:
2438 WARN_ON_ONCE(1);
2439 break;
2440 }
2441
2442 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2443 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
Florian Westphalc7b37c72019-06-24 22:04:48 +02002445EXPORT_SYMBOL_GPL(xfrm_state_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Ilan Tayariffdb5212017-08-01 12:49:08 +03002447int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002448{
Florian Westphalc9500d72019-03-29 21:16:32 +01002449 const struct xfrm_mode *inner_mode;
2450 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002451 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002452 int err;
2453
Florian Westphale4681742019-05-03 17:46:16 +02002454 if (family == AF_INET &&
2455 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2456 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Herbert Xud094cd82005-06-20 13:19:41 -07002457
2458 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002459
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002460 if (x->sel.family != AF_UNSPEC) {
2461 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2462 if (inner_mode == NULL)
2463 goto error;
2464
2465 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002466 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002467 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002468
Florian Westphalc9500d72019-03-29 21:16:32 +01002469 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002470 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002471 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002472 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002473
Martin Willid81d2282008-12-03 15:38:07 -08002474 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002475 if (inner_mode == NULL)
2476 goto error;
2477
Florian Westphal4c145dc2019-03-29 21:16:31 +01002478 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002479 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002480
Florian Westphalc9500d72019-03-29 21:16:32 +01002481 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002482
Martin Willid81d2282008-12-03 15:38:07 -08002483 if (x->props.family == AF_INET)
2484 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002485
Martin Willid81d2282008-12-03 15:38:07 -08002486 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2487 if (inner_mode_iaf) {
2488 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002489 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002490 }
2491 }
Herbert Xu13996372007-10-17 21:35:51 -07002492
Herbert Xud094cd82005-06-20 13:19:41 -07002493 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002494 if (x->type == NULL)
2495 goto error;
2496
Ilan Tayariffdb5212017-08-01 12:49:08 +03002497 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002498
Herbert Xu72cb6962005-06-20 13:18:08 -07002499 err = x->type->init_state(x);
2500 if (err)
2501 goto error;
2502
Florian Westphalc9500d72019-03-29 21:16:32 +01002503 outer_mode = xfrm_get_mode(x->props.mode, family);
2504 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002505 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002506 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002507 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002508
Florian Westphalc9500d72019-03-29 21:16:32 +01002509 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002510 if (init_replay) {
2511 err = xfrm_init_replay(x);
2512 if (err)
2513 goto error;
2514 }
2515
Herbert Xu72cb6962005-06-20 13:18:08 -07002516error:
2517 return err;
2518}
2519
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002520EXPORT_SYMBOL(__xfrm_init_state);
2521
2522int xfrm_init_state(struct xfrm_state *x)
2523{
Yossi Kupermancc015722018-01-17 15:52:41 +02002524 int err;
2525
2526 err = __xfrm_init_state(x, true, false);
2527 if (!err)
2528 x->km.state = XFRM_STATE_VALID;
2529
2530 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002531}
2532
Herbert Xu72cb6962005-06-20 13:18:08 -07002533EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002534
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002535int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
David S. Millerf034b5d2006-08-24 03:08:07 -07002537 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Mathias Krause565f0fa2018-05-03 10:55:07 +02002539 if (net_eq(net, &init_net))
2540 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2541 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2542
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002543 INIT_LIST_HEAD(&net->xfrm.state_all);
2544
David S. Millerf034b5d2006-08-24 03:08:07 -07002545 sz = sizeof(struct hlist_head) * 8;
2546
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002547 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2548 if (!net->xfrm.state_bydst)
2549 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002550 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2551 if (!net->xfrm.state_bysrc)
2552 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002553 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2554 if (!net->xfrm.state_byspi)
2555 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002556 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002557
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002558 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002559 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002560 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002561 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002562
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002563out_byspi:
2564 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002565out_bysrc:
2566 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002567out_bydst:
2568 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002569}
2570
2571void xfrm_state_fini(struct net *net)
2572{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002573 unsigned int sz;
2574
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002575 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002576 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002577 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002578
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002579 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002580
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002581 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002582 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2583 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002584 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2585 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002586 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2587 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
Joy Lattenab5f5e82007-09-17 11:51:22 -07002590#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002591static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2592 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002593{
Paul Moore68277ac2007-12-20 20:49:33 -08002594 struct xfrm_sec_ctx *ctx = x->security;
2595 u32 spi = ntohl(x->id.spi);
2596
2597 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002598 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002599 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002600
Weilong Chen9b7a7872013-12-24 09:43:46 +08002601 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002602 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002603 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2604 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002605 break;
2606 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002607 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002608 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002609 break;
2610 }
Paul Moore68277ac2007-12-20 20:49:33 -08002611
2612 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002613}
2614
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002615static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2616 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002617{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002618 const struct iphdr *iph4;
2619 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002620
2621 switch (family) {
2622 case AF_INET:
2623 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002624 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2625 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002626 break;
2627 case AF_INET6:
2628 iph6 = ipv6_hdr(skb);
2629 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002630 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002631 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002632 iph6->flow_lbl[0] & 0x0f,
2633 iph6->flow_lbl[1],
2634 iph6->flow_lbl[2]);
2635 break;
2636 }
2637}
2638
Tetsuo Handa2e710292014-04-22 21:48:30 +09002639void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002640{
2641 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002642
Paul Mooreafeb14b2007-12-21 14:58:11 -08002643 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002644 if (audit_buf == NULL)
2645 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002646 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002647 xfrm_audit_helper_sainfo(x, audit_buf);
2648 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002649 audit_log_end(audit_buf);
2650}
2651EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2652
Tetsuo Handa2e710292014-04-22 21:48:30 +09002653void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002654{
2655 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002656
Paul Mooreafeb14b2007-12-21 14:58:11 -08002657 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002658 if (audit_buf == NULL)
2659 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002660 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002661 xfrm_audit_helper_sainfo(x, audit_buf);
2662 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002663 audit_log_end(audit_buf);
2664}
2665EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002666
2667void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2668 struct sk_buff *skb)
2669{
2670 struct audit_buffer *audit_buf;
2671 u32 spi;
2672
2673 audit_buf = xfrm_audit_start("SA-replay-overflow");
2674 if (audit_buf == NULL)
2675 return;
2676 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2677 /* don't record the sequence number because it's inherent in this kind
2678 * of audit message */
2679 spi = ntohl(x->id.spi);
2680 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2681 audit_log_end(audit_buf);
2682}
2683EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2684
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002685void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002686 struct sk_buff *skb, __be32 net_seq)
2687{
2688 struct audit_buffer *audit_buf;
2689 u32 spi;
2690
2691 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2692 if (audit_buf == NULL)
2693 return;
2694 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2695 spi = ntohl(x->id.spi);
2696 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2697 spi, spi, ntohl(net_seq));
2698 audit_log_end(audit_buf);
2699}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002700EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002701
2702void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2703{
2704 struct audit_buffer *audit_buf;
2705
2706 audit_buf = xfrm_audit_start("SA-notfound");
2707 if (audit_buf == NULL)
2708 return;
2709 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2710 audit_log_end(audit_buf);
2711}
2712EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2713
2714void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2715 __be32 net_spi, __be32 net_seq)
2716{
2717 struct audit_buffer *audit_buf;
2718 u32 spi;
2719
2720 audit_buf = xfrm_audit_start("SA-notfound");
2721 if (audit_buf == NULL)
2722 return;
2723 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2724 spi = ntohl(net_spi);
2725 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2726 spi, spi, ntohl(net_seq));
2727 audit_log_end(audit_buf);
2728}
2729EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2730
2731void xfrm_audit_state_icvfail(struct xfrm_state *x,
2732 struct sk_buff *skb, u8 proto)
2733{
2734 struct audit_buffer *audit_buf;
2735 __be32 net_spi;
2736 __be32 net_seq;
2737
2738 audit_buf = xfrm_audit_start("SA-icv-failure");
2739 if (audit_buf == NULL)
2740 return;
2741 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2742 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2743 u32 spi = ntohl(net_spi);
2744 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2745 spi, spi, ntohl(net_seq));
2746 }
2747 audit_log_end(audit_buf);
2748}
2749EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002750#endif /* CONFIG_AUDITSYSCALL */