xref: /linux/net/xfrm/xfrm_state.c (revision 8e621c9a337555c914cf1664605edfaa6f839774)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
14  *
15  */
16 
17 #include <linux/compat.h>
18 #include <linux/workqueue.h>
19 #include <net/xfrm.h>
20 #include <linux/pfkeyv2.h>
21 #include <linux/ipsec.h>
22 #include <linux/module.h>
23 #include <linux/cache.h>
24 #include <linux/audit.h>
25 #include <linux/uaccess.h>
26 #include <linux/ktime.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 
31 #include <crypto/aead.h>
32 
33 #include "xfrm_hash.h"
34 
35 #define xfrm_state_deref_prot(table, net) \
36 	rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
37 #define xfrm_state_deref_check(table, net) \
38 	rcu_dereference_check((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
39 
40 static void xfrm_state_gc_task(struct work_struct *work);
41 
42 /* Each xfrm_state may be linked to two tables:
43 
44    1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
45    2. Hash table by (daddr,family,reqid) to find what SAs exist for given
46       destination/tunnel endpoint. (output)
47  */
48 
49 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
50 static struct kmem_cache *xfrm_state_cache __ro_after_init;
51 
52 static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
53 static HLIST_HEAD(xfrm_state_gc_list);
54 static HLIST_HEAD(xfrm_state_dev_gc_list);
55 
xfrm_state_hold_rcu(struct xfrm_state __rcu * x)56 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
57 {
58 	return refcount_inc_not_zero(&x->refcnt);
59 }
60 
xfrm_dst_hash(struct net * net,const xfrm_address_t * daddr,const xfrm_address_t * saddr,u32 reqid,unsigned short family)61 static inline unsigned int xfrm_dst_hash(struct net *net,
62 					 const xfrm_address_t *daddr,
63 					 const xfrm_address_t *saddr,
64 					 u32 reqid,
65 					 unsigned short family)
66 {
67 	lockdep_assert_held(&net->xfrm.xfrm_state_lock);
68 
69 	return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
70 }
71 
xfrm_src_hash(struct net * net,const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family)72 static inline unsigned int xfrm_src_hash(struct net *net,
73 					 const xfrm_address_t *daddr,
74 					 const xfrm_address_t *saddr,
75 					 unsigned short family)
76 {
77 	lockdep_assert_held(&net->xfrm.xfrm_state_lock);
78 
79 	return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
80 }
81 
82 static inline unsigned int
xfrm_spi_hash(struct net * net,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)83 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
84 	      __be32 spi, u8 proto, unsigned short family)
85 {
86 	lockdep_assert_held(&net->xfrm.xfrm_state_lock);
87 
88 	return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
89 }
90 
xfrm_seq_hash(struct net * net,u32 seq)91 static unsigned int xfrm_seq_hash(struct net *net, u32 seq)
92 {
93 	lockdep_assert_held(&net->xfrm.xfrm_state_lock);
94 
95 	return __xfrm_seq_hash(seq, net->xfrm.state_hmask);
96 }
97 
98 #define XFRM_STATE_INSERT(by, _n, _h, _type)                               \
99 	{                                                                  \
100 		struct xfrm_state *_x = NULL;                              \
101 									   \
102 		if (_type != XFRM_DEV_OFFLOAD_PACKET) {                    \
103 			hlist_for_each_entry_rcu(_x, _h, by) {             \
104 				if (_x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
105 					continue;                          \
106 				break;                                     \
107 			}                                                  \
108 		}                                                          \
109 									   \
110 		if (!_x || _x->xso.type == XFRM_DEV_OFFLOAD_PACKET)        \
111 			/* SAD is empty or consist from HW SAs only */     \
112 			hlist_add_head_rcu(_n, _h);                        \
113 		else                                                       \
114 			hlist_add_before_rcu(_n, &_x->by);                 \
115 	}
116 
xfrm_hash_transfer(struct hlist_head * list,struct hlist_head * ndsttable,struct hlist_head * nsrctable,struct hlist_head * nspitable,struct hlist_head * nseqtable,unsigned int nhashmask)117 static void xfrm_hash_transfer(struct hlist_head *list,
118 			       struct hlist_head *ndsttable,
119 			       struct hlist_head *nsrctable,
120 			       struct hlist_head *nspitable,
121 			       struct hlist_head *nseqtable,
122 			       unsigned int nhashmask)
123 {
124 	struct hlist_node *tmp;
125 	struct xfrm_state *x;
126 
127 	hlist_for_each_entry_safe(x, tmp, list, bydst) {
128 		unsigned int h;
129 
130 		h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
131 				    x->props.reqid, x->props.family,
132 				    nhashmask);
133 		XFRM_STATE_INSERT(bydst, &x->bydst, ndsttable + h, x->xso.type);
134 
135 		h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
136 				    x->props.family,
137 				    nhashmask);
138 		XFRM_STATE_INSERT(bysrc, &x->bysrc, nsrctable + h, x->xso.type);
139 
140 		if (x->id.spi) {
141 			h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
142 					    x->id.proto, x->props.family,
143 					    nhashmask);
144 			XFRM_STATE_INSERT(byspi, &x->byspi, nspitable + h,
145 					  x->xso.type);
146 		}
147 
148 		if (x->km.seq) {
149 			h = __xfrm_seq_hash(x->km.seq, nhashmask);
150 			XFRM_STATE_INSERT(byseq, &x->byseq, nseqtable + h,
151 					  x->xso.type);
152 		}
153 	}
154 }
155 
xfrm_hash_new_size(unsigned int state_hmask)156 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
157 {
158 	return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
159 }
160 
xfrm_hash_resize(struct work_struct * work)161 static void xfrm_hash_resize(struct work_struct *work)
162 {
163 	struct net *net = container_of(work, struct net, xfrm.state_hash_work);
164 	struct hlist_head *ndst, *nsrc, *nspi, *nseq, *odst, *osrc, *ospi, *oseq;
165 	unsigned long nsize, osize;
166 	unsigned int nhashmask, ohashmask;
167 	int i;
168 
169 	nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
170 	ndst = xfrm_hash_alloc(nsize);
171 	if (!ndst)
172 		return;
173 	nsrc = xfrm_hash_alloc(nsize);
174 	if (!nsrc) {
175 		xfrm_hash_free(ndst, nsize);
176 		return;
177 	}
178 	nspi = xfrm_hash_alloc(nsize);
179 	if (!nspi) {
180 		xfrm_hash_free(ndst, nsize);
181 		xfrm_hash_free(nsrc, nsize);
182 		return;
183 	}
184 	nseq = xfrm_hash_alloc(nsize);
185 	if (!nseq) {
186 		xfrm_hash_free(ndst, nsize);
187 		xfrm_hash_free(nsrc, nsize);
188 		xfrm_hash_free(nspi, nsize);
189 		return;
190 	}
191 
192 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
193 	write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
194 
195 	nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
196 	odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
197 	for (i = net->xfrm.state_hmask; i >= 0; i--)
198 		xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nseq, nhashmask);
199 
200 	osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
201 	ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
202 	oseq = xfrm_state_deref_prot(net->xfrm.state_byseq, net);
203 	ohashmask = net->xfrm.state_hmask;
204 
205 	rcu_assign_pointer(net->xfrm.state_bydst, ndst);
206 	rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
207 	rcu_assign_pointer(net->xfrm.state_byspi, nspi);
208 	rcu_assign_pointer(net->xfrm.state_byseq, nseq);
209 	net->xfrm.state_hmask = nhashmask;
210 
211 	write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
212 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
213 
214 	osize = (ohashmask + 1) * sizeof(struct hlist_head);
215 
216 	synchronize_rcu();
217 
218 	xfrm_hash_free(odst, osize);
219 	xfrm_hash_free(osrc, osize);
220 	xfrm_hash_free(ospi, osize);
221 	xfrm_hash_free(oseq, osize);
222 }
223 
224 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
225 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
226 
227 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
228 static DEFINE_SPINLOCK(xfrm_state_dev_gc_lock);
229 
230 int __xfrm_state_delete(struct xfrm_state *x);
231 
232 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
233 static bool km_is_alive(const struct km_event *c);
234 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
235 
xfrm_register_type(const struct xfrm_type * type,unsigned short family)236 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
237 {
238 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
239 	int err = 0;
240 
241 	if (!afinfo)
242 		return -EAFNOSUPPORT;
243 
244 #define X(afi, T, name) do {			\
245 		WARN_ON((afi)->type_ ## name);	\
246 		(afi)->type_ ## name = (T);	\
247 	} while (0)
248 
249 	switch (type->proto) {
250 	case IPPROTO_COMP:
251 		X(afinfo, type, comp);
252 		break;
253 	case IPPROTO_AH:
254 		X(afinfo, type, ah);
255 		break;
256 	case IPPROTO_ESP:
257 		X(afinfo, type, esp);
258 		break;
259 	case IPPROTO_IPIP:
260 		X(afinfo, type, ipip);
261 		break;
262 	case IPPROTO_DSTOPTS:
263 		X(afinfo, type, dstopts);
264 		break;
265 	case IPPROTO_ROUTING:
266 		X(afinfo, type, routing);
267 		break;
268 	case IPPROTO_IPV6:
269 		X(afinfo, type, ipip6);
270 		break;
271 	default:
272 		WARN_ON(1);
273 		err = -EPROTONOSUPPORT;
274 		break;
275 	}
276 #undef X
277 	rcu_read_unlock();
278 	return err;
279 }
280 EXPORT_SYMBOL(xfrm_register_type);
281 
xfrm_unregister_type(const struct xfrm_type * type,unsigned short family)282 void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
283 {
284 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
285 
286 	if (unlikely(afinfo == NULL))
287 		return;
288 
289 #define X(afi, T, name) do {				\
290 		WARN_ON((afi)->type_ ## name != (T));	\
291 		(afi)->type_ ## name = NULL;		\
292 	} while (0)
293 
294 	switch (type->proto) {
295 	case IPPROTO_COMP:
296 		X(afinfo, type, comp);
297 		break;
298 	case IPPROTO_AH:
299 		X(afinfo, type, ah);
300 		break;
301 	case IPPROTO_ESP:
302 		X(afinfo, type, esp);
303 		break;
304 	case IPPROTO_IPIP:
305 		X(afinfo, type, ipip);
306 		break;
307 	case IPPROTO_DSTOPTS:
308 		X(afinfo, type, dstopts);
309 		break;
310 	case IPPROTO_ROUTING:
311 		X(afinfo, type, routing);
312 		break;
313 	case IPPROTO_IPV6:
314 		X(afinfo, type, ipip6);
315 		break;
316 	default:
317 		WARN_ON(1);
318 		break;
319 	}
320 #undef X
321 	rcu_read_unlock();
322 }
323 EXPORT_SYMBOL(xfrm_unregister_type);
324 
xfrm_get_type(u8 proto,unsigned short family)325 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
326 {
327 	const struct xfrm_type *type = NULL;
328 	struct xfrm_state_afinfo *afinfo;
329 	int modload_attempted = 0;
330 
331 retry:
332 	afinfo = xfrm_state_get_afinfo(family);
333 	if (unlikely(afinfo == NULL))
334 		return NULL;
335 
336 	switch (proto) {
337 	case IPPROTO_COMP:
338 		type = afinfo->type_comp;
339 		break;
340 	case IPPROTO_AH:
341 		type = afinfo->type_ah;
342 		break;
343 	case IPPROTO_ESP:
344 		type = afinfo->type_esp;
345 		break;
346 	case IPPROTO_IPIP:
347 		type = afinfo->type_ipip;
348 		break;
349 	case IPPROTO_DSTOPTS:
350 		type = afinfo->type_dstopts;
351 		break;
352 	case IPPROTO_ROUTING:
353 		type = afinfo->type_routing;
354 		break;
355 	case IPPROTO_IPV6:
356 		type = afinfo->type_ipip6;
357 		break;
358 	default:
359 		break;
360 	}
361 
362 	if (unlikely(type && !try_module_get(type->owner)))
363 		type = NULL;
364 
365 	rcu_read_unlock();
366 
367 	if (!type && !modload_attempted) {
368 		request_module("xfrm-type-%d-%d", family, proto);
369 		modload_attempted = 1;
370 		goto retry;
371 	}
372 
373 	return type;
374 }
375 
xfrm_put_type(const struct xfrm_type * type)376 static void xfrm_put_type(const struct xfrm_type *type)
377 {
378 	module_put(type->owner);
379 }
380 
xfrm_register_type_offload(const struct xfrm_type_offload * type,unsigned short family)381 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
382 			       unsigned short family)
383 {
384 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
385 	int err = 0;
386 
387 	if (unlikely(afinfo == NULL))
388 		return -EAFNOSUPPORT;
389 
390 	switch (type->proto) {
391 	case IPPROTO_ESP:
392 		WARN_ON(afinfo->type_offload_esp);
393 		afinfo->type_offload_esp = type;
394 		break;
395 	default:
396 		WARN_ON(1);
397 		err = -EPROTONOSUPPORT;
398 		break;
399 	}
400 
401 	rcu_read_unlock();
402 	return err;
403 }
404 EXPORT_SYMBOL(xfrm_register_type_offload);
405 
xfrm_unregister_type_offload(const struct xfrm_type_offload * type,unsigned short family)406 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
407 				  unsigned short family)
408 {
409 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
410 
411 	if (unlikely(afinfo == NULL))
412 		return;
413 
414 	switch (type->proto) {
415 	case IPPROTO_ESP:
416 		WARN_ON(afinfo->type_offload_esp != type);
417 		afinfo->type_offload_esp = NULL;
418 		break;
419 	default:
420 		WARN_ON(1);
421 		break;
422 	}
423 	rcu_read_unlock();
424 }
425 EXPORT_SYMBOL(xfrm_unregister_type_offload);
426 
xfrm_set_type_offload(struct xfrm_state * x,bool try_load)427 void xfrm_set_type_offload(struct xfrm_state *x, bool try_load)
428 {
429 	const struct xfrm_type_offload *type = NULL;
430 	struct xfrm_state_afinfo *afinfo;
431 
432 retry:
433 	afinfo = xfrm_state_get_afinfo(x->props.family);
434 	if (unlikely(afinfo == NULL))
435 		goto out;
436 
437 	switch (x->id.proto) {
438 	case IPPROTO_ESP:
439 		type = afinfo->type_offload_esp;
440 		break;
441 	default:
442 		break;
443 	}
444 
445 	if ((type && !try_module_get(type->owner)))
446 		type = NULL;
447 
448 	rcu_read_unlock();
449 
450 	if (!type && try_load) {
451 		request_module("xfrm-offload-%d-%d", x->props.family,
452 			       x->id.proto);
453 		try_load = false;
454 		goto retry;
455 	}
456 
457 out:
458 	x->type_offload = type;
459 }
460 EXPORT_SYMBOL(xfrm_set_type_offload);
461 
462 static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
463 	[XFRM_MODE_BEET] = {
464 		.encap = XFRM_MODE_BEET,
465 		.flags = XFRM_MODE_FLAG_TUNNEL,
466 		.family = AF_INET,
467 	},
468 	[XFRM_MODE_TRANSPORT] = {
469 		.encap = XFRM_MODE_TRANSPORT,
470 		.family = AF_INET,
471 	},
472 	[XFRM_MODE_TUNNEL] = {
473 		.encap = XFRM_MODE_TUNNEL,
474 		.flags = XFRM_MODE_FLAG_TUNNEL,
475 		.family = AF_INET,
476 	},
477 	[XFRM_MODE_IPTFS] = {
478 		.encap = XFRM_MODE_IPTFS,
479 		.flags = XFRM_MODE_FLAG_TUNNEL,
480 		.family = AF_INET,
481 	},
482 };
483 
484 static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
485 	[XFRM_MODE_BEET] = {
486 		.encap = XFRM_MODE_BEET,
487 		.flags = XFRM_MODE_FLAG_TUNNEL,
488 		.family = AF_INET6,
489 	},
490 	[XFRM_MODE_ROUTEOPTIMIZATION] = {
491 		.encap = XFRM_MODE_ROUTEOPTIMIZATION,
492 		.family = AF_INET6,
493 	},
494 	[XFRM_MODE_TRANSPORT] = {
495 		.encap = XFRM_MODE_TRANSPORT,
496 		.family = AF_INET6,
497 	},
498 	[XFRM_MODE_TUNNEL] = {
499 		.encap = XFRM_MODE_TUNNEL,
500 		.flags = XFRM_MODE_FLAG_TUNNEL,
501 		.family = AF_INET6,
502 	},
503 	[XFRM_MODE_IPTFS] = {
504 		.encap = XFRM_MODE_IPTFS,
505 		.flags = XFRM_MODE_FLAG_TUNNEL,
506 		.family = AF_INET6,
507 	},
508 };
509 
xfrm_get_mode(unsigned int encap,int family)510 static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
511 {
512 	const struct xfrm_mode *mode;
513 
514 	if (unlikely(encap >= XFRM_MODE_MAX))
515 		return NULL;
516 
517 	switch (family) {
518 	case AF_INET:
519 		mode = &xfrm4_mode_map[encap];
520 		if (mode->family == family)
521 			return mode;
522 		break;
523 	case AF_INET6:
524 		mode = &xfrm6_mode_map[encap];
525 		if (mode->family == family)
526 			return mode;
527 		break;
528 	default:
529 		break;
530 	}
531 
532 	return NULL;
533 }
534 
535 static const struct xfrm_mode_cbs  __rcu *xfrm_mode_cbs_map[XFRM_MODE_MAX];
536 static DEFINE_SPINLOCK(xfrm_mode_cbs_map_lock);
537 
xfrm_register_mode_cbs(u8 mode,const struct xfrm_mode_cbs * mode_cbs)538 int xfrm_register_mode_cbs(u8 mode, const struct xfrm_mode_cbs *mode_cbs)
539 {
540 	if (mode >= XFRM_MODE_MAX)
541 		return -EINVAL;
542 
543 	spin_lock_bh(&xfrm_mode_cbs_map_lock);
544 	rcu_assign_pointer(xfrm_mode_cbs_map[mode], mode_cbs);
545 	spin_unlock_bh(&xfrm_mode_cbs_map_lock);
546 
547 	return 0;
548 }
549 EXPORT_SYMBOL(xfrm_register_mode_cbs);
550 
xfrm_unregister_mode_cbs(u8 mode)551 void xfrm_unregister_mode_cbs(u8 mode)
552 {
553 	if (mode >= XFRM_MODE_MAX)
554 		return;
555 
556 	spin_lock_bh(&xfrm_mode_cbs_map_lock);
557 	RCU_INIT_POINTER(xfrm_mode_cbs_map[mode], NULL);
558 	spin_unlock_bh(&xfrm_mode_cbs_map_lock);
559 	synchronize_rcu();
560 }
561 EXPORT_SYMBOL(xfrm_unregister_mode_cbs);
562 
xfrm_get_mode_cbs(u8 mode)563 static const struct xfrm_mode_cbs *xfrm_get_mode_cbs(u8 mode)
564 {
565 	const struct xfrm_mode_cbs *cbs;
566 	bool try_load = true;
567 
568 	if (mode >= XFRM_MODE_MAX)
569 		return NULL;
570 
571 retry:
572 	rcu_read_lock();
573 
574 	cbs = rcu_dereference(xfrm_mode_cbs_map[mode]);
575 	if (cbs && !try_module_get(cbs->owner))
576 		cbs = NULL;
577 
578 	rcu_read_unlock();
579 
580 	if (mode == XFRM_MODE_IPTFS && !cbs && try_load) {
581 		request_module("xfrm-iptfs");
582 		try_load = false;
583 		goto retry;
584 	}
585 
586 	return cbs;
587 }
588 
xfrm_state_free(struct xfrm_state * x)589 void xfrm_state_free(struct xfrm_state *x)
590 {
591 	kmem_cache_free(xfrm_state_cache, x);
592 }
593 EXPORT_SYMBOL(xfrm_state_free);
594 
595 static void xfrm_state_delete_tunnel(struct xfrm_state *x);
xfrm_state_gc_destroy(struct xfrm_state * x)596 static void xfrm_state_gc_destroy(struct xfrm_state *x)
597 {
598 	if (x->mode_cbs && x->mode_cbs->destroy_state)
599 		x->mode_cbs->destroy_state(x);
600 	hrtimer_cancel(&x->mtimer);
601 	timer_delete_sync(&x->rtimer);
602 	kfree_sensitive(x->aead);
603 	kfree_sensitive(x->aalg);
604 	kfree_sensitive(x->ealg);
605 	kfree(x->calg);
606 	kfree(x->encap);
607 	kfree(x->coaddr);
608 	kfree(x->replay_esn);
609 	kfree(x->preplay_esn);
610 	xfrm_unset_type_offload(x);
611 	xfrm_state_delete_tunnel(x);
612 	if (x->type) {
613 		x->type->destructor(x);
614 		xfrm_put_type(x->type);
615 	}
616 	if (x->xfrag.page)
617 		put_page(x->xfrag.page);
618 	xfrm_dev_state_free(x);
619 	security_xfrm_state_free(x);
620 	xfrm_state_free(x);
621 }
622 
xfrm_state_gc_task(struct work_struct * work)623 static void xfrm_state_gc_task(struct work_struct *work)
624 {
625 	struct xfrm_state *x;
626 	struct hlist_node *tmp;
627 	struct hlist_head gc_list;
628 
629 	spin_lock_bh(&xfrm_state_gc_lock);
630 	hlist_move_list(&xfrm_state_gc_list, &gc_list);
631 	spin_unlock_bh(&xfrm_state_gc_lock);
632 
633 	synchronize_rcu();
634 
635 	hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
636 		xfrm_state_gc_destroy(x);
637 }
638 
xfrm_timer_handler(struct hrtimer * me)639 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
640 {
641 	struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
642 	enum hrtimer_restart ret = HRTIMER_NORESTART;
643 	time64_t now = ktime_get_real_seconds();
644 	time64_t next = TIME64_MAX;
645 	int warn = 0;
646 	int err = 0;
647 
648 	spin_lock(&x->lock);
649 	xfrm_dev_state_update_stats(x);
650 
651 	if (x->km.state == XFRM_STATE_DEAD)
652 		goto out;
653 	if (x->km.state == XFRM_STATE_EXPIRED)
654 		goto expired;
655 	if (x->lft.hard_add_expires_seconds) {
656 		time64_t tmo = x->lft.hard_add_expires_seconds +
657 			x->curlft.add_time - now;
658 		if (tmo <= 0) {
659 			if (x->xflags & XFRM_SOFT_EXPIRE) {
660 				/* enter hard expire without soft expire first?!
661 				 * setting a new date could trigger this.
662 				 * workaround: fix x->curflt.add_time by below:
663 				 */
664 				x->curlft.add_time = now - x->saved_tmo - 1;
665 				tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
666 			} else
667 				goto expired;
668 		}
669 		if (tmo < next)
670 			next = tmo;
671 	}
672 	if (x->lft.hard_use_expires_seconds) {
673 		time64_t tmo = x->lft.hard_use_expires_seconds +
674 			(READ_ONCE(x->curlft.use_time) ? : now) - now;
675 		if (tmo <= 0)
676 			goto expired;
677 		if (tmo < next)
678 			next = tmo;
679 	}
680 	if (x->km.dying)
681 		goto resched;
682 	if (x->lft.soft_add_expires_seconds) {
683 		time64_t tmo = x->lft.soft_add_expires_seconds +
684 			x->curlft.add_time - now;
685 		if (tmo <= 0) {
686 			warn = 1;
687 			x->xflags &= ~XFRM_SOFT_EXPIRE;
688 		} else if (tmo < next) {
689 			next = tmo;
690 			x->xflags |= XFRM_SOFT_EXPIRE;
691 			x->saved_tmo = tmo;
692 		}
693 	}
694 	if (x->lft.soft_use_expires_seconds) {
695 		time64_t tmo = x->lft.soft_use_expires_seconds +
696 			(READ_ONCE(x->curlft.use_time) ? : now) - now;
697 		if (tmo <= 0)
698 			warn = 1;
699 		else if (tmo < next)
700 			next = tmo;
701 	}
702 
703 	x->km.dying = warn;
704 	if (warn)
705 		km_state_expired(x, 0, 0);
706 resched:
707 	if (next != TIME64_MAX) {
708 		hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
709 		ret = HRTIMER_RESTART;
710 	}
711 
712 	goto out;
713 
714 expired:
715 	if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
716 		x->km.state = XFRM_STATE_EXPIRED;
717 
718 	err = __xfrm_state_delete(x);
719 	if (!err)
720 		km_state_expired(x, 1, 0);
721 
722 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
723 
724 out:
725 	spin_unlock(&x->lock);
726 	return ret;
727 }
728 
729 static void xfrm_replay_timer_handler(struct timer_list *t);
730 
xfrm_state_alloc(struct net * net)731 struct xfrm_state *xfrm_state_alloc(struct net *net)
732 {
733 	struct xfrm_state *x;
734 
735 	x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
736 
737 	if (x) {
738 		write_pnet(&x->xs_net, net);
739 		refcount_set(&x->refcnt, 1);
740 		atomic_set(&x->tunnel_users, 0);
741 		INIT_LIST_HEAD(&x->km.all);
742 		INIT_HLIST_NODE(&x->state_cache);
743 		INIT_HLIST_NODE(&x->bydst);
744 		INIT_HLIST_NODE(&x->bysrc);
745 		INIT_HLIST_NODE(&x->byspi);
746 		INIT_HLIST_NODE(&x->byseq);
747 		hrtimer_setup(&x->mtimer, xfrm_timer_handler, CLOCK_BOOTTIME,
748 			      HRTIMER_MODE_ABS_SOFT);
749 		timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
750 		x->curlft.add_time = ktime_get_real_seconds();
751 		x->lft.soft_byte_limit = XFRM_INF;
752 		x->lft.soft_packet_limit = XFRM_INF;
753 		x->lft.hard_byte_limit = XFRM_INF;
754 		x->lft.hard_packet_limit = XFRM_INF;
755 		x->replay_maxage = 0;
756 		x->replay_maxdiff = 0;
757 		x->pcpu_num = UINT_MAX;
758 		spin_lock_init(&x->lock);
759 		x->mode_data = NULL;
760 	}
761 	return x;
762 }
763 EXPORT_SYMBOL(xfrm_state_alloc);
764 
765 #ifdef CONFIG_XFRM_OFFLOAD
xfrm_dev_state_delete(struct xfrm_state * x)766 void xfrm_dev_state_delete(struct xfrm_state *x)
767 {
768 	struct xfrm_dev_offload *xso = &x->xso;
769 	struct net_device *dev = READ_ONCE(xso->dev);
770 
771 	if (dev) {
772 		dev->xfrmdev_ops->xdo_dev_state_delete(dev, x);
773 		spin_lock_bh(&xfrm_state_dev_gc_lock);
774 		hlist_add_head(&x->dev_gclist, &xfrm_state_dev_gc_list);
775 		spin_unlock_bh(&xfrm_state_dev_gc_lock);
776 	}
777 }
778 EXPORT_SYMBOL_GPL(xfrm_dev_state_delete);
779 
xfrm_dev_state_free(struct xfrm_state * x)780 void xfrm_dev_state_free(struct xfrm_state *x)
781 {
782 	struct xfrm_dev_offload *xso = &x->xso;
783 	struct net_device *dev = READ_ONCE(xso->dev);
784 
785 	if (dev && dev->xfrmdev_ops) {
786 		spin_lock_bh(&xfrm_state_dev_gc_lock);
787 		if (!hlist_unhashed(&x->dev_gclist))
788 			hlist_del(&x->dev_gclist);
789 		spin_unlock_bh(&xfrm_state_dev_gc_lock);
790 
791 		if (dev->xfrmdev_ops->xdo_dev_state_free)
792 			dev->xfrmdev_ops->xdo_dev_state_free(dev, x);
793 		WRITE_ONCE(xso->dev, NULL);
794 		xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
795 		netdev_put(dev, &xso->dev_tracker);
796 	}
797 }
798 #endif
799 
__xfrm_state_destroy(struct xfrm_state * x)800 void __xfrm_state_destroy(struct xfrm_state *x)
801 {
802 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
803 
804 	spin_lock_bh(&xfrm_state_gc_lock);
805 	hlist_add_head(&x->gclist, &xfrm_state_gc_list);
806 	spin_unlock_bh(&xfrm_state_gc_lock);
807 	schedule_work(&xfrm_state_gc_work);
808 }
809 EXPORT_SYMBOL(__xfrm_state_destroy);
810 
__xfrm_state_delete(struct xfrm_state * x)811 int __xfrm_state_delete(struct xfrm_state *x)
812 {
813 	struct net *net = xs_net(x);
814 	int err = -ESRCH;
815 
816 	if (x->km.state != XFRM_STATE_DEAD) {
817 		x->km.state = XFRM_STATE_DEAD;
818 
819 		spin_lock(&net->xfrm.xfrm_state_lock);
820 		list_del(&x->km.all);
821 		hlist_del_rcu(&x->bydst);
822 		hlist_del_rcu(&x->bysrc);
823 		if (x->km.seq)
824 			hlist_del_rcu(&x->byseq);
825 		if (!hlist_unhashed(&x->state_cache))
826 			hlist_del_rcu(&x->state_cache);
827 		if (!hlist_unhashed(&x->state_cache_input))
828 			hlist_del_rcu(&x->state_cache_input);
829 
830 		if (x->id.spi)
831 			hlist_del_rcu(&x->byspi);
832 		net->xfrm.state_num--;
833 		xfrm_nat_keepalive_state_updated(x);
834 		spin_unlock(&net->xfrm.xfrm_state_lock);
835 
836 		xfrm_dev_state_delete(x);
837 
838 		xfrm_state_delete_tunnel(x);
839 
840 		/* All xfrm_state objects are created by xfrm_state_alloc.
841 		 * The xfrm_state_alloc call gives a reference, and that
842 		 * is what we are dropping here.
843 		 */
844 		xfrm_state_put(x);
845 		err = 0;
846 	}
847 
848 	return err;
849 }
850 EXPORT_SYMBOL(__xfrm_state_delete);
851 
xfrm_state_delete(struct xfrm_state * x)852 int xfrm_state_delete(struct xfrm_state *x)
853 {
854 	int err;
855 
856 	spin_lock_bh(&x->lock);
857 	err = __xfrm_state_delete(x);
858 	spin_unlock_bh(&x->lock);
859 
860 	return err;
861 }
862 EXPORT_SYMBOL(xfrm_state_delete);
863 
864 #ifdef CONFIG_SECURITY_NETWORK_XFRM
865 static inline int
xfrm_state_flush_secctx_check(struct net * net,u8 proto,bool task_valid)866 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
867 {
868 	int i, err = 0;
869 
870 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
871 		struct xfrm_state *x;
872 
873 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
874 			if (xfrm_id_proto_match(x->id.proto, proto) &&
875 			   (err = security_xfrm_state_delete(x)) != 0) {
876 				xfrm_audit_state_delete(x, 0, task_valid);
877 				return err;
878 			}
879 		}
880 	}
881 
882 	return err;
883 }
884 
885 static inline int
xfrm_dev_state_flush_secctx_check(struct net * net,struct net_device * dev,bool task_valid)886 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
887 {
888 	int i, err = 0;
889 
890 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
891 		struct xfrm_state *x;
892 		struct xfrm_dev_offload *xso;
893 
894 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
895 			xso = &x->xso;
896 
897 			if (xso->dev == dev &&
898 			   (err = security_xfrm_state_delete(x)) != 0) {
899 				xfrm_audit_state_delete(x, 0, task_valid);
900 				return err;
901 			}
902 		}
903 	}
904 
905 	return err;
906 }
907 #else
908 static inline int
xfrm_state_flush_secctx_check(struct net * net,u8 proto,bool task_valid)909 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
910 {
911 	return 0;
912 }
913 
914 static inline int
xfrm_dev_state_flush_secctx_check(struct net * net,struct net_device * dev,bool task_valid)915 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
916 {
917 	return 0;
918 }
919 #endif
920 
xfrm_state_flush(struct net * net,u8 proto,bool task_valid)921 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
922 {
923 	int i, err = 0, cnt = 0;
924 
925 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
926 	err = xfrm_state_flush_secctx_check(net, proto, task_valid);
927 	if (err)
928 		goto out;
929 
930 	err = -ESRCH;
931 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
932 		struct xfrm_state *x;
933 restart:
934 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
935 			if (!xfrm_state_kern(x) &&
936 			    xfrm_id_proto_match(x->id.proto, proto)) {
937 				xfrm_state_hold(x);
938 				spin_unlock_bh(&net->xfrm.xfrm_state_lock);
939 
940 				err = xfrm_state_delete(x);
941 				xfrm_audit_state_delete(x, err ? 0 : 1,
942 							task_valid);
943 				xfrm_state_put(x);
944 				if (!err)
945 					cnt++;
946 
947 				spin_lock_bh(&net->xfrm.xfrm_state_lock);
948 				goto restart;
949 			}
950 		}
951 	}
952 out:
953 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
954 	if (cnt)
955 		err = 0;
956 
957 	return err;
958 }
959 EXPORT_SYMBOL(xfrm_state_flush);
960 
xfrm_dev_state_flush(struct net * net,struct net_device * dev,bool task_valid)961 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
962 {
963 	struct xfrm_state *x;
964 	struct hlist_node *tmp;
965 	struct xfrm_dev_offload *xso;
966 	int i, err = 0, cnt = 0;
967 
968 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
969 	err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
970 	if (err)
971 		goto out;
972 
973 	err = -ESRCH;
974 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
975 restart:
976 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
977 			xso = &x->xso;
978 
979 			if (!xfrm_state_kern(x) && xso->dev == dev) {
980 				xfrm_state_hold(x);
981 				spin_unlock_bh(&net->xfrm.xfrm_state_lock);
982 
983 				err = xfrm_state_delete(x);
984 				xfrm_dev_state_free(x);
985 
986 				xfrm_audit_state_delete(x, err ? 0 : 1,
987 							task_valid);
988 				xfrm_state_put(x);
989 				if (!err)
990 					cnt++;
991 
992 				spin_lock_bh(&net->xfrm.xfrm_state_lock);
993 				goto restart;
994 			}
995 		}
996 	}
997 	if (cnt)
998 		err = 0;
999 
1000 out:
1001 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1002 
1003 	spin_lock_bh(&xfrm_state_dev_gc_lock);
1004 restart_gc:
1005 	hlist_for_each_entry_safe(x, tmp, &xfrm_state_dev_gc_list, dev_gclist) {
1006 		xso = &x->xso;
1007 
1008 		if (xso->dev == dev) {
1009 			spin_unlock_bh(&xfrm_state_dev_gc_lock);
1010 			xfrm_dev_state_free(x);
1011 			spin_lock_bh(&xfrm_state_dev_gc_lock);
1012 			goto restart_gc;
1013 		}
1014 
1015 	}
1016 	spin_unlock_bh(&xfrm_state_dev_gc_lock);
1017 
1018 	xfrm_flush_gc();
1019 
1020 	return err;
1021 }
1022 EXPORT_SYMBOL(xfrm_dev_state_flush);
1023 
xfrm_sad_getinfo(struct net * net,struct xfrmk_sadinfo * si)1024 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
1025 {
1026 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1027 	si->sadcnt = net->xfrm.state_num;
1028 	si->sadhcnt = net->xfrm.state_hmask + 1;
1029 	si->sadhmcnt = xfrm_state_hashmax;
1030 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1031 }
1032 EXPORT_SYMBOL(xfrm_sad_getinfo);
1033 
1034 static void
__xfrm4_init_tempsel(struct xfrm_selector * sel,const struct flowi * fl)1035 __xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
1036 {
1037 	const struct flowi4 *fl4 = &fl->u.ip4;
1038 
1039 	sel->daddr.a4 = fl4->daddr;
1040 	sel->saddr.a4 = fl4->saddr;
1041 	sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
1042 	sel->dport_mask = htons(0xffff);
1043 	sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
1044 	sel->sport_mask = htons(0xffff);
1045 	sel->family = AF_INET;
1046 	sel->prefixlen_d = 32;
1047 	sel->prefixlen_s = 32;
1048 	sel->proto = fl4->flowi4_proto;
1049 	sel->ifindex = fl4->flowi4_oif;
1050 }
1051 
1052 static void
__xfrm6_init_tempsel(struct xfrm_selector * sel,const struct flowi * fl)1053 __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
1054 {
1055 	const struct flowi6 *fl6 = &fl->u.ip6;
1056 
1057 	/* Initialize temporary selector matching only to current session. */
1058 	*(struct in6_addr *)&sel->daddr = fl6->daddr;
1059 	*(struct in6_addr *)&sel->saddr = fl6->saddr;
1060 	sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
1061 	sel->dport_mask = htons(0xffff);
1062 	sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
1063 	sel->sport_mask = htons(0xffff);
1064 	sel->family = AF_INET6;
1065 	sel->prefixlen_d = 128;
1066 	sel->prefixlen_s = 128;
1067 	sel->proto = fl6->flowi6_proto;
1068 	sel->ifindex = fl6->flowi6_oif;
1069 }
1070 
1071 static void
xfrm_init_tempstate(struct xfrm_state * x,const struct flowi * fl,const struct xfrm_tmpl * tmpl,const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family)1072 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
1073 		    const struct xfrm_tmpl *tmpl,
1074 		    const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1075 		    unsigned short family)
1076 {
1077 	switch (family) {
1078 	case AF_INET:
1079 		__xfrm4_init_tempsel(&x->sel, fl);
1080 		break;
1081 	case AF_INET6:
1082 		__xfrm6_init_tempsel(&x->sel, fl);
1083 		break;
1084 	}
1085 
1086 	x->id = tmpl->id;
1087 
1088 	switch (tmpl->encap_family) {
1089 	case AF_INET:
1090 		if (x->id.daddr.a4 == 0)
1091 			x->id.daddr.a4 = daddr->a4;
1092 		x->props.saddr = tmpl->saddr;
1093 		if (x->props.saddr.a4 == 0)
1094 			x->props.saddr.a4 = saddr->a4;
1095 		break;
1096 	case AF_INET6:
1097 		if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
1098 			memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
1099 		memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
1100 		if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
1101 			memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
1102 		break;
1103 	}
1104 
1105 	x->props.mode = tmpl->mode;
1106 	x->props.reqid = tmpl->reqid;
1107 	x->props.family = tmpl->encap_family;
1108 }
1109 
1110 struct xfrm_hash_state_ptrs {
1111 	const struct hlist_head *bydst;
1112 	const struct hlist_head *bysrc;
1113 	const struct hlist_head *byspi;
1114 	unsigned int hmask;
1115 };
1116 
xfrm_hash_ptrs_get(const struct net * net,struct xfrm_hash_state_ptrs * ptrs)1117 static void xfrm_hash_ptrs_get(const struct net *net, struct xfrm_hash_state_ptrs *ptrs)
1118 {
1119 	unsigned int sequence;
1120 
1121 	do {
1122 		sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
1123 
1124 		ptrs->bydst = xfrm_state_deref_check(net->xfrm.state_bydst, net);
1125 		ptrs->bysrc = xfrm_state_deref_check(net->xfrm.state_bysrc, net);
1126 		ptrs->byspi = xfrm_state_deref_check(net->xfrm.state_byspi, net);
1127 		ptrs->hmask = net->xfrm.state_hmask;
1128 	} while (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence));
1129 }
1130 
__xfrm_state_lookup_all(const struct xfrm_hash_state_ptrs * state_ptrs,u32 mark,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family,struct xfrm_dev_offload * xdo)1131 static struct xfrm_state *__xfrm_state_lookup_all(const struct xfrm_hash_state_ptrs *state_ptrs,
1132 						  u32 mark,
1133 						  const xfrm_address_t *daddr,
1134 						  __be32 spi, u8 proto,
1135 						  unsigned short family,
1136 						  struct xfrm_dev_offload *xdo)
1137 {
1138 	unsigned int h = __xfrm_spi_hash(daddr, spi, proto, family, state_ptrs->hmask);
1139 	struct xfrm_state *x;
1140 
1141 	hlist_for_each_entry_rcu(x, state_ptrs->byspi + h, byspi) {
1142 #ifdef CONFIG_XFRM_OFFLOAD
1143 		if (xdo->type == XFRM_DEV_OFFLOAD_PACKET) {
1144 			if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
1145 				/* HW states are in the head of list, there is
1146 				 * no need to iterate further.
1147 				 */
1148 				break;
1149 
1150 			/* Packet offload: both policy and SA should
1151 			 * have same device.
1152 			 */
1153 			if (xdo->dev != x->xso.dev)
1154 				continue;
1155 		} else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
1156 			/* Skip HW policy for SW lookups */
1157 			continue;
1158 #endif
1159 		if (x->props.family != family ||
1160 		    x->id.spi       != spi ||
1161 		    x->id.proto     != proto ||
1162 		    !xfrm_addr_equal(&x->id.daddr, daddr, family))
1163 			continue;
1164 
1165 		if ((mark & x->mark.m) != x->mark.v)
1166 			continue;
1167 		if (!xfrm_state_hold_rcu(x))
1168 			continue;
1169 		return x;
1170 	}
1171 
1172 	return NULL;
1173 }
1174 
__xfrm_state_lookup(const struct xfrm_hash_state_ptrs * state_ptrs,u32 mark,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)1175 static struct xfrm_state *__xfrm_state_lookup(const struct xfrm_hash_state_ptrs *state_ptrs,
1176 					      u32 mark,
1177 					      const xfrm_address_t *daddr,
1178 					      __be32 spi, u8 proto,
1179 					      unsigned short family)
1180 {
1181 	unsigned int h = __xfrm_spi_hash(daddr, spi, proto, family, state_ptrs->hmask);
1182 	struct xfrm_state *x;
1183 
1184 	hlist_for_each_entry_rcu(x, state_ptrs->byspi + h, byspi) {
1185 		if (x->props.family != family ||
1186 		    x->id.spi       != spi ||
1187 		    x->id.proto     != proto ||
1188 		    !xfrm_addr_equal(&x->id.daddr, daddr, family))
1189 			continue;
1190 
1191 		if ((mark & x->mark.m) != x->mark.v)
1192 			continue;
1193 		if (!xfrm_state_hold_rcu(x))
1194 			continue;
1195 		return x;
1196 	}
1197 
1198 	return NULL;
1199 }
1200 
xfrm_input_state_lookup(struct net * net,u32 mark,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)1201 struct xfrm_state *xfrm_input_state_lookup(struct net *net, u32 mark,
1202 					   const xfrm_address_t *daddr,
1203 					   __be32 spi, u8 proto,
1204 					   unsigned short family)
1205 {
1206 	struct xfrm_hash_state_ptrs state_ptrs;
1207 	struct hlist_head *state_cache_input;
1208 	struct xfrm_state *x = NULL;
1209 
1210 	state_cache_input = raw_cpu_ptr(net->xfrm.state_cache_input);
1211 
1212 	rcu_read_lock();
1213 	hlist_for_each_entry_rcu(x, state_cache_input, state_cache_input) {
1214 		if (x->props.family != family ||
1215 		    x->id.spi       != spi ||
1216 		    x->id.proto     != proto ||
1217 		    !xfrm_addr_equal(&x->id.daddr, daddr, family))
1218 			continue;
1219 
1220 		if ((mark & x->mark.m) != x->mark.v)
1221 			continue;
1222 		if (!xfrm_state_hold_rcu(x))
1223 			continue;
1224 		goto out;
1225 	}
1226 
1227 	xfrm_hash_ptrs_get(net, &state_ptrs);
1228 
1229 	x = __xfrm_state_lookup(&state_ptrs, mark, daddr, spi, proto, family);
1230 
1231 	if (x && x->km.state == XFRM_STATE_VALID) {
1232 		spin_lock_bh(&net->xfrm.xfrm_state_lock);
1233 		if (hlist_unhashed(&x->state_cache_input)) {
1234 			hlist_add_head_rcu(&x->state_cache_input, state_cache_input);
1235 		} else {
1236 			hlist_del_rcu(&x->state_cache_input);
1237 			hlist_add_head_rcu(&x->state_cache_input, state_cache_input);
1238 		}
1239 		spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1240 	}
1241 
1242 out:
1243 	rcu_read_unlock();
1244 	return x;
1245 }
1246 EXPORT_SYMBOL(xfrm_input_state_lookup);
1247 
__xfrm_state_lookup_byaddr(const struct xfrm_hash_state_ptrs * state_ptrs,u32 mark,const xfrm_address_t * daddr,const xfrm_address_t * saddr,u8 proto,unsigned short family)1248 static struct xfrm_state *__xfrm_state_lookup_byaddr(const struct xfrm_hash_state_ptrs *state_ptrs,
1249 						     u32 mark,
1250 						     const xfrm_address_t *daddr,
1251 						     const xfrm_address_t *saddr,
1252 						     u8 proto, unsigned short family)
1253 {
1254 	unsigned int h = __xfrm_src_hash(daddr, saddr, family, state_ptrs->hmask);
1255 	struct xfrm_state *x;
1256 
1257 	hlist_for_each_entry_rcu(x, state_ptrs->bysrc + h, bysrc) {
1258 		if (x->props.family != family ||
1259 		    x->id.proto     != proto ||
1260 		    !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1261 		    !xfrm_addr_equal(&x->props.saddr, saddr, family))
1262 			continue;
1263 
1264 		if ((mark & x->mark.m) != x->mark.v)
1265 			continue;
1266 		if (!xfrm_state_hold_rcu(x))
1267 			continue;
1268 		return x;
1269 	}
1270 
1271 	return NULL;
1272 }
1273 
1274 static inline struct xfrm_state *
__xfrm_state_locate(struct xfrm_state * x,int use_spi,int family)1275 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
1276 {
1277 	struct xfrm_hash_state_ptrs state_ptrs;
1278 	struct net *net = xs_net(x);
1279 	u32 mark = x->mark.v & x->mark.m;
1280 
1281 	xfrm_hash_ptrs_get(net, &state_ptrs);
1282 
1283 	if (use_spi)
1284 		return __xfrm_state_lookup(&state_ptrs, mark, &x->id.daddr,
1285 					   x->id.spi, x->id.proto, family);
1286 	else
1287 		return __xfrm_state_lookup_byaddr(&state_ptrs, mark,
1288 						  &x->id.daddr,
1289 						  &x->props.saddr,
1290 						  x->id.proto, family);
1291 }
1292 
xfrm_hash_grow_check(struct net * net,int have_hash_collision)1293 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
1294 {
1295 	if (have_hash_collision &&
1296 	    (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
1297 	    net->xfrm.state_num > net->xfrm.state_hmask)
1298 		schedule_work(&net->xfrm.state_hash_work);
1299 }
1300 
xfrm_state_look_at(struct xfrm_policy * pol,struct xfrm_state * x,const struct flowi * fl,unsigned short family,struct xfrm_state ** best,int * acq_in_progress,int * error,unsigned int pcpu_id)1301 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
1302 			       const struct flowi *fl, unsigned short family,
1303 			       struct xfrm_state **best, int *acq_in_progress,
1304 			       int *error, unsigned int pcpu_id)
1305 {
1306 	/* Resolution logic:
1307 	 * 1. There is a valid state with matching selector. Done.
1308 	 * 2. Valid state with inappropriate selector. Skip.
1309 	 *
1310 	 * Entering area of "sysdeps".
1311 	 *
1312 	 * 3. If state is not valid, selector is temporary, it selects
1313 	 *    only session which triggered previous resolution. Key
1314 	 *    manager will do something to install a state with proper
1315 	 *    selector.
1316 	 */
1317 	if (x->km.state == XFRM_STATE_VALID) {
1318 		if ((x->sel.family &&
1319 		     (x->sel.family != family ||
1320 		      !xfrm_selector_match(&x->sel, fl, family))) ||
1321 		    !security_xfrm_state_pol_flow_match(x, pol,
1322 							&fl->u.__fl_common))
1323 			return;
1324 
1325 		if (x->pcpu_num != UINT_MAX && x->pcpu_num != pcpu_id)
1326 			return;
1327 
1328 		if (!*best ||
1329 		    ((*best)->pcpu_num == UINT_MAX && x->pcpu_num == pcpu_id) ||
1330 		    (*best)->km.dying > x->km.dying ||
1331 		    ((*best)->km.dying == x->km.dying &&
1332 		     (*best)->curlft.add_time < x->curlft.add_time))
1333 			*best = x;
1334 	} else if (x->km.state == XFRM_STATE_ACQ) {
1335 		if (!*best || x->pcpu_num == pcpu_id)
1336 			*acq_in_progress = 1;
1337 	} else if (x->km.state == XFRM_STATE_ERROR ||
1338 		   x->km.state == XFRM_STATE_EXPIRED) {
1339 		if ((!x->sel.family ||
1340 		     (x->sel.family == family &&
1341 		      xfrm_selector_match(&x->sel, fl, family))) &&
1342 		    security_xfrm_state_pol_flow_match(x, pol,
1343 						       &fl->u.__fl_common))
1344 			*error = -ESRCH;
1345 	}
1346 }
1347 
1348 struct xfrm_state *
xfrm_state_find(const xfrm_address_t * daddr,const xfrm_address_t * saddr,const struct flowi * fl,struct xfrm_tmpl * tmpl,struct xfrm_policy * pol,int * err,unsigned short family,u32 if_id)1349 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1350 		const struct flowi *fl, struct xfrm_tmpl *tmpl,
1351 		struct xfrm_policy *pol, int *err,
1352 		unsigned short family, u32 if_id)
1353 {
1354 	static xfrm_address_t saddr_wildcard = { };
1355 	struct xfrm_hash_state_ptrs state_ptrs;
1356 	struct net *net = xp_net(pol);
1357 	unsigned int h, h_wildcard;
1358 	struct xfrm_state *x, *x0, *to_put;
1359 	int acquire_in_progress = 0;
1360 	int error = 0;
1361 	struct xfrm_state *best = NULL;
1362 	u32 mark = pol->mark.v & pol->mark.m;
1363 	unsigned short encap_family = tmpl->encap_family;
1364 	unsigned int sequence;
1365 	struct km_event c;
1366 	unsigned int pcpu_id;
1367 	bool cached = false;
1368 
1369 	/* We need the cpu id just as a lookup key,
1370 	 * we don't require it to be stable.
1371 	 */
1372 	pcpu_id = raw_smp_processor_id();
1373 
1374 	to_put = NULL;
1375 
1376 	sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
1377 
1378 	rcu_read_lock();
1379 	xfrm_hash_ptrs_get(net, &state_ptrs);
1380 
1381 	hlist_for_each_entry_rcu(x, &pol->state_cache_list, state_cache) {
1382 		if (x->props.family == encap_family &&
1383 		    x->props.reqid == tmpl->reqid &&
1384 		    (mark & x->mark.m) == x->mark.v &&
1385 		    x->if_id == if_id &&
1386 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
1387 		    xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1388 		    tmpl->mode == x->props.mode &&
1389 		    tmpl->id.proto == x->id.proto &&
1390 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1391 			xfrm_state_look_at(pol, x, fl, encap_family,
1392 					   &best, &acquire_in_progress, &error, pcpu_id);
1393 	}
1394 
1395 	if (best)
1396 		goto cached;
1397 
1398 	hlist_for_each_entry_rcu(x, &pol->state_cache_list, state_cache) {
1399 		if (x->props.family == encap_family &&
1400 		    x->props.reqid == tmpl->reqid &&
1401 		    (mark & x->mark.m) == x->mark.v &&
1402 		    x->if_id == if_id &&
1403 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
1404 		    xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1405 		    tmpl->mode == x->props.mode &&
1406 		    tmpl->id.proto == x->id.proto &&
1407 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1408 			xfrm_state_look_at(pol, x, fl, family,
1409 					   &best, &acquire_in_progress, &error, pcpu_id);
1410 	}
1411 
1412 cached:
1413 	cached = true;
1414 	if (best)
1415 		goto found;
1416 	else if (error)
1417 		best = NULL;
1418 	else if (acquire_in_progress) /* XXX: acquire_in_progress should not happen */
1419 		WARN_ON(1);
1420 
1421 	h = __xfrm_dst_hash(daddr, saddr, tmpl->reqid, encap_family, state_ptrs.hmask);
1422 	hlist_for_each_entry_rcu(x, state_ptrs.bydst + h, bydst) {
1423 #ifdef CONFIG_XFRM_OFFLOAD
1424 		if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
1425 			if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
1426 				/* HW states are in the head of list, there is
1427 				 * no need to iterate further.
1428 				 */
1429 				break;
1430 
1431 			/* Packet offload: both policy and SA should
1432 			 * have same device.
1433 			 */
1434 			if (pol->xdo.dev != x->xso.dev)
1435 				continue;
1436 		} else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
1437 			/* Skip HW policy for SW lookups */
1438 			continue;
1439 #endif
1440 		if (x->props.family == encap_family &&
1441 		    x->props.reqid == tmpl->reqid &&
1442 		    (mark & x->mark.m) == x->mark.v &&
1443 		    x->if_id == if_id &&
1444 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
1445 		    xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1446 		    tmpl->mode == x->props.mode &&
1447 		    tmpl->id.proto == x->id.proto &&
1448 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1449 			xfrm_state_look_at(pol, x, fl, family,
1450 					   &best, &acquire_in_progress, &error, pcpu_id);
1451 	}
1452 	if (best || acquire_in_progress)
1453 		goto found;
1454 
1455 	h_wildcard = __xfrm_dst_hash(daddr, &saddr_wildcard, tmpl->reqid,
1456 				     encap_family, state_ptrs.hmask);
1457 	hlist_for_each_entry_rcu(x, state_ptrs.bydst + h_wildcard, bydst) {
1458 #ifdef CONFIG_XFRM_OFFLOAD
1459 		if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
1460 			if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
1461 				/* HW states are in the head of list, there is
1462 				 * no need to iterate further.
1463 				 */
1464 				break;
1465 
1466 			/* Packet offload: both policy and SA should
1467 			 * have same device.
1468 			 */
1469 			if (pol->xdo.dev != x->xso.dev)
1470 				continue;
1471 		} else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
1472 			/* Skip HW policy for SW lookups */
1473 			continue;
1474 #endif
1475 		if (x->props.family == encap_family &&
1476 		    x->props.reqid == tmpl->reqid &&
1477 		    (mark & x->mark.m) == x->mark.v &&
1478 		    x->if_id == if_id &&
1479 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
1480 		    xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1481 		    tmpl->mode == x->props.mode &&
1482 		    tmpl->id.proto == x->id.proto &&
1483 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1484 			xfrm_state_look_at(pol, x, fl, family,
1485 					   &best, &acquire_in_progress, &error, pcpu_id);
1486 	}
1487 
1488 found:
1489 	if (!(pol->flags & XFRM_POLICY_CPU_ACQUIRE) ||
1490 	    (best && (best->pcpu_num == pcpu_id)))
1491 		x = best;
1492 
1493 	if (!x && !error && !acquire_in_progress) {
1494 		if (tmpl->id.spi &&
1495 		    (x0 = __xfrm_state_lookup_all(&state_ptrs, mark, daddr,
1496 						  tmpl->id.spi, tmpl->id.proto,
1497 						  encap_family,
1498 						  &pol->xdo)) != NULL) {
1499 			to_put = x0;
1500 			error = -EEXIST;
1501 			goto out;
1502 		}
1503 
1504 		c.net = net;
1505 		/* If the KMs have no listeners (yet...), avoid allocating an SA
1506 		 * for each and every packet - garbage collection might not
1507 		 * handle the flood.
1508 		 */
1509 		if (!km_is_alive(&c)) {
1510 			error = -ESRCH;
1511 			goto out;
1512 		}
1513 
1514 		x = xfrm_state_alloc(net);
1515 		if (x == NULL) {
1516 			error = -ENOMEM;
1517 			goto out;
1518 		}
1519 		/* Initialize temporary state matching only
1520 		 * to current session. */
1521 		xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1522 		memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1523 		x->if_id = if_id;
1524 		if ((pol->flags & XFRM_POLICY_CPU_ACQUIRE) && best)
1525 			x->pcpu_num = pcpu_id;
1526 
1527 		error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1528 		if (error) {
1529 			x->km.state = XFRM_STATE_DEAD;
1530 			to_put = x;
1531 			x = NULL;
1532 			goto out;
1533 		}
1534 #ifdef CONFIG_XFRM_OFFLOAD
1535 		if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
1536 			struct xfrm_dev_offload *xdo = &pol->xdo;
1537 			struct xfrm_dev_offload *xso = &x->xso;
1538 			struct net_device *dev = xdo->dev;
1539 
1540 			xso->type = XFRM_DEV_OFFLOAD_PACKET;
1541 			xso->dir = xdo->dir;
1542 			xso->dev = dev;
1543 			xso->flags = XFRM_DEV_OFFLOAD_FLAG_ACQ;
1544 			netdev_hold(dev, &xso->dev_tracker, GFP_ATOMIC);
1545 			error = dev->xfrmdev_ops->xdo_dev_state_add(dev, x,
1546 								    NULL);
1547 			if (error) {
1548 				xso->dir = 0;
1549 				netdev_put(dev, &xso->dev_tracker);
1550 				xso->dev = NULL;
1551 				xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
1552 				x->km.state = XFRM_STATE_DEAD;
1553 				to_put = x;
1554 				x = NULL;
1555 				goto out;
1556 			}
1557 		}
1558 #endif
1559 		if (km_query(x, tmpl, pol) == 0) {
1560 			spin_lock_bh(&net->xfrm.xfrm_state_lock);
1561 			x->km.state = XFRM_STATE_ACQ;
1562 			x->dir = XFRM_SA_DIR_OUT;
1563 			list_add(&x->km.all, &net->xfrm.state_all);
1564 			h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
1565 			XFRM_STATE_INSERT(bydst, &x->bydst,
1566 					  net->xfrm.state_bydst + h,
1567 					  x->xso.type);
1568 			h = xfrm_src_hash(net, daddr, saddr, encap_family);
1569 			XFRM_STATE_INSERT(bysrc, &x->bysrc,
1570 					  net->xfrm.state_bysrc + h,
1571 					  x->xso.type);
1572 			INIT_HLIST_NODE(&x->state_cache);
1573 			if (x->id.spi) {
1574 				h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1575 				XFRM_STATE_INSERT(byspi, &x->byspi,
1576 						  net->xfrm.state_byspi + h,
1577 						  x->xso.type);
1578 			}
1579 			if (x->km.seq) {
1580 				h = xfrm_seq_hash(net, x->km.seq);
1581 				XFRM_STATE_INSERT(byseq, &x->byseq,
1582 						  net->xfrm.state_byseq + h,
1583 						  x->xso.type);
1584 			}
1585 			x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1586 			hrtimer_start(&x->mtimer,
1587 				      ktime_set(net->xfrm.sysctl_acq_expires, 0),
1588 				      HRTIMER_MODE_REL_SOFT);
1589 			net->xfrm.state_num++;
1590 			xfrm_hash_grow_check(net, x->bydst.next != NULL);
1591 			spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1592 		} else {
1593 #ifdef CONFIG_XFRM_OFFLOAD
1594 			struct xfrm_dev_offload *xso = &x->xso;
1595 
1596 			if (xso->type == XFRM_DEV_OFFLOAD_PACKET) {
1597 				xfrm_dev_state_delete(x);
1598 				xfrm_dev_state_free(x);
1599 			}
1600 #endif
1601 			x->km.state = XFRM_STATE_DEAD;
1602 			to_put = x;
1603 			x = NULL;
1604 			error = -ESRCH;
1605 		}
1606 
1607 		/* Use the already installed 'fallback' while the CPU-specific
1608 		 * SA acquire is handled*/
1609 		if (best)
1610 			x = best;
1611 	}
1612 out:
1613 	if (x) {
1614 		if (!xfrm_state_hold_rcu(x)) {
1615 			*err = -EAGAIN;
1616 			x = NULL;
1617 		}
1618 	} else {
1619 		*err = acquire_in_progress ? -EAGAIN : error;
1620 	}
1621 
1622 	if (x && x->km.state == XFRM_STATE_VALID && !cached &&
1623 	    (!(pol->flags & XFRM_POLICY_CPU_ACQUIRE) || x->pcpu_num == pcpu_id)) {
1624 		spin_lock_bh(&net->xfrm.xfrm_state_lock);
1625 		if (hlist_unhashed(&x->state_cache))
1626 			hlist_add_head_rcu(&x->state_cache, &pol->state_cache_list);
1627 		spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1628 	}
1629 
1630 	rcu_read_unlock();
1631 	if (to_put)
1632 		xfrm_state_put(to_put);
1633 
1634 	if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
1635 		*err = -EAGAIN;
1636 		if (x) {
1637 			xfrm_state_put(x);
1638 			x = NULL;
1639 		}
1640 	}
1641 
1642 	return x;
1643 }
1644 
1645 struct xfrm_state *
xfrm_stateonly_find(struct net * net,u32 mark,u32 if_id,xfrm_address_t * daddr,xfrm_address_t * saddr,unsigned short family,u8 mode,u8 proto,u32 reqid)1646 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1647 		    xfrm_address_t *daddr, xfrm_address_t *saddr,
1648 		    unsigned short family, u8 mode, u8 proto, u32 reqid)
1649 {
1650 	unsigned int h;
1651 	struct xfrm_state *rx = NULL, *x = NULL;
1652 
1653 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1654 	h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1655 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1656 		if (x->props.family == family &&
1657 		    x->props.reqid == reqid &&
1658 		    (mark & x->mark.m) == x->mark.v &&
1659 		    x->if_id == if_id &&
1660 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
1661 		    xfrm_state_addr_check(x, daddr, saddr, family) &&
1662 		    mode == x->props.mode &&
1663 		    proto == x->id.proto &&
1664 		    x->km.state == XFRM_STATE_VALID) {
1665 			rx = x;
1666 			break;
1667 		}
1668 	}
1669 
1670 	if (rx)
1671 		xfrm_state_hold(rx);
1672 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1673 
1674 
1675 	return rx;
1676 }
1677 EXPORT_SYMBOL(xfrm_stateonly_find);
1678 
xfrm_state_lookup_byspi(struct net * net,__be32 spi,unsigned short family)1679 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1680 					      unsigned short family)
1681 {
1682 	struct xfrm_state *x;
1683 	struct xfrm_state_walk *w;
1684 
1685 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1686 	list_for_each_entry(w, &net->xfrm.state_all, all) {
1687 		x = container_of(w, struct xfrm_state, km);
1688 		if (x->props.family != family ||
1689 			x->id.spi != spi)
1690 			continue;
1691 
1692 		xfrm_state_hold(x);
1693 		spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1694 		return x;
1695 	}
1696 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1697 	return NULL;
1698 }
1699 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1700 
xfrm_state_lookup_spi_proto(struct net * net,__be32 spi,u8 proto)1701 static struct xfrm_state *xfrm_state_lookup_spi_proto(struct net *net, __be32 spi, u8 proto)
1702 {
1703 	struct xfrm_state *x;
1704 	unsigned int i;
1705 
1706 	rcu_read_lock();
1707 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
1708 		hlist_for_each_entry_rcu(x, &net->xfrm.state_byspi[i], byspi) {
1709 			if (x->id.spi == spi && x->id.proto == proto) {
1710 				if (!xfrm_state_hold_rcu(x))
1711 					continue;
1712 				rcu_read_unlock();
1713 				return x;
1714 			}
1715 		}
1716 	}
1717 	rcu_read_unlock();
1718 	return NULL;
1719 }
1720 
__xfrm_state_insert(struct xfrm_state * x)1721 static void __xfrm_state_insert(struct xfrm_state *x)
1722 {
1723 	struct net *net = xs_net(x);
1724 	unsigned int h;
1725 
1726 	list_add(&x->km.all, &net->xfrm.state_all);
1727 
1728 	/* Sanitize mark before store */
1729 	x->mark.v &= x->mark.m;
1730 
1731 	h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1732 			  x->props.reqid, x->props.family);
1733 	XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
1734 			  x->xso.type);
1735 
1736 	h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1737 	XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
1738 			  x->xso.type);
1739 
1740 	if (x->id.spi) {
1741 		h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1742 				  x->props.family);
1743 
1744 		XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
1745 				  x->xso.type);
1746 	}
1747 
1748 	if (x->km.seq) {
1749 		h = xfrm_seq_hash(net, x->km.seq);
1750 
1751 		XFRM_STATE_INSERT(byseq, &x->byseq, net->xfrm.state_byseq + h,
1752 				  x->xso.type);
1753 	}
1754 
1755 	hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
1756 	if (x->replay_maxage)
1757 		mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1758 
1759 	net->xfrm.state_num++;
1760 
1761 	xfrm_hash_grow_check(net, x->bydst.next != NULL);
1762 	xfrm_nat_keepalive_state_updated(x);
1763 }
1764 
1765 /* net->xfrm.xfrm_state_lock is held */
__xfrm_state_bump_genids(struct xfrm_state * xnew)1766 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1767 {
1768 	struct net *net = xs_net(xnew);
1769 	unsigned short family = xnew->props.family;
1770 	u32 reqid = xnew->props.reqid;
1771 	struct xfrm_state *x;
1772 	unsigned int h;
1773 	u32 mark = xnew->mark.v & xnew->mark.m;
1774 	u32 if_id = xnew->if_id;
1775 	u32 cpu_id = xnew->pcpu_num;
1776 
1777 	h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1778 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1779 		if (x->props.family	== family &&
1780 		    x->props.reqid	== reqid &&
1781 		    x->if_id		== if_id &&
1782 		    x->pcpu_num		== cpu_id &&
1783 		    (mark & x->mark.m) == x->mark.v &&
1784 		    xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1785 		    xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1786 			x->genid++;
1787 	}
1788 }
1789 
xfrm_state_insert(struct xfrm_state * x)1790 void xfrm_state_insert(struct xfrm_state *x)
1791 {
1792 	struct net *net = xs_net(x);
1793 
1794 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1795 	__xfrm_state_bump_genids(x);
1796 	__xfrm_state_insert(x);
1797 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1798 }
1799 EXPORT_SYMBOL(xfrm_state_insert);
1800 
1801 /* net->xfrm.xfrm_state_lock is held */
__find_acq_core(struct net * net,const struct xfrm_mark * m,unsigned short family,u8 mode,u32 reqid,u32 if_id,u32 pcpu_num,u8 proto,const xfrm_address_t * daddr,const xfrm_address_t * saddr,int create)1802 static struct xfrm_state *__find_acq_core(struct net *net,
1803 					  const struct xfrm_mark *m,
1804 					  unsigned short family, u8 mode,
1805 					  u32 reqid, u32 if_id, u32 pcpu_num, u8 proto,
1806 					  const xfrm_address_t *daddr,
1807 					  const xfrm_address_t *saddr,
1808 					  int create)
1809 {
1810 	unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1811 	struct xfrm_state *x;
1812 	u32 mark = m->v & m->m;
1813 
1814 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1815 		if (x->props.reqid  != reqid ||
1816 		    x->props.mode   != mode ||
1817 		    x->props.family != family ||
1818 		    x->km.state     != XFRM_STATE_ACQ ||
1819 		    x->id.spi       != 0 ||
1820 		    x->id.proto	    != proto ||
1821 		    (mark & x->mark.m) != x->mark.v ||
1822 		    x->pcpu_num != pcpu_num ||
1823 		    !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1824 		    !xfrm_addr_equal(&x->props.saddr, saddr, family))
1825 			continue;
1826 
1827 		xfrm_state_hold(x);
1828 		return x;
1829 	}
1830 
1831 	if (!create)
1832 		return NULL;
1833 
1834 	x = xfrm_state_alloc(net);
1835 	if (likely(x)) {
1836 		switch (family) {
1837 		case AF_INET:
1838 			x->sel.daddr.a4 = daddr->a4;
1839 			x->sel.saddr.a4 = saddr->a4;
1840 			x->sel.prefixlen_d = 32;
1841 			x->sel.prefixlen_s = 32;
1842 			x->props.saddr.a4 = saddr->a4;
1843 			x->id.daddr.a4 = daddr->a4;
1844 			break;
1845 
1846 		case AF_INET6:
1847 			x->sel.daddr.in6 = daddr->in6;
1848 			x->sel.saddr.in6 = saddr->in6;
1849 			x->sel.prefixlen_d = 128;
1850 			x->sel.prefixlen_s = 128;
1851 			x->props.saddr.in6 = saddr->in6;
1852 			x->id.daddr.in6 = daddr->in6;
1853 			break;
1854 		}
1855 
1856 		x->pcpu_num = pcpu_num;
1857 		x->km.state = XFRM_STATE_ACQ;
1858 		x->id.proto = proto;
1859 		x->props.family = family;
1860 		x->props.mode = mode;
1861 		x->props.reqid = reqid;
1862 		x->if_id = if_id;
1863 		x->mark.v = m->v;
1864 		x->mark.m = m->m;
1865 		x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1866 		xfrm_state_hold(x);
1867 		hrtimer_start(&x->mtimer,
1868 			      ktime_set(net->xfrm.sysctl_acq_expires, 0),
1869 			      HRTIMER_MODE_REL_SOFT);
1870 		list_add(&x->km.all, &net->xfrm.state_all);
1871 		XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
1872 				  x->xso.type);
1873 		h = xfrm_src_hash(net, daddr, saddr, family);
1874 		XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
1875 				  x->xso.type);
1876 
1877 		net->xfrm.state_num++;
1878 
1879 		xfrm_hash_grow_check(net, x->bydst.next != NULL);
1880 	}
1881 
1882 	return x;
1883 }
1884 
1885 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num);
1886 
xfrm_state_add(struct xfrm_state * x)1887 int xfrm_state_add(struct xfrm_state *x)
1888 {
1889 	struct net *net = xs_net(x);
1890 	struct xfrm_state *x1, *to_put;
1891 	int family;
1892 	int err;
1893 	u32 mark = x->mark.v & x->mark.m;
1894 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1895 
1896 	family = x->props.family;
1897 
1898 	to_put = NULL;
1899 
1900 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1901 
1902 	x1 = __xfrm_state_locate(x, use_spi, family);
1903 	if (x1) {
1904 		to_put = x1;
1905 		x1 = NULL;
1906 		err = -EEXIST;
1907 		goto out;
1908 	}
1909 
1910 	if (use_spi && x->km.seq) {
1911 		x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq, x->pcpu_num);
1912 		if (x1 && ((x1->id.proto != x->id.proto) ||
1913 		    !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1914 			to_put = x1;
1915 			x1 = NULL;
1916 		}
1917 	}
1918 
1919 	if (use_spi && !x1)
1920 		x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1921 				     x->props.reqid, x->if_id, x->pcpu_num, x->id.proto,
1922 				     &x->id.daddr, &x->props.saddr, 0);
1923 
1924 	__xfrm_state_bump_genids(x);
1925 	__xfrm_state_insert(x);
1926 	err = 0;
1927 
1928 out:
1929 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1930 
1931 	if (x1) {
1932 		xfrm_state_delete(x1);
1933 		xfrm_state_put(x1);
1934 	}
1935 
1936 	if (to_put)
1937 		xfrm_state_put(to_put);
1938 
1939 	return err;
1940 }
1941 EXPORT_SYMBOL(xfrm_state_add);
1942 
1943 #ifdef CONFIG_XFRM_MIGRATE
clone_security(struct xfrm_state * x,struct xfrm_sec_ctx * security)1944 static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1945 {
1946 	struct xfrm_user_sec_ctx *uctx;
1947 	int size = sizeof(*uctx) + security->ctx_len;
1948 	int err;
1949 
1950 	uctx = kmalloc(size, GFP_KERNEL);
1951 	if (!uctx)
1952 		return -ENOMEM;
1953 
1954 	uctx->exttype = XFRMA_SEC_CTX;
1955 	uctx->len = size;
1956 	uctx->ctx_doi = security->ctx_doi;
1957 	uctx->ctx_alg = security->ctx_alg;
1958 	uctx->ctx_len = security->ctx_len;
1959 	memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1960 	err = security_xfrm_state_alloc(x, uctx);
1961 	kfree(uctx);
1962 	if (err)
1963 		return err;
1964 
1965 	return 0;
1966 }
1967 
xfrm_state_clone_and_setup(struct xfrm_state * orig,struct xfrm_encap_tmpl * encap,struct xfrm_migrate * m)1968 static struct xfrm_state *xfrm_state_clone_and_setup(struct xfrm_state *orig,
1969 					   struct xfrm_encap_tmpl *encap,
1970 					   struct xfrm_migrate *m)
1971 {
1972 	struct net *net = xs_net(orig);
1973 	struct xfrm_state *x = xfrm_state_alloc(net);
1974 	if (!x)
1975 		goto out;
1976 
1977 	memcpy(&x->id, &orig->id, sizeof(x->id));
1978 	memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1979 	memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1980 	x->props.mode = orig->props.mode;
1981 	x->props.replay_window = orig->props.replay_window;
1982 	x->props.reqid = orig->props.reqid;
1983 	x->props.family = orig->props.family;
1984 	x->props.saddr = orig->props.saddr;
1985 
1986 	if (orig->aalg) {
1987 		x->aalg = xfrm_algo_auth_clone(orig->aalg);
1988 		if (!x->aalg)
1989 			goto error;
1990 	}
1991 	x->props.aalgo = orig->props.aalgo;
1992 
1993 	if (orig->aead) {
1994 		x->aead = xfrm_algo_aead_clone(orig->aead);
1995 		x->geniv = orig->geniv;
1996 		if (!x->aead)
1997 			goto error;
1998 	}
1999 	if (orig->ealg) {
2000 		x->ealg = xfrm_algo_clone(orig->ealg);
2001 		if (!x->ealg)
2002 			goto error;
2003 	}
2004 	x->props.ealgo = orig->props.ealgo;
2005 
2006 	if (orig->calg) {
2007 		x->calg = xfrm_algo_clone(orig->calg);
2008 		if (!x->calg)
2009 			goto error;
2010 	}
2011 	x->props.calgo = orig->props.calgo;
2012 
2013 	if (encap || orig->encap) {
2014 		if (encap)
2015 			x->encap = kmemdup(encap, sizeof(*x->encap),
2016 					GFP_KERNEL);
2017 		else
2018 			x->encap = kmemdup(orig->encap, sizeof(*x->encap),
2019 					GFP_KERNEL);
2020 
2021 		if (!x->encap)
2022 			goto error;
2023 	}
2024 
2025 	if (orig->security)
2026 		if (clone_security(x, orig->security))
2027 			goto error;
2028 
2029 	if (orig->coaddr) {
2030 		x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
2031 				    GFP_KERNEL);
2032 		if (!x->coaddr)
2033 			goto error;
2034 	}
2035 
2036 	if (orig->replay_esn) {
2037 		if (xfrm_replay_clone(x, orig))
2038 			goto error;
2039 	}
2040 
2041 	memcpy(&x->mark, &orig->mark, sizeof(x->mark));
2042 	memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
2043 
2044 	x->props.flags = orig->props.flags;
2045 	x->props.extra_flags = orig->props.extra_flags;
2046 
2047 	x->pcpu_num = orig->pcpu_num;
2048 	x->if_id = orig->if_id;
2049 	x->tfcpad = orig->tfcpad;
2050 	x->replay_maxdiff = orig->replay_maxdiff;
2051 	x->replay_maxage = orig->replay_maxage;
2052 	memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
2053 	x->km.state = orig->km.state;
2054 	x->km.seq = orig->km.seq;
2055 	x->replay = orig->replay;
2056 	x->preplay = orig->preplay;
2057 	x->mapping_maxage = orig->mapping_maxage;
2058 	x->lastused = orig->lastused;
2059 	x->new_mapping = 0;
2060 	x->new_mapping_sport = 0;
2061 	x->dir = orig->dir;
2062 
2063 	x->mode_cbs = orig->mode_cbs;
2064 	if (x->mode_cbs && x->mode_cbs->clone_state) {
2065 		if (x->mode_cbs->clone_state(x, orig))
2066 			goto error;
2067 	}
2068 
2069 
2070 	x->props.family = m->new_family;
2071 	memcpy(&x->id.daddr, &m->new_daddr, sizeof(x->id.daddr));
2072 	memcpy(&x->props.saddr, &m->new_saddr, sizeof(x->props.saddr));
2073 
2074 	return x;
2075 
2076  error:
2077 	x->km.state = XFRM_STATE_DEAD;
2078 	xfrm_state_put(x);
2079 out:
2080 	return NULL;
2081 }
2082 
xfrm_migrate_state_find(struct xfrm_migrate * m,struct net * net,u32 if_id)2083 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
2084 						u32 if_id)
2085 {
2086 	unsigned int h;
2087 	struct xfrm_state *x = NULL;
2088 
2089 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2090 
2091 	if (m->reqid) {
2092 		h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
2093 				  m->reqid, m->old_family);
2094 		hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
2095 			if (x->props.mode != m->mode ||
2096 			    x->id.proto != m->proto)
2097 				continue;
2098 			if (m->reqid && x->props.reqid != m->reqid)
2099 				continue;
2100 			if (if_id != 0 && x->if_id != if_id)
2101 				continue;
2102 			if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
2103 					     m->old_family) ||
2104 			    !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
2105 					     m->old_family))
2106 				continue;
2107 			xfrm_state_hold(x);
2108 			break;
2109 		}
2110 	} else {
2111 		h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
2112 				  m->old_family);
2113 		hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
2114 			if (x->props.mode != m->mode ||
2115 			    x->id.proto != m->proto)
2116 				continue;
2117 			if (if_id != 0 && x->if_id != if_id)
2118 				continue;
2119 			if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
2120 					     m->old_family) ||
2121 			    !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
2122 					     m->old_family))
2123 				continue;
2124 			xfrm_state_hold(x);
2125 			break;
2126 		}
2127 	}
2128 
2129 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2130 
2131 	return x;
2132 }
2133 EXPORT_SYMBOL(xfrm_migrate_state_find);
2134 
xfrm_state_migrate(struct xfrm_state * x,struct xfrm_migrate * m,struct xfrm_encap_tmpl * encap,struct net * net,struct xfrm_user_offload * xuo,struct netlink_ext_ack * extack)2135 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
2136 				      struct xfrm_migrate *m,
2137 				      struct xfrm_encap_tmpl *encap,
2138 				      struct net *net,
2139 				      struct xfrm_user_offload *xuo,
2140 				      struct netlink_ext_ack *extack)
2141 {
2142 	struct xfrm_state *xc;
2143 
2144 	xc = xfrm_state_clone_and_setup(x, encap, m);
2145 	if (!xc)
2146 		return NULL;
2147 
2148 	if (xfrm_init_state(xc) < 0)
2149 		goto error;
2150 
2151 	/* configure the hardware if offload is requested */
2152 	if (xuo && xfrm_dev_state_add(net, xc, xuo, extack))
2153 		goto error;
2154 
2155 	/* add state */
2156 	if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
2157 		/* a care is needed when the destination address of the
2158 		   state is to be updated as it is a part of triplet */
2159 		xfrm_state_insert(xc);
2160 	} else {
2161 		if (xfrm_state_add(xc) < 0)
2162 			goto error_add;
2163 	}
2164 
2165 	return xc;
2166 error_add:
2167 	if (xuo)
2168 		xfrm_dev_state_delete(xc);
2169 error:
2170 	xc->km.state = XFRM_STATE_DEAD;
2171 	xfrm_state_put(xc);
2172 	return NULL;
2173 }
2174 EXPORT_SYMBOL(xfrm_state_migrate);
2175 #endif
2176 
xfrm_state_update(struct xfrm_state * x)2177 int xfrm_state_update(struct xfrm_state *x)
2178 {
2179 	struct xfrm_state *x1, *to_put;
2180 	int err;
2181 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
2182 	struct net *net = xs_net(x);
2183 
2184 	to_put = NULL;
2185 
2186 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2187 	x1 = __xfrm_state_locate(x, use_spi, x->props.family);
2188 
2189 	err = -ESRCH;
2190 	if (!x1)
2191 		goto out;
2192 
2193 	if (xfrm_state_kern(x1)) {
2194 		to_put = x1;
2195 		err = -EEXIST;
2196 		goto out;
2197 	}
2198 
2199 	if (x1->km.state == XFRM_STATE_ACQ) {
2200 		if (x->dir && x1->dir != x->dir) {
2201 			to_put = x1;
2202 			goto out;
2203 		}
2204 
2205 		__xfrm_state_insert(x);
2206 		x = NULL;
2207 	} else {
2208 		if (x1->dir != x->dir) {
2209 			to_put = x1;
2210 			goto out;
2211 		}
2212 	}
2213 	err = 0;
2214 
2215 out:
2216 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2217 
2218 	if (to_put)
2219 		xfrm_state_put(to_put);
2220 
2221 	if (err)
2222 		return err;
2223 
2224 	if (!x) {
2225 		xfrm_state_delete(x1);
2226 		xfrm_state_put(x1);
2227 		return 0;
2228 	}
2229 
2230 	err = -EINVAL;
2231 	spin_lock_bh(&x1->lock);
2232 	if (likely(x1->km.state == XFRM_STATE_VALID)) {
2233 		if (x->encap && x1->encap &&
2234 		    x->encap->encap_type == x1->encap->encap_type)
2235 			memcpy(x1->encap, x->encap, sizeof(*x1->encap));
2236 		else if (x->encap || x1->encap)
2237 			goto fail;
2238 
2239 		if (x->coaddr && x1->coaddr) {
2240 			memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
2241 		}
2242 		if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
2243 			memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
2244 		memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
2245 		x1->km.dying = 0;
2246 
2247 		hrtimer_start(&x1->mtimer, ktime_set(1, 0),
2248 			      HRTIMER_MODE_REL_SOFT);
2249 		if (READ_ONCE(x1->curlft.use_time))
2250 			xfrm_state_check_expire(x1);
2251 
2252 		if (x->props.smark.m || x->props.smark.v || x->if_id) {
2253 			spin_lock_bh(&net->xfrm.xfrm_state_lock);
2254 
2255 			if (x->props.smark.m || x->props.smark.v)
2256 				x1->props.smark = x->props.smark;
2257 
2258 			if (x->if_id)
2259 				x1->if_id = x->if_id;
2260 
2261 			__xfrm_state_bump_genids(x1);
2262 			spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2263 		}
2264 
2265 		err = 0;
2266 		x->km.state = XFRM_STATE_DEAD;
2267 		__xfrm_state_put(x);
2268 	}
2269 
2270 fail:
2271 	spin_unlock_bh(&x1->lock);
2272 
2273 	xfrm_state_put(x1);
2274 
2275 	return err;
2276 }
2277 EXPORT_SYMBOL(xfrm_state_update);
2278 
xfrm_state_check_expire(struct xfrm_state * x)2279 int xfrm_state_check_expire(struct xfrm_state *x)
2280 {
2281 	/* All counters which are needed to decide if state is expired
2282 	 * are handled by SW for non-packet offload modes. Simply skip
2283 	 * the following update and save extra boilerplate in drivers.
2284 	 */
2285 	if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
2286 		xfrm_dev_state_update_stats(x);
2287 
2288 	if (!READ_ONCE(x->curlft.use_time))
2289 		WRITE_ONCE(x->curlft.use_time, ktime_get_real_seconds());
2290 
2291 	if (x->curlft.bytes >= x->lft.hard_byte_limit ||
2292 	    x->curlft.packets >= x->lft.hard_packet_limit) {
2293 		x->km.state = XFRM_STATE_EXPIRED;
2294 		hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
2295 		return -EINVAL;
2296 	}
2297 
2298 	if (!x->km.dying &&
2299 	    (x->curlft.bytes >= x->lft.soft_byte_limit ||
2300 	     x->curlft.packets >= x->lft.soft_packet_limit)) {
2301 		x->km.dying = 1;
2302 		km_state_expired(x, 0, 0);
2303 	}
2304 	return 0;
2305 }
2306 EXPORT_SYMBOL(xfrm_state_check_expire);
2307 
xfrm_state_update_stats(struct net * net)2308 void xfrm_state_update_stats(struct net *net)
2309 {
2310 	struct xfrm_state *x;
2311 	int i;
2312 
2313 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2314 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
2315 		hlist_for_each_entry(x, net->xfrm.state_bydst + i, bydst)
2316 			xfrm_dev_state_update_stats(x);
2317 	}
2318 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2319 }
2320 
2321 struct xfrm_state *
xfrm_state_lookup(struct net * net,u32 mark,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)2322 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
2323 		  u8 proto, unsigned short family)
2324 {
2325 	struct xfrm_hash_state_ptrs state_ptrs;
2326 	struct xfrm_state *x;
2327 
2328 	rcu_read_lock();
2329 	xfrm_hash_ptrs_get(net, &state_ptrs);
2330 
2331 	x = __xfrm_state_lookup(&state_ptrs, mark, daddr, spi, proto, family);
2332 	rcu_read_unlock();
2333 	return x;
2334 }
2335 EXPORT_SYMBOL(xfrm_state_lookup);
2336 
2337 struct xfrm_state *
xfrm_state_lookup_byaddr(struct net * net,u32 mark,const xfrm_address_t * daddr,const xfrm_address_t * saddr,u8 proto,unsigned short family)2338 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
2339 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
2340 			 u8 proto, unsigned short family)
2341 {
2342 	struct xfrm_hash_state_ptrs state_ptrs;
2343 	struct xfrm_state *x;
2344 
2345 	rcu_read_lock();
2346 
2347 	xfrm_hash_ptrs_get(net, &state_ptrs);
2348 
2349 	x = __xfrm_state_lookup_byaddr(&state_ptrs, mark, daddr, saddr, proto, family);
2350 	rcu_read_unlock();
2351 	return x;
2352 }
2353 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
2354 
2355 struct xfrm_state *
xfrm_find_acq(struct net * net,const struct xfrm_mark * mark,u8 mode,u32 reqid,u32 if_id,u32 pcpu_num,u8 proto,const xfrm_address_t * daddr,const xfrm_address_t * saddr,int create,unsigned short family)2356 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
2357 	      u32 if_id, u32 pcpu_num, u8 proto, const xfrm_address_t *daddr,
2358 	      const xfrm_address_t *saddr, int create, unsigned short family)
2359 {
2360 	struct xfrm_state *x;
2361 
2362 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2363 	x = __find_acq_core(net, mark, family, mode, reqid, if_id, pcpu_num,
2364 			    proto, daddr, saddr, create);
2365 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2366 
2367 	return x;
2368 }
2369 EXPORT_SYMBOL(xfrm_find_acq);
2370 
2371 #ifdef CONFIG_XFRM_SUB_POLICY
2372 #if IS_ENABLED(CONFIG_IPV6)
2373 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
2374 static void
__xfrm6_sort(void ** dst,void ** src,int n,int (* cmp)(const void * p),int maxclass)2375 __xfrm6_sort(void **dst, void **src, int n,
2376 	     int (*cmp)(const void *p), int maxclass)
2377 {
2378 	int count[XFRM_MAX_DEPTH] = { };
2379 	int class[XFRM_MAX_DEPTH];
2380 	int i;
2381 
2382 	for (i = 0; i < n; i++) {
2383 		int c = cmp(src[i]);
2384 
2385 		class[i] = c;
2386 		count[c]++;
2387 	}
2388 
2389 	for (i = 2; i < maxclass; i++)
2390 		count[i] += count[i - 1];
2391 
2392 	for (i = 0; i < n; i++) {
2393 		dst[count[class[i] - 1]++] = src[i];
2394 		src[i] = NULL;
2395 	}
2396 }
2397 
2398 /* Rule for xfrm_state:
2399  *
2400  * rule 1: select IPsec transport except AH
2401  * rule 2: select MIPv6 RO or inbound trigger
2402  * rule 3: select IPsec transport AH
2403  * rule 4: select IPsec tunnel
2404  * rule 5: others
2405  */
__xfrm6_state_sort_cmp(const void * p)2406 static int __xfrm6_state_sort_cmp(const void *p)
2407 {
2408 	const struct xfrm_state *v = p;
2409 
2410 	switch (v->props.mode) {
2411 	case XFRM_MODE_TRANSPORT:
2412 		if (v->id.proto != IPPROTO_AH)
2413 			return 1;
2414 		else
2415 			return 3;
2416 #if IS_ENABLED(CONFIG_IPV6_MIP6)
2417 	case XFRM_MODE_ROUTEOPTIMIZATION:
2418 	case XFRM_MODE_IN_TRIGGER:
2419 		return 2;
2420 #endif
2421 	case XFRM_MODE_TUNNEL:
2422 	case XFRM_MODE_BEET:
2423 	case XFRM_MODE_IPTFS:
2424 		return 4;
2425 	}
2426 	return 5;
2427 }
2428 
2429 /* Rule for xfrm_tmpl:
2430  *
2431  * rule 1: select IPsec transport
2432  * rule 2: select MIPv6 RO or inbound trigger
2433  * rule 3: select IPsec tunnel
2434  * rule 4: others
2435  */
__xfrm6_tmpl_sort_cmp(const void * p)2436 static int __xfrm6_tmpl_sort_cmp(const void *p)
2437 {
2438 	const struct xfrm_tmpl *v = p;
2439 
2440 	switch (v->mode) {
2441 	case XFRM_MODE_TRANSPORT:
2442 		return 1;
2443 #if IS_ENABLED(CONFIG_IPV6_MIP6)
2444 	case XFRM_MODE_ROUTEOPTIMIZATION:
2445 	case XFRM_MODE_IN_TRIGGER:
2446 		return 2;
2447 #endif
2448 	case XFRM_MODE_TUNNEL:
2449 	case XFRM_MODE_BEET:
2450 	case XFRM_MODE_IPTFS:
2451 		return 3;
2452 	}
2453 	return 4;
2454 }
2455 #else
__xfrm6_state_sort_cmp(const void * p)2456 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
__xfrm6_tmpl_sort_cmp(const void * p)2457 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
2458 
2459 static inline void
__xfrm6_sort(void ** dst,void ** src,int n,int (* cmp)(const void * p),int maxclass)2460 __xfrm6_sort(void **dst, void **src, int n,
2461 	     int (*cmp)(const void *p), int maxclass)
2462 {
2463 	int i;
2464 
2465 	for (i = 0; i < n; i++)
2466 		dst[i] = src[i];
2467 }
2468 #endif /* CONFIG_IPV6 */
2469 
2470 void
xfrm_tmpl_sort(struct xfrm_tmpl ** dst,struct xfrm_tmpl ** src,int n,unsigned short family)2471 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
2472 	       unsigned short family)
2473 {
2474 	int i;
2475 
2476 	if (family == AF_INET6)
2477 		__xfrm6_sort((void **)dst, (void **)src, n,
2478 			     __xfrm6_tmpl_sort_cmp, 5);
2479 	else
2480 		for (i = 0; i < n; i++)
2481 			dst[i] = src[i];
2482 }
2483 
2484 void
xfrm_state_sort(struct xfrm_state ** dst,struct xfrm_state ** src,int n,unsigned short family)2485 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
2486 		unsigned short family)
2487 {
2488 	int i;
2489 
2490 	if (family == AF_INET6)
2491 		__xfrm6_sort((void **)dst, (void **)src, n,
2492 			     __xfrm6_state_sort_cmp, 6);
2493 	else
2494 		for (i = 0; i < n; i++)
2495 			dst[i] = src[i];
2496 }
2497 #endif
2498 
2499 /* Silly enough, but I'm lazy to build resolution list */
2500 
__xfrm_find_acq_byseq(struct net * net,u32 mark,u32 seq,u32 pcpu_num)2501 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num)
2502 {
2503 	unsigned int h = xfrm_seq_hash(net, seq);
2504 	struct xfrm_state *x;
2505 
2506 	hlist_for_each_entry_rcu(x, net->xfrm.state_byseq + h, byseq) {
2507 		if (x->km.seq == seq &&
2508 		    (mark & x->mark.m) == x->mark.v &&
2509 		    x->pcpu_num == pcpu_num &&
2510 		    x->km.state == XFRM_STATE_ACQ) {
2511 			xfrm_state_hold(x);
2512 			return x;
2513 		}
2514 	}
2515 
2516 	return NULL;
2517 }
2518 
xfrm_find_acq_byseq(struct net * net,u32 mark,u32 seq,u32 pcpu_num)2519 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num)
2520 {
2521 	struct xfrm_state *x;
2522 
2523 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2524 	x = __xfrm_find_acq_byseq(net, mark, seq, pcpu_num);
2525 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2526 	return x;
2527 }
2528 EXPORT_SYMBOL(xfrm_find_acq_byseq);
2529 
xfrm_get_acqseq(void)2530 u32 xfrm_get_acqseq(void)
2531 {
2532 	u32 res;
2533 	static atomic_t acqseq;
2534 
2535 	do {
2536 		res = atomic_inc_return(&acqseq);
2537 	} while (!res);
2538 
2539 	return res;
2540 }
2541 EXPORT_SYMBOL(xfrm_get_acqseq);
2542 
verify_spi_info(u8 proto,u32 min,u32 max,struct netlink_ext_ack * extack)2543 int verify_spi_info(u8 proto, u32 min, u32 max, struct netlink_ext_ack *extack)
2544 {
2545 	switch (proto) {
2546 	case IPPROTO_AH:
2547 	case IPPROTO_ESP:
2548 		break;
2549 
2550 	case IPPROTO_COMP:
2551 		/* IPCOMP spi is 16-bits. */
2552 		if (max >= 0x10000) {
2553 			NL_SET_ERR_MSG(extack, "IPCOMP SPI must be <= 65535");
2554 			return -EINVAL;
2555 		}
2556 		break;
2557 
2558 	default:
2559 		NL_SET_ERR_MSG(extack, "Invalid protocol, must be one of AH, ESP, IPCOMP");
2560 		return -EINVAL;
2561 	}
2562 
2563 	if (min > max) {
2564 		NL_SET_ERR_MSG(extack, "Invalid SPI range: min > max");
2565 		return -EINVAL;
2566 	}
2567 
2568 	return 0;
2569 }
2570 EXPORT_SYMBOL(verify_spi_info);
2571 
xfrm_alloc_spi(struct xfrm_state * x,u32 low,u32 high,struct netlink_ext_ack * extack)2572 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high,
2573 		   struct netlink_ext_ack *extack)
2574 {
2575 	struct net *net = xs_net(x);
2576 	unsigned int h;
2577 	struct xfrm_state *x0;
2578 	int err = -ENOENT;
2579 	u32 range = high - low + 1;
2580 	__be32 newspi = 0;
2581 
2582 	spin_lock_bh(&x->lock);
2583 	if (x->km.state == XFRM_STATE_DEAD) {
2584 		NL_SET_ERR_MSG(extack, "Target ACQUIRE is in DEAD state");
2585 		goto unlock;
2586 	}
2587 
2588 	err = 0;
2589 	if (x->id.spi)
2590 		goto unlock;
2591 
2592 	err = -ENOENT;
2593 
2594 	for (h = 0; h < range; h++) {
2595 		u32 spi = (low == high) ? low : get_random_u32_inclusive(low, high);
2596 		if (spi == 0)
2597 			goto next;
2598 		newspi = htonl(spi);
2599 
2600 		spin_lock_bh(&net->xfrm.xfrm_state_lock);
2601 		x0 = xfrm_state_lookup_spi_proto(net, newspi, x->id.proto);
2602 		if (!x0) {
2603 			x->id.spi = newspi;
2604 			h = xfrm_spi_hash(net, &x->id.daddr, newspi, x->id.proto, x->props.family);
2605 			XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h, x->xso.type);
2606 			spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2607 			err = 0;
2608 			goto unlock;
2609 		}
2610 		xfrm_state_put(x0);
2611 		spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2612 
2613 next:
2614 		if (signal_pending(current)) {
2615 			err = -ERESTARTSYS;
2616 			goto unlock;
2617 		}
2618 
2619 		if (low == high)
2620 			break;
2621 	}
2622 
2623 	if (err)
2624 		NL_SET_ERR_MSG(extack, "No SPI available in the requested range");
2625 
2626 unlock:
2627 	spin_unlock_bh(&x->lock);
2628 
2629 	return err;
2630 }
2631 EXPORT_SYMBOL(xfrm_alloc_spi);
2632 
__xfrm_state_filter_match(struct xfrm_state * x,struct xfrm_address_filter * filter)2633 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2634 				      struct xfrm_address_filter *filter)
2635 {
2636 	if (filter) {
2637 		if ((filter->family == AF_INET ||
2638 		     filter->family == AF_INET6) &&
2639 		    x->props.family != filter->family)
2640 			return false;
2641 
2642 		return addr_match(&x->props.saddr, &filter->saddr,
2643 				  filter->splen) &&
2644 		       addr_match(&x->id.daddr, &filter->daddr,
2645 				  filter->dplen);
2646 	}
2647 	return true;
2648 }
2649 
xfrm_state_walk(struct net * net,struct xfrm_state_walk * walk,int (* func)(struct xfrm_state *,int,void *),void * data)2650 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2651 		    int (*func)(struct xfrm_state *, int, void*),
2652 		    void *data)
2653 {
2654 	struct xfrm_state *state;
2655 	struct xfrm_state_walk *x;
2656 	int err = 0;
2657 
2658 	if (walk->seq != 0 && list_empty(&walk->all))
2659 		return 0;
2660 
2661 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2662 	if (list_empty(&walk->all))
2663 		x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2664 	else
2665 		x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2666 	list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2667 		if (x->state == XFRM_STATE_DEAD)
2668 			continue;
2669 		state = container_of(x, struct xfrm_state, km);
2670 		if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2671 			continue;
2672 		if (!__xfrm_state_filter_match(state, walk->filter))
2673 			continue;
2674 		err = func(state, walk->seq, data);
2675 		if (err) {
2676 			list_move_tail(&walk->all, &x->all);
2677 			goto out;
2678 		}
2679 		walk->seq++;
2680 	}
2681 	if (walk->seq == 0) {
2682 		err = -ENOENT;
2683 		goto out;
2684 	}
2685 	list_del_init(&walk->all);
2686 out:
2687 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2688 	return err;
2689 }
2690 EXPORT_SYMBOL(xfrm_state_walk);
2691 
xfrm_state_walk_init(struct xfrm_state_walk * walk,u8 proto,struct xfrm_address_filter * filter)2692 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2693 			  struct xfrm_address_filter *filter)
2694 {
2695 	INIT_LIST_HEAD(&walk->all);
2696 	walk->proto = proto;
2697 	walk->state = XFRM_STATE_DEAD;
2698 	walk->seq = 0;
2699 	walk->filter = filter;
2700 }
2701 EXPORT_SYMBOL(xfrm_state_walk_init);
2702 
xfrm_state_walk_done(struct xfrm_state_walk * walk,struct net * net)2703 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2704 {
2705 	kfree(walk->filter);
2706 
2707 	if (list_empty(&walk->all))
2708 		return;
2709 
2710 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2711 	list_del(&walk->all);
2712 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2713 }
2714 EXPORT_SYMBOL(xfrm_state_walk_done);
2715 
xfrm_replay_timer_handler(struct timer_list * t)2716 static void xfrm_replay_timer_handler(struct timer_list *t)
2717 {
2718 	struct xfrm_state *x = timer_container_of(x, t, rtimer);
2719 
2720 	spin_lock(&x->lock);
2721 
2722 	if (x->km.state == XFRM_STATE_VALID) {
2723 		if (xfrm_aevent_is_on(xs_net(x)))
2724 			xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
2725 		else
2726 			x->xflags |= XFRM_TIME_DEFER;
2727 	}
2728 
2729 	spin_unlock(&x->lock);
2730 }
2731 
2732 static LIST_HEAD(xfrm_km_list);
2733 
km_policy_notify(struct xfrm_policy * xp,int dir,const struct km_event * c)2734 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2735 {
2736 	struct xfrm_mgr *km;
2737 
2738 	rcu_read_lock();
2739 	list_for_each_entry_rcu(km, &xfrm_km_list, list)
2740 		if (km->notify_policy)
2741 			km->notify_policy(xp, dir, c);
2742 	rcu_read_unlock();
2743 }
2744 
km_state_notify(struct xfrm_state * x,const struct km_event * c)2745 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2746 {
2747 	struct xfrm_mgr *km;
2748 	rcu_read_lock();
2749 	list_for_each_entry_rcu(km, &xfrm_km_list, list)
2750 		if (km->notify)
2751 			km->notify(x, c);
2752 	rcu_read_unlock();
2753 }
2754 
2755 EXPORT_SYMBOL(km_policy_notify);
2756 EXPORT_SYMBOL(km_state_notify);
2757 
km_state_expired(struct xfrm_state * x,int hard,u32 portid)2758 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2759 {
2760 	struct km_event c;
2761 
2762 	c.data.hard = hard;
2763 	c.portid = portid;
2764 	c.event = XFRM_MSG_EXPIRE;
2765 	km_state_notify(x, &c);
2766 }
2767 
2768 EXPORT_SYMBOL(km_state_expired);
2769 /*
2770  * We send to all registered managers regardless of failure
2771  * We are happy with one success
2772 */
km_query(struct xfrm_state * x,struct xfrm_tmpl * t,struct xfrm_policy * pol)2773 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2774 {
2775 	int err = -EINVAL, acqret;
2776 	struct xfrm_mgr *km;
2777 
2778 	rcu_read_lock();
2779 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2780 		acqret = km->acquire(x, t, pol);
2781 		if (!acqret)
2782 			err = acqret;
2783 	}
2784 	rcu_read_unlock();
2785 	return err;
2786 }
2787 EXPORT_SYMBOL(km_query);
2788 
__km_new_mapping(struct xfrm_state * x,xfrm_address_t * ipaddr,__be16 sport)2789 static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2790 {
2791 	int err = -EINVAL;
2792 	struct xfrm_mgr *km;
2793 
2794 	rcu_read_lock();
2795 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2796 		if (km->new_mapping)
2797 			err = km->new_mapping(x, ipaddr, sport);
2798 		if (!err)
2799 			break;
2800 	}
2801 	rcu_read_unlock();
2802 	return err;
2803 }
2804 
km_new_mapping(struct xfrm_state * x,xfrm_address_t * ipaddr,__be16 sport)2805 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2806 {
2807 	int ret = 0;
2808 
2809 	if (x->mapping_maxage) {
2810 		if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
2811 		    x->new_mapping_sport != sport) {
2812 			x->new_mapping_sport = sport;
2813 			x->new_mapping = jiffies / HZ;
2814 			ret = __km_new_mapping(x, ipaddr, sport);
2815 		}
2816 	} else {
2817 		ret = __km_new_mapping(x, ipaddr, sport);
2818 	}
2819 
2820 	return ret;
2821 }
2822 EXPORT_SYMBOL(km_new_mapping);
2823 
km_policy_expired(struct xfrm_policy * pol,int dir,int hard,u32 portid)2824 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2825 {
2826 	struct km_event c;
2827 
2828 	c.data.hard = hard;
2829 	c.portid = portid;
2830 	c.event = XFRM_MSG_POLEXPIRE;
2831 	km_policy_notify(pol, dir, &c);
2832 }
2833 EXPORT_SYMBOL(km_policy_expired);
2834 
2835 #ifdef CONFIG_XFRM_MIGRATE
km_migrate(const struct xfrm_selector * sel,u8 dir,u8 type,const struct xfrm_migrate * m,int num_migrate,const struct xfrm_kmaddress * k,const struct xfrm_encap_tmpl * encap)2836 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2837 	       const struct xfrm_migrate *m, int num_migrate,
2838 	       const struct xfrm_kmaddress *k,
2839 	       const struct xfrm_encap_tmpl *encap)
2840 {
2841 	int err = -EINVAL;
2842 	int ret;
2843 	struct xfrm_mgr *km;
2844 
2845 	rcu_read_lock();
2846 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2847 		if (km->migrate) {
2848 			ret = km->migrate(sel, dir, type, m, num_migrate, k,
2849 					  encap);
2850 			if (!ret)
2851 				err = ret;
2852 		}
2853 	}
2854 	rcu_read_unlock();
2855 	return err;
2856 }
2857 EXPORT_SYMBOL(km_migrate);
2858 #endif
2859 
km_report(struct net * net,u8 proto,struct xfrm_selector * sel,xfrm_address_t * addr)2860 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2861 {
2862 	int err = -EINVAL;
2863 	int ret;
2864 	struct xfrm_mgr *km;
2865 
2866 	rcu_read_lock();
2867 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2868 		if (km->report) {
2869 			ret = km->report(net, proto, sel, addr);
2870 			if (!ret)
2871 				err = ret;
2872 		}
2873 	}
2874 	rcu_read_unlock();
2875 	return err;
2876 }
2877 EXPORT_SYMBOL(km_report);
2878 
km_is_alive(const struct km_event * c)2879 static bool km_is_alive(const struct km_event *c)
2880 {
2881 	struct xfrm_mgr *km;
2882 	bool is_alive = false;
2883 
2884 	rcu_read_lock();
2885 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2886 		if (km->is_alive && km->is_alive(c)) {
2887 			is_alive = true;
2888 			break;
2889 		}
2890 	}
2891 	rcu_read_unlock();
2892 
2893 	return is_alive;
2894 }
2895 
2896 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2897 static DEFINE_SPINLOCK(xfrm_translator_lock);
2898 static struct xfrm_translator __rcu *xfrm_translator;
2899 
xfrm_get_translator(void)2900 struct xfrm_translator *xfrm_get_translator(void)
2901 {
2902 	struct xfrm_translator *xtr;
2903 
2904 	rcu_read_lock();
2905 	xtr = rcu_dereference(xfrm_translator);
2906 	if (unlikely(!xtr))
2907 		goto out;
2908 	if (!try_module_get(xtr->owner))
2909 		xtr = NULL;
2910 out:
2911 	rcu_read_unlock();
2912 	return xtr;
2913 }
2914 EXPORT_SYMBOL_GPL(xfrm_get_translator);
2915 
xfrm_put_translator(struct xfrm_translator * xtr)2916 void xfrm_put_translator(struct xfrm_translator *xtr)
2917 {
2918 	module_put(xtr->owner);
2919 }
2920 EXPORT_SYMBOL_GPL(xfrm_put_translator);
2921 
xfrm_register_translator(struct xfrm_translator * xtr)2922 int xfrm_register_translator(struct xfrm_translator *xtr)
2923 {
2924 	int err = 0;
2925 
2926 	spin_lock_bh(&xfrm_translator_lock);
2927 	if (unlikely(xfrm_translator != NULL))
2928 		err = -EEXIST;
2929 	else
2930 		rcu_assign_pointer(xfrm_translator, xtr);
2931 	spin_unlock_bh(&xfrm_translator_lock);
2932 
2933 	return err;
2934 }
2935 EXPORT_SYMBOL_GPL(xfrm_register_translator);
2936 
xfrm_unregister_translator(struct xfrm_translator * xtr)2937 int xfrm_unregister_translator(struct xfrm_translator *xtr)
2938 {
2939 	int err = 0;
2940 
2941 	spin_lock_bh(&xfrm_translator_lock);
2942 	if (likely(xfrm_translator != NULL)) {
2943 		if (rcu_access_pointer(xfrm_translator) != xtr)
2944 			err = -EINVAL;
2945 		else
2946 			RCU_INIT_POINTER(xfrm_translator, NULL);
2947 	}
2948 	spin_unlock_bh(&xfrm_translator_lock);
2949 	synchronize_rcu();
2950 
2951 	return err;
2952 }
2953 EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2954 #endif
2955 
xfrm_user_policy(struct sock * sk,int optname,sockptr_t optval,int optlen)2956 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
2957 {
2958 	int err;
2959 	u8 *data;
2960 	struct xfrm_mgr *km;
2961 	struct xfrm_policy *pol = NULL;
2962 
2963 	if (sockptr_is_null(optval) && !optlen) {
2964 		xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2965 		xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2966 		__sk_dst_reset(sk);
2967 		return 0;
2968 	}
2969 
2970 	if (optlen <= 0 || optlen > PAGE_SIZE)
2971 		return -EMSGSIZE;
2972 
2973 	data = memdup_sockptr(optval, optlen);
2974 	if (IS_ERR(data))
2975 		return PTR_ERR(data);
2976 
2977 	if (in_compat_syscall()) {
2978 		struct xfrm_translator *xtr = xfrm_get_translator();
2979 
2980 		if (!xtr) {
2981 			kfree(data);
2982 			return -EOPNOTSUPP;
2983 		}
2984 
2985 		err = xtr->xlate_user_policy_sockptr(&data, optlen);
2986 		xfrm_put_translator(xtr);
2987 		if (err) {
2988 			kfree(data);
2989 			return err;
2990 		}
2991 	}
2992 
2993 	err = -EINVAL;
2994 	rcu_read_lock();
2995 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2996 		pol = km->compile_policy(sk, optname, data,
2997 					 optlen, &err);
2998 		if (err >= 0)
2999 			break;
3000 	}
3001 	rcu_read_unlock();
3002 
3003 	if (err >= 0) {
3004 		xfrm_sk_policy_insert(sk, err, pol);
3005 		xfrm_pol_put(pol);
3006 		__sk_dst_reset(sk);
3007 		err = 0;
3008 	}
3009 
3010 	kfree(data);
3011 	return err;
3012 }
3013 EXPORT_SYMBOL(xfrm_user_policy);
3014 
3015 static DEFINE_SPINLOCK(xfrm_km_lock);
3016 
xfrm_register_km(struct xfrm_mgr * km)3017 void xfrm_register_km(struct xfrm_mgr *km)
3018 {
3019 	spin_lock_bh(&xfrm_km_lock);
3020 	list_add_tail_rcu(&km->list, &xfrm_km_list);
3021 	spin_unlock_bh(&xfrm_km_lock);
3022 }
3023 EXPORT_SYMBOL(xfrm_register_km);
3024 
xfrm_unregister_km(struct xfrm_mgr * km)3025 void xfrm_unregister_km(struct xfrm_mgr *km)
3026 {
3027 	spin_lock_bh(&xfrm_km_lock);
3028 	list_del_rcu(&km->list);
3029 	spin_unlock_bh(&xfrm_km_lock);
3030 	synchronize_rcu();
3031 }
3032 EXPORT_SYMBOL(xfrm_unregister_km);
3033 
xfrm_state_register_afinfo(struct xfrm_state_afinfo * afinfo)3034 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
3035 {
3036 	int err = 0;
3037 
3038 	if (WARN_ON(afinfo->family >= NPROTO))
3039 		return -EAFNOSUPPORT;
3040 
3041 	spin_lock_bh(&xfrm_state_afinfo_lock);
3042 	if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
3043 		err = -EEXIST;
3044 	else
3045 		rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
3046 	spin_unlock_bh(&xfrm_state_afinfo_lock);
3047 	return err;
3048 }
3049 EXPORT_SYMBOL(xfrm_state_register_afinfo);
3050 
xfrm_state_unregister_afinfo(struct xfrm_state_afinfo * afinfo)3051 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
3052 {
3053 	int err = 0, family = afinfo->family;
3054 
3055 	if (WARN_ON(family >= NPROTO))
3056 		return -EAFNOSUPPORT;
3057 
3058 	spin_lock_bh(&xfrm_state_afinfo_lock);
3059 	if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
3060 		if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
3061 			err = -EINVAL;
3062 		else
3063 			RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
3064 	}
3065 	spin_unlock_bh(&xfrm_state_afinfo_lock);
3066 	synchronize_rcu();
3067 	return err;
3068 }
3069 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
3070 
xfrm_state_afinfo_get_rcu(unsigned int family)3071 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
3072 {
3073 	if (unlikely(family >= NPROTO))
3074 		return NULL;
3075 
3076 	return rcu_dereference(xfrm_state_afinfo[family]);
3077 }
3078 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
3079 
xfrm_state_get_afinfo(unsigned int family)3080 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
3081 {
3082 	struct xfrm_state_afinfo *afinfo;
3083 	if (unlikely(family >= NPROTO))
3084 		return NULL;
3085 	rcu_read_lock();
3086 	afinfo = rcu_dereference(xfrm_state_afinfo[family]);
3087 	if (unlikely(!afinfo))
3088 		rcu_read_unlock();
3089 	return afinfo;
3090 }
3091 
xfrm_flush_gc(void)3092 void xfrm_flush_gc(void)
3093 {
3094 	flush_work(&xfrm_state_gc_work);
3095 }
3096 EXPORT_SYMBOL(xfrm_flush_gc);
3097 
xfrm_state_delete_tunnel(struct xfrm_state * x)3098 static void xfrm_state_delete_tunnel(struct xfrm_state *x)
3099 {
3100 	if (x->tunnel) {
3101 		struct xfrm_state *t = x->tunnel;
3102 
3103 		if (atomic_dec_return(&t->tunnel_users) == 1)
3104 			xfrm_state_delete(t);
3105 		xfrm_state_put(t);
3106 		x->tunnel = NULL;
3107 	}
3108 }
3109 
xfrm_state_mtu(struct xfrm_state * x,int mtu)3110 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
3111 {
3112 	const struct xfrm_type *type = READ_ONCE(x->type);
3113 	struct crypto_aead *aead;
3114 	u32 blksize, net_adj = 0;
3115 
3116 	if (x->km.state != XFRM_STATE_VALID ||
3117 	    !type || type->proto != IPPROTO_ESP)
3118 		return mtu - x->props.header_len;
3119 
3120 	aead = x->data;
3121 	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
3122 
3123 	switch (x->props.mode) {
3124 	case XFRM_MODE_TRANSPORT:
3125 	case XFRM_MODE_BEET:
3126 		if (x->props.family == AF_INET)
3127 			net_adj = sizeof(struct iphdr);
3128 		else if (x->props.family == AF_INET6)
3129 			net_adj = sizeof(struct ipv6hdr);
3130 		break;
3131 	case XFRM_MODE_TUNNEL:
3132 		break;
3133 	default:
3134 		if (x->mode_cbs && x->mode_cbs->get_inner_mtu)
3135 			return x->mode_cbs->get_inner_mtu(x, mtu);
3136 
3137 		WARN_ON_ONCE(1);
3138 		break;
3139 	}
3140 
3141 	return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
3142 		 net_adj) & ~(blksize - 1)) + net_adj - 2;
3143 }
3144 EXPORT_SYMBOL_GPL(xfrm_state_mtu);
3145 
__xfrm_init_state(struct xfrm_state * x,struct netlink_ext_ack * extack)3146 int __xfrm_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
3147 {
3148 	const struct xfrm_mode *inner_mode;
3149 	const struct xfrm_mode *outer_mode;
3150 	int family = x->props.family;
3151 	int err;
3152 
3153 	if (family == AF_INET &&
3154 	    READ_ONCE(xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc))
3155 		x->props.flags |= XFRM_STATE_NOPMTUDISC;
3156 
3157 	err = -EPROTONOSUPPORT;
3158 
3159 	if (x->sel.family != AF_UNSPEC) {
3160 		inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
3161 		if (inner_mode == NULL) {
3162 			NL_SET_ERR_MSG(extack, "Requested mode not found");
3163 			goto error;
3164 		}
3165 
3166 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
3167 		    family != x->sel.family) {
3168 			NL_SET_ERR_MSG(extack, "Only tunnel modes can accommodate a change of family");
3169 			goto error;
3170 		}
3171 
3172 		x->inner_mode = *inner_mode;
3173 	} else {
3174 		const struct xfrm_mode *inner_mode_iaf;
3175 		int iafamily = AF_INET;
3176 
3177 		inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
3178 		if (inner_mode == NULL) {
3179 			NL_SET_ERR_MSG(extack, "Requested mode not found");
3180 			goto error;
3181 		}
3182 
3183 		x->inner_mode = *inner_mode;
3184 
3185 		if (x->props.family == AF_INET)
3186 			iafamily = AF_INET6;
3187 
3188 		inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
3189 		if (inner_mode_iaf) {
3190 			if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
3191 				x->inner_mode_iaf = *inner_mode_iaf;
3192 		}
3193 	}
3194 
3195 	x->type = xfrm_get_type(x->id.proto, family);
3196 	if (x->type == NULL) {
3197 		NL_SET_ERR_MSG(extack, "Requested type not found");
3198 		goto error;
3199 	}
3200 
3201 	err = x->type->init_state(x, extack);
3202 	if (err)
3203 		goto error;
3204 
3205 	outer_mode = xfrm_get_mode(x->props.mode, family);
3206 	if (!outer_mode) {
3207 		NL_SET_ERR_MSG(extack, "Requested mode not found");
3208 		err = -EPROTONOSUPPORT;
3209 		goto error;
3210 	}
3211 
3212 	x->outer_mode = *outer_mode;
3213 	if (x->nat_keepalive_interval) {
3214 		if (x->dir != XFRM_SA_DIR_OUT) {
3215 			NL_SET_ERR_MSG(extack, "NAT keepalive is only supported for outbound SAs");
3216 			err = -EINVAL;
3217 			goto error;
3218 		}
3219 
3220 		if (!x->encap || x->encap->encap_type != UDP_ENCAP_ESPINUDP) {
3221 			NL_SET_ERR_MSG(extack,
3222 				       "NAT keepalive is only supported for UDP encapsulation");
3223 			err = -EINVAL;
3224 			goto error;
3225 		}
3226 	}
3227 
3228 	x->mode_cbs = xfrm_get_mode_cbs(x->props.mode);
3229 	if (x->mode_cbs) {
3230 		if (x->mode_cbs->init_state)
3231 			err = x->mode_cbs->init_state(x);
3232 		module_put(x->mode_cbs->owner);
3233 	}
3234 error:
3235 	return err;
3236 }
3237 
3238 EXPORT_SYMBOL(__xfrm_init_state);
3239 
xfrm_init_state(struct xfrm_state * x)3240 int xfrm_init_state(struct xfrm_state *x)
3241 {
3242 	int err;
3243 
3244 	err = __xfrm_init_state(x, NULL);
3245 	if (err)
3246 		return err;
3247 
3248 	err = xfrm_init_replay(x, NULL);
3249 	if (err)
3250 		return err;
3251 
3252 	x->km.state = XFRM_STATE_VALID;
3253 	return 0;
3254 }
3255 
3256 EXPORT_SYMBOL(xfrm_init_state);
3257 
xfrm_state_init(struct net * net)3258 int __net_init xfrm_state_init(struct net *net)
3259 {
3260 	unsigned int sz;
3261 
3262 	if (net_eq(net, &init_net))
3263 		xfrm_state_cache = KMEM_CACHE(xfrm_state,
3264 					      SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3265 
3266 	INIT_LIST_HEAD(&net->xfrm.state_all);
3267 
3268 	sz = sizeof(struct hlist_head) * 8;
3269 
3270 	net->xfrm.state_bydst = xfrm_hash_alloc(sz);
3271 	if (!net->xfrm.state_bydst)
3272 		goto out_bydst;
3273 	net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
3274 	if (!net->xfrm.state_bysrc)
3275 		goto out_bysrc;
3276 	net->xfrm.state_byspi = xfrm_hash_alloc(sz);
3277 	if (!net->xfrm.state_byspi)
3278 		goto out_byspi;
3279 	net->xfrm.state_byseq = xfrm_hash_alloc(sz);
3280 	if (!net->xfrm.state_byseq)
3281 		goto out_byseq;
3282 
3283 	net->xfrm.state_cache_input = alloc_percpu(struct hlist_head);
3284 	if (!net->xfrm.state_cache_input)
3285 		goto out_state_cache_input;
3286 
3287 	net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
3288 
3289 	net->xfrm.state_num = 0;
3290 	INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
3291 	spin_lock_init(&net->xfrm.xfrm_state_lock);
3292 	seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation,
3293 			       &net->xfrm.xfrm_state_lock);
3294 	return 0;
3295 
3296 out_state_cache_input:
3297 	xfrm_hash_free(net->xfrm.state_byseq, sz);
3298 out_byseq:
3299 	xfrm_hash_free(net->xfrm.state_byspi, sz);
3300 out_byspi:
3301 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
3302 out_bysrc:
3303 	xfrm_hash_free(net->xfrm.state_bydst, sz);
3304 out_bydst:
3305 	return -ENOMEM;
3306 }
3307 
xfrm_state_fini(struct net * net)3308 void xfrm_state_fini(struct net *net)
3309 {
3310 	unsigned int sz;
3311 	int i;
3312 
3313 	flush_work(&net->xfrm.state_hash_work);
3314 	xfrm_state_flush(net, 0, false);
3315 	flush_work(&xfrm_state_gc_work);
3316 
3317 	WARN_ON(!list_empty(&net->xfrm.state_all));
3318 
3319 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
3320 		WARN_ON(!hlist_empty(net->xfrm.state_byseq + i));
3321 		WARN_ON(!hlist_empty(net->xfrm.state_byspi + i));
3322 		WARN_ON(!hlist_empty(net->xfrm.state_bysrc + i));
3323 		WARN_ON(!hlist_empty(net->xfrm.state_bydst + i));
3324 	}
3325 
3326 	sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
3327 	xfrm_hash_free(net->xfrm.state_byseq, sz);
3328 	xfrm_hash_free(net->xfrm.state_byspi, sz);
3329 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
3330 	xfrm_hash_free(net->xfrm.state_bydst, sz);
3331 	free_percpu(net->xfrm.state_cache_input);
3332 }
3333 
3334 #ifdef CONFIG_AUDITSYSCALL
xfrm_audit_helper_sainfo(struct xfrm_state * x,struct audit_buffer * audit_buf)3335 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
3336 				     struct audit_buffer *audit_buf)
3337 {
3338 	struct xfrm_sec_ctx *ctx = x->security;
3339 	u32 spi = ntohl(x->id.spi);
3340 
3341 	if (ctx)
3342 		audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3343 				 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3344 
3345 	switch (x->props.family) {
3346 	case AF_INET:
3347 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
3348 				 &x->props.saddr.a4, &x->id.daddr.a4);
3349 		break;
3350 	case AF_INET6:
3351 		audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
3352 				 x->props.saddr.a6, x->id.daddr.a6);
3353 		break;
3354 	}
3355 
3356 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
3357 }
3358 
xfrm_audit_helper_pktinfo(struct sk_buff * skb,u16 family,struct audit_buffer * audit_buf)3359 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
3360 				      struct audit_buffer *audit_buf)
3361 {
3362 	const struct iphdr *iph4;
3363 	const struct ipv6hdr *iph6;
3364 
3365 	switch (family) {
3366 	case AF_INET:
3367 		iph4 = ip_hdr(skb);
3368 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
3369 				 &iph4->saddr, &iph4->daddr);
3370 		break;
3371 	case AF_INET6:
3372 		iph6 = ipv6_hdr(skb);
3373 		audit_log_format(audit_buf,
3374 				 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
3375 				 &iph6->saddr, &iph6->daddr,
3376 				 iph6->flow_lbl[0] & 0x0f,
3377 				 iph6->flow_lbl[1],
3378 				 iph6->flow_lbl[2]);
3379 		break;
3380 	}
3381 }
3382 
xfrm_audit_state_add(struct xfrm_state * x,int result,bool task_valid)3383 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
3384 {
3385 	struct audit_buffer *audit_buf;
3386 
3387 	audit_buf = xfrm_audit_start("SAD-add");
3388 	if (audit_buf == NULL)
3389 		return;
3390 	xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3391 	xfrm_audit_helper_sainfo(x, audit_buf);
3392 	audit_log_format(audit_buf, " res=%u", result);
3393 	audit_log_end(audit_buf);
3394 }
3395 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
3396 
xfrm_audit_state_delete(struct xfrm_state * x,int result,bool task_valid)3397 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
3398 {
3399 	struct audit_buffer *audit_buf;
3400 
3401 	audit_buf = xfrm_audit_start("SAD-delete");
3402 	if (audit_buf == NULL)
3403 		return;
3404 	xfrm_audit_helper_usrinfo(task_valid, audit_buf);
3405 	xfrm_audit_helper_sainfo(x, audit_buf);
3406 	audit_log_format(audit_buf, " res=%u", result);
3407 	audit_log_end(audit_buf);
3408 }
3409 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
3410 
xfrm_audit_state_replay_overflow(struct xfrm_state * x,struct sk_buff * skb)3411 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
3412 				      struct sk_buff *skb)
3413 {
3414 	struct audit_buffer *audit_buf;
3415 	u32 spi;
3416 
3417 	audit_buf = xfrm_audit_start("SA-replay-overflow");
3418 	if (audit_buf == NULL)
3419 		return;
3420 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3421 	/* don't record the sequence number because it's inherent in this kind
3422 	 * of audit message */
3423 	spi = ntohl(x->id.spi);
3424 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
3425 	audit_log_end(audit_buf);
3426 }
3427 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
3428 
xfrm_audit_state_replay(struct xfrm_state * x,struct sk_buff * skb,__be32 net_seq)3429 void xfrm_audit_state_replay(struct xfrm_state *x,
3430 			     struct sk_buff *skb, __be32 net_seq)
3431 {
3432 	struct audit_buffer *audit_buf;
3433 	u32 spi;
3434 
3435 	audit_buf = xfrm_audit_start("SA-replayed-pkt");
3436 	if (audit_buf == NULL)
3437 		return;
3438 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3439 	spi = ntohl(x->id.spi);
3440 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3441 			 spi, spi, ntohl(net_seq));
3442 	audit_log_end(audit_buf);
3443 }
3444 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
3445 
xfrm_audit_state_notfound_simple(struct sk_buff * skb,u16 family)3446 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
3447 {
3448 	struct audit_buffer *audit_buf;
3449 
3450 	audit_buf = xfrm_audit_start("SA-notfound");
3451 	if (audit_buf == NULL)
3452 		return;
3453 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
3454 	audit_log_end(audit_buf);
3455 }
3456 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
3457 
xfrm_audit_state_notfound(struct sk_buff * skb,u16 family,__be32 net_spi,__be32 net_seq)3458 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
3459 			       __be32 net_spi, __be32 net_seq)
3460 {
3461 	struct audit_buffer *audit_buf;
3462 	u32 spi;
3463 
3464 	audit_buf = xfrm_audit_start("SA-notfound");
3465 	if (audit_buf == NULL)
3466 		return;
3467 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
3468 	spi = ntohl(net_spi);
3469 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3470 			 spi, spi, ntohl(net_seq));
3471 	audit_log_end(audit_buf);
3472 }
3473 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
3474 
xfrm_audit_state_icvfail(struct xfrm_state * x,struct sk_buff * skb,u8 proto)3475 void xfrm_audit_state_icvfail(struct xfrm_state *x,
3476 			      struct sk_buff *skb, u8 proto)
3477 {
3478 	struct audit_buffer *audit_buf;
3479 	__be32 net_spi;
3480 	__be32 net_seq;
3481 
3482 	audit_buf = xfrm_audit_start("SA-icv-failure");
3483 	if (audit_buf == NULL)
3484 		return;
3485 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3486 	if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
3487 		u32 spi = ntohl(net_spi);
3488 		audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3489 				 spi, spi, ntohl(net_seq));
3490 	}
3491 	audit_log_end(audit_buf);
3492 }
3493 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
3494 #endif /* CONFIG_AUDITSYSCALL */
3495