blob: ee6ac32bb06d75ead168bc4a6bf984616ac50a25 [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 &&
Herbert Xue94ee172020-09-25 14:42:56 +10001022 (x->sel.family != family ||
1023 !xfrm_selector_match(&x->sel, fl, family))) ||
David S. Miller08ec9af2009-03-13 14:22:40 -07001024 !security_xfrm_state_pol_flow_match(x, pol, fl))
1025 return;
1026
1027 if (!*best ||
1028 (*best)->km.dying > x->km.dying ||
1029 ((*best)->km.dying == x->km.dying &&
1030 (*best)->curlft.add_time < x->curlft.add_time))
1031 *best = x;
1032 } else if (x->km.state == XFRM_STATE_ACQ) {
1033 *acq_in_progress = 1;
1034 } else if (x->km.state == XFRM_STATE_ERROR ||
1035 x->km.state == XFRM_STATE_EXPIRED) {
Herbert Xue94ee172020-09-25 14:42:56 +10001036 if ((!x->sel.family ||
1037 (x->sel.family == family &&
1038 xfrm_selector_match(&x->sel, fl, family))) &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001039 security_xfrm_state_pol_flow_match(x, pol, fl))
1040 *error = -ESRCH;
1041 }
1042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -05001045xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -08001046 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -07001048 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
David S. Miller08ec9af2009-03-13 14:22:40 -07001050 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001051 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -07001052 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -07001053 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int acquire_in_progress = 0;
1055 int error = 0;
1056 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001057 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001058 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +02001059 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +02001060 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001061
David S. Miller37b08e32008-09-02 20:14:15 -07001062 to_put = NULL;
1063
Florian Westphalb65e3d72016-08-09 12:16:07 +02001064 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
1065
Florian Westphald737a582016-08-09 12:16:09 +02001066 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -07001067 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001068 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001069 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001071 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001072 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -07001073 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -07001074 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 tmpl->mode == x->props.mode &&
1076 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001077 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Herbert Xue94ee172020-09-25 14:42:56 +10001078 xfrm_state_look_at(pol, x, fl, family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001079 &best, &acquire_in_progress, &error);
1080 }
Fan Du6f115632013-09-23 17:18:25 +08001081 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -07001082 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Thomas Egerer8444cf72010-09-20 11:11:38 -07001084 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001085 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001086 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001087 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001088 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001089 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001090 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +08001091 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001092 tmpl->mode == x->props.mode &&
1093 tmpl->id.proto == x->id.proto &&
1094 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Herbert Xue94ee172020-09-25 14:42:56 +10001095 xfrm_state_look_at(pol, x, fl, family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001096 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
David S. Miller08ec9af2009-03-13 14:22:40 -07001099found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 x = best;
1101 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -07001102 if (tmpl->id.spi &&
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001103 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
Thomas Egerer8444cf72010-09-20 11:11:38 -07001104 tmpl->id.proto, encap_family)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -07001105 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 error = -EEXIST;
1107 goto out;
1108 }
Horia Geanta0f245582014-02-12 16:20:06 +02001109
1110 c.net = net;
1111 /* If the KMs have no listeners (yet...), avoid allocating an SA
1112 * for each and every packet - garbage collection might not
1113 * handle the flood.
1114 */
1115 if (!km_is_alive(&c)) {
1116 error = -ESRCH;
1117 goto out;
1118 }
1119
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001120 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (x == NULL) {
1122 error = -ENOMEM;
1123 goto out;
1124 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001125 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001127 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001128 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +02001129 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
David S. Miller1d28f422011-03-12 00:29:39 -05001131 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001132 if (error) {
1133 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001134 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001135 x = NULL;
1136 goto out;
1137 }
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001140 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001142 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001143 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001144 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001145 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001147 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001148 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001150 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001151 hrtimer_start(&x->mtimer,
1152 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1153 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001154 net->xfrm.state_num++;
1155 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } else {
1158 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001159 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 x = NULL;
1161 error = -ESRCH;
1162 }
1163 }
1164out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001165 if (x) {
1166 if (!xfrm_state_hold_rcu(x)) {
1167 *err = -EAGAIN;
1168 x = NULL;
1169 }
1170 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001172 }
Florian Westphald737a582016-08-09 12:16:09 +02001173 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001174 if (to_put)
1175 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001176
1177 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
1178 *err = -EAGAIN;
1179 if (x) {
1180 xfrm_state_put(x);
1181 x = NULL;
1182 }
1183 }
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return x;
1186}
1187
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001188struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001189xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001190 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001191 unsigned short family, u8 mode, u8 proto, u32 reqid)
1192{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001193 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001194 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001195
Fan Du4ae770b2014-01-03 11:18:29 +08001196 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001197 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001198 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001199 if (x->props.family == family &&
1200 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001201 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001202 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001203 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1204 xfrm_state_addr_check(x, daddr, saddr, family) &&
1205 mode == x->props.mode &&
1206 proto == x->id.proto &&
1207 x->km.state == XFRM_STATE_VALID) {
1208 rx = x;
1209 break;
1210 }
1211 }
1212
1213 if (rx)
1214 xfrm_state_hold(rx);
Fan Du4ae770b2014-01-03 11:18:29 +08001215 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001216
1217
1218 return rx;
1219}
1220EXPORT_SYMBOL(xfrm_stateonly_find);
1221
Fan Duc4549972014-01-03 11:18:32 +08001222struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1223 unsigned short family)
1224{
1225 struct xfrm_state *x;
1226 struct xfrm_state_walk *w;
1227
1228 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1229 list_for_each_entry(w, &net->xfrm.state_all, all) {
1230 x = container_of(w, struct xfrm_state, km);
1231 if (x->props.family != family ||
1232 x->id.spi != spi)
1233 continue;
1234
Fan Duc4549972014-01-03 11:18:32 +08001235 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001236 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001237 return x;
1238 }
1239 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1240 return NULL;
1241}
1242EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static void __xfrm_state_insert(struct xfrm_state *x)
1245{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001246 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001247 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001249 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001250
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001251 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001252 x->props.reqid, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001253 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001255 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001256 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001258 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001259 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001260 x->props.family);
1261
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001262 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001263 }
1264
Thomas Gleixner671422b2019-03-01 23:48:20 +01001265 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
David S. Millera47f0ce2006-08-24 03:54:22 -07001266 if (x->replay_maxage)
1267 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001268
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001269 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001270
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001271 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Fan Du283bc9f2013-11-07 17:47:50 +08001274/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001275static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1276{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001277 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001278 unsigned short family = xnew->props.family;
1279 u32 reqid = xnew->props.reqid;
1280 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001281 unsigned int h;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001282 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001283 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001284
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001285 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001286 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001287 if (x->props.family == family &&
1288 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001289 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001290 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001291 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1292 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001293 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001294 }
1295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297void xfrm_state_insert(struct xfrm_state *x)
1298{
Fan Du283bc9f2013-11-07 17:47:50 +08001299 struct net *net = xs_net(x);
1300
1301 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001302 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001304 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306EXPORT_SYMBOL(xfrm_state_insert);
1307
Fan Du283bc9f2013-11-07 17:47:50 +08001308/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001309static struct xfrm_state *__find_acq_core(struct net *net,
1310 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001311 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001312 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001313 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001314 const xfrm_address_t *saddr,
1315 int create)
David S. Miller27708342006-08-24 00:13:10 -07001316{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001317 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001318 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001319 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001320
Sasha Levinb67bfe02013-02-27 17:06:00 -08001321 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001322 if (x->props.reqid != reqid ||
1323 x->props.mode != mode ||
1324 x->props.family != family ||
1325 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001326 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001327 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001328 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001329 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1330 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001331 continue;
1332
David S. Miller27708342006-08-24 00:13:10 -07001333 xfrm_state_hold(x);
1334 return x;
1335 }
1336
1337 if (!create)
1338 return NULL;
1339
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001340 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001341 if (likely(x)) {
1342 switch (family) {
1343 case AF_INET:
1344 x->sel.daddr.a4 = daddr->a4;
1345 x->sel.saddr.a4 = saddr->a4;
1346 x->sel.prefixlen_d = 32;
1347 x->sel.prefixlen_s = 32;
1348 x->props.saddr.a4 = saddr->a4;
1349 x->id.daddr.a4 = daddr->a4;
1350 break;
1351
1352 case AF_INET6:
Jiri Benc15e318b2015-03-29 16:59:24 +02001353 x->sel.daddr.in6 = daddr->in6;
1354 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001355 x->sel.prefixlen_d = 128;
1356 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001357 x->props.saddr.in6 = saddr->in6;
1358 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001359 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001360 }
David S. Miller27708342006-08-24 00:13:10 -07001361
1362 x->km.state = XFRM_STATE_ACQ;
1363 x->id.proto = proto;
1364 x->props.family = family;
1365 x->props.mode = mode;
1366 x->props.reqid = reqid;
Steffen Klassert7e652642018-06-12 14:07:07 +02001367 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001368 x->mark.v = m->v;
1369 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001370 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001371 xfrm_state_hold(x);
Thomas Gleixner671422b2019-03-01 23:48:20 +01001372 hrtimer_start(&x->mtimer,
1373 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1374 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001375 list_add(&x->km.all, &net->xfrm.state_all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001376 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001377 h = xfrm_src_hash(net, daddr, saddr, family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001378 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
David S. Miller918049f2006-10-12 22:03:24 -07001379
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001380 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001381
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001382 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001383 }
1384
1385 return x;
1386}
1387
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001388static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390int xfrm_state_add(struct xfrm_state *x)
1391{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001392 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001393 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int family;
1395 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001396 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001397 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
David S. Miller37b08e32008-09-02 20:14:15 -07001401 to_put = NULL;
1402
Fan Du283bc9f2013-11-07 17:47:50 +08001403 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
David S. Milleredcd5822006-08-24 00:42:45 -07001405 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001407 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 x1 = NULL;
1409 err = -EEXIST;
1410 goto out;
1411 }
1412
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001413 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001414 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001415 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001416 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001417 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 x1 = NULL;
1419 }
1420 }
1421
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001422 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001423 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001424 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001425 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
David S. Millerc7f5ea32006-08-24 03:29:04 -07001427 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 __xfrm_state_insert(x);
1429 err = 0;
1430
1431out:
Fan Du283bc9f2013-11-07 17:47:50 +08001432 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (x1) {
1435 xfrm_state_delete(x1);
1436 xfrm_state_put(x1);
1437 }
1438
David S. Miller37b08e32008-09-02 20:14:15 -07001439 if (to_put)
1440 xfrm_state_put(to_put);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return err;
1443}
1444EXPORT_SYMBOL(xfrm_state_add);
1445
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001446#ifdef CONFIG_XFRM_MIGRATE
Antony Antony7aa05d32020-09-04 08:50:11 +02001447static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1448{
1449 struct xfrm_user_sec_ctx *uctx;
1450 int size = sizeof(*uctx) + security->ctx_len;
1451 int err;
1452
1453 uctx = kmalloc(size, GFP_KERNEL);
1454 if (!uctx)
1455 return -ENOMEM;
1456
1457 uctx->exttype = XFRMA_SEC_CTX;
1458 uctx->len = size;
1459 uctx->ctx_doi = security->ctx_doi;
1460 uctx->ctx_alg = security->ctx_alg;
1461 uctx->ctx_len = security->ctx_len;
1462 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1463 err = security_xfrm_state_alloc(x, uctx);
1464 kfree(uctx);
1465 if (err)
1466 return err;
1467
1468 return 0;
1469}
1470
Antony Antony4ab47d42017-06-06 12:12:13 +02001471static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1472 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001473{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001474 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001475 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001476 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001477 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001478
1479 memcpy(&x->id, &orig->id, sizeof(x->id));
1480 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1481 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1482 x->props.mode = orig->props.mode;
1483 x->props.replay_window = orig->props.replay_window;
1484 x->props.reqid = orig->props.reqid;
1485 x->props.family = orig->props.family;
1486 x->props.saddr = orig->props.saddr;
1487
1488 if (orig->aalg) {
Martin Willi4447bb32009-11-25 00:29:52 +00001489 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001490 if (!x->aalg)
1491 goto error;
1492 }
1493 x->props.aalgo = orig->props.aalgo;
1494
Steffen Klassertee5c2312014-02-19 13:33:24 +01001495 if (orig->aead) {
1496 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001497 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001498 if (!x->aead)
1499 goto error;
1500 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001501 if (orig->ealg) {
1502 x->ealg = xfrm_algo_clone(orig->ealg);
1503 if (!x->ealg)
1504 goto error;
1505 }
1506 x->props.ealgo = orig->props.ealgo;
1507
1508 if (orig->calg) {
1509 x->calg = xfrm_algo_clone(orig->calg);
1510 if (!x->calg)
1511 goto error;
1512 }
1513 x->props.calgo = orig->props.calgo;
1514
Antony Antony4ab47d42017-06-06 12:12:13 +02001515 if (encap || orig->encap) {
1516 if (encap)
1517 x->encap = kmemdup(encap, sizeof(*x->encap),
1518 GFP_KERNEL);
1519 else
1520 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1521 GFP_KERNEL);
1522
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001523 if (!x->encap)
1524 goto error;
1525 }
1526
Antony Antony7aa05d32020-09-04 08:50:11 +02001527 if (orig->security)
1528 if (clone_security(x, orig->security))
1529 goto error;
1530
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001531 if (orig->coaddr) {
1532 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1533 GFP_KERNEL);
1534 if (!x->coaddr)
1535 goto error;
1536 }
1537
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001538 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001539 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001540 goto error;
1541 }
1542
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001543 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
Antony Antony545e5c52020-09-04 08:49:38 +02001544 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001545
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001546 if (xfrm_init_state(x) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001547 goto error;
1548
1549 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001550 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001551
Steffen Klassert7e652642018-06-12 14:07:07 +02001552 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001553 x->tfcpad = orig->tfcpad;
1554 x->replay_maxdiff = orig->replay_maxdiff;
1555 x->replay_maxage = orig->replay_maxage;
Antony Antony83666852020-09-04 08:50:29 +02001556 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001557 x->km.state = orig->km.state;
1558 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001559 x->replay = orig->replay;
1560 x->preplay = orig->preplay;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001561
1562 return x;
1563
1564 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001565 xfrm_state_put(x);
1566out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001567 return NULL;
1568}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001569
Fan Du283bc9f2013-11-07 17:47:50 +08001570struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001571{
1572 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001573 struct xfrm_state *x = NULL;
1574
1575 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001576
1577 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001578 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001579 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001580 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001581 if (x->props.mode != m->mode ||
1582 x->id.proto != m->proto)
1583 continue;
1584 if (m->reqid && x->props.reqid != m->reqid)
1585 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001586 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1587 m->old_family) ||
1588 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1589 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001590 continue;
1591 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001592 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001593 }
1594 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001595 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001596 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001597 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001598 if (x->props.mode != m->mode ||
1599 x->id.proto != m->proto)
1600 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001601 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1602 m->old_family) ||
1603 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1604 m->old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001605 continue;
1606 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001607 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001608 }
1609 }
1610
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001611 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1612
1613 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001614}
1615EXPORT_SYMBOL(xfrm_migrate_state_find);
1616
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001617struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001618 struct xfrm_migrate *m,
1619 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001620{
1621 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001622
Antony Antony4ab47d42017-06-06 12:12:13 +02001623 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001624 if (!xc)
1625 return NULL;
1626
1627 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1628 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1629
1630 /* add state */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001631 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001632 /* a care is needed when the destination address of the
1633 state is to be updated as it is a part of triplet */
1634 xfrm_state_insert(xc);
1635 } else {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001636 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001637 goto error;
1638 }
1639
1640 return xc;
1641error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001642 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001643 return NULL;
1644}
1645EXPORT_SYMBOL(xfrm_state_migrate);
1646#endif
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648int xfrm_state_update(struct xfrm_state *x)
1649{
David S. Miller37b08e32008-09-02 20:14:15 -07001650 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001652 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001653 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
David S. Miller37b08e32008-09-02 20:14:15 -07001655 to_put = NULL;
1656
Fan Du283bc9f2013-11-07 17:47:50 +08001657 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001658 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 err = -ESRCH;
1661 if (!x1)
1662 goto out;
1663
1664 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001665 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 err = -EEXIST;
1667 goto out;
1668 }
1669
1670 if (x1->km.state == XFRM_STATE_ACQ) {
1671 __xfrm_state_insert(x);
1672 x = NULL;
1673 }
1674 err = 0;
1675
1676out:
Fan Du283bc9f2013-11-07 17:47:50 +08001677 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
David S. Miller37b08e32008-09-02 20:14:15 -07001679 if (to_put)
1680 xfrm_state_put(to_put);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (err)
1683 return err;
1684
1685 if (!x) {
1686 xfrm_state_delete(x1);
1687 xfrm_state_put(x1);
1688 return 0;
1689 }
1690
1691 err = -EINVAL;
1692 spin_lock_bh(&x1->lock);
1693 if (likely(x1->km.state == XFRM_STATE_VALID)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001694 if (x->encap && x1->encap &&
1695 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001697 else if (x->encap || x1->encap)
1698 goto fail;
1699
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001700 if (x->coaddr && x1->coaddr) {
1701 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1702 }
1703 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1704 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1706 x1->km.dying = 0;
1707
Thomas Gleixner671422b2019-03-01 23:48:20 +01001708 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1709 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (x1->curlft.use_time)
1711 xfrm_state_check_expire(x1);
1712
Nathan Harold5baf4f92018-07-19 19:07:47 -07001713 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001714 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1715
Nathan Harold5baf4f92018-07-19 19:07:47 -07001716 if (x->props.smark.m || x->props.smark.v)
1717 x1->props.smark = x->props.smark;
1718
1719 if (x->if_id)
1720 x1->if_id = x->if_id;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001721
1722 __xfrm_state_bump_genids(x1);
1723 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1724 }
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001727 x->km.state = XFRM_STATE_DEAD;
1728 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001730
1731fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 spin_unlock_bh(&x1->lock);
1733
1734 xfrm_state_put(x1);
1735
1736 return err;
1737}
1738EXPORT_SYMBOL(xfrm_state_update);
1739
1740int xfrm_state_check_expire(struct xfrm_state *x)
1741{
1742 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001743 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1746 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001747 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001748 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return -EINVAL;
1750 }
1751
1752 if (!x->km.dying &&
1753 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001754 x->curlft.packets >= x->lft.soft_packet_limit)) {
1755 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001756 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759}
1760EXPORT_SYMBOL(xfrm_state_check_expire);
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001763xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001764 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
1766 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Florian Westphalc2f672f2016-09-20 15:45:26 +02001768 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001769 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001770 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return x;
1772}
1773EXPORT_SYMBOL(xfrm_state_lookup);
1774
1775struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001776xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001777 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001778 u8 proto, unsigned short family)
1779{
1780 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001781
Fan Du283bc9f2013-11-07 17:47:50 +08001782 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001783 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001784 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001785 return x;
1786}
1787EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1788
1789struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001790xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001791 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001792 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Fan Du283bc9f2013-11-07 17:47:50 +08001796 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001797 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001798 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return x;
1801}
1802EXPORT_SYMBOL(xfrm_find_acq);
1803
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001804#ifdef CONFIG_XFRM_SUB_POLICY
Florian Westphal3aaf3912019-05-03 17:46:17 +02001805#if IS_ENABLED(CONFIG_IPV6)
1806/* distribution counting sort function for xfrm_state and xfrm_tmpl */
1807static void
1808__xfrm6_sort(void **dst, void **src, int n,
1809 int (*cmp)(const void *p), int maxclass)
1810{
1811 int count[XFRM_MAX_DEPTH] = { };
1812 int class[XFRM_MAX_DEPTH];
1813 int i;
1814
1815 for (i = 0; i < n; i++) {
1816 int c = cmp(src[i]);
1817
1818 class[i] = c;
1819 count[c]++;
1820 }
1821
1822 for (i = 2; i < maxclass; i++)
1823 count[i] += count[i - 1];
1824
1825 for (i = 0; i < n; i++) {
1826 dst[count[class[i] - 1]++] = src[i];
1827 src[i] = NULL;
1828 }
1829}
1830
1831/* Rule for xfrm_state:
1832 *
1833 * rule 1: select IPsec transport except AH
1834 * rule 2: select MIPv6 RO or inbound trigger
1835 * rule 3: select IPsec transport AH
1836 * rule 4: select IPsec tunnel
1837 * rule 5: others
1838 */
1839static int __xfrm6_state_sort_cmp(const void *p)
1840{
1841 const struct xfrm_state *v = p;
1842
1843 switch (v->props.mode) {
1844 case XFRM_MODE_TRANSPORT:
1845 if (v->id.proto != IPPROTO_AH)
1846 return 1;
1847 else
1848 return 3;
1849#if IS_ENABLED(CONFIG_IPV6_MIP6)
1850 case XFRM_MODE_ROUTEOPTIMIZATION:
1851 case XFRM_MODE_IN_TRIGGER:
1852 return 2;
1853#endif
1854 case XFRM_MODE_TUNNEL:
1855 case XFRM_MODE_BEET:
1856 return 4;
1857 }
1858 return 5;
1859}
1860
1861/* Rule for xfrm_tmpl:
1862 *
1863 * rule 1: select IPsec transport
1864 * rule 2: select MIPv6 RO or inbound trigger
1865 * rule 3: select IPsec tunnel
1866 * rule 4: others
1867 */
1868static int __xfrm6_tmpl_sort_cmp(const void *p)
1869{
1870 const struct xfrm_tmpl *v = p;
1871
1872 switch (v->mode) {
1873 case XFRM_MODE_TRANSPORT:
1874 return 1;
1875#if IS_ENABLED(CONFIG_IPV6_MIP6)
1876 case XFRM_MODE_ROUTEOPTIMIZATION:
1877 case XFRM_MODE_IN_TRIGGER:
1878 return 2;
1879#endif
1880 case XFRM_MODE_TUNNEL:
1881 case XFRM_MODE_BEET:
1882 return 3;
1883 }
1884 return 4;
1885}
1886#else
1887static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1888static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1889
1890static inline void
1891__xfrm6_sort(void **dst, void **src, int n,
1892 int (*cmp)(const void *p), int maxclass)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001893{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001894 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001895
Florian Westphal3aaf3912019-05-03 17:46:17 +02001896 for (i = 0; i < n; i++)
1897 dst[i] = src[i];
1898}
1899#endif /* CONFIG_IPV6 */
1900
1901void
1902xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1903 unsigned short family)
1904{
1905 int i;
1906
1907 if (family == AF_INET6)
1908 __xfrm6_sort((void **)dst, (void **)src, n,
1909 __xfrm6_tmpl_sort_cmp, 5);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001910 else
1911 for (i = 0; i < n; i++)
1912 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001913}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001914
Florian Westphal3aaf3912019-05-03 17:46:17 +02001915void
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001916xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1917 unsigned short family)
1918{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001919 int i;
Fan Du283bc9f2013-11-07 17:47:50 +08001920
Florian Westphal3aaf3912019-05-03 17:46:17 +02001921 if (family == AF_INET6)
1922 __xfrm6_sort((void **)dst, (void **)src, n,
1923 __xfrm6_state_sort_cmp, 6);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09001924 else
1925 for (i = 0; i < n; i++)
1926 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001927}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001928#endif
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930/* Silly enough, but I'm lazy to build resolution list */
1931
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001932static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001936 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -07001937 struct xfrm_state *x;
1938
Sasha Levinb67bfe02013-02-27 17:06:00 -08001939 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
David S. Miller8f126e32006-08-24 02:45:07 -07001940 if (x->km.seq == seq &&
Jamal Hadi Salim3d6acfa2010-02-22 11:32:56 +00001941 (mark & x->mark.m) == x->mark.v &&
David S. Miller8f126e32006-08-24 02:45:07 -07001942 x->km.state == XFRM_STATE_ACQ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 xfrm_state_hold(x);
1944 return x;
1945 }
1946 }
1947 }
1948 return NULL;
1949}
1950
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001951struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 struct xfrm_state *x;
1954
Fan Du283bc9f2013-11-07 17:47:50 +08001955 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001956 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08001957 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return x;
1959}
1960EXPORT_SYMBOL(xfrm_find_acq_byseq);
1961
1962u32 xfrm_get_acqseq(void)
1963{
1964 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00001965 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
jamal6836b9b2010-02-16 02:01:22 +00001967 do {
1968 res = atomic_inc_return(&acqseq);
1969 } while (!res);
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return res;
1972}
1973EXPORT_SYMBOL(xfrm_get_acqseq);
1974
Fan Du776e9dd2013-12-16 18:47:49 +08001975int verify_spi_info(u8 proto, u32 min, u32 max)
1976{
1977 switch (proto) {
1978 case IPPROTO_AH:
1979 case IPPROTO_ESP:
1980 break;
1981
1982 case IPPROTO_COMP:
1983 /* IPCOMP spi is 16-bits. */
1984 if (max >= 0x10000)
1985 return -EINVAL;
1986 break;
1987
1988 default:
1989 return -EINVAL;
1990 }
1991
1992 if (min > max)
1993 return -EINVAL;
1994
1995 return 0;
1996}
1997EXPORT_SYMBOL(verify_spi_info);
1998
Herbert Xu658b2192007-10-09 13:29:52 -07001999int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08002001 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07002002 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07002004 int err = -ENOENT;
2005 __be32 minspi = htonl(low);
2006 __be32 maxspi = htonl(high);
zhuoliang zhanga779d912020-10-23 09:05:35 +02002007 __be32 newspi = 0;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002008 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Herbert Xu658b2192007-10-09 13:29:52 -07002010 spin_lock_bh(&x->lock);
2011 if (x->km.state == XFRM_STATE_DEAD)
2012 goto unlock;
2013
2014 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07002016 goto unlock;
2017
2018 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002021 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (x0) {
2023 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07002024 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
zhuoliang zhanga779d912020-10-23 09:05:35 +02002026 newspi = minspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 } else {
2028 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08002029 for (h = 0; h < high-low+1; h++) {
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002030 spi = low + prandom_u32()%(high-low+1);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002031 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (x0 == NULL) {
zhuoliang zhanga779d912020-10-23 09:05:35 +02002033 newspi = htonl(spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
2035 }
2036 xfrm_state_put(x0);
2037 }
2038 }
zhuoliang zhanga779d912020-10-23 09:05:35 +02002039 if (newspi) {
Fan Du283bc9f2013-11-07 17:47:50 +08002040 spin_lock_bh(&net->xfrm.xfrm_state_lock);
zhuoliang zhanga779d912020-10-23 09:05:35 +02002041 x->id.spi = newspi;
Alexey Dobriyan12604d82008-11-25 17:31:18 -08002042 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02002043 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
Fan Du283bc9f2013-11-07 17:47:50 +08002044 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07002045
2046 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Herbert Xu658b2192007-10-09 13:29:52 -07002048
2049unlock:
2050 spin_unlock_bh(&x->lock);
2051
2052 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054EXPORT_SYMBOL(xfrm_alloc_spi);
2055
Nicolas Dichteld3623092014-02-14 15:30:36 +01002056static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002057 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01002058{
2059 if (filter) {
2060 if ((filter->family == AF_INET ||
2061 filter->family == AF_INET6) &&
2062 x->props.family != filter->family)
2063 return false;
2064
2065 return addr_match(&x->props.saddr, &filter->saddr,
2066 filter->splen) &&
2067 addr_match(&x->id.daddr, &filter->daddr,
2068 filter->dplen);
2069 }
2070 return true;
2071}
2072
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002073int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08002074 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 void *data)
2076{
Herbert Xu12a169e2008-10-01 07:03:24 -07002077 struct xfrm_state *state;
2078 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int err = 0;
2080
Herbert Xu12a169e2008-10-01 07:03:24 -07002081 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08002082 return 0;
2083
Fan Du283bc9f2013-11-07 17:47:50 +08002084 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002085 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002086 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07002087 else
Li RongQing80077702015-04-22 17:09:54 +08002088 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002089 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07002090 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08002091 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002092 state = container_of(x, struct xfrm_state, km);
2093 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08002094 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002095 if (!__xfrm_state_filter_match(state, walk->filter))
2096 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002097 err = func(state, walk->seq, data);
2098 if (err) {
2099 list_move_tail(&walk->all, &x->all);
2100 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002102 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002104 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 err = -ENOENT;
2106 goto out;
2107 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002108 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109out:
Fan Du283bc9f2013-11-07 17:47:50 +08002110 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return err;
2112}
2113EXPORT_SYMBOL(xfrm_state_walk);
2114
Nicolas Dichteld3623092014-02-14 15:30:36 +01002115void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002116 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07002117{
Herbert Xu12a169e2008-10-01 07:03:24 -07002118 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07002119 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07002120 walk->state = XFRM_STATE_DEAD;
2121 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002122 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07002123}
2124EXPORT_SYMBOL(xfrm_state_walk_init);
2125
Fan Du283bc9f2013-11-07 17:47:50 +08002126void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07002127{
Nicolas Dichteld3623092014-02-14 15:30:36 +01002128 kfree(walk->filter);
2129
Herbert Xu12a169e2008-10-01 07:03:24 -07002130 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07002131 return;
Herbert Xu5c182452008-09-22 19:48:19 -07002132
Fan Du283bc9f2013-11-07 17:47:50 +08002133 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002134 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08002135 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07002136}
2137EXPORT_SYMBOL(xfrm_state_walk_done);
2138
Kees Cooke99e88a2017-10-16 14:43:17 -07002139static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002140{
Kees Cooke99e88a2017-10-16 14:43:17 -07002141 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002142
2143 spin_lock(&x->lock);
2144
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002145 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002146 if (xfrm_aevent_is_on(xs_net(x)))
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002147 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002148 else
2149 x->xflags |= XFRM_TIME_DEFER;
2150 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002151
2152 spin_unlock(&x->lock);
2153}
2154
Denis Chengdf018122007-12-07 00:51:11 -08002155static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
David S. Miller214e0052011-02-24 00:02:38 -05002157void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159 struct xfrm_mgr *km;
2160
Cong Wang85168c02013-01-16 16:05:06 +08002161 rcu_read_lock();
2162 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002163 if (km->notify_policy)
2164 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08002165 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002166}
2167
David S. Miller214e0052011-02-24 00:02:38 -05002168void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002169{
2170 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08002171 rcu_read_lock();
2172 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002173 if (km->notify)
2174 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08002175 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002176}
2177
2178EXPORT_SYMBOL(km_policy_notify);
2179EXPORT_SYMBOL(km_state_notify);
2180
Eric W. Biederman15e47302012-09-07 20:12:54 +00002181void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002182{
2183 struct km_event c;
2184
Herbert Xubf08867f92005-06-18 22:44:00 -07002185 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002186 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002187 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002188 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002191EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002192/*
2193 * We send to all registered managers regardless of failure
2194 * We are happy with one success
2195*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002196int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002198 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct xfrm_mgr *km;
2200
Cong Wang85168c02013-01-16 16:05:06 +08002201 rcu_read_lock();
2202 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08002203 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002204 if (!acqret)
2205 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Cong Wang85168c02013-01-16 16:05:06 +08002207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return err;
2209}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002210EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Al Viro5d36b182006-11-08 00:24:06 -08002212int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int err = -EINVAL;
2215 struct xfrm_mgr *km;
2216
Cong Wang85168c02013-01-16 16:05:06 +08002217 rcu_read_lock();
2218 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (km->new_mapping)
2220 err = km->new_mapping(x, ipaddr, sport);
2221 if (!err)
2222 break;
2223 }
Cong Wang85168c02013-01-16 16:05:06 +08002224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return err;
2226}
2227EXPORT_SYMBOL(km_new_mapping);
2228
Eric W. Biederman15e47302012-09-07 20:12:54 +00002229void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002231 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Herbert Xubf08867f92005-06-18 22:44:00 -07002233 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002234 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002235 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002236 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
David S. Millera70fcb02006-03-20 19:18:52 -08002238EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002240#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002241int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2242 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002243 const struct xfrm_kmaddress *k,
2244 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002245{
2246 int err = -EINVAL;
2247 int ret;
2248 struct xfrm_mgr *km;
2249
Cong Wang85168c02013-01-16 16:05:06 +08002250 rcu_read_lock();
2251 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002252 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002253 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2254 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002255 if (!ret)
2256 err = ret;
2257 }
2258 }
Cong Wang85168c02013-01-16 16:05:06 +08002259 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002260 return err;
2261}
2262EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002263#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002264
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002265int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002266{
2267 int err = -EINVAL;
2268 int ret;
2269 struct xfrm_mgr *km;
2270
Cong Wang85168c02013-01-16 16:05:06 +08002271 rcu_read_lock();
2272 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002273 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002274 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002275 if (!ret)
2276 err = ret;
2277 }
2278 }
Cong Wang85168c02013-01-16 16:05:06 +08002279 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002280 return err;
2281}
2282EXPORT_SYMBOL(km_report);
2283
Florian Westphalbb9cd072019-04-17 11:45:13 +02002284static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002285{
2286 struct xfrm_mgr *km;
2287 bool is_alive = false;
2288
2289 rcu_read_lock();
2290 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2291 if (km->is_alive && km->is_alive(c)) {
2292 is_alive = true;
2293 break;
2294 }
2295 }
2296 rcu_read_unlock();
2297
2298 return is_alive;
2299}
Horia Geanta0f245582014-02-12 16:20:06 +02002300
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002301int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 int err;
2304 u8 *data;
2305 struct xfrm_mgr *km;
2306 struct xfrm_policy *pol = NULL;
2307
Steffen Klassert19d7df62018-02-01 08:49:23 +01002308 if (in_compat_syscall())
2309 return -EOPNOTSUPP;
Steffen Klassert19d7df62018-02-01 08:49:23 +01002310
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002311 if (sockptr_is_null(optval) && !optlen) {
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002312 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2313 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2314 __sk_dst_reset(sk);
2315 return 0;
2316 }
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (optlen <= 0 || optlen > PAGE_SIZE)
2319 return -EMSGSIZE;
2320
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002321 data = memdup_sockptr(optval, optlen);
Geliang Tanga133d932017-05-06 23:42:21 +08002322 if (IS_ERR(data))
2323 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002326 rcu_read_lock();
2327 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002328 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 optlen, &err);
2330 if (err >= 0)
2331 break;
2332 }
Cong Wang85168c02013-01-16 16:05:06 +08002333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 if (err >= 0) {
2336 xfrm_sk_policy_insert(sk, err, pol);
2337 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002338 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 err = 0;
2340 }
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 kfree(data);
2343 return err;
2344}
2345EXPORT_SYMBOL(xfrm_user_policy);
2346
Cong Wang85168c02013-01-16 16:05:06 +08002347static DEFINE_SPINLOCK(xfrm_km_lock);
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349int xfrm_register_km(struct xfrm_mgr *km)
2350{
Cong Wang85168c02013-01-16 16:05:06 +08002351 spin_lock_bh(&xfrm_km_lock);
2352 list_add_tail_rcu(&km->list, &xfrm_km_list);
2353 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return 0;
2355}
2356EXPORT_SYMBOL(xfrm_register_km);
2357
2358int xfrm_unregister_km(struct xfrm_mgr *km)
2359{
Cong Wang85168c02013-01-16 16:05:06 +08002360 spin_lock_bh(&xfrm_km_lock);
2361 list_del_rcu(&km->list);
2362 spin_unlock_bh(&xfrm_km_lock);
2363 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return 0;
2365}
2366EXPORT_SYMBOL(xfrm_unregister_km);
2367
2368int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2369{
2370 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002371
2372 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002374
Cong Wang44abdc32013-01-16 16:05:05 +08002375 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002377 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002378 else
Cong Wang44abdc32013-01-16 16:05:05 +08002379 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2380 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return err;
2382}
2383EXPORT_SYMBOL(xfrm_state_register_afinfo);
2384
2385int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2386{
Florian Westphal423826a2017-01-09 14:20:46 +01002387 int err = 0, family = afinfo->family;
2388
2389 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002391
Cong Wang44abdc32013-01-16 16:05:05 +08002392 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002394 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002396 else
Cong Wang44abdc32013-01-16 16:05:05 +08002397 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Cong Wang44abdc32013-01-16 16:05:05 +08002399 spin_unlock_bh(&xfrm_state_afinfo_lock);
2400 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return err;
2402}
2403EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2404
Florian Westphal711059b2017-01-09 14:20:48 +01002405struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2406{
2407 if (unlikely(family >= NPROTO))
2408 return NULL;
2409
2410 return rcu_dereference(xfrm_state_afinfo[family]);
2411}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002412EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002413
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002414struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
2416 struct xfrm_state_afinfo *afinfo;
2417 if (unlikely(family >= NPROTO))
2418 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002419 rcu_read_lock();
2420 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002421 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002422 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return afinfo;
2424}
2425
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002426void xfrm_flush_gc(void)
2427{
2428 flush_work(&xfrm_state_gc_work);
2429}
2430EXPORT_SYMBOL(xfrm_flush_gc);
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2433void xfrm_state_delete_tunnel(struct xfrm_state *x)
2434{
2435 if (x->tunnel) {
2436 struct xfrm_state *t = x->tunnel;
2437
2438 if (atomic_read(&t->tunnel_users) == 2)
2439 xfrm_state_delete(t);
2440 atomic_dec(&t->tunnel_users);
Cong Wangf75a2802019-01-31 13:05:49 -08002441 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 x->tunnel = NULL;
2443 }
2444}
2445EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2446
Florian Westphalc7b37c72019-06-24 22:04:48 +02002447u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002449 const struct xfrm_type *type = READ_ONCE(x->type);
Florian Westphalc7b37c72019-06-24 22:04:48 +02002450 struct crypto_aead *aead;
2451 u32 blksize, net_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Florian Westphalc7b37c72019-06-24 22:04:48 +02002453 if (x->km.state != XFRM_STATE_VALID ||
2454 !type || type->proto != IPPROTO_ESP)
2455 return mtu - x->props.header_len;
Florian Westphalb3b73b82017-01-05 13:23:58 +01002456
Florian Westphalc7b37c72019-06-24 22:04:48 +02002457 aead = x->data;
2458 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2459
2460 switch (x->props.mode) {
2461 case XFRM_MODE_TRANSPORT:
2462 case XFRM_MODE_BEET:
2463 if (x->props.family == AF_INET)
2464 net_adj = sizeof(struct iphdr);
2465 else if (x->props.family == AF_INET6)
2466 net_adj = sizeof(struct ipv6hdr);
2467 break;
2468 case XFRM_MODE_TUNNEL:
2469 break;
2470 default:
2471 WARN_ON_ONCE(1);
2472 break;
2473 }
2474
2475 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2476 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
Florian Westphalc7b37c72019-06-24 22:04:48 +02002478EXPORT_SYMBOL_GPL(xfrm_state_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Ilan Tayariffdb5212017-08-01 12:49:08 +03002480int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
Herbert Xu72cb6962005-06-20 13:18:08 -07002481{
Florian Westphalc9500d72019-03-29 21:16:32 +01002482 const struct xfrm_mode *inner_mode;
2483 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002484 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002485 int err;
2486
Florian Westphale4681742019-05-03 17:46:16 +02002487 if (family == AF_INET &&
2488 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2489 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Herbert Xud094cd82005-06-20 13:19:41 -07002490
2491 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002492
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002493 if (x->sel.family != AF_UNSPEC) {
2494 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2495 if (inner_mode == NULL)
2496 goto error;
2497
2498 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Florian Westphal4c145dc2019-03-29 21:16:31 +01002499 family != x->sel.family)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002500 goto error;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002501
Florian Westphalc9500d72019-03-29 21:16:32 +01002502 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002503 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002504 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002505 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002506
Martin Willid81d2282008-12-03 15:38:07 -08002507 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002508 if (inner_mode == NULL)
2509 goto error;
2510
Florian Westphal4c145dc2019-03-29 21:16:31 +01002511 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002512 goto error;
Florian Westphal4c145dc2019-03-29 21:16:31 +01002513
Florian Westphalc9500d72019-03-29 21:16:32 +01002514 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002515
Martin Willid81d2282008-12-03 15:38:07 -08002516 if (x->props.family == AF_INET)
2517 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002518
Martin Willid81d2282008-12-03 15:38:07 -08002519 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2520 if (inner_mode_iaf) {
2521 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002522 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002523 }
2524 }
Herbert Xu13996372007-10-17 21:35:51 -07002525
Herbert Xud094cd82005-06-20 13:19:41 -07002526 x->type = xfrm_get_type(x->id.proto, family);
Herbert Xu72cb6962005-06-20 13:18:08 -07002527 if (x->type == NULL)
2528 goto error;
2529
Ilan Tayariffdb5212017-08-01 12:49:08 +03002530 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002531
Herbert Xu72cb6962005-06-20 13:18:08 -07002532 err = x->type->init_state(x);
2533 if (err)
2534 goto error;
2535
Florian Westphalc9500d72019-03-29 21:16:32 +01002536 outer_mode = xfrm_get_mode(x->props.mode, family);
2537 if (!outer_mode) {
Julia Lawall599901c2012-08-29 06:49:15 +00002538 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002539 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002540 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002541
Florian Westphalc9500d72019-03-29 21:16:32 +01002542 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002543 if (init_replay) {
2544 err = xfrm_init_replay(x);
2545 if (err)
2546 goto error;
2547 }
2548
Herbert Xu72cb6962005-06-20 13:18:08 -07002549error:
2550 return err;
2551}
2552
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002553EXPORT_SYMBOL(__xfrm_init_state);
2554
2555int xfrm_init_state(struct xfrm_state *x)
2556{
Yossi Kupermancc015722018-01-17 15:52:41 +02002557 int err;
2558
2559 err = __xfrm_init_state(x, true, false);
2560 if (!err)
2561 x->km.state = XFRM_STATE_VALID;
2562
2563 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002564}
2565
Herbert Xu72cb6962005-06-20 13:18:08 -07002566EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002567
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002568int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
David S. Millerf034b5d2006-08-24 03:08:07 -07002570 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Mathias Krause565f0fa2018-05-03 10:55:07 +02002572 if (net_eq(net, &init_net))
2573 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2574 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2575
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002576 INIT_LIST_HEAD(&net->xfrm.state_all);
2577
David S. Millerf034b5d2006-08-24 03:08:07 -07002578 sz = sizeof(struct hlist_head) * 8;
2579
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002580 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2581 if (!net->xfrm.state_bydst)
2582 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002583 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2584 if (!net->xfrm.state_bysrc)
2585 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002586 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2587 if (!net->xfrm.state_byspi)
2588 goto out_byspi;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002589 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002590
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002591 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002592 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002593 spin_lock_init(&net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002594 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002595
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002596out_byspi:
2597 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002598out_bysrc:
2599 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002600out_bydst:
2601 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002602}
2603
2604void xfrm_state_fini(struct net *net)
2605{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002606 unsigned int sz;
2607
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002608 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002609 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002610 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002611
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002612 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002613
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002614 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002615 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2616 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002617 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2618 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002619 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2620 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Joy Lattenab5f5e82007-09-17 11:51:22 -07002623#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002624static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2625 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002626{
Paul Moore68277ac2007-12-20 20:49:33 -08002627 struct xfrm_sec_ctx *ctx = x->security;
2628 u32 spi = ntohl(x->id.spi);
2629
2630 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002631 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002632 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002633
Weilong Chen9b7a7872013-12-24 09:43:46 +08002634 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002635 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002636 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2637 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002638 break;
2639 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002640 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002641 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002642 break;
2643 }
Paul Moore68277ac2007-12-20 20:49:33 -08002644
2645 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002646}
2647
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002648static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2649 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002650{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002651 const struct iphdr *iph4;
2652 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002653
2654 switch (family) {
2655 case AF_INET:
2656 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002657 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2658 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002659 break;
2660 case AF_INET6:
2661 iph6 = ipv6_hdr(skb);
2662 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002663 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002664 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002665 iph6->flow_lbl[0] & 0x0f,
2666 iph6->flow_lbl[1],
2667 iph6->flow_lbl[2]);
2668 break;
2669 }
2670}
2671
Tetsuo Handa2e710292014-04-22 21:48:30 +09002672void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002673{
2674 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002675
Paul Mooreafeb14b2007-12-21 14:58:11 -08002676 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002677 if (audit_buf == NULL)
2678 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002679 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002680 xfrm_audit_helper_sainfo(x, audit_buf);
2681 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002682 audit_log_end(audit_buf);
2683}
2684EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2685
Tetsuo Handa2e710292014-04-22 21:48:30 +09002686void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002687{
2688 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002689
Paul Mooreafeb14b2007-12-21 14:58:11 -08002690 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002691 if (audit_buf == NULL)
2692 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002693 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002694 xfrm_audit_helper_sainfo(x, audit_buf);
2695 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002696 audit_log_end(audit_buf);
2697}
2698EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002699
2700void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2701 struct sk_buff *skb)
2702{
2703 struct audit_buffer *audit_buf;
2704 u32 spi;
2705
2706 audit_buf = xfrm_audit_start("SA-replay-overflow");
2707 if (audit_buf == NULL)
2708 return;
2709 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2710 /* don't record the sequence number because it's inherent in this kind
2711 * of audit message */
2712 spi = ntohl(x->id.spi);
2713 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2714 audit_log_end(audit_buf);
2715}
2716EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2717
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002718void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002719 struct sk_buff *skb, __be32 net_seq)
2720{
2721 struct audit_buffer *audit_buf;
2722 u32 spi;
2723
2724 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2725 if (audit_buf == NULL)
2726 return;
2727 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2728 spi = ntohl(x->id.spi);
2729 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2730 spi, spi, ntohl(net_seq));
2731 audit_log_end(audit_buf);
2732}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00002733EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002734
2735void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2736{
2737 struct audit_buffer *audit_buf;
2738
2739 audit_buf = xfrm_audit_start("SA-notfound");
2740 if (audit_buf == NULL)
2741 return;
2742 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2743 audit_log_end(audit_buf);
2744}
2745EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2746
2747void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2748 __be32 net_spi, __be32 net_seq)
2749{
2750 struct audit_buffer *audit_buf;
2751 u32 spi;
2752
2753 audit_buf = xfrm_audit_start("SA-notfound");
2754 if (audit_buf == NULL)
2755 return;
2756 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2757 spi = ntohl(net_spi);
2758 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2759 spi, spi, ntohl(net_seq));
2760 audit_log_end(audit_buf);
2761}
2762EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2763
2764void xfrm_audit_state_icvfail(struct xfrm_state *x,
2765 struct sk_buff *skb, u8 proto)
2766{
2767 struct audit_buffer *audit_buf;
2768 __be32 net_spi;
2769 __be32 net_seq;
2770
2771 audit_buf = xfrm_audit_start("SA-icv-failure");
2772 if (audit_buf == NULL)
2773 return;
2774 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2775 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2776 u32 spi = ntohl(net_spi);
2777 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2778 spi, spi, ntohl(net_seq));
2779 }
2780 audit_log_end(audit_buf);
2781}
2782EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002783#endif /* CONFIG_AUDITSYSCALL */