blob: 69520ad3d83bfb14fa793819259888631f98ee66 [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));
1513
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001514 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001515 goto error;
1516
1517 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001518 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001519
Steffen Klassert7e652642018-06-12 14:07:07 +02001520 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001521 x->tfcpad = orig->tfcpad;
1522 x->replay_maxdiff = orig->replay_maxdiff;
1523 x->replay_maxage = orig->replay_maxage;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001524 x->curlft.add_time = orig->curlft.add_time;
1525 x->km.state = orig->km.state;
1526 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001527 x->replay = orig->replay;
1528 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001529
1530 return x;
1531
1532 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001533 xfrm_state_put(x);
1534out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001535 return NULL;
1536}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001537
Fan Du283bc9f2013-11-07 17:47:50 +08001538struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001539{
1540 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001541 struct xfrm_state *x = NULL;
1542
1543 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001544
1545 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001546 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001547 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001548 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001549 if (x->props.mode != m->mode ||
1550 x->id.proto != m->proto)
1551 continue;
1552 if (m->reqid && x->props.reqid != m->reqid)
1553 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001554 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1555 m->old_family) ||
1556 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1557 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001558 continue;
1559 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001560 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001561 }
1562 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001563 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001564 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001565 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001566 if (x->props.mode != m->mode ||
1567 x->id.proto != m->proto)
1568 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001569 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1570 m->old_family) ||
1571 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1572 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001573 continue;
1574 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001575 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001576 }
1577 }
1578
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001579 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1580
1581 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001582}
1583EXPORT_SYMBOL(xfrm_migrate_state_find);
1584
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001585struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001586 struct xfrm_migrate *m,
1587 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001588{
1589 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001590
Antony Antony4ab47d42017-06-06 12:12:13 +02001591 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001592 if (!xc)
1593 return NULL;
1594
1595 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1596 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1597
1598 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001599 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001600 /* a care is needed when the destination address of the
1601 state is to be updated as it is a part of triplet */
1602 xfrm_state_insert(xc);
1603 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001604 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001605 goto error;
1606 }
1607
1608 return xc;
1609error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001610 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001611 return NULL;
1612}
1613EXPORT_SYMBOL(xfrm_state_migrate);
1614#endif
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616int xfrm_state_update(struct xfrm_state *x)
1617{
David S. Miller37b08e32008-09-02 20:14:15 -07001618 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001620 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001621 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
David S. Miller37b08e32008-09-02 20:14:15 -07001623 to_put = NULL;
1624
Fan Du283bc9f2013-11-07 17:47:50 +08001625 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001626 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 err = -ESRCH;
1629 if (!x1)
1630 goto out;
1631
1632 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001633 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 err = -EEXIST;
1635 goto out;
1636 }
1637
1638 if (x1->km.state == XFRM_STATE_ACQ) {
1639 __xfrm_state_insert(x);
1640 x = NULL;
1641 }
1642 err = 0;
1643
1644out:
Fan Du283bc9f2013-11-07 17:47:50 +08001645 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
David S. Miller37b08e32008-09-02 20:14:15 -07001647 if (to_put)
1648 xfrm_state_put(to_put);
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (err)
1651 return err;
1652
1653 if (!x) {
1654 xfrm_state_delete(x1);
1655 xfrm_state_put(x1);
1656 return 0;
1657 }
1658
1659 err = -EINVAL;
1660 spin_lock_bh(&x1->lock);
1661 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001662 if (x->encap && x1->encap &&
1663 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001665 else if (x->encap || x1->encap)
1666 goto fail;
1667
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001668 if (x->coaddr && x1->coaddr) {
1669 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1670 }
1671 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1672 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1674 x1->km.dying = 0;
1675
Thomas Gleixner671422b2019-03-01 23:48:20 +01001676 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1677 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (x1->curlft.use_time)
1679 xfrm_state_check_expire(x1);
1680
Nathan Harold5baf4f92018-07-19 19:07:47 -07001681 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001682 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1683
Nathan Harold5baf4f92018-07-19 19:07:47 -07001684 if (x->props.smark.m || x->props.smark.v)
1685 x1->props.smark = x->props.smark;
1686
1687 if (x->if_id)
1688 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001689
1690 __xfrm_state_bump_genids(x1);
1691 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1692 }
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001695 x->km.state = XFRM_STATE_DEAD;
1696 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001698
1699fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 spin_unlock_bh(&x1->lock);
1701
1702 xfrm_state_put(x1);
1703
1704 return err;
1705}
1706EXPORT_SYMBOL(xfrm_state_update);
1707
1708int xfrm_state_check_expire(struct xfrm_state *x)
1709{
1710 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001711 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1714 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001715 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001716 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return -EINVAL;
1718 }
1719
1720 if (!x->km.dying &&
1721 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001722 x->curlft.packets >= x->lft.soft_packet_limit)) {
1723 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001724 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 0;
1727}
1728EXPORT_SYMBOL(xfrm_state_check_expire);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001731xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001732 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Florian Westphalc2f672f2016-09-20 15:45:26 +02001736 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001737 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001738 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return x;
1740}
1741EXPORT_SYMBOL(xfrm_state_lookup);
1742
1743struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001744xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001745 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001746 u8 proto, unsigned short family)
1747{
1748 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001749
Fan Du283bc9f2013-11-07 17:47:50 +08001750 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001751 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001752 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001753 return x;
1754}
1755EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1756
1757struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001758xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001759 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001760 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Fan Du283bc9f2013-11-07 17:47:50 +08001764 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001765 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001766 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return x;
1769}
1770EXPORT_SYMBOL(xfrm_find_acq);
1771
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001772#ifdef CONFIG_XFRM_SUB_POLICY
Florian Westphal3aaf3912019-05-03 17:46:17 +02001773#if IS_ENABLED(CONFIG_IPV6)
1774/* distribution counting sort function for xfrm_state and xfrm_tmpl */
1775static void
1776__xfrm6_sort(void **dst, void **src, int n,
1777 int (*cmp)(const void *p), int maxclass)
1778{
1779 int count[XFRM_MAX_DEPTH] = { };
1780 int class[XFRM_MAX_DEPTH];
1781 int i;
1782
1783 for (i = 0; i < n; i++) {
1784 int c = cmp(src[i]);
1785
1786 class[i] = c;
1787 count[c]++;
1788 }
1789
1790 for (i = 2; i < maxclass; i++)
1791 count[i] += count[i - 1];
1792
1793 for (i = 0; i < n; i++) {
1794 dst[count[class[i] - 1]++] = src[i];
1795 src[i] = NULL;
1796 }
1797}
1798
1799/* Rule for xfrm_state:
1800 *
1801 * rule 1: select IPsec transport except AH
1802 * rule 2: select MIPv6 RO or inbound trigger
1803 * rule 3: select IPsec transport AH
1804 * rule 4: select IPsec tunnel
1805 * rule 5: others
1806 */
1807static int __xfrm6_state_sort_cmp(const void *p)
1808{
1809 const struct xfrm_state *v = p;
1810
1811 switch (v->props.mode) {
1812 case XFRM_MODE_TRANSPORT:
1813 if (v->id.proto != IPPROTO_AH)
1814 return 1;
1815 else
1816 return 3;
1817#if IS_ENABLED(CONFIG_IPV6_MIP6)
1818 case XFRM_MODE_ROUTEOPTIMIZATION:
1819 case XFRM_MODE_IN_TRIGGER:
1820 return 2;
1821#endif
1822 case XFRM_MODE_TUNNEL:
1823 case XFRM_MODE_BEET:
1824 return 4;
1825 }
1826 return 5;
1827}
1828
1829/* Rule for xfrm_tmpl:
1830 *
1831 * rule 1: select IPsec transport
1832 * rule 2: select MIPv6 RO or inbound trigger
1833 * rule 3: select IPsec tunnel
1834 * rule 4: others
1835 */
1836static int __xfrm6_tmpl_sort_cmp(const void *p)
1837{
1838 const struct xfrm_tmpl *v = p;
1839
1840 switch (v->mode) {
1841 case XFRM_MODE_TRANSPORT:
1842 return 1;
1843#if IS_ENABLED(CONFIG_IPV6_MIP6)
1844 case XFRM_MODE_ROUTEOPTIMIZATION:
1845 case XFRM_MODE_IN_TRIGGER:
1846 return 2;
1847#endif
1848 case XFRM_MODE_TUNNEL:
1849 case XFRM_MODE_BEET:
1850 return 3;
1851 }
1852 return 4;
1853}
1854#else
1855static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1856static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1857
1858static inline void
1859__xfrm6_sort(void **dst, void **src, int n,
1860 int (*cmp)(const void *p), int maxclass)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001861{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001862 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001863
Florian Westphal3aaf3912019-05-03 17:46:17 +02001864 for (i = 0; i < n; i++)
1865 dst[i] = src[i];
1866}
1867#endif /* CONFIG_IPV6 */
1868
1869void
1870xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1871 unsigned short family)
1872{
1873 int i;
1874
1875 if (family == AF_INET6)
1876 __xfrm6_sort((void **)dst, (void **)src, n,
1877 __xfrm6_tmpl_sort_cmp, 5);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001878 else
1879 for (i = 0; i < n; i++)
1880 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001881}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001882
Florian Westphal3aaf3912019-05-03 17:46:17 +02001883void
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001884xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1885 unsigned short family)
1886{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001887 int i;
Fan Du283bc9f2013-11-07 17:47:50 +08001888
Florian Westphal3aaf3912019-05-03 17:46:17 +02001889 if (family == AF_INET6)
1890 __xfrm6_sort((void **)dst, (void **)src, n,
1891 __xfrm6_state_sort_cmp, 6);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001892 else
1893 for (i = 0; i < n; i++)
1894 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001895}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001896#endif
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/* Silly enough, but I'm lazy to build resolution list */
1899
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001900static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001904 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001905 struct xfrm_state *x;
1906
Sasha Levinb67bfe02013-02-27 17:06:00 -08001907 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001908 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001909 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001910 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 xfrm_state_hold(x);
1912 return x;
1913 }
1914 }
1915 }
1916 return NULL;
1917}
1918
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001919struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 struct xfrm_state *x;
1922
Fan Du283bc9f2013-11-07 17:47:50 +08001923 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001924 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001925 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return x;
1927}
1928EXPORT_SYMBOL(xfrm_find_acq_byseq);
1929
1930u32 xfrm_get_acqseq(void)
1931{
1932 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001933 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
jamal6836b9b2010-02-16 02:01:22 +00001935 do {
1936 res = atomic_inc_return(&acqseq);
1937 } while (!res);
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return res;
1940}
1941EXPORT_SYMBOL(xfrm_get_acqseq);
1942
Fan Du776e9dd2013-12-16 18:47:49 +08001943int verify_spi_info(u8 proto, u32 min, u32 max)
1944{
1945 switch (proto) {
1946 case IPPROTO_AH:
1947 case IPPROTO_ESP:
1948 break;
1949
1950 case IPPROTO_COMP:
1951 /* IPCOMP spi is 16-bits. */
1952 if (max >= 0x10000)
1953 return -EINVAL;
1954 break;
1955
1956 default:
1957 return -EINVAL;
1958 }
1959
1960 if (min > max)
1961 return -EINVAL;
1962
1963 return 0;
1964}
1965EXPORT_SYMBOL(verify_spi_info);
1966
Herbert Xu658b2192007-10-09 13:29:52 -07001967int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001969 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07001970 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07001972 int err = -ENOENT;
1973 __be32 minspi = htonl(low);
1974 __be32 maxspi = htonl(high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001975 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Herbert Xu658b2192007-10-09 13:29:52 -07001977 spin_lock_bh(&x->lock);
1978 if (x->km.state == XFRM_STATE_DEAD)
1979 goto unlock;
1980
1981 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07001983 goto unlock;
1984
1985 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001988 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (x0) {
1990 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07001991 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 x->id.spi = minspi;
1994 } else {
1995 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001996 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05001997 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001998 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (x0 == NULL) {
2000 x->id.spi = htonl(spi);
2001 break;
2002 }
2003 xfrm_state_put(x0);
2004 }
2005 }
2006 if (x->id.spi) {
Fan Du283bc9f2013-11-07 17:47:50 +08002007 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan12604d82008-11-25 17:31:18 -08002008 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02002009 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08002010 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07002011
2012 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Herbert Xu658b2192007-10-09 13:29:52 -07002014
2015unlock:
2016 spin_unlock_bh(&x->lock);
2017
2018 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020EXPORT_SYMBOL(xfrm_alloc_spi);
2021
Nicolas Dichteld3623092014-02-14 15:30:36 +01002022static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002023 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01002024{
2025 if (filter) {
2026 if ((filter->family == AF_INET ||
2027 filter->family == AF_INET6) &&
2028 x->props.family != filter->family)
2029 return false;
2030
2031 return addr_match(&x->props.saddr, &filter->saddr,
2032 filter->splen) &&
2033 addr_match(&x->id.daddr, &filter->daddr,
2034 filter->dplen);
2035 }
2036 return true;
2037}
2038
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002039int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08002040 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 void *data)
2042{
Herbert Xu12a169e2008-10-01 07:03:24 -07002043 struct xfrm_state *state;
2044 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int err = 0;
2046
Herbert Xu12a169e2008-10-01 07:03:24 -07002047 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08002048 return 0;
2049
Fan Du283bc9f2013-11-07 17:47:50 +08002050 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002051 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002052 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07002053 else
Li RongQing80077702015-04-22 17:09:54 +08002054 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002055 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07002056 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08002057 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002058 state = container_of(x, struct xfrm_state, km);
2059 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08002060 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002061 if (!__xfrm_state_filter_match(state, walk->filter))
2062 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002063 err = func(state, walk->seq, data);
2064 if (err) {
2065 list_move_tail(&walk->all, &x->all);
2066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002068 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002070 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 err = -ENOENT;
2072 goto out;
2073 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002074 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075out:
Fan Du283bc9f2013-11-07 17:47:50 +08002076 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return err;
2078}
2079EXPORT_SYMBOL(xfrm_state_walk);
2080
Nicolas Dichteld3623092014-02-14 15:30:36 +01002081void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002082 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07002083{
Herbert Xu12a169e2008-10-01 07:03:24 -07002084 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07002085 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07002086 walk->state = XFRM_STATE_DEAD;
2087 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002088 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07002089}
2090EXPORT_SYMBOL(xfrm_state_walk_init);
2091
Fan Du283bc9f2013-11-07 17:47:50 +08002092void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07002093{
Nicolas Dichteld3623092014-02-14 15:30:36 +01002094 kfree(walk->filter);
2095
Herbert Xu12a169e2008-10-01 07:03:24 -07002096 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07002097 return;
Herbert Xu5c182452008-09-22 19:48:19 -07002098
Fan Du283bc9f2013-11-07 17:47:50 +08002099 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002100 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08002101 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07002102}
2103EXPORT_SYMBOL(xfrm_state_walk_done);
2104
Kees Cooke99e88a2017-10-16 14:43:17 -07002105static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002106{
Kees Cooke99e88a2017-10-16 14:43:17 -07002107 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002108
2109 spin_lock(&x->lock);
2110
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002111 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002112 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002113 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002114 else
2115 x->xflags |= XFRM_TIME_DEFER;
2116 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002117
2118 spin_unlock(&x->lock);
2119}
2120
Denis Chengdf018122007-12-07 00:51:11 -08002121static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
David S. Miller214e0052011-02-24 00:02:38 -05002123void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 struct xfrm_mgr *km;
2126
Cong Wang85168c02013-01-16 16:05:06 +08002127 rcu_read_lock();
2128 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002129 if (km->notify_policy)
2130 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08002131 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002132}
2133
David S. Miller214e0052011-02-24 00:02:38 -05002134void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002135{
2136 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08002137 rcu_read_lock();
2138 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002139 if (km->notify)
2140 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08002141 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002142}
2143
2144EXPORT_SYMBOL(km_policy_notify);
2145EXPORT_SYMBOL(km_state_notify);
2146
Eric W. Biederman15e47302012-09-07 20:12:54 +00002147void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002148{
2149 struct km_event c;
2150
Herbert Xubf08867f92005-06-18 22:44:00 -07002151 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002152 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002153 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002154 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002157EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002158/*
2159 * We send to all registered managers regardless of failure
2160 * We are happy with one success
2161*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002162int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002164 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct xfrm_mgr *km;
2166
Cong Wang85168c02013-01-16 16:05:06 +08002167 rcu_read_lock();
2168 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08002169 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002170 if (!acqret)
2171 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Cong Wang85168c02013-01-16 16:05:06 +08002173 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return err;
2175}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002176EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Al Viro5d36b182006-11-08 00:24:06 -08002178int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 int err = -EINVAL;
2181 struct xfrm_mgr *km;
2182
Cong Wang85168c02013-01-16 16:05:06 +08002183 rcu_read_lock();
2184 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (km->new_mapping)
2186 err = km->new_mapping(x, ipaddr, sport);
2187 if (!err)
2188 break;
2189 }
Cong Wang85168c02013-01-16 16:05:06 +08002190 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return err;
2192}
2193EXPORT_SYMBOL(km_new_mapping);
2194
Eric W. Biederman15e47302012-09-07 20:12:54 +00002195void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002197 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Herbert Xubf08867f92005-06-18 22:44:00 -07002199 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002200 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002201 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002202 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
David S. Millera70fcb02006-03-20 19:18:52 -08002204EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002206#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002207int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2208 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002209 const struct xfrm_kmaddress *k,
2210 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002211{
2212 int err = -EINVAL;
2213 int ret;
2214 struct xfrm_mgr *km;
2215
Cong Wang85168c02013-01-16 16:05:06 +08002216 rcu_read_lock();
2217 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002218 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002219 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2220 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002221 if (!ret)
2222 err = ret;
2223 }
2224 }
Cong Wang85168c02013-01-16 16:05:06 +08002225 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002226 return err;
2227}
2228EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002229#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002230
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002231int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002232{
2233 int err = -EINVAL;
2234 int ret;
2235 struct xfrm_mgr *km;
2236
Cong Wang85168c02013-01-16 16:05:06 +08002237 rcu_read_lock();
2238 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002239 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002240 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002241 if (!ret)
2242 err = ret;
2243 }
2244 }
Cong Wang85168c02013-01-16 16:05:06 +08002245 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002246 return err;
2247}
2248EXPORT_SYMBOL(km_report);
2249
Florian Westphalbb9cd072019-04-17 11:45:13 +02002250static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002251{
2252 struct xfrm_mgr *km;
2253 bool is_alive = false;
2254
2255 rcu_read_lock();
2256 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2257 if (km->is_alive && km->is_alive(c)) {
2258 is_alive = true;
2259 break;
2260 }
2261 }
2262 rcu_read_unlock();
2263
2264 return is_alive;
2265}
Horia Geanta0f245582014-02-12 16:20:06 +02002266
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002267int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 int err;
2270 u8 *data;
2271 struct xfrm_mgr *km;
2272 struct xfrm_policy *pol = NULL;
2273
Steffen Klassert19d7df62018-02-01 08:49:23 +01002274 if (in_compat_syscall())
2275 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002276
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002277 if (sockptr_is_null(optval) && !optlen) {
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002278 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2279 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2280 __sk_dst_reset(sk);
2281 return 0;
2282 }
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (optlen <= 0 || optlen > PAGE_SIZE)
2285 return -EMSGSIZE;
2286
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002287 data = memdup_sockptr(optval, optlen);
Geliang Tanga133d932017-05-06 23:42:21 +08002288 if (IS_ERR(data))
2289 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002292 rcu_read_lock();
2293 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002294 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 optlen, &err);
2296 if (err >= 0)
2297 break;
2298 }
Cong Wang85168c02013-01-16 16:05:06 +08002299 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 if (err >= 0) {
2302 xfrm_sk_policy_insert(sk, err, pol);
2303 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002304 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 err = 0;
2306 }
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 kfree(data);
2309 return err;
2310}
2311EXPORT_SYMBOL(xfrm_user_policy);
2312
Cong Wang85168c02013-01-16 16:05:06 +08002313static DEFINE_SPINLOCK(xfrm_km_lock);
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315int xfrm_register_km(struct xfrm_mgr *km)
2316{
Cong Wang85168c02013-01-16 16:05:06 +08002317 spin_lock_bh(&xfrm_km_lock);
2318 list_add_tail_rcu(&km->list, &xfrm_km_list);
2319 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 0;
2321}
2322EXPORT_SYMBOL(xfrm_register_km);
2323
2324int xfrm_unregister_km(struct xfrm_mgr *km)
2325{
Cong Wang85168c02013-01-16 16:05:06 +08002326 spin_lock_bh(&xfrm_km_lock);
2327 list_del_rcu(&km->list);
2328 spin_unlock_bh(&xfrm_km_lock);
2329 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331}
2332EXPORT_SYMBOL(xfrm_unregister_km);
2333
2334int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2335{
2336 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002337
2338 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002340
Cong Wang44abdc32013-01-16 16:05:05 +08002341 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002343 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002344 else
Cong Wang44abdc32013-01-16 16:05:05 +08002345 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2346 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return err;
2348}
2349EXPORT_SYMBOL(xfrm_state_register_afinfo);
2350
2351int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2352{
Florian Westphal423826a2017-01-09 14:20:46 +01002353 int err = 0, family = afinfo->family;
2354
2355 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002357
Cong Wang44abdc32013-01-16 16:05:05 +08002358 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002360 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002362 else
Cong Wang44abdc32013-01-16 16:05:05 +08002363 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Cong Wang44abdc32013-01-16 16:05:05 +08002365 spin_unlock_bh(&xfrm_state_afinfo_lock);
2366 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return err;
2368}
2369EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2370
Florian Westphal711059b2017-01-09 14:20:48 +01002371struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2372{
2373 if (unlikely(family >= NPROTO))
2374 return NULL;
2375
2376 return rcu_dereference(xfrm_state_afinfo[family]);
2377}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002378EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002379
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002380struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 struct xfrm_state_afinfo *afinfo;
2383 if (unlikely(family >= NPROTO))
2384 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002385 rcu_read_lock();
2386 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002387 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002388 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return afinfo;
2390}
2391
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002392void xfrm_flush_gc(void)
2393{
2394 flush_work(&xfrm_state_gc_work);
2395}
2396EXPORT_SYMBOL(xfrm_flush_gc);
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2399void xfrm_state_delete_tunnel(struct xfrm_state *x)
2400{
2401 if (x->tunnel) {
2402 struct xfrm_state *t = x->tunnel;
2403
2404 if (atomic_read(&t->tunnel_users) == 2)
2405 xfrm_state_delete(t);
2406 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002407 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 x->tunnel = NULL;
2409 }
2410}
2411EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2412
Florian Westphalc7b37c72019-06-24 22:04:48 +02002413u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002415 const struct xfrm_type *type = READ_ONCE(x->type);
Florian Westphalc7b37c72019-06-24 22:04:48 +02002416 struct crypto_aead *aead;
2417 u32 blksize, net_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Florian Westphalc7b37c72019-06-24 22:04:48 +02002419 if (x->km.state != XFRM_STATE_VALID ||
2420 !type || type->proto != IPPROTO_ESP)
2421 return mtu - x->props.header_len;
Florian Westphalb3b73b82017-01-05 13:23:58 +01002422
Florian Westphalc7b37c72019-06-24 22:04:48 +02002423 aead = x->data;
2424 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2425
2426 switch (x->props.mode) {
2427 case XFRM_MODE_TRANSPORT:
2428 case XFRM_MODE_BEET:
2429 if (x->props.family == AF_INET)
2430 net_adj = sizeof(struct iphdr);
2431 else if (x->props.family == AF_INET6)
2432 net_adj = sizeof(struct ipv6hdr);
2433 break;
2434 case XFRM_MODE_TUNNEL:
2435 break;
2436 default:
2437 WARN_ON_ONCE(1);
2438 break;
2439 }
2440
2441 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2442 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
Florian Westphalc7b37c72019-06-24 22:04:48 +02002444EXPORT_SYMBOL_GPL(xfrm_state_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Ilan Tayariffdb5212017-08-01 12:49:08 +03002446int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002447{
Florian Westphalc9500d72019-03-29 21:16:32 +01002448 const struct xfrm_mode *inner_mode;
2449 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002450 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002451 int err;
2452
Florian Westphale4681742019-05-03 17:46:16 +02002453 if (family == AF_INET &&
2454 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2455 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Herbert Xud094cd82005-06-20 13:19:41 -07002456
2457 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002458
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002459 if (x->sel.family != AF_UNSPEC) {
2460 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2461 if (inner_mode == NULL)
2462 goto error;
2463
2464 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002465 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002466 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002467
Florian Westphalc9500d72019-03-29 21:16:32 +01002468 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002469 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002470 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002471 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002472
Martin Willid81d2282008-12-03 15:38:07 -08002473 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002474 if (inner_mode == NULL)
2475 goto error;
2476
Florian Westphal4c145dc2019-03-29 21:16:31 +01002477 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002478 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002479
Florian Westphalc9500d72019-03-29 21:16:32 +01002480 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002481
Martin Willid81d2282008-12-03 15:38:07 -08002482 if (x->props.family == AF_INET)
2483 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002484
Martin Willid81d2282008-12-03 15:38:07 -08002485 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2486 if (inner_mode_iaf) {
2487 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002488 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002489 }
2490 }
Herbert Xu13996372007-10-17 21:35:51 -07002491
Herbert Xud094cd82005-06-20 13:19:41 -07002492 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002493 if (x->type == NULL)
2494 goto error;
2495
Ilan Tayariffdb5212017-08-01 12:49:08 +03002496 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002497
Herbert Xu72cb6962005-06-20 13:18:08 -07002498 err = x->type->init_state(x);
2499 if (err)
2500 goto error;
2501
Florian Westphalc9500d72019-03-29 21:16:32 +01002502 outer_mode = xfrm_get_mode(x->props.mode, family);
2503 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002504 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002505 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002506 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002507
Florian Westphalc9500d72019-03-29 21:16:32 +01002508 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002509 if (init_replay) {
2510 err = xfrm_init_replay(x);
2511 if (err)
2512 goto error;
2513 }
2514
Herbert Xu72cb6962005-06-20 13:18:08 -07002515error:
2516 return err;
2517}
2518
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002519EXPORT_SYMBOL(__xfrm_init_state);
2520
2521int xfrm_init_state(struct xfrm_state *x)
2522{
Yossi Kupermancc015722018-01-17 15:52:41 +02002523 int err;
2524
2525 err = __xfrm_init_state(x, true, false);
2526 if (!err)
2527 x->km.state = XFRM_STATE_VALID;
2528
2529 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002530}
2531
Herbert Xu72cb6962005-06-20 13:18:08 -07002532EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002533
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002534int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
David S. Millerf034b5d2006-08-24 03:08:07 -07002536 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Mathias Krause565f0fa2018-05-03 10:55:07 +02002538 if (net_eq(net, &init_net))
2539 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2540 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2541
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002542 INIT_LIST_HEAD(&net->xfrm.state_all);
2543
David S. Millerf034b5d2006-08-24 03:08:07 -07002544 sz = sizeof(struct hlist_head) * 8;
2545
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002546 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2547 if (!net->xfrm.state_bydst)
2548 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002549 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2550 if (!net->xfrm.state_bysrc)
2551 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002552 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2553 if (!net->xfrm.state_byspi)
2554 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002555 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002556
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002557 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002558 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002559 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002560 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002561
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002562out_byspi:
2563 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002564out_bysrc:
2565 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002566out_bydst:
2567 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002568}
2569
2570void xfrm_state_fini(struct net *net)
2571{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002572 unsigned int sz;
2573
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002574 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002575 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002576 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002577
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002578 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002579
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002580 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002581 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2582 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002583 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2584 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002585 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2586 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
Joy Lattenab5f5e82007-09-17 11:51:22 -07002589#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002590static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2591 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002592{
Paul Moore68277ac2007-12-20 20:49:33 -08002593 struct xfrm_sec_ctx *ctx = x->security;
2594 u32 spi = ntohl(x->id.spi);
2595
2596 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002597 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002598 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002599
Weilong Chen9b7a7872013-12-24 09:43:46 +08002600 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002601 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002602 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2603 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002604 break;
2605 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002606 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002607 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002608 break;
2609 }
Paul Moore68277ac2007-12-20 20:49:33 -08002610
2611 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002612}
2613
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002614static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2615 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002616{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002617 const struct iphdr *iph4;
2618 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002619
2620 switch (family) {
2621 case AF_INET:
2622 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002623 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2624 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002625 break;
2626 case AF_INET6:
2627 iph6 = ipv6_hdr(skb);
2628 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002629 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002630 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002631 iph6->flow_lbl[0] & 0x0f,
2632 iph6->flow_lbl[1],
2633 iph6->flow_lbl[2]);
2634 break;
2635 }
2636}
2637
Tetsuo Handa2e710292014-04-22 21:48:30 +09002638void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002639{
2640 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002641
Paul Mooreafeb14b2007-12-21 14:58:11 -08002642 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002643 if (audit_buf == NULL)
2644 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002645 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002646 xfrm_audit_helper_sainfo(x, audit_buf);
2647 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002648 audit_log_end(audit_buf);
2649}
2650EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2651
Tetsuo Handa2e710292014-04-22 21:48:30 +09002652void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002653{
2654 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002655
Paul Mooreafeb14b2007-12-21 14:58:11 -08002656 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002657 if (audit_buf == NULL)
2658 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002659 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002660 xfrm_audit_helper_sainfo(x, audit_buf);
2661 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002662 audit_log_end(audit_buf);
2663}
2664EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002665
2666void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2667 struct sk_buff *skb)
2668{
2669 struct audit_buffer *audit_buf;
2670 u32 spi;
2671
2672 audit_buf = xfrm_audit_start("SA-replay-overflow");
2673 if (audit_buf == NULL)
2674 return;
2675 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2676 /* don't record the sequence number because it's inherent in this kind
2677 * of audit message */
2678 spi = ntohl(x->id.spi);
2679 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2680 audit_log_end(audit_buf);
2681}
2682EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2683
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002684void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002685 struct sk_buff *skb, __be32 net_seq)
2686{
2687 struct audit_buffer *audit_buf;
2688 u32 spi;
2689
2690 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2691 if (audit_buf == NULL)
2692 return;
2693 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2694 spi = ntohl(x->id.spi);
2695 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2696 spi, spi, ntohl(net_seq));
2697 audit_log_end(audit_buf);
2698}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002699EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002700
2701void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2702{
2703 struct audit_buffer *audit_buf;
2704
2705 audit_buf = xfrm_audit_start("SA-notfound");
2706 if (audit_buf == NULL)
2707 return;
2708 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2709 audit_log_end(audit_buf);
2710}
2711EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2712
2713void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2714 __be32 net_spi, __be32 net_seq)
2715{
2716 struct audit_buffer *audit_buf;
2717 u32 spi;
2718
2719 audit_buf = xfrm_audit_start("SA-notfound");
2720 if (audit_buf == NULL)
2721 return;
2722 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2723 spi = ntohl(net_spi);
2724 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2725 spi, spi, ntohl(net_seq));
2726 audit_log_end(audit_buf);
2727}
2728EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2729
2730void xfrm_audit_state_icvfail(struct xfrm_state *x,
2731 struct sk_buff *skb, u8 proto)
2732{
2733 struct audit_buffer *audit_buf;
2734 __be32 net_spi;
2735 __be32 net_seq;
2736
2737 audit_buf = xfrm_audit_start("SA-icv-failure");
2738 if (audit_buf == NULL)
2739 return;
2740 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2741 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2742 u32 spi = ntohl(net_spi);
2743 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2744 spi, spi, ntohl(net_seq));
2745 }
2746 audit_log_end(audit_buf);
2747}
2748EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002749#endif /* CONFIG_AUDITSYSCALL */