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