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