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