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