xref: /openbmc/linux/net/xfrm/xfrm_state.c (revision 92d0c2eb)
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_state_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_state_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 	if (xfrm_init_state(x) < 0)
1583 		goto error;
1584 
1585 	x->props.flags = orig->props.flags;
1586 	x->props.extra_flags = orig->props.extra_flags;
1587 
1588 	x->if_id = orig->if_id;
1589 	x->tfcpad = orig->tfcpad;
1590 	x->replay_maxdiff = orig->replay_maxdiff;
1591 	x->replay_maxage = orig->replay_maxage;
1592 	memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
1593 	x->km.state = orig->km.state;
1594 	x->km.seq = orig->km.seq;
1595 	x->replay = orig->replay;
1596 	x->preplay = orig->preplay;
1597 	x->mapping_maxage = orig->mapping_maxage;
1598 	x->new_mapping = 0;
1599 	x->new_mapping_sport = 0;
1600 
1601 	return x;
1602 
1603  error:
1604 	xfrm_state_put(x);
1605 out:
1606 	return NULL;
1607 }
1608 
1609 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1610 {
1611 	unsigned int h;
1612 	struct xfrm_state *x = NULL;
1613 
1614 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1615 
1616 	if (m->reqid) {
1617 		h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1618 				  m->reqid, m->old_family);
1619 		hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1620 			if (x->props.mode != m->mode ||
1621 			    x->id.proto != m->proto)
1622 				continue;
1623 			if (m->reqid && x->props.reqid != m->reqid)
1624 				continue;
1625 			if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1626 					     m->old_family) ||
1627 			    !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1628 					     m->old_family))
1629 				continue;
1630 			xfrm_state_hold(x);
1631 			break;
1632 		}
1633 	} else {
1634 		h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1635 				  m->old_family);
1636 		hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1637 			if (x->props.mode != m->mode ||
1638 			    x->id.proto != m->proto)
1639 				continue;
1640 			if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1641 					     m->old_family) ||
1642 			    !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1643 					     m->old_family))
1644 				continue;
1645 			xfrm_state_hold(x);
1646 			break;
1647 		}
1648 	}
1649 
1650 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1651 
1652 	return x;
1653 }
1654 EXPORT_SYMBOL(xfrm_migrate_state_find);
1655 
1656 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1657 				      struct xfrm_migrate *m,
1658 				      struct xfrm_encap_tmpl *encap)
1659 {
1660 	struct xfrm_state *xc;
1661 
1662 	xc = xfrm_state_clone(x, encap);
1663 	if (!xc)
1664 		return NULL;
1665 
1666 	memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1667 	memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1668 
1669 	/* add state */
1670 	if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1671 		/* a care is needed when the destination address of the
1672 		   state is to be updated as it is a part of triplet */
1673 		xfrm_state_insert(xc);
1674 	} else {
1675 		if (xfrm_state_add(xc) < 0)
1676 			goto error;
1677 	}
1678 
1679 	return xc;
1680 error:
1681 	xfrm_state_put(xc);
1682 	return NULL;
1683 }
1684 EXPORT_SYMBOL(xfrm_state_migrate);
1685 #endif
1686 
1687 int xfrm_state_update(struct xfrm_state *x)
1688 {
1689 	struct xfrm_state *x1, *to_put;
1690 	int err;
1691 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1692 	struct net *net = xs_net(x);
1693 
1694 	to_put = NULL;
1695 
1696 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1697 	x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1698 
1699 	err = -ESRCH;
1700 	if (!x1)
1701 		goto out;
1702 
1703 	if (xfrm_state_kern(x1)) {
1704 		to_put = x1;
1705 		err = -EEXIST;
1706 		goto out;
1707 	}
1708 
1709 	if (x1->km.state == XFRM_STATE_ACQ) {
1710 		__xfrm_state_insert(x);
1711 		x = NULL;
1712 	}
1713 	err = 0;
1714 
1715 out:
1716 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1717 
1718 	if (to_put)
1719 		xfrm_state_put(to_put);
1720 
1721 	if (err)
1722 		return err;
1723 
1724 	if (!x) {
1725 		xfrm_state_delete(x1);
1726 		xfrm_state_put(x1);
1727 		return 0;
1728 	}
1729 
1730 	err = -EINVAL;
1731 	spin_lock_bh(&x1->lock);
1732 	if (likely(x1->km.state == XFRM_STATE_VALID)) {
1733 		if (x->encap && x1->encap &&
1734 		    x->encap->encap_type == x1->encap->encap_type)
1735 			memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1736 		else if (x->encap || x1->encap)
1737 			goto fail;
1738 
1739 		if (x->coaddr && x1->coaddr) {
1740 			memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1741 		}
1742 		if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1743 			memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1744 		memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1745 		x1->km.dying = 0;
1746 
1747 		hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1748 			      HRTIMER_MODE_REL_SOFT);
1749 		if (x1->curlft.use_time)
1750 			xfrm_state_check_expire(x1);
1751 
1752 		if (x->props.smark.m || x->props.smark.v || x->if_id) {
1753 			spin_lock_bh(&net->xfrm.xfrm_state_lock);
1754 
1755 			if (x->props.smark.m || x->props.smark.v)
1756 				x1->props.smark = x->props.smark;
1757 
1758 			if (x->if_id)
1759 				x1->if_id = x->if_id;
1760 
1761 			__xfrm_state_bump_genids(x1);
1762 			spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1763 		}
1764 
1765 		err = 0;
1766 		x->km.state = XFRM_STATE_DEAD;
1767 		__xfrm_state_put(x);
1768 	}
1769 
1770 fail:
1771 	spin_unlock_bh(&x1->lock);
1772 
1773 	xfrm_state_put(x1);
1774 
1775 	return err;
1776 }
1777 EXPORT_SYMBOL(xfrm_state_update);
1778 
1779 int xfrm_state_check_expire(struct xfrm_state *x)
1780 {
1781 	if (!x->curlft.use_time)
1782 		x->curlft.use_time = ktime_get_real_seconds();
1783 
1784 	if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1785 	    x->curlft.packets >= x->lft.hard_packet_limit) {
1786 		x->km.state = XFRM_STATE_EXPIRED;
1787 		hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
1788 		return -EINVAL;
1789 	}
1790 
1791 	if (!x->km.dying &&
1792 	    (x->curlft.bytes >= x->lft.soft_byte_limit ||
1793 	     x->curlft.packets >= x->lft.soft_packet_limit)) {
1794 		x->km.dying = 1;
1795 		km_state_expired(x, 0, 0);
1796 	}
1797 	return 0;
1798 }
1799 EXPORT_SYMBOL(xfrm_state_check_expire);
1800 
1801 struct xfrm_state *
1802 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1803 		  u8 proto, unsigned short family)
1804 {
1805 	struct xfrm_state *x;
1806 
1807 	rcu_read_lock();
1808 	x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1809 	rcu_read_unlock();
1810 	return x;
1811 }
1812 EXPORT_SYMBOL(xfrm_state_lookup);
1813 
1814 struct xfrm_state *
1815 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1816 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1817 			 u8 proto, unsigned short family)
1818 {
1819 	struct xfrm_state *x;
1820 
1821 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1822 	x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1823 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1824 	return x;
1825 }
1826 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1827 
1828 struct xfrm_state *
1829 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1830 	      u32 if_id, u8 proto, const xfrm_address_t *daddr,
1831 	      const xfrm_address_t *saddr, int create, unsigned short family)
1832 {
1833 	struct xfrm_state *x;
1834 
1835 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1836 	x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
1837 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1838 
1839 	return x;
1840 }
1841 EXPORT_SYMBOL(xfrm_find_acq);
1842 
1843 #ifdef CONFIG_XFRM_SUB_POLICY
1844 #if IS_ENABLED(CONFIG_IPV6)
1845 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
1846 static void
1847 __xfrm6_sort(void **dst, void **src, int n,
1848 	     int (*cmp)(const void *p), int maxclass)
1849 {
1850 	int count[XFRM_MAX_DEPTH] = { };
1851 	int class[XFRM_MAX_DEPTH];
1852 	int i;
1853 
1854 	for (i = 0; i < n; i++) {
1855 		int c = cmp(src[i]);
1856 
1857 		class[i] = c;
1858 		count[c]++;
1859 	}
1860 
1861 	for (i = 2; i < maxclass; i++)
1862 		count[i] += count[i - 1];
1863 
1864 	for (i = 0; i < n; i++) {
1865 		dst[count[class[i] - 1]++] = src[i];
1866 		src[i] = NULL;
1867 	}
1868 }
1869 
1870 /* Rule for xfrm_state:
1871  *
1872  * rule 1: select IPsec transport except AH
1873  * rule 2: select MIPv6 RO or inbound trigger
1874  * rule 3: select IPsec transport AH
1875  * rule 4: select IPsec tunnel
1876  * rule 5: others
1877  */
1878 static int __xfrm6_state_sort_cmp(const void *p)
1879 {
1880 	const struct xfrm_state *v = p;
1881 
1882 	switch (v->props.mode) {
1883 	case XFRM_MODE_TRANSPORT:
1884 		if (v->id.proto != IPPROTO_AH)
1885 			return 1;
1886 		else
1887 			return 3;
1888 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1889 	case XFRM_MODE_ROUTEOPTIMIZATION:
1890 	case XFRM_MODE_IN_TRIGGER:
1891 		return 2;
1892 #endif
1893 	case XFRM_MODE_TUNNEL:
1894 	case XFRM_MODE_BEET:
1895 		return 4;
1896 	}
1897 	return 5;
1898 }
1899 
1900 /* Rule for xfrm_tmpl:
1901  *
1902  * rule 1: select IPsec transport
1903  * rule 2: select MIPv6 RO or inbound trigger
1904  * rule 3: select IPsec tunnel
1905  * rule 4: others
1906  */
1907 static int __xfrm6_tmpl_sort_cmp(const void *p)
1908 {
1909 	const struct xfrm_tmpl *v = p;
1910 
1911 	switch (v->mode) {
1912 	case XFRM_MODE_TRANSPORT:
1913 		return 1;
1914 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1915 	case XFRM_MODE_ROUTEOPTIMIZATION:
1916 	case XFRM_MODE_IN_TRIGGER:
1917 		return 2;
1918 #endif
1919 	case XFRM_MODE_TUNNEL:
1920 	case XFRM_MODE_BEET:
1921 		return 3;
1922 	}
1923 	return 4;
1924 }
1925 #else
1926 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
1927 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1928 
1929 static inline void
1930 __xfrm6_sort(void **dst, void **src, int n,
1931 	     int (*cmp)(const void *p), int maxclass)
1932 {
1933 	int i;
1934 
1935 	for (i = 0; i < n; i++)
1936 		dst[i] = src[i];
1937 }
1938 #endif /* CONFIG_IPV6 */
1939 
1940 void
1941 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1942 	       unsigned short family)
1943 {
1944 	int i;
1945 
1946 	if (family == AF_INET6)
1947 		__xfrm6_sort((void **)dst, (void **)src, n,
1948 			     __xfrm6_tmpl_sort_cmp, 5);
1949 	else
1950 		for (i = 0; i < n; i++)
1951 			dst[i] = src[i];
1952 }
1953 
1954 void
1955 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1956 		unsigned short family)
1957 {
1958 	int i;
1959 
1960 	if (family == AF_INET6)
1961 		__xfrm6_sort((void **)dst, (void **)src, n,
1962 			     __xfrm6_state_sort_cmp, 6);
1963 	else
1964 		for (i = 0; i < n; i++)
1965 			dst[i] = src[i];
1966 }
1967 #endif
1968 
1969 /* Silly enough, but I'm lazy to build resolution list */
1970 
1971 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1972 {
1973 	unsigned int h = xfrm_seq_hash(net, seq);
1974 	struct xfrm_state *x;
1975 
1976 	hlist_for_each_entry_rcu(x, net->xfrm.state_byseq + h, byseq) {
1977 		if (x->km.seq == seq &&
1978 		    (mark & x->mark.m) == x->mark.v &&
1979 		    x->km.state == XFRM_STATE_ACQ) {
1980 			xfrm_state_hold(x);
1981 			return x;
1982 		}
1983 	}
1984 
1985 	return NULL;
1986 }
1987 
1988 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1989 {
1990 	struct xfrm_state *x;
1991 
1992 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1993 	x = __xfrm_find_acq_byseq(net, mark, seq);
1994 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1995 	return x;
1996 }
1997 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1998 
1999 u32 xfrm_get_acqseq(void)
2000 {
2001 	u32 res;
2002 	static atomic_t acqseq;
2003 
2004 	do {
2005 		res = atomic_inc_return(&acqseq);
2006 	} while (!res);
2007 
2008 	return res;
2009 }
2010 EXPORT_SYMBOL(xfrm_get_acqseq);
2011 
2012 int verify_spi_info(u8 proto, u32 min, u32 max)
2013 {
2014 	switch (proto) {
2015 	case IPPROTO_AH:
2016 	case IPPROTO_ESP:
2017 		break;
2018 
2019 	case IPPROTO_COMP:
2020 		/* IPCOMP spi is 16-bits. */
2021 		if (max >= 0x10000)
2022 			return -EINVAL;
2023 		break;
2024 
2025 	default:
2026 		return -EINVAL;
2027 	}
2028 
2029 	if (min > max)
2030 		return -EINVAL;
2031 
2032 	return 0;
2033 }
2034 EXPORT_SYMBOL(verify_spi_info);
2035 
2036 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
2037 {
2038 	struct net *net = xs_net(x);
2039 	unsigned int h;
2040 	struct xfrm_state *x0;
2041 	int err = -ENOENT;
2042 	__be32 minspi = htonl(low);
2043 	__be32 maxspi = htonl(high);
2044 	__be32 newspi = 0;
2045 	u32 mark = x->mark.v & x->mark.m;
2046 
2047 	spin_lock_bh(&x->lock);
2048 	if (x->km.state == XFRM_STATE_DEAD)
2049 		goto unlock;
2050 
2051 	err = 0;
2052 	if (x->id.spi)
2053 		goto unlock;
2054 
2055 	err = -ENOENT;
2056 
2057 	if (minspi == maxspi) {
2058 		x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
2059 		if (x0) {
2060 			xfrm_state_put(x0);
2061 			goto unlock;
2062 		}
2063 		newspi = minspi;
2064 	} else {
2065 		u32 spi = 0;
2066 		for (h = 0; h < high-low+1; h++) {
2067 			spi = low + prandom_u32()%(high-low+1);
2068 			x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
2069 			if (x0 == NULL) {
2070 				newspi = htonl(spi);
2071 				break;
2072 			}
2073 			xfrm_state_put(x0);
2074 		}
2075 	}
2076 	if (newspi) {
2077 		spin_lock_bh(&net->xfrm.xfrm_state_lock);
2078 		x->id.spi = newspi;
2079 		h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2080 		hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
2081 		spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2082 
2083 		err = 0;
2084 	}
2085 
2086 unlock:
2087 	spin_unlock_bh(&x->lock);
2088 
2089 	return err;
2090 }
2091 EXPORT_SYMBOL(xfrm_alloc_spi);
2092 
2093 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2094 				      struct xfrm_address_filter *filter)
2095 {
2096 	if (filter) {
2097 		if ((filter->family == AF_INET ||
2098 		     filter->family == AF_INET6) &&
2099 		    x->props.family != filter->family)
2100 			return false;
2101 
2102 		return addr_match(&x->props.saddr, &filter->saddr,
2103 				  filter->splen) &&
2104 		       addr_match(&x->id.daddr, &filter->daddr,
2105 				  filter->dplen);
2106 	}
2107 	return true;
2108 }
2109 
2110 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2111 		    int (*func)(struct xfrm_state *, int, void*),
2112 		    void *data)
2113 {
2114 	struct xfrm_state *state;
2115 	struct xfrm_state_walk *x;
2116 	int err = 0;
2117 
2118 	if (walk->seq != 0 && list_empty(&walk->all))
2119 		return 0;
2120 
2121 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2122 	if (list_empty(&walk->all))
2123 		x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2124 	else
2125 		x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2126 	list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2127 		if (x->state == XFRM_STATE_DEAD)
2128 			continue;
2129 		state = container_of(x, struct xfrm_state, km);
2130 		if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2131 			continue;
2132 		if (!__xfrm_state_filter_match(state, walk->filter))
2133 			continue;
2134 		err = func(state, walk->seq, data);
2135 		if (err) {
2136 			list_move_tail(&walk->all, &x->all);
2137 			goto out;
2138 		}
2139 		walk->seq++;
2140 	}
2141 	if (walk->seq == 0) {
2142 		err = -ENOENT;
2143 		goto out;
2144 	}
2145 	list_del_init(&walk->all);
2146 out:
2147 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2148 	return err;
2149 }
2150 EXPORT_SYMBOL(xfrm_state_walk);
2151 
2152 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2153 			  struct xfrm_address_filter *filter)
2154 {
2155 	INIT_LIST_HEAD(&walk->all);
2156 	walk->proto = proto;
2157 	walk->state = XFRM_STATE_DEAD;
2158 	walk->seq = 0;
2159 	walk->filter = filter;
2160 }
2161 EXPORT_SYMBOL(xfrm_state_walk_init);
2162 
2163 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2164 {
2165 	kfree(walk->filter);
2166 
2167 	if (list_empty(&walk->all))
2168 		return;
2169 
2170 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
2171 	list_del(&walk->all);
2172 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2173 }
2174 EXPORT_SYMBOL(xfrm_state_walk_done);
2175 
2176 static void xfrm_replay_timer_handler(struct timer_list *t)
2177 {
2178 	struct xfrm_state *x = from_timer(x, t, rtimer);
2179 
2180 	spin_lock(&x->lock);
2181 
2182 	if (x->km.state == XFRM_STATE_VALID) {
2183 		if (xfrm_aevent_is_on(xs_net(x)))
2184 			xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
2185 		else
2186 			x->xflags |= XFRM_TIME_DEFER;
2187 	}
2188 
2189 	spin_unlock(&x->lock);
2190 }
2191 
2192 static LIST_HEAD(xfrm_km_list);
2193 
2194 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2195 {
2196 	struct xfrm_mgr *km;
2197 
2198 	rcu_read_lock();
2199 	list_for_each_entry_rcu(km, &xfrm_km_list, list)
2200 		if (km->notify_policy)
2201 			km->notify_policy(xp, dir, c);
2202 	rcu_read_unlock();
2203 }
2204 
2205 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2206 {
2207 	struct xfrm_mgr *km;
2208 	rcu_read_lock();
2209 	list_for_each_entry_rcu(km, &xfrm_km_list, list)
2210 		if (km->notify)
2211 			km->notify(x, c);
2212 	rcu_read_unlock();
2213 }
2214 
2215 EXPORT_SYMBOL(km_policy_notify);
2216 EXPORT_SYMBOL(km_state_notify);
2217 
2218 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2219 {
2220 	struct km_event c;
2221 
2222 	c.data.hard = hard;
2223 	c.portid = portid;
2224 	c.event = XFRM_MSG_EXPIRE;
2225 	km_state_notify(x, &c);
2226 }
2227 
2228 EXPORT_SYMBOL(km_state_expired);
2229 /*
2230  * We send to all registered managers regardless of failure
2231  * We are happy with one success
2232 */
2233 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2234 {
2235 	int err = -EINVAL, acqret;
2236 	struct xfrm_mgr *km;
2237 
2238 	rcu_read_lock();
2239 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2240 		acqret = km->acquire(x, t, pol);
2241 		if (!acqret)
2242 			err = acqret;
2243 	}
2244 	rcu_read_unlock();
2245 	return err;
2246 }
2247 EXPORT_SYMBOL(km_query);
2248 
2249 static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2250 {
2251 	int err = -EINVAL;
2252 	struct xfrm_mgr *km;
2253 
2254 	rcu_read_lock();
2255 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2256 		if (km->new_mapping)
2257 			err = km->new_mapping(x, ipaddr, sport);
2258 		if (!err)
2259 			break;
2260 	}
2261 	rcu_read_unlock();
2262 	return err;
2263 }
2264 
2265 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2266 {
2267 	int ret = 0;
2268 
2269 	if (x->mapping_maxage) {
2270 		if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
2271 		    x->new_mapping_sport != sport) {
2272 			x->new_mapping_sport = sport;
2273 			x->new_mapping = jiffies / HZ;
2274 			ret = __km_new_mapping(x, ipaddr, sport);
2275 		}
2276 	} else {
2277 		ret = __km_new_mapping(x, ipaddr, sport);
2278 	}
2279 
2280 	return ret;
2281 }
2282 EXPORT_SYMBOL(km_new_mapping);
2283 
2284 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2285 {
2286 	struct km_event c;
2287 
2288 	c.data.hard = hard;
2289 	c.portid = portid;
2290 	c.event = XFRM_MSG_POLEXPIRE;
2291 	km_policy_notify(pol, dir, &c);
2292 }
2293 EXPORT_SYMBOL(km_policy_expired);
2294 
2295 #ifdef CONFIG_XFRM_MIGRATE
2296 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2297 	       const struct xfrm_migrate *m, int num_migrate,
2298 	       const struct xfrm_kmaddress *k,
2299 	       const struct xfrm_encap_tmpl *encap)
2300 {
2301 	int err = -EINVAL;
2302 	int ret;
2303 	struct xfrm_mgr *km;
2304 
2305 	rcu_read_lock();
2306 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2307 		if (km->migrate) {
2308 			ret = km->migrate(sel, dir, type, m, num_migrate, k,
2309 					  encap);
2310 			if (!ret)
2311 				err = ret;
2312 		}
2313 	}
2314 	rcu_read_unlock();
2315 	return err;
2316 }
2317 EXPORT_SYMBOL(km_migrate);
2318 #endif
2319 
2320 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2321 {
2322 	int err = -EINVAL;
2323 	int ret;
2324 	struct xfrm_mgr *km;
2325 
2326 	rcu_read_lock();
2327 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2328 		if (km->report) {
2329 			ret = km->report(net, proto, sel, addr);
2330 			if (!ret)
2331 				err = ret;
2332 		}
2333 	}
2334 	rcu_read_unlock();
2335 	return err;
2336 }
2337 EXPORT_SYMBOL(km_report);
2338 
2339 static bool km_is_alive(const struct km_event *c)
2340 {
2341 	struct xfrm_mgr *km;
2342 	bool is_alive = false;
2343 
2344 	rcu_read_lock();
2345 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2346 		if (km->is_alive && km->is_alive(c)) {
2347 			is_alive = true;
2348 			break;
2349 		}
2350 	}
2351 	rcu_read_unlock();
2352 
2353 	return is_alive;
2354 }
2355 
2356 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2357 static DEFINE_SPINLOCK(xfrm_translator_lock);
2358 static struct xfrm_translator __rcu *xfrm_translator;
2359 
2360 struct xfrm_translator *xfrm_get_translator(void)
2361 {
2362 	struct xfrm_translator *xtr;
2363 
2364 	rcu_read_lock();
2365 	xtr = rcu_dereference(xfrm_translator);
2366 	if (unlikely(!xtr))
2367 		goto out;
2368 	if (!try_module_get(xtr->owner))
2369 		xtr = NULL;
2370 out:
2371 	rcu_read_unlock();
2372 	return xtr;
2373 }
2374 EXPORT_SYMBOL_GPL(xfrm_get_translator);
2375 
2376 void xfrm_put_translator(struct xfrm_translator *xtr)
2377 {
2378 	module_put(xtr->owner);
2379 }
2380 EXPORT_SYMBOL_GPL(xfrm_put_translator);
2381 
2382 int xfrm_register_translator(struct xfrm_translator *xtr)
2383 {
2384 	int err = 0;
2385 
2386 	spin_lock_bh(&xfrm_translator_lock);
2387 	if (unlikely(xfrm_translator != NULL))
2388 		err = -EEXIST;
2389 	else
2390 		rcu_assign_pointer(xfrm_translator, xtr);
2391 	spin_unlock_bh(&xfrm_translator_lock);
2392 
2393 	return err;
2394 }
2395 EXPORT_SYMBOL_GPL(xfrm_register_translator);
2396 
2397 int xfrm_unregister_translator(struct xfrm_translator *xtr)
2398 {
2399 	int err = 0;
2400 
2401 	spin_lock_bh(&xfrm_translator_lock);
2402 	if (likely(xfrm_translator != NULL)) {
2403 		if (rcu_access_pointer(xfrm_translator) != xtr)
2404 			err = -EINVAL;
2405 		else
2406 			RCU_INIT_POINTER(xfrm_translator, NULL);
2407 	}
2408 	spin_unlock_bh(&xfrm_translator_lock);
2409 	synchronize_rcu();
2410 
2411 	return err;
2412 }
2413 EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2414 #endif
2415 
2416 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
2417 {
2418 	int err;
2419 	u8 *data;
2420 	struct xfrm_mgr *km;
2421 	struct xfrm_policy *pol = NULL;
2422 
2423 	if (sockptr_is_null(optval) && !optlen) {
2424 		xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2425 		xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2426 		__sk_dst_reset(sk);
2427 		return 0;
2428 	}
2429 
2430 	if (optlen <= 0 || optlen > PAGE_SIZE)
2431 		return -EMSGSIZE;
2432 
2433 	data = memdup_sockptr(optval, optlen);
2434 	if (IS_ERR(data))
2435 		return PTR_ERR(data);
2436 
2437 	if (in_compat_syscall()) {
2438 		struct xfrm_translator *xtr = xfrm_get_translator();
2439 
2440 		if (!xtr) {
2441 			kfree(data);
2442 			return -EOPNOTSUPP;
2443 		}
2444 
2445 		err = xtr->xlate_user_policy_sockptr(&data, optlen);
2446 		xfrm_put_translator(xtr);
2447 		if (err) {
2448 			kfree(data);
2449 			return err;
2450 		}
2451 	}
2452 
2453 	err = -EINVAL;
2454 	rcu_read_lock();
2455 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2456 		pol = km->compile_policy(sk, optname, data,
2457 					 optlen, &err);
2458 		if (err >= 0)
2459 			break;
2460 	}
2461 	rcu_read_unlock();
2462 
2463 	if (err >= 0) {
2464 		xfrm_sk_policy_insert(sk, err, pol);
2465 		xfrm_pol_put(pol);
2466 		__sk_dst_reset(sk);
2467 		err = 0;
2468 	}
2469 
2470 	kfree(data);
2471 	return err;
2472 }
2473 EXPORT_SYMBOL(xfrm_user_policy);
2474 
2475 static DEFINE_SPINLOCK(xfrm_km_lock);
2476 
2477 int xfrm_register_km(struct xfrm_mgr *km)
2478 {
2479 	spin_lock_bh(&xfrm_km_lock);
2480 	list_add_tail_rcu(&km->list, &xfrm_km_list);
2481 	spin_unlock_bh(&xfrm_km_lock);
2482 	return 0;
2483 }
2484 EXPORT_SYMBOL(xfrm_register_km);
2485 
2486 int xfrm_unregister_km(struct xfrm_mgr *km)
2487 {
2488 	spin_lock_bh(&xfrm_km_lock);
2489 	list_del_rcu(&km->list);
2490 	spin_unlock_bh(&xfrm_km_lock);
2491 	synchronize_rcu();
2492 	return 0;
2493 }
2494 EXPORT_SYMBOL(xfrm_unregister_km);
2495 
2496 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2497 {
2498 	int err = 0;
2499 
2500 	if (WARN_ON(afinfo->family >= NPROTO))
2501 		return -EAFNOSUPPORT;
2502 
2503 	spin_lock_bh(&xfrm_state_afinfo_lock);
2504 	if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2505 		err = -EEXIST;
2506 	else
2507 		rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2508 	spin_unlock_bh(&xfrm_state_afinfo_lock);
2509 	return err;
2510 }
2511 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2512 
2513 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2514 {
2515 	int err = 0, family = afinfo->family;
2516 
2517 	if (WARN_ON(family >= NPROTO))
2518 		return -EAFNOSUPPORT;
2519 
2520 	spin_lock_bh(&xfrm_state_afinfo_lock);
2521 	if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2522 		if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2523 			err = -EINVAL;
2524 		else
2525 			RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2526 	}
2527 	spin_unlock_bh(&xfrm_state_afinfo_lock);
2528 	synchronize_rcu();
2529 	return err;
2530 }
2531 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2532 
2533 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2534 {
2535 	if (unlikely(family >= NPROTO))
2536 		return NULL;
2537 
2538 	return rcu_dereference(xfrm_state_afinfo[family]);
2539 }
2540 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
2541 
2542 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2543 {
2544 	struct xfrm_state_afinfo *afinfo;
2545 	if (unlikely(family >= NPROTO))
2546 		return NULL;
2547 	rcu_read_lock();
2548 	afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2549 	if (unlikely(!afinfo))
2550 		rcu_read_unlock();
2551 	return afinfo;
2552 }
2553 
2554 void xfrm_flush_gc(void)
2555 {
2556 	flush_work(&xfrm_state_gc_work);
2557 }
2558 EXPORT_SYMBOL(xfrm_flush_gc);
2559 
2560 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2561 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2562 {
2563 	if (x->tunnel) {
2564 		struct xfrm_state *t = x->tunnel;
2565 
2566 		if (atomic_read(&t->tunnel_users) == 2)
2567 			xfrm_state_delete(t);
2568 		atomic_dec(&t->tunnel_users);
2569 		xfrm_state_put_sync(t);
2570 		x->tunnel = NULL;
2571 	}
2572 }
2573 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2574 
2575 u32 __xfrm_state_mtu(struct xfrm_state *x, int mtu)
2576 {
2577 	const struct xfrm_type *type = READ_ONCE(x->type);
2578 	struct crypto_aead *aead;
2579 	u32 blksize, net_adj = 0;
2580 
2581 	if (x->km.state != XFRM_STATE_VALID ||
2582 	    !type || type->proto != IPPROTO_ESP)
2583 		return mtu - x->props.header_len;
2584 
2585 	aead = x->data;
2586 	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2587 
2588 	switch (x->props.mode) {
2589 	case XFRM_MODE_TRANSPORT:
2590 	case XFRM_MODE_BEET:
2591 		if (x->props.family == AF_INET)
2592 			net_adj = sizeof(struct iphdr);
2593 		else if (x->props.family == AF_INET6)
2594 			net_adj = sizeof(struct ipv6hdr);
2595 		break;
2596 	case XFRM_MODE_TUNNEL:
2597 		break;
2598 	default:
2599 		WARN_ON_ONCE(1);
2600 		break;
2601 	}
2602 
2603 	return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2604 		 net_adj) & ~(blksize - 1)) + net_adj - 2;
2605 }
2606 EXPORT_SYMBOL_GPL(__xfrm_state_mtu);
2607 
2608 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
2609 {
2610 	mtu = __xfrm_state_mtu(x, mtu);
2611 
2612 	if (x->props.family == AF_INET6 && mtu < IPV6_MIN_MTU)
2613 		return IPV6_MIN_MTU;
2614 
2615 	return mtu;
2616 }
2617 
2618 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2619 {
2620 	const struct xfrm_mode *inner_mode;
2621 	const struct xfrm_mode *outer_mode;
2622 	int family = x->props.family;
2623 	int err;
2624 
2625 	if (family == AF_INET &&
2626 	    xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2627 		x->props.flags |= XFRM_STATE_NOPMTUDISC;
2628 
2629 	err = -EPROTONOSUPPORT;
2630 
2631 	if (x->sel.family != AF_UNSPEC) {
2632 		inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2633 		if (inner_mode == NULL)
2634 			goto error;
2635 
2636 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2637 		    family != x->sel.family)
2638 			goto error;
2639 
2640 		x->inner_mode = *inner_mode;
2641 	} else {
2642 		const struct xfrm_mode *inner_mode_iaf;
2643 		int iafamily = AF_INET;
2644 
2645 		inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2646 		if (inner_mode == NULL)
2647 			goto error;
2648 
2649 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
2650 			goto error;
2651 
2652 		x->inner_mode = *inner_mode;
2653 
2654 		if (x->props.family == AF_INET)
2655 			iafamily = AF_INET6;
2656 
2657 		inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2658 		if (inner_mode_iaf) {
2659 			if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2660 				x->inner_mode_iaf = *inner_mode_iaf;
2661 		}
2662 	}
2663 
2664 	x->type = xfrm_get_type(x->id.proto, family);
2665 	if (x->type == NULL)
2666 		goto error;
2667 
2668 	x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2669 
2670 	err = x->type->init_state(x);
2671 	if (err)
2672 		goto error;
2673 
2674 	outer_mode = xfrm_get_mode(x->props.mode, family);
2675 	if (!outer_mode) {
2676 		err = -EPROTONOSUPPORT;
2677 		goto error;
2678 	}
2679 
2680 	x->outer_mode = *outer_mode;
2681 	if (init_replay) {
2682 		err = xfrm_init_replay(x);
2683 		if (err)
2684 			goto error;
2685 	}
2686 
2687 error:
2688 	return err;
2689 }
2690 
2691 EXPORT_SYMBOL(__xfrm_init_state);
2692 
2693 int xfrm_init_state(struct xfrm_state *x)
2694 {
2695 	int err;
2696 
2697 	err = __xfrm_init_state(x, true, false);
2698 	if (!err)
2699 		x->km.state = XFRM_STATE_VALID;
2700 
2701 	return err;
2702 }
2703 
2704 EXPORT_SYMBOL(xfrm_init_state);
2705 
2706 int __net_init xfrm_state_init(struct net *net)
2707 {
2708 	unsigned int sz;
2709 
2710 	if (net_eq(net, &init_net))
2711 		xfrm_state_cache = KMEM_CACHE(xfrm_state,
2712 					      SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2713 
2714 	INIT_LIST_HEAD(&net->xfrm.state_all);
2715 
2716 	sz = sizeof(struct hlist_head) * 8;
2717 
2718 	net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2719 	if (!net->xfrm.state_bydst)
2720 		goto out_bydst;
2721 	net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2722 	if (!net->xfrm.state_bysrc)
2723 		goto out_bysrc;
2724 	net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2725 	if (!net->xfrm.state_byspi)
2726 		goto out_byspi;
2727 	net->xfrm.state_byseq = xfrm_hash_alloc(sz);
2728 	if (!net->xfrm.state_byseq)
2729 		goto out_byseq;
2730 	net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2731 
2732 	net->xfrm.state_num = 0;
2733 	INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2734 	spin_lock_init(&net->xfrm.xfrm_state_lock);
2735 	seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation,
2736 			       &net->xfrm.xfrm_state_lock);
2737 	return 0;
2738 
2739 out_byseq:
2740 	xfrm_hash_free(net->xfrm.state_byspi, sz);
2741 out_byspi:
2742 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
2743 out_bysrc:
2744 	xfrm_hash_free(net->xfrm.state_bydst, sz);
2745 out_bydst:
2746 	return -ENOMEM;
2747 }
2748 
2749 void xfrm_state_fini(struct net *net)
2750 {
2751 	unsigned int sz;
2752 
2753 	flush_work(&net->xfrm.state_hash_work);
2754 	flush_work(&xfrm_state_gc_work);
2755 	xfrm_state_flush(net, 0, false, true);
2756 
2757 	WARN_ON(!list_empty(&net->xfrm.state_all));
2758 
2759 	sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2760 	WARN_ON(!hlist_empty(net->xfrm.state_byseq));
2761 	xfrm_hash_free(net->xfrm.state_byseq, sz);
2762 	WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2763 	xfrm_hash_free(net->xfrm.state_byspi, sz);
2764 	WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2765 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
2766 	WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2767 	xfrm_hash_free(net->xfrm.state_bydst, sz);
2768 }
2769 
2770 #ifdef CONFIG_AUDITSYSCALL
2771 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2772 				     struct audit_buffer *audit_buf)
2773 {
2774 	struct xfrm_sec_ctx *ctx = x->security;
2775 	u32 spi = ntohl(x->id.spi);
2776 
2777 	if (ctx)
2778 		audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2779 				 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2780 
2781 	switch (x->props.family) {
2782 	case AF_INET:
2783 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2784 				 &x->props.saddr.a4, &x->id.daddr.a4);
2785 		break;
2786 	case AF_INET6:
2787 		audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2788 				 x->props.saddr.a6, x->id.daddr.a6);
2789 		break;
2790 	}
2791 
2792 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2793 }
2794 
2795 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2796 				      struct audit_buffer *audit_buf)
2797 {
2798 	const struct iphdr *iph4;
2799 	const struct ipv6hdr *iph6;
2800 
2801 	switch (family) {
2802 	case AF_INET:
2803 		iph4 = ip_hdr(skb);
2804 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2805 				 &iph4->saddr, &iph4->daddr);
2806 		break;
2807 	case AF_INET6:
2808 		iph6 = ipv6_hdr(skb);
2809 		audit_log_format(audit_buf,
2810 				 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2811 				 &iph6->saddr, &iph6->daddr,
2812 				 iph6->flow_lbl[0] & 0x0f,
2813 				 iph6->flow_lbl[1],
2814 				 iph6->flow_lbl[2]);
2815 		break;
2816 	}
2817 }
2818 
2819 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2820 {
2821 	struct audit_buffer *audit_buf;
2822 
2823 	audit_buf = xfrm_audit_start("SAD-add");
2824 	if (audit_buf == NULL)
2825 		return;
2826 	xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2827 	xfrm_audit_helper_sainfo(x, audit_buf);
2828 	audit_log_format(audit_buf, " res=%u", result);
2829 	audit_log_end(audit_buf);
2830 }
2831 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2832 
2833 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2834 {
2835 	struct audit_buffer *audit_buf;
2836 
2837 	audit_buf = xfrm_audit_start("SAD-delete");
2838 	if (audit_buf == NULL)
2839 		return;
2840 	xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2841 	xfrm_audit_helper_sainfo(x, audit_buf);
2842 	audit_log_format(audit_buf, " res=%u", result);
2843 	audit_log_end(audit_buf);
2844 }
2845 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2846 
2847 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2848 				      struct sk_buff *skb)
2849 {
2850 	struct audit_buffer *audit_buf;
2851 	u32 spi;
2852 
2853 	audit_buf = xfrm_audit_start("SA-replay-overflow");
2854 	if (audit_buf == NULL)
2855 		return;
2856 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2857 	/* don't record the sequence number because it's inherent in this kind
2858 	 * of audit message */
2859 	spi = ntohl(x->id.spi);
2860 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2861 	audit_log_end(audit_buf);
2862 }
2863 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2864 
2865 void xfrm_audit_state_replay(struct xfrm_state *x,
2866 			     struct sk_buff *skb, __be32 net_seq)
2867 {
2868 	struct audit_buffer *audit_buf;
2869 	u32 spi;
2870 
2871 	audit_buf = xfrm_audit_start("SA-replayed-pkt");
2872 	if (audit_buf == NULL)
2873 		return;
2874 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2875 	spi = ntohl(x->id.spi);
2876 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2877 			 spi, spi, ntohl(net_seq));
2878 	audit_log_end(audit_buf);
2879 }
2880 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2881 
2882 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2883 {
2884 	struct audit_buffer *audit_buf;
2885 
2886 	audit_buf = xfrm_audit_start("SA-notfound");
2887 	if (audit_buf == NULL)
2888 		return;
2889 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2890 	audit_log_end(audit_buf);
2891 }
2892 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2893 
2894 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2895 			       __be32 net_spi, __be32 net_seq)
2896 {
2897 	struct audit_buffer *audit_buf;
2898 	u32 spi;
2899 
2900 	audit_buf = xfrm_audit_start("SA-notfound");
2901 	if (audit_buf == NULL)
2902 		return;
2903 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2904 	spi = ntohl(net_spi);
2905 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2906 			 spi, spi, ntohl(net_seq));
2907 	audit_log_end(audit_buf);
2908 }
2909 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2910 
2911 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2912 			      struct sk_buff *skb, u8 proto)
2913 {
2914 	struct audit_buffer *audit_buf;
2915 	__be32 net_spi;
2916 	__be32 net_seq;
2917 
2918 	audit_buf = xfrm_audit_start("SA-icv-failure");
2919 	if (audit_buf == NULL)
2920 		return;
2921 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2922 	if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2923 		u32 spi = ntohl(net_spi);
2924 		audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2925 				 spi, spi, ntohl(net_seq));
2926 	}
2927 	audit_log_end(audit_buf);
2928 }
2929 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2930 #endif /* CONFIG_AUDITSYSCALL */
2931