xref: /openbmc/linux/net/xfrm/xfrm_state.c (revision a454f0cc)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * xfrm_state.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Changes:
51da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI
61da177e4SLinus Torvalds  * 	Kazunori MIYAZAWA @USAGI
71da177e4SLinus Torvalds  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
81da177e4SLinus Torvalds  * 		IPv6 support
91da177e4SLinus Torvalds  * 	YOSHIFUJI Hideaki @USAGI
101da177e4SLinus Torvalds  * 		Split up af-specific functions
111da177e4SLinus Torvalds  *	Derek Atkins <derek@ihtfp.com>
121da177e4SLinus Torvalds  *		Add UDP Encapsulation
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/workqueue.h>
171da177e4SLinus Torvalds #include <net/xfrm.h>
181da177e4SLinus Torvalds #include <linux/pfkeyv2.h>
191da177e4SLinus Torvalds #include <linux/ipsec.h>
201da177e4SLinus Torvalds #include <linux/module.h>
21f034b5d4SDavid S. Miller #include <linux/cache.h>
2268277accSPaul Moore #include <linux/audit.h>
23b5890d8bSJesper Juhl #include <asm/uaccess.h>
249e0d57fdSYury Polyanskiy #include <linux/ktime.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
269e0d57fdSYury Polyanskiy #include <linux/interrupt.h>
279e0d57fdSYury Polyanskiy #include <linux/kernel.h>
281da177e4SLinus Torvalds 
2944e36b42SDavid S. Miller #include "xfrm_hash.h"
3044e36b42SDavid S. Miller 
311da177e4SLinus Torvalds /* Each xfrm_state may be linked to two tables:
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds    1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
34a624c108SDavid S. Miller    2. Hash table by (daddr,family,reqid) to find what SAs exist for given
351da177e4SLinus Torvalds       destination/tunnel endpoint. (output)
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static DEFINE_SPINLOCK(xfrm_state_lock);
391da177e4SLinus Torvalds 
40f034b5d4SDavid S. Miller static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
411da177e4SLinus Torvalds 
4217c2a42aSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
4317c2a42aSHerbert Xu static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
4417c2a42aSHerbert Xu 
4564d0cd00SAlexey Dobriyan static inline unsigned int xfrm_dst_hash(struct net *net,
462ab38503SDavid S. Miller 					 const xfrm_address_t *daddr,
472ab38503SDavid S. Miller 					 const xfrm_address_t *saddr,
48c1969f29SDavid S. Miller 					 u32 reqid,
49a624c108SDavid S. Miller 					 unsigned short family)
50a624c108SDavid S. Miller {
5164d0cd00SAlexey Dobriyan 	return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
52a624c108SDavid S. Miller }
53a624c108SDavid S. Miller 
5464d0cd00SAlexey Dobriyan static inline unsigned int xfrm_src_hash(struct net *net,
552ab38503SDavid S. Miller 					 const xfrm_address_t *daddr,
562ab38503SDavid S. Miller 					 const xfrm_address_t *saddr,
5744e36b42SDavid S. Miller 					 unsigned short family)
58f034b5d4SDavid S. Miller {
5964d0cd00SAlexey Dobriyan 	return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
60f034b5d4SDavid S. Miller }
61f034b5d4SDavid S. Miller 
622575b654SDavid S. Miller static inline unsigned int
632ab38503SDavid S. Miller xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
642ab38503SDavid S. Miller 	      __be32 spi, u8 proto, unsigned short family)
65f034b5d4SDavid S. Miller {
6664d0cd00SAlexey Dobriyan 	return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
67f034b5d4SDavid S. Miller }
68f034b5d4SDavid S. Miller 
69f034b5d4SDavid S. Miller static void xfrm_hash_transfer(struct hlist_head *list,
70f034b5d4SDavid S. Miller 			       struct hlist_head *ndsttable,
71f034b5d4SDavid S. Miller 			       struct hlist_head *nsrctable,
72f034b5d4SDavid S. Miller 			       struct hlist_head *nspitable,
73f034b5d4SDavid S. Miller 			       unsigned int nhashmask)
74f034b5d4SDavid S. Miller {
75f034b5d4SDavid S. Miller 	struct hlist_node *entry, *tmp;
76f034b5d4SDavid S. Miller 	struct xfrm_state *x;
77f034b5d4SDavid S. Miller 
78f034b5d4SDavid S. Miller 	hlist_for_each_entry_safe(x, entry, tmp, list, bydst) {
79f034b5d4SDavid S. Miller 		unsigned int h;
80f034b5d4SDavid S. Miller 
81c1969f29SDavid S. Miller 		h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
82c1969f29SDavid S. Miller 				    x->props.reqid, x->props.family,
83c1969f29SDavid S. Miller 				    nhashmask);
84f034b5d4SDavid S. Miller 		hlist_add_head(&x->bydst, ndsttable+h);
85f034b5d4SDavid S. Miller 
86667bbcb6SMasahide NAKAMURA 		h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
87667bbcb6SMasahide NAKAMURA 				    x->props.family,
88f034b5d4SDavid S. Miller 				    nhashmask);
89f034b5d4SDavid S. Miller 		hlist_add_head(&x->bysrc, nsrctable+h);
90f034b5d4SDavid S. Miller 
917b4dc360SMasahide NAKAMURA 		if (x->id.spi) {
927b4dc360SMasahide NAKAMURA 			h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
937b4dc360SMasahide NAKAMURA 					    x->id.proto, x->props.family,
947b4dc360SMasahide NAKAMURA 					    nhashmask);
95f034b5d4SDavid S. Miller 			hlist_add_head(&x->byspi, nspitable+h);
96f034b5d4SDavid S. Miller 		}
97f034b5d4SDavid S. Miller 	}
987b4dc360SMasahide NAKAMURA }
99f034b5d4SDavid S. Miller 
10063082733SAlexey Dobriyan static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
101f034b5d4SDavid S. Miller {
10263082733SAlexey Dobriyan 	return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
103f034b5d4SDavid S. Miller }
104f034b5d4SDavid S. Miller 
105f034b5d4SDavid S. Miller static DEFINE_MUTEX(hash_resize_mutex);
106f034b5d4SDavid S. Miller 
10763082733SAlexey Dobriyan static void xfrm_hash_resize(struct work_struct *work)
108f034b5d4SDavid S. Miller {
10963082733SAlexey Dobriyan 	struct net *net = container_of(work, struct net, xfrm.state_hash_work);
110f034b5d4SDavid S. Miller 	struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
111f034b5d4SDavid S. Miller 	unsigned long nsize, osize;
112f034b5d4SDavid S. Miller 	unsigned int nhashmask, ohashmask;
113f034b5d4SDavid S. Miller 	int i;
114f034b5d4SDavid S. Miller 
115f034b5d4SDavid S. Miller 	mutex_lock(&hash_resize_mutex);
116f034b5d4SDavid S. Miller 
11763082733SAlexey Dobriyan 	nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
11844e36b42SDavid S. Miller 	ndst = xfrm_hash_alloc(nsize);
119f034b5d4SDavid S. Miller 	if (!ndst)
120f034b5d4SDavid S. Miller 		goto out_unlock;
12144e36b42SDavid S. Miller 	nsrc = xfrm_hash_alloc(nsize);
122f034b5d4SDavid S. Miller 	if (!nsrc) {
12344e36b42SDavid S. Miller 		xfrm_hash_free(ndst, nsize);
124f034b5d4SDavid S. Miller 		goto out_unlock;
125f034b5d4SDavid S. Miller 	}
12644e36b42SDavid S. Miller 	nspi = xfrm_hash_alloc(nsize);
127f034b5d4SDavid S. Miller 	if (!nspi) {
12844e36b42SDavid S. Miller 		xfrm_hash_free(ndst, nsize);
12944e36b42SDavid S. Miller 		xfrm_hash_free(nsrc, nsize);
130f034b5d4SDavid S. Miller 		goto out_unlock;
131f034b5d4SDavid S. Miller 	}
132f034b5d4SDavid S. Miller 
133f034b5d4SDavid S. Miller 	spin_lock_bh(&xfrm_state_lock);
134f034b5d4SDavid S. Miller 
135f034b5d4SDavid S. Miller 	nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
13663082733SAlexey Dobriyan 	for (i = net->xfrm.state_hmask; i >= 0; i--)
13763082733SAlexey Dobriyan 		xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
138f034b5d4SDavid S. Miller 				   nhashmask);
139f034b5d4SDavid S. Miller 
14063082733SAlexey Dobriyan 	odst = net->xfrm.state_bydst;
14163082733SAlexey Dobriyan 	osrc = net->xfrm.state_bysrc;
14263082733SAlexey Dobriyan 	ospi = net->xfrm.state_byspi;
14363082733SAlexey Dobriyan 	ohashmask = net->xfrm.state_hmask;
144f034b5d4SDavid S. Miller 
14563082733SAlexey Dobriyan 	net->xfrm.state_bydst = ndst;
14663082733SAlexey Dobriyan 	net->xfrm.state_bysrc = nsrc;
14763082733SAlexey Dobriyan 	net->xfrm.state_byspi = nspi;
14863082733SAlexey Dobriyan 	net->xfrm.state_hmask = nhashmask;
149f034b5d4SDavid S. Miller 
150f034b5d4SDavid S. Miller 	spin_unlock_bh(&xfrm_state_lock);
151f034b5d4SDavid S. Miller 
152f034b5d4SDavid S. Miller 	osize = (ohashmask + 1) * sizeof(struct hlist_head);
15344e36b42SDavid S. Miller 	xfrm_hash_free(odst, osize);
15444e36b42SDavid S. Miller 	xfrm_hash_free(osrc, osize);
15544e36b42SDavid S. Miller 	xfrm_hash_free(ospi, osize);
156f034b5d4SDavid S. Miller 
157f034b5d4SDavid S. Miller out_unlock:
158f034b5d4SDavid S. Miller 	mutex_unlock(&hash_resize_mutex);
159f034b5d4SDavid S. Miller }
160f034b5d4SDavid S. Miller 
1611da177e4SLinus Torvalds static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
1621da177e4SLinus Torvalds static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static DEFINE_SPINLOCK(xfrm_state_gc_lock);
1651da177e4SLinus Torvalds 
16653bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x);
1671da177e4SLinus Torvalds 
168980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
16953bc6b4dSJamal Hadi Salim void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
1701da177e4SLinus Torvalds 
171aa5d62ccSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
172aa5d62ccSHerbert Xu {
173aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
174aa5d62ccSHerbert Xu 	if (unlikely(family >= NPROTO))
175aa5d62ccSHerbert Xu 		return NULL;
176aa5d62ccSHerbert Xu 	write_lock_bh(&xfrm_state_afinfo_lock);
177aa5d62ccSHerbert Xu 	afinfo = xfrm_state_afinfo[family];
178aa5d62ccSHerbert Xu 	if (unlikely(!afinfo))
179aa5d62ccSHerbert Xu 		write_unlock_bh(&xfrm_state_afinfo_lock);
180aa5d62ccSHerbert Xu 	return afinfo;
181aa5d62ccSHerbert Xu }
182aa5d62ccSHerbert Xu 
183aa5d62ccSHerbert Xu static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
1849a429c49SEric Dumazet 	__releases(xfrm_state_afinfo_lock)
185aa5d62ccSHerbert Xu {
186aa5d62ccSHerbert Xu 	write_unlock_bh(&xfrm_state_afinfo_lock);
187aa5d62ccSHerbert Xu }
188aa5d62ccSHerbert Xu 
189533cb5b0SEric Dumazet int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
190aa5d62ccSHerbert Xu {
191aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
192533cb5b0SEric Dumazet 	const struct xfrm_type **typemap;
193aa5d62ccSHerbert Xu 	int err = 0;
194aa5d62ccSHerbert Xu 
195aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
196aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
197aa5d62ccSHerbert Xu 	typemap = afinfo->type_map;
198aa5d62ccSHerbert Xu 
199aa5d62ccSHerbert Xu 	if (likely(typemap[type->proto] == NULL))
200aa5d62ccSHerbert Xu 		typemap[type->proto] = type;
201aa5d62ccSHerbert Xu 	else
202aa5d62ccSHerbert Xu 		err = -EEXIST;
203aa5d62ccSHerbert Xu 	xfrm_state_unlock_afinfo(afinfo);
204aa5d62ccSHerbert Xu 	return err;
205aa5d62ccSHerbert Xu }
206aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_type);
207aa5d62ccSHerbert Xu 
208533cb5b0SEric Dumazet int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
209aa5d62ccSHerbert Xu {
210aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
211533cb5b0SEric Dumazet 	const struct xfrm_type **typemap;
212aa5d62ccSHerbert Xu 	int err = 0;
213aa5d62ccSHerbert Xu 
214aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
215aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
216aa5d62ccSHerbert Xu 	typemap = afinfo->type_map;
217aa5d62ccSHerbert Xu 
218aa5d62ccSHerbert Xu 	if (unlikely(typemap[type->proto] != type))
219aa5d62ccSHerbert Xu 		err = -ENOENT;
220aa5d62ccSHerbert Xu 	else
221aa5d62ccSHerbert Xu 		typemap[type->proto] = NULL;
222aa5d62ccSHerbert Xu 	xfrm_state_unlock_afinfo(afinfo);
223aa5d62ccSHerbert Xu 	return err;
224aa5d62ccSHerbert Xu }
225aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_type);
226aa5d62ccSHerbert Xu 
227533cb5b0SEric Dumazet static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
228aa5d62ccSHerbert Xu {
229aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
230533cb5b0SEric Dumazet 	const struct xfrm_type **typemap;
231533cb5b0SEric Dumazet 	const struct xfrm_type *type;
232aa5d62ccSHerbert Xu 	int modload_attempted = 0;
233aa5d62ccSHerbert Xu 
234aa5d62ccSHerbert Xu retry:
235aa5d62ccSHerbert Xu 	afinfo = xfrm_state_get_afinfo(family);
236aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
237aa5d62ccSHerbert Xu 		return NULL;
238aa5d62ccSHerbert Xu 	typemap = afinfo->type_map;
239aa5d62ccSHerbert Xu 
240aa5d62ccSHerbert Xu 	type = typemap[proto];
241aa5d62ccSHerbert Xu 	if (unlikely(type && !try_module_get(type->owner)))
242aa5d62ccSHerbert Xu 		type = NULL;
243aa5d62ccSHerbert Xu 	if (!type && !modload_attempted) {
244aa5d62ccSHerbert Xu 		xfrm_state_put_afinfo(afinfo);
245aa5d62ccSHerbert Xu 		request_module("xfrm-type-%d-%d", family, proto);
246aa5d62ccSHerbert Xu 		modload_attempted = 1;
247aa5d62ccSHerbert Xu 		goto retry;
248aa5d62ccSHerbert Xu 	}
249aa5d62ccSHerbert Xu 
250aa5d62ccSHerbert Xu 	xfrm_state_put_afinfo(afinfo);
251aa5d62ccSHerbert Xu 	return type;
252aa5d62ccSHerbert Xu }
253aa5d62ccSHerbert Xu 
254533cb5b0SEric Dumazet static void xfrm_put_type(const struct xfrm_type *type)
255aa5d62ccSHerbert Xu {
256aa5d62ccSHerbert Xu 	module_put(type->owner);
257aa5d62ccSHerbert Xu }
258aa5d62ccSHerbert Xu 
259aa5d62ccSHerbert Xu int xfrm_register_mode(struct xfrm_mode *mode, int family)
260aa5d62ccSHerbert Xu {
261aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
262aa5d62ccSHerbert Xu 	struct xfrm_mode **modemap;
263aa5d62ccSHerbert Xu 	int err;
264aa5d62ccSHerbert Xu 
265aa5d62ccSHerbert Xu 	if (unlikely(mode->encap >= XFRM_MODE_MAX))
266aa5d62ccSHerbert Xu 		return -EINVAL;
267aa5d62ccSHerbert Xu 
268aa5d62ccSHerbert Xu 	afinfo = xfrm_state_lock_afinfo(family);
269aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
270aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
271aa5d62ccSHerbert Xu 
272aa5d62ccSHerbert Xu 	err = -EEXIST;
273aa5d62ccSHerbert Xu 	modemap = afinfo->mode_map;
27417c2a42aSHerbert Xu 	if (modemap[mode->encap])
27517c2a42aSHerbert Xu 		goto out;
27617c2a42aSHerbert Xu 
27717c2a42aSHerbert Xu 	err = -ENOENT;
27817c2a42aSHerbert Xu 	if (!try_module_get(afinfo->owner))
27917c2a42aSHerbert Xu 		goto out;
28017c2a42aSHerbert Xu 
28117c2a42aSHerbert Xu 	mode->afinfo = afinfo;
282aa5d62ccSHerbert Xu 	modemap[mode->encap] = mode;
283aa5d62ccSHerbert Xu 	err = 0;
284aa5d62ccSHerbert Xu 
28517c2a42aSHerbert Xu out:
286aa5d62ccSHerbert Xu 	xfrm_state_unlock_afinfo(afinfo);
287aa5d62ccSHerbert Xu 	return err;
288aa5d62ccSHerbert Xu }
289aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_mode);
290aa5d62ccSHerbert Xu 
291aa5d62ccSHerbert Xu int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
292aa5d62ccSHerbert Xu {
293aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
294aa5d62ccSHerbert Xu 	struct xfrm_mode **modemap;
295aa5d62ccSHerbert Xu 	int err;
296aa5d62ccSHerbert Xu 
297aa5d62ccSHerbert Xu 	if (unlikely(mode->encap >= XFRM_MODE_MAX))
298aa5d62ccSHerbert Xu 		return -EINVAL;
299aa5d62ccSHerbert Xu 
300aa5d62ccSHerbert Xu 	afinfo = xfrm_state_lock_afinfo(family);
301aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
302aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
303aa5d62ccSHerbert Xu 
304aa5d62ccSHerbert Xu 	err = -ENOENT;
305aa5d62ccSHerbert Xu 	modemap = afinfo->mode_map;
306aa5d62ccSHerbert Xu 	if (likely(modemap[mode->encap] == mode)) {
307aa5d62ccSHerbert Xu 		modemap[mode->encap] = NULL;
30817c2a42aSHerbert Xu 		module_put(mode->afinfo->owner);
309aa5d62ccSHerbert Xu 		err = 0;
310aa5d62ccSHerbert Xu 	}
311aa5d62ccSHerbert Xu 
312aa5d62ccSHerbert Xu 	xfrm_state_unlock_afinfo(afinfo);
313aa5d62ccSHerbert Xu 	return err;
314aa5d62ccSHerbert Xu }
315aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_mode);
316aa5d62ccSHerbert Xu 
317aa5d62ccSHerbert Xu static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
318aa5d62ccSHerbert Xu {
319aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
320aa5d62ccSHerbert Xu 	struct xfrm_mode *mode;
321aa5d62ccSHerbert Xu 	int modload_attempted = 0;
322aa5d62ccSHerbert Xu 
323aa5d62ccSHerbert Xu 	if (unlikely(encap >= XFRM_MODE_MAX))
324aa5d62ccSHerbert Xu 		return NULL;
325aa5d62ccSHerbert Xu 
326aa5d62ccSHerbert Xu retry:
327aa5d62ccSHerbert Xu 	afinfo = xfrm_state_get_afinfo(family);
328aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
329aa5d62ccSHerbert Xu 		return NULL;
330aa5d62ccSHerbert Xu 
331aa5d62ccSHerbert Xu 	mode = afinfo->mode_map[encap];
332aa5d62ccSHerbert Xu 	if (unlikely(mode && !try_module_get(mode->owner)))
333aa5d62ccSHerbert Xu 		mode = NULL;
334aa5d62ccSHerbert Xu 	if (!mode && !modload_attempted) {
335aa5d62ccSHerbert Xu 		xfrm_state_put_afinfo(afinfo);
336aa5d62ccSHerbert Xu 		request_module("xfrm-mode-%d-%d", family, encap);
337aa5d62ccSHerbert Xu 		modload_attempted = 1;
338aa5d62ccSHerbert Xu 		goto retry;
339aa5d62ccSHerbert Xu 	}
340aa5d62ccSHerbert Xu 
341aa5d62ccSHerbert Xu 	xfrm_state_put_afinfo(afinfo);
342aa5d62ccSHerbert Xu 	return mode;
343aa5d62ccSHerbert Xu }
344aa5d62ccSHerbert Xu 
345aa5d62ccSHerbert Xu static void xfrm_put_mode(struct xfrm_mode *mode)
346aa5d62ccSHerbert Xu {
347aa5d62ccSHerbert Xu 	module_put(mode->owner);
348aa5d62ccSHerbert Xu }
349aa5d62ccSHerbert Xu 
3501da177e4SLinus Torvalds static void xfrm_state_gc_destroy(struct xfrm_state *x)
3511da177e4SLinus Torvalds {
3529e0d57fdSYury Polyanskiy 	tasklet_hrtimer_cancel(&x->mtimer);
353a47f0ce0SDavid S. Miller 	del_timer_sync(&x->rtimer);
3541da177e4SLinus Torvalds 	kfree(x->aalg);
3551da177e4SLinus Torvalds 	kfree(x->ealg);
3561da177e4SLinus Torvalds 	kfree(x->calg);
3571da177e4SLinus Torvalds 	kfree(x->encap);
358060f02a3SNoriaki TAKAMIYA 	kfree(x->coaddr);
359d8647b79SSteffen Klassert 	kfree(x->replay_esn);
360d8647b79SSteffen Klassert 	kfree(x->preplay_esn);
36113996378SHerbert Xu 	if (x->inner_mode)
36213996378SHerbert Xu 		xfrm_put_mode(x->inner_mode);
363df9dcb45SKazunori MIYAZAWA 	if (x->inner_mode_iaf)
364df9dcb45SKazunori MIYAZAWA 		xfrm_put_mode(x->inner_mode_iaf);
36513996378SHerbert Xu 	if (x->outer_mode)
36613996378SHerbert Xu 		xfrm_put_mode(x->outer_mode);
3671da177e4SLinus Torvalds 	if (x->type) {
3681da177e4SLinus Torvalds 		x->type->destructor(x);
3691da177e4SLinus Torvalds 		xfrm_put_type(x->type);
3701da177e4SLinus Torvalds 	}
371df71837dSTrent Jaeger 	security_xfrm_state_free(x);
3721da177e4SLinus Torvalds 	kfree(x);
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
375c7837144SAlexey Dobriyan static void xfrm_state_gc_task(struct work_struct *work)
3761da177e4SLinus Torvalds {
377c7837144SAlexey Dobriyan 	struct net *net = container_of(work, struct net, xfrm.state_gc_work);
37812a169e7SHerbert Xu 	struct xfrm_state *x;
37912a169e7SHerbert Xu 	struct hlist_node *entry, *tmp;
38012a169e7SHerbert Xu 	struct hlist_head gc_list;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_gc_lock);
383c7837144SAlexey Dobriyan 	hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
3841da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_gc_lock);
3851da177e4SLinus Torvalds 
38612a169e7SHerbert Xu 	hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
3871da177e4SLinus Torvalds 		xfrm_state_gc_destroy(x);
3888f126e37SDavid S. Miller 
38950a30657SAlexey Dobriyan 	wake_up(&net->xfrm.km_waitq);
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds static inline unsigned long make_jiffies(long secs)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
3951da177e4SLinus Torvalds 		return MAX_SCHEDULE_TIMEOUT-1;
3961da177e4SLinus Torvalds 	else
3971da177e4SLinus Torvalds 		return secs*HZ;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4009e0d57fdSYury Polyanskiy static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
4011da177e4SLinus Torvalds {
4029e0d57fdSYury Polyanskiy 	struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
4039e0d57fdSYury Polyanskiy 	struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
40498806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
4059d729f72SJames Morris 	unsigned long now = get_seconds();
4061da177e4SLinus Torvalds 	long next = LONG_MAX;
4071da177e4SLinus Torvalds 	int warn = 0;
408161a09e7SJoy Latten 	int err = 0;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	spin_lock(&x->lock);
4111da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_DEAD)
4121da177e4SLinus Torvalds 		goto out;
4131da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_EXPIRED)
4141da177e4SLinus Torvalds 		goto expired;
4151da177e4SLinus Torvalds 	if (x->lft.hard_add_expires_seconds) {
4161da177e4SLinus Torvalds 		long tmo = x->lft.hard_add_expires_seconds +
4171da177e4SLinus Torvalds 			x->curlft.add_time - now;
4181da177e4SLinus Torvalds 		if (tmo <= 0)
4191da177e4SLinus Torvalds 			goto expired;
4201da177e4SLinus Torvalds 		if (tmo < next)
4211da177e4SLinus Torvalds 			next = tmo;
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 	if (x->lft.hard_use_expires_seconds) {
4241da177e4SLinus Torvalds 		long tmo = x->lft.hard_use_expires_seconds +
4251da177e4SLinus Torvalds 			(x->curlft.use_time ? : now) - now;
4261da177e4SLinus Torvalds 		if (tmo <= 0)
4271da177e4SLinus Torvalds 			goto expired;
4281da177e4SLinus Torvalds 		if (tmo < next)
4291da177e4SLinus Torvalds 			next = tmo;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	if (x->km.dying)
4321da177e4SLinus Torvalds 		goto resched;
4331da177e4SLinus Torvalds 	if (x->lft.soft_add_expires_seconds) {
4341da177e4SLinus Torvalds 		long tmo = x->lft.soft_add_expires_seconds +
4351da177e4SLinus Torvalds 			x->curlft.add_time - now;
4361da177e4SLinus Torvalds 		if (tmo <= 0)
4371da177e4SLinus Torvalds 			warn = 1;
4381da177e4SLinus Torvalds 		else if (tmo < next)
4391da177e4SLinus Torvalds 			next = tmo;
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 	if (x->lft.soft_use_expires_seconds) {
4421da177e4SLinus Torvalds 		long tmo = x->lft.soft_use_expires_seconds +
4431da177e4SLinus Torvalds 			(x->curlft.use_time ? : now) - now;
4441da177e4SLinus Torvalds 		if (tmo <= 0)
4451da177e4SLinus Torvalds 			warn = 1;
4461da177e4SLinus Torvalds 		else if (tmo < next)
4471da177e4SLinus Torvalds 			next = tmo;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
4504666faabSHerbert Xu 	x->km.dying = warn;
4511da177e4SLinus Torvalds 	if (warn)
45253bc6b4dSJamal Hadi Salim 		km_state_expired(x, 0, 0);
4531da177e4SLinus Torvalds resched:
4549e0d57fdSYury Polyanskiy 	if (next != LONG_MAX){
4559e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
4569e0d57fdSYury Polyanskiy 	}
457a47f0ce0SDavid S. Miller 
4581da177e4SLinus Torvalds 	goto out;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds expired:
4611da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
4621da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_EXPIRED;
46398806f75SAlexey Dobriyan 		wake_up(&net->xfrm.km_waitq);
4641da177e4SLinus Torvalds 		next = 2;
4651da177e4SLinus Torvalds 		goto resched;
4661da177e4SLinus Torvalds 	}
467161a09e7SJoy Latten 
468161a09e7SJoy Latten 	err = __xfrm_state_delete(x);
469161a09e7SJoy Latten 	if (!err && x->id.spi)
47053bc6b4dSJamal Hadi Salim 		km_state_expired(x, 1, 0);
4711da177e4SLinus Torvalds 
472ab5f5e8bSJoy Latten 	xfrm_audit_state_delete(x, err ? 0 : 1,
4732532386fSEric Paris 				audit_get_loginuid(current),
4742532386fSEric Paris 				audit_get_sessionid(current), 0);
475161a09e7SJoy Latten 
4761da177e4SLinus Torvalds out:
4771da177e4SLinus Torvalds 	spin_unlock(&x->lock);
4789e0d57fdSYury Polyanskiy 	return HRTIMER_NORESTART;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
4810ac84752SDavid S. Miller static void xfrm_replay_timer_handler(unsigned long data);
4820ac84752SDavid S. Miller 
483673c09beSAlexey Dobriyan struct xfrm_state *xfrm_state_alloc(struct net *net)
4841da177e4SLinus Torvalds {
4851da177e4SLinus Torvalds 	struct xfrm_state *x;
4861da177e4SLinus Torvalds 
4870da974f4SPanagiotis Issaris 	x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (x) {
490673c09beSAlexey Dobriyan 		write_pnet(&x->xs_net, net);
4911da177e4SLinus Torvalds 		atomic_set(&x->refcnt, 1);
4921da177e4SLinus Torvalds 		atomic_set(&x->tunnel_users, 0);
49312a169e7SHerbert Xu 		INIT_LIST_HEAD(&x->km.all);
4948f126e37SDavid S. Miller 		INIT_HLIST_NODE(&x->bydst);
4958f126e37SDavid S. Miller 		INIT_HLIST_NODE(&x->bysrc);
4968f126e37SDavid S. Miller 		INIT_HLIST_NODE(&x->byspi);
4979e0d57fdSYury Polyanskiy 		tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler, CLOCK_REALTIME, HRTIMER_MODE_ABS);
498b24b8a24SPavel Emelyanov 		setup_timer(&x->rtimer, xfrm_replay_timer_handler,
499b24b8a24SPavel Emelyanov 				(unsigned long)x);
5009d729f72SJames Morris 		x->curlft.add_time = get_seconds();
5011da177e4SLinus Torvalds 		x->lft.soft_byte_limit = XFRM_INF;
5021da177e4SLinus Torvalds 		x->lft.soft_packet_limit = XFRM_INF;
5031da177e4SLinus Torvalds 		x->lft.hard_byte_limit = XFRM_INF;
5041da177e4SLinus Torvalds 		x->lft.hard_packet_limit = XFRM_INF;
505f8cd5488SJamal Hadi Salim 		x->replay_maxage = 0;
506f8cd5488SJamal Hadi Salim 		x->replay_maxdiff = 0;
507df9dcb45SKazunori MIYAZAWA 		x->inner_mode = NULL;
508df9dcb45SKazunori MIYAZAWA 		x->inner_mode_iaf = NULL;
5091da177e4SLinus Torvalds 		spin_lock_init(&x->lock);
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 	return x;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_alloc);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds void __xfrm_state_destroy(struct xfrm_state *x)
5161da177e4SLinus Torvalds {
51798806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
51898806f75SAlexey Dobriyan 
519547b792cSIlpo Järvinen 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_gc_lock);
52298806f75SAlexey Dobriyan 	hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
5231da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_gc_lock);
52498806f75SAlexey Dobriyan 	schedule_work(&net->xfrm.state_gc_work);
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds EXPORT_SYMBOL(__xfrm_state_destroy);
5271da177e4SLinus Torvalds 
52853bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x)
5291da177e4SLinus Torvalds {
53098806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
53126b15dadSJamal Hadi Salim 	int err = -ESRCH;
53226b15dadSJamal Hadi Salim 
5331da177e4SLinus Torvalds 	if (x->km.state != XFRM_STATE_DEAD) {
5341da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
5351da177e4SLinus Torvalds 		spin_lock(&xfrm_state_lock);
53612a169e7SHerbert Xu 		list_del(&x->km.all);
5378f126e37SDavid S. Miller 		hlist_del(&x->bydst);
5388f126e37SDavid S. Miller 		hlist_del(&x->bysrc);
539a47f0ce0SDavid S. Miller 		if (x->id.spi)
5408f126e37SDavid S. Miller 			hlist_del(&x->byspi);
54198806f75SAlexey Dobriyan 		net->xfrm.state_num--;
5421da177e4SLinus Torvalds 		spin_unlock(&xfrm_state_lock);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		/* All xfrm_state objects are created by xfrm_state_alloc.
5451da177e4SLinus Torvalds 		 * The xfrm_state_alloc call gives a reference, and that
5461da177e4SLinus Torvalds 		 * is what we are dropping here.
5471da177e4SLinus Torvalds 		 */
5485dba4797SPatrick McHardy 		xfrm_state_put(x);
54926b15dadSJamal Hadi Salim 		err = 0;
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 
55226b15dadSJamal Hadi Salim 	return err;
55326b15dadSJamal Hadi Salim }
55453bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(__xfrm_state_delete);
55526b15dadSJamal Hadi Salim 
55626b15dadSJamal Hadi Salim int xfrm_state_delete(struct xfrm_state *x)
5571da177e4SLinus Torvalds {
55826b15dadSJamal Hadi Salim 	int err;
55926b15dadSJamal Hadi Salim 
5601da177e4SLinus Torvalds 	spin_lock_bh(&x->lock);
56126b15dadSJamal Hadi Salim 	err = __xfrm_state_delete(x);
5621da177e4SLinus Torvalds 	spin_unlock_bh(&x->lock);
56326b15dadSJamal Hadi Salim 
56426b15dadSJamal Hadi Salim 	return err;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete);
5671da177e4SLinus Torvalds 
5684aa2e62cSJoy Latten #ifdef CONFIG_SECURITY_NETWORK_XFRM
5694aa2e62cSJoy Latten static inline int
5700e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
5711da177e4SLinus Torvalds {
5724aa2e62cSJoy Latten 	int i, err = 0;
5734aa2e62cSJoy Latten 
5740e602451SAlexey Dobriyan 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
5754aa2e62cSJoy Latten 		struct hlist_node *entry;
5764aa2e62cSJoy Latten 		struct xfrm_state *x;
5774aa2e62cSJoy Latten 
5780e602451SAlexey Dobriyan 		hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
5794aa2e62cSJoy Latten 			if (xfrm_id_proto_match(x->id.proto, proto) &&
5804aa2e62cSJoy Latten 			   (err = security_xfrm_state_delete(x)) != 0) {
581ab5f5e8bSJoy Latten 				xfrm_audit_state_delete(x, 0,
582ab5f5e8bSJoy Latten 							audit_info->loginuid,
5832532386fSEric Paris 							audit_info->sessionid,
584ab5f5e8bSJoy Latten 							audit_info->secid);
5854aa2e62cSJoy Latten 				return err;
5864aa2e62cSJoy Latten 			}
5874aa2e62cSJoy Latten 		}
5884aa2e62cSJoy Latten 	}
5894aa2e62cSJoy Latten 
5904aa2e62cSJoy Latten 	return err;
5914aa2e62cSJoy Latten }
5924aa2e62cSJoy Latten #else
5934aa2e62cSJoy Latten static inline int
5940e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
5954aa2e62cSJoy Latten {
5964aa2e62cSJoy Latten 	return 0;
5974aa2e62cSJoy Latten }
5984aa2e62cSJoy Latten #endif
5994aa2e62cSJoy Latten 
6000e602451SAlexey Dobriyan int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
6014aa2e62cSJoy Latten {
6029e64cc95SJamal Hadi Salim 	int i, err = 0, cnt = 0;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
6050e602451SAlexey Dobriyan 	err = xfrm_state_flush_secctx_check(net, proto, audit_info);
6064aa2e62cSJoy Latten 	if (err)
6074aa2e62cSJoy Latten 		goto out;
6084aa2e62cSJoy Latten 
6099e64cc95SJamal Hadi Salim 	err = -ESRCH;
6100e602451SAlexey Dobriyan 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
6118f126e37SDavid S. Miller 		struct hlist_node *entry;
6128f126e37SDavid S. Miller 		struct xfrm_state *x;
6131da177e4SLinus Torvalds restart:
6140e602451SAlexey Dobriyan 		hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
6151da177e4SLinus Torvalds 			if (!xfrm_state_kern(x) &&
6165794708fSMasahide NAKAMURA 			    xfrm_id_proto_match(x->id.proto, proto)) {
6171da177e4SLinus Torvalds 				xfrm_state_hold(x);
6181da177e4SLinus Torvalds 				spin_unlock_bh(&xfrm_state_lock);
6191da177e4SLinus Torvalds 
620161a09e7SJoy Latten 				err = xfrm_state_delete(x);
621ab5f5e8bSJoy Latten 				xfrm_audit_state_delete(x, err ? 0 : 1,
622ab5f5e8bSJoy Latten 							audit_info->loginuid,
6232532386fSEric Paris 							audit_info->sessionid,
624ab5f5e8bSJoy Latten 							audit_info->secid);
6251da177e4SLinus Torvalds 				xfrm_state_put(x);
6269e64cc95SJamal Hadi Salim 				if (!err)
6279e64cc95SJamal Hadi Salim 					cnt++;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 				spin_lock_bh(&xfrm_state_lock);
6301da177e4SLinus Torvalds 				goto restart;
6311da177e4SLinus Torvalds 			}
6321da177e4SLinus Torvalds 		}
6331da177e4SLinus Torvalds 	}
6349e64cc95SJamal Hadi Salim 	if (cnt)
6354aa2e62cSJoy Latten 		err = 0;
6364aa2e62cSJoy Latten 
6374aa2e62cSJoy Latten out:
6381da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
6390e602451SAlexey Dobriyan 	wake_up(&net->xfrm.km_waitq);
6404aa2e62cSJoy Latten 	return err;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_flush);
6431da177e4SLinus Torvalds 
644e071041bSAlexey Dobriyan void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
64528d8909bSJamal Hadi Salim {
64628d8909bSJamal Hadi Salim 	spin_lock_bh(&xfrm_state_lock);
647e071041bSAlexey Dobriyan 	si->sadcnt = net->xfrm.state_num;
648e071041bSAlexey Dobriyan 	si->sadhcnt = net->xfrm.state_hmask;
64928d8909bSJamal Hadi Salim 	si->sadhmcnt = xfrm_state_hashmax;
65028d8909bSJamal Hadi Salim 	spin_unlock_bh(&xfrm_state_lock);
65128d8909bSJamal Hadi Salim }
65228d8909bSJamal Hadi Salim EXPORT_SYMBOL(xfrm_sad_getinfo);
65328d8909bSJamal Hadi Salim 
6541da177e4SLinus Torvalds static int
6551a898592SDavid S. Miller xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
65604686013SDavid S. Miller 		    const struct xfrm_tmpl *tmpl,
65733765d06SDavid S. Miller 		    const xfrm_address_t *daddr, const xfrm_address_t *saddr,
6581da177e4SLinus Torvalds 		    unsigned short family)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
6611da177e4SLinus Torvalds 	if (!afinfo)
6621da177e4SLinus Torvalds 		return -1;
6638444cf71SThomas Egerer 	afinfo->init_tempsel(&x->sel, fl);
6648444cf71SThomas Egerer 
6658444cf71SThomas Egerer 	if (family != tmpl->encap_family) {
6668444cf71SThomas Egerer 		xfrm_state_put_afinfo(afinfo);
6678444cf71SThomas Egerer 		afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
6688444cf71SThomas Egerer 		if (!afinfo)
6698444cf71SThomas Egerer 			return -1;
6708444cf71SThomas Egerer 	}
6718444cf71SThomas Egerer 	afinfo->init_temprop(x, tmpl, daddr, saddr);
6721da177e4SLinus Torvalds 	xfrm_state_put_afinfo(afinfo);
6731da177e4SLinus Torvalds 	return 0;
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6769aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
6779aa60088SDavid S. Miller 					      const xfrm_address_t *daddr,
6789aa60088SDavid S. Miller 					      __be32 spi, u8 proto,
6799aa60088SDavid S. Miller 					      unsigned short family)
680edcd5821SDavid S. Miller {
681221df1edSAlexey Dobriyan 	unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
682edcd5821SDavid S. Miller 	struct xfrm_state *x;
6838f126e37SDavid S. Miller 	struct hlist_node *entry;
684edcd5821SDavid S. Miller 
685221df1edSAlexey Dobriyan 	hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
686edcd5821SDavid S. Miller 		if (x->props.family != family ||
687edcd5821SDavid S. Miller 		    x->id.spi       != spi ||
6881802571bSWei Yongjun 		    x->id.proto     != proto ||
6891802571bSWei Yongjun 		    xfrm_addr_cmp(&x->id.daddr, daddr, family))
690edcd5821SDavid S. Miller 			continue;
691edcd5821SDavid S. Miller 
6923d6acfa7SJamal Hadi Salim 		if ((mark & x->mark.m) != x->mark.v)
6933d6acfa7SJamal Hadi Salim 			continue;
694edcd5821SDavid S. Miller 		xfrm_state_hold(x);
695edcd5821SDavid S. Miller 		return x;
696edcd5821SDavid S. Miller 	}
697edcd5821SDavid S. Miller 
698edcd5821SDavid S. Miller 	return NULL;
699edcd5821SDavid S. Miller }
700edcd5821SDavid S. Miller 
7019aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
7029aa60088SDavid S. Miller 						     const xfrm_address_t *daddr,
7039aa60088SDavid S. Miller 						     const xfrm_address_t *saddr,
7049aa60088SDavid S. Miller 						     u8 proto, unsigned short family)
705edcd5821SDavid S. Miller {
706221df1edSAlexey Dobriyan 	unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
707edcd5821SDavid S. Miller 	struct xfrm_state *x;
7088f126e37SDavid S. Miller 	struct hlist_node *entry;
709edcd5821SDavid S. Miller 
710221df1edSAlexey Dobriyan 	hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
711edcd5821SDavid S. Miller 		if (x->props.family != family ||
7121802571bSWei Yongjun 		    x->id.proto     != proto ||
7131802571bSWei Yongjun 		    xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
7141802571bSWei Yongjun 		    xfrm_addr_cmp(&x->props.saddr, saddr, family))
715edcd5821SDavid S. Miller 			continue;
716edcd5821SDavid S. Miller 
7173d6acfa7SJamal Hadi Salim 		if ((mark & x->mark.m) != x->mark.v)
7183d6acfa7SJamal Hadi Salim 			continue;
719edcd5821SDavid S. Miller 		xfrm_state_hold(x);
720edcd5821SDavid S. Miller 		return x;
721edcd5821SDavid S. Miller 	}
722edcd5821SDavid S. Miller 
723edcd5821SDavid S. Miller 	return NULL;
724edcd5821SDavid S. Miller }
725edcd5821SDavid S. Miller 
726edcd5821SDavid S. Miller static inline struct xfrm_state *
727edcd5821SDavid S. Miller __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
728edcd5821SDavid S. Miller {
729221df1edSAlexey Dobriyan 	struct net *net = xs_net(x);
730bd55775cSJamal Hadi Salim 	u32 mark = x->mark.v & x->mark.m;
731221df1edSAlexey Dobriyan 
732edcd5821SDavid S. Miller 	if (use_spi)
733bd55775cSJamal Hadi Salim 		return __xfrm_state_lookup(net, mark, &x->id.daddr,
734bd55775cSJamal Hadi Salim 					   x->id.spi, x->id.proto, family);
735edcd5821SDavid S. Miller 	else
736bd55775cSJamal Hadi Salim 		return __xfrm_state_lookup_byaddr(net, mark,
737bd55775cSJamal Hadi Salim 						  &x->id.daddr,
738edcd5821SDavid S. Miller 						  &x->props.saddr,
739edcd5821SDavid S. Miller 						  x->id.proto, family);
740edcd5821SDavid S. Miller }
741edcd5821SDavid S. Miller 
74298806f75SAlexey Dobriyan static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
7432fab22f2SPatrick McHardy {
7442fab22f2SPatrick McHardy 	if (have_hash_collision &&
74598806f75SAlexey Dobriyan 	    (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
74698806f75SAlexey Dobriyan 	    net->xfrm.state_num > net->xfrm.state_hmask)
74798806f75SAlexey Dobriyan 		schedule_work(&net->xfrm.state_hash_work);
7482fab22f2SPatrick McHardy }
7492fab22f2SPatrick McHardy 
75008ec9af1SDavid S. Miller static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
7514a08ab0fSDavid S. Miller 			       const struct flowi *fl, unsigned short family,
75208ec9af1SDavid S. Miller 			       struct xfrm_state **best, int *acq_in_progress,
75308ec9af1SDavid S. Miller 			       int *error)
75408ec9af1SDavid S. Miller {
75508ec9af1SDavid S. Miller 	/* Resolution logic:
75608ec9af1SDavid S. Miller 	 * 1. There is a valid state with matching selector. Done.
75708ec9af1SDavid S. Miller 	 * 2. Valid state with inappropriate selector. Skip.
75808ec9af1SDavid S. Miller 	 *
75908ec9af1SDavid S. Miller 	 * Entering area of "sysdeps".
76008ec9af1SDavid S. Miller 	 *
76108ec9af1SDavid S. Miller 	 * 3. If state is not valid, selector is temporary, it selects
76208ec9af1SDavid S. Miller 	 *    only session which triggered previous resolution. Key
76308ec9af1SDavid S. Miller 	 *    manager will do something to install a state with proper
76408ec9af1SDavid S. Miller 	 *    selector.
76508ec9af1SDavid S. Miller 	 */
76608ec9af1SDavid S. Miller 	if (x->km.state == XFRM_STATE_VALID) {
76708ec9af1SDavid S. Miller 		if ((x->sel.family &&
76808ec9af1SDavid S. Miller 		     !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
76908ec9af1SDavid S. Miller 		    !security_xfrm_state_pol_flow_match(x, pol, fl))
77008ec9af1SDavid S. Miller 			return;
77108ec9af1SDavid S. Miller 
77208ec9af1SDavid S. Miller 		if (!*best ||
77308ec9af1SDavid S. Miller 		    (*best)->km.dying > x->km.dying ||
77408ec9af1SDavid S. Miller 		    ((*best)->km.dying == x->km.dying &&
77508ec9af1SDavid S. Miller 		     (*best)->curlft.add_time < x->curlft.add_time))
77608ec9af1SDavid S. Miller 			*best = x;
77708ec9af1SDavid S. Miller 	} else if (x->km.state == XFRM_STATE_ACQ) {
77808ec9af1SDavid S. Miller 		*acq_in_progress = 1;
77908ec9af1SDavid S. Miller 	} else if (x->km.state == XFRM_STATE_ERROR ||
78008ec9af1SDavid S. Miller 		   x->km.state == XFRM_STATE_EXPIRED) {
78108ec9af1SDavid S. Miller 		if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
78208ec9af1SDavid S. Miller 		    security_xfrm_state_pol_flow_match(x, pol, fl))
78308ec9af1SDavid S. Miller 			*error = -ESRCH;
78408ec9af1SDavid S. Miller 	}
78508ec9af1SDavid S. Miller }
78608ec9af1SDavid S. Miller 
7871da177e4SLinus Torvalds struct xfrm_state *
78833765d06SDavid S. Miller xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
789b520e9f6SDavid S. Miller 		const struct flowi *fl, struct xfrm_tmpl *tmpl,
7901da177e4SLinus Torvalds 		struct xfrm_policy *pol, int *err,
7911da177e4SLinus Torvalds 		unsigned short family)
7921da177e4SLinus Torvalds {
79308ec9af1SDavid S. Miller 	static xfrm_address_t saddr_wildcard = { };
7945447c5e4SAlexey Dobriyan 	struct net *net = xp_net(pol);
7956a783c90SNicolas Dichtel 	unsigned int h, h_wildcard;
7968f126e37SDavid S. Miller 	struct hlist_node *entry;
79737b08e34SDavid S. Miller 	struct xfrm_state *x, *x0, *to_put;
7981da177e4SLinus Torvalds 	int acquire_in_progress = 0;
7991da177e4SLinus Torvalds 	int error = 0;
8001da177e4SLinus Torvalds 	struct xfrm_state *best = NULL;
801bd55775cSJamal Hadi Salim 	u32 mark = pol->mark.v & pol->mark.m;
8028444cf71SThomas Egerer 	unsigned short encap_family = tmpl->encap_family;
8031da177e4SLinus Torvalds 
80437b08e34SDavid S. Miller 	to_put = NULL;
80537b08e34SDavid S. Miller 
8061da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
8078444cf71SThomas Egerer 	h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
8085447c5e4SAlexey Dobriyan 	hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
8098444cf71SThomas Egerer 		if (x->props.family == encap_family &&
8101da177e4SLinus Torvalds 		    x->props.reqid == tmpl->reqid &&
8113d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
812fbd9a5b4SMasahide NAKAMURA 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
8138444cf71SThomas Egerer 		    xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
8141da177e4SLinus Torvalds 		    tmpl->mode == x->props.mode &&
8151da177e4SLinus Torvalds 		    tmpl->id.proto == x->id.proto &&
81608ec9af1SDavid S. Miller 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
8171f673c5fSDavid S. Miller 			xfrm_state_look_at(pol, x, fl, encap_family,
81808ec9af1SDavid S. Miller 					   &best, &acquire_in_progress, &error);
8191da177e4SLinus Torvalds 	}
82008ec9af1SDavid S. Miller 	if (best)
82108ec9af1SDavid S. Miller 		goto found;
82208ec9af1SDavid S. Miller 
8238444cf71SThomas Egerer 	h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
8246a783c90SNicolas Dichtel 	hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h_wildcard, bydst) {
8258444cf71SThomas Egerer 		if (x->props.family == encap_family &&
82608ec9af1SDavid S. Miller 		    x->props.reqid == tmpl->reqid &&
8273d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
82808ec9af1SDavid S. Miller 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
8298444cf71SThomas Egerer 		    xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
83008ec9af1SDavid S. Miller 		    tmpl->mode == x->props.mode &&
83108ec9af1SDavid S. Miller 		    tmpl->id.proto == x->id.proto &&
83208ec9af1SDavid S. Miller 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
8331f673c5fSDavid S. Miller 			xfrm_state_look_at(pol, x, fl, encap_family,
83408ec9af1SDavid S. Miller 					   &best, &acquire_in_progress, &error);
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
83708ec9af1SDavid S. Miller found:
8381da177e4SLinus Torvalds 	x = best;
8391da177e4SLinus Torvalds 	if (!x && !error && !acquire_in_progress) {
8405c5d281aSPatrick McHardy 		if (tmpl->id.spi &&
841bd55775cSJamal Hadi Salim 		    (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
8428444cf71SThomas Egerer 					      tmpl->id.proto, encap_family)) != NULL) {
84337b08e34SDavid S. Miller 			to_put = x0;
8441da177e4SLinus Torvalds 			error = -EEXIST;
8451da177e4SLinus Torvalds 			goto out;
8461da177e4SLinus Torvalds 		}
8475447c5e4SAlexey Dobriyan 		x = xfrm_state_alloc(net);
8481da177e4SLinus Torvalds 		if (x == NULL) {
8491da177e4SLinus Torvalds 			error = -ENOMEM;
8501da177e4SLinus Torvalds 			goto out;
8511da177e4SLinus Torvalds 		}
8528444cf71SThomas Egerer 		/* Initialize temporary state matching only
8531da177e4SLinus Torvalds 		 * to current session. */
8548444cf71SThomas Egerer 		xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
855bd55775cSJamal Hadi Salim 		memcpy(&x->mark, &pol->mark, sizeof(x->mark));
8561da177e4SLinus Torvalds 
8571d28f42cSDavid S. Miller 		error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
858e0d1caa7SVenkat Yekkirala 		if (error) {
859e0d1caa7SVenkat Yekkirala 			x->km.state = XFRM_STATE_DEAD;
86037b08e34SDavid S. Miller 			to_put = x;
861e0d1caa7SVenkat Yekkirala 			x = NULL;
862e0d1caa7SVenkat Yekkirala 			goto out;
863e0d1caa7SVenkat Yekkirala 		}
864e0d1caa7SVenkat Yekkirala 
8651da177e4SLinus Torvalds 		if (km_query(x, tmpl, pol) == 0) {
8661da177e4SLinus Torvalds 			x->km.state = XFRM_STATE_ACQ;
8675447c5e4SAlexey Dobriyan 			list_add(&x->km.all, &net->xfrm.state_all);
8685447c5e4SAlexey Dobriyan 			hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
8698444cf71SThomas Egerer 			h = xfrm_src_hash(net, daddr, saddr, encap_family);
8705447c5e4SAlexey Dobriyan 			hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
8711da177e4SLinus Torvalds 			if (x->id.spi) {
8728444cf71SThomas Egerer 				h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
8735447c5e4SAlexey Dobriyan 				hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
8741da177e4SLinus Torvalds 			}
875b27aeadbSAlexey Dobriyan 			x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
8769e0d57fdSYury Polyanskiy 			tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
8775447c5e4SAlexey Dobriyan 			net->xfrm.state_num++;
8785447c5e4SAlexey Dobriyan 			xfrm_hash_grow_check(net, x->bydst.next != NULL);
8791da177e4SLinus Torvalds 		} else {
8801da177e4SLinus Torvalds 			x->km.state = XFRM_STATE_DEAD;
88137b08e34SDavid S. Miller 			to_put = x;
8821da177e4SLinus Torvalds 			x = NULL;
8831da177e4SLinus Torvalds 			error = -ESRCH;
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds out:
8871da177e4SLinus Torvalds 	if (x)
8881da177e4SLinus Torvalds 		xfrm_state_hold(x);
8891da177e4SLinus Torvalds 	else
8901da177e4SLinus Torvalds 		*err = acquire_in_progress ? -EAGAIN : error;
8911da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
89237b08e34SDavid S. Miller 	if (to_put)
89337b08e34SDavid S. Miller 		xfrm_state_put(to_put);
8941da177e4SLinus Torvalds 	return x;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
897628529b6SJamal Hadi Salim struct xfrm_state *
898bd55775cSJamal Hadi Salim xfrm_stateonly_find(struct net *net, u32 mark,
8995447c5e4SAlexey Dobriyan 		    xfrm_address_t *daddr, xfrm_address_t *saddr,
900628529b6SJamal Hadi Salim 		    unsigned short family, u8 mode, u8 proto, u32 reqid)
901628529b6SJamal Hadi Salim {
9024bda4f25SPavel Emelyanov 	unsigned int h;
903628529b6SJamal Hadi Salim 	struct xfrm_state *rx = NULL, *x = NULL;
904628529b6SJamal Hadi Salim 	struct hlist_node *entry;
905628529b6SJamal Hadi Salim 
906628529b6SJamal Hadi Salim 	spin_lock(&xfrm_state_lock);
9075447c5e4SAlexey Dobriyan 	h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
9085447c5e4SAlexey Dobriyan 	hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
909628529b6SJamal Hadi Salim 		if (x->props.family == family &&
910628529b6SJamal Hadi Salim 		    x->props.reqid == reqid &&
9113d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
912628529b6SJamal Hadi Salim 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
913628529b6SJamal Hadi Salim 		    xfrm_state_addr_check(x, daddr, saddr, family) &&
914628529b6SJamal Hadi Salim 		    mode == x->props.mode &&
915628529b6SJamal Hadi Salim 		    proto == x->id.proto &&
916628529b6SJamal Hadi Salim 		    x->km.state == XFRM_STATE_VALID) {
917628529b6SJamal Hadi Salim 			rx = x;
918628529b6SJamal Hadi Salim 			break;
919628529b6SJamal Hadi Salim 		}
920628529b6SJamal Hadi Salim 	}
921628529b6SJamal Hadi Salim 
922628529b6SJamal Hadi Salim 	if (rx)
923628529b6SJamal Hadi Salim 		xfrm_state_hold(rx);
924628529b6SJamal Hadi Salim 	spin_unlock(&xfrm_state_lock);
925628529b6SJamal Hadi Salim 
926628529b6SJamal Hadi Salim 
927628529b6SJamal Hadi Salim 	return rx;
928628529b6SJamal Hadi Salim }
929628529b6SJamal Hadi Salim EXPORT_SYMBOL(xfrm_stateonly_find);
930628529b6SJamal Hadi Salim 
9311da177e4SLinus Torvalds static void __xfrm_state_insert(struct xfrm_state *x)
9321da177e4SLinus Torvalds {
93398806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
934a624c108SDavid S. Miller 	unsigned int h;
9351da177e4SLinus Torvalds 
93698806f75SAlexey Dobriyan 	list_add(&x->km.all, &net->xfrm.state_all);
9374c563f76STimo Teras 
93898806f75SAlexey Dobriyan 	h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
939c1969f29SDavid S. Miller 			  x->props.reqid, x->props.family);
94098806f75SAlexey Dobriyan 	hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
9411da177e4SLinus Torvalds 
94298806f75SAlexey Dobriyan 	h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
94398806f75SAlexey Dobriyan 	hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
9446c44e6b7SMasahide NAKAMURA 
9457b4dc360SMasahide NAKAMURA 	if (x->id.spi) {
94698806f75SAlexey Dobriyan 		h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
9476c44e6b7SMasahide NAKAMURA 				  x->props.family);
9481da177e4SLinus Torvalds 
94998806f75SAlexey Dobriyan 		hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
9506c44e6b7SMasahide NAKAMURA 	}
9511da177e4SLinus Torvalds 
9529e0d57fdSYury Polyanskiy 	tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
953a47f0ce0SDavid S. Miller 	if (x->replay_maxage)
954a47f0ce0SDavid S. Miller 		mod_timer(&x->rtimer, jiffies + x->replay_maxage);
955f8cd5488SJamal Hadi Salim 
95698806f75SAlexey Dobriyan 	wake_up(&net->xfrm.km_waitq);
957f034b5d4SDavid S. Miller 
95898806f75SAlexey Dobriyan 	net->xfrm.state_num++;
959f034b5d4SDavid S. Miller 
96098806f75SAlexey Dobriyan 	xfrm_hash_grow_check(net, x->bydst.next != NULL);
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
963c7f5ea3aSDavid S. Miller /* xfrm_state_lock is held */
964c7f5ea3aSDavid S. Miller static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
965c7f5ea3aSDavid S. Miller {
96698806f75SAlexey Dobriyan 	struct net *net = xs_net(xnew);
967c7f5ea3aSDavid S. Miller 	unsigned short family = xnew->props.family;
968c7f5ea3aSDavid S. Miller 	u32 reqid = xnew->props.reqid;
969c7f5ea3aSDavid S. Miller 	struct xfrm_state *x;
970c7f5ea3aSDavid S. Miller 	struct hlist_node *entry;
971c7f5ea3aSDavid S. Miller 	unsigned int h;
9723d6acfa7SJamal Hadi Salim 	u32 mark = xnew->mark.v & xnew->mark.m;
973c7f5ea3aSDavid S. Miller 
97498806f75SAlexey Dobriyan 	h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
97598806f75SAlexey Dobriyan 	hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
976c7f5ea3aSDavid S. Miller 		if (x->props.family	== family &&
977c7f5ea3aSDavid S. Miller 		    x->props.reqid	== reqid &&
9783d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
979c1969f29SDavid S. Miller 		    !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
980c1969f29SDavid S. Miller 		    !xfrm_addr_cmp(&x->props.saddr, &xnew->props.saddr, family))
98134996cb9SHerbert Xu 			x->genid++;
982c7f5ea3aSDavid S. Miller 	}
983c7f5ea3aSDavid S. Miller }
984c7f5ea3aSDavid S. Miller 
9851da177e4SLinus Torvalds void xfrm_state_insert(struct xfrm_state *x)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
988c7f5ea3aSDavid S. Miller 	__xfrm_state_bump_genids(x);
9891da177e4SLinus Torvalds 	__xfrm_state_insert(x);
9901da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_insert);
9931da177e4SLinus Torvalds 
9942770834cSDavid S. Miller /* xfrm_state_lock is held */
995a70486f0SDavid S. Miller static struct xfrm_state *__find_acq_core(struct net *net, struct xfrm_mark *m,
996a70486f0SDavid S. Miller 					  unsigned short family, u8 mode,
997a70486f0SDavid S. Miller 					  u32 reqid, u8 proto,
998a70486f0SDavid S. Miller 					  const xfrm_address_t *daddr,
999a70486f0SDavid S. Miller 					  const xfrm_address_t *saddr, int create)
10002770834cSDavid S. Miller {
10015447c5e4SAlexey Dobriyan 	unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
10028f126e37SDavid S. Miller 	struct hlist_node *entry;
10032770834cSDavid S. Miller 	struct xfrm_state *x;
10043d6acfa7SJamal Hadi Salim 	u32 mark = m->v & m->m;
10052770834cSDavid S. Miller 
10065447c5e4SAlexey Dobriyan 	hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
10072770834cSDavid S. Miller 		if (x->props.reqid  != reqid ||
10082770834cSDavid S. Miller 		    x->props.mode   != mode ||
10092770834cSDavid S. Miller 		    x->props.family != family ||
10102770834cSDavid S. Miller 		    x->km.state     != XFRM_STATE_ACQ ||
101175e252d9SJoy Latten 		    x->id.spi       != 0 ||
10121802571bSWei Yongjun 		    x->id.proto	    != proto ||
10133d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) != x->mark.v ||
10141802571bSWei Yongjun 		    xfrm_addr_cmp(&x->id.daddr, daddr, family) ||
10151802571bSWei Yongjun 		    xfrm_addr_cmp(&x->props.saddr, saddr, family))
10162770834cSDavid S. Miller 			continue;
10172770834cSDavid S. Miller 
10182770834cSDavid S. Miller 		xfrm_state_hold(x);
10192770834cSDavid S. Miller 		return x;
10202770834cSDavid S. Miller 	}
10212770834cSDavid S. Miller 
10222770834cSDavid S. Miller 	if (!create)
10232770834cSDavid S. Miller 		return NULL;
10242770834cSDavid S. Miller 
10255447c5e4SAlexey Dobriyan 	x = xfrm_state_alloc(net);
10262770834cSDavid S. Miller 	if (likely(x)) {
10272770834cSDavid S. Miller 		switch (family) {
10282770834cSDavid S. Miller 		case AF_INET:
10292770834cSDavid S. Miller 			x->sel.daddr.a4 = daddr->a4;
10302770834cSDavid S. Miller 			x->sel.saddr.a4 = saddr->a4;
10312770834cSDavid S. Miller 			x->sel.prefixlen_d = 32;
10322770834cSDavid S. Miller 			x->sel.prefixlen_s = 32;
10332770834cSDavid S. Miller 			x->props.saddr.a4 = saddr->a4;
10342770834cSDavid S. Miller 			x->id.daddr.a4 = daddr->a4;
10352770834cSDavid S. Miller 			break;
10362770834cSDavid S. Miller 
10372770834cSDavid S. Miller 		case AF_INET6:
10382770834cSDavid S. Miller 			ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
10392770834cSDavid S. Miller 				       (struct in6_addr *)daddr);
10402770834cSDavid S. Miller 			ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
10412770834cSDavid S. Miller 				       (struct in6_addr *)saddr);
10422770834cSDavid S. Miller 			x->sel.prefixlen_d = 128;
10432770834cSDavid S. Miller 			x->sel.prefixlen_s = 128;
10442770834cSDavid S. Miller 			ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
10452770834cSDavid S. Miller 				       (struct in6_addr *)saddr);
10462770834cSDavid S. Miller 			ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
10472770834cSDavid S. Miller 				       (struct in6_addr *)daddr);
10482770834cSDavid S. Miller 			break;
10493ff50b79SStephen Hemminger 		}
10502770834cSDavid S. Miller 
10512770834cSDavid S. Miller 		x->km.state = XFRM_STATE_ACQ;
10522770834cSDavid S. Miller 		x->id.proto = proto;
10532770834cSDavid S. Miller 		x->props.family = family;
10542770834cSDavid S. Miller 		x->props.mode = mode;
10552770834cSDavid S. Miller 		x->props.reqid = reqid;
1056bd55775cSJamal Hadi Salim 		x->mark.v = m->v;
1057bd55775cSJamal Hadi Salim 		x->mark.m = m->m;
1058b27aeadbSAlexey Dobriyan 		x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
10592770834cSDavid S. Miller 		xfrm_state_hold(x);
10609e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
10615447c5e4SAlexey Dobriyan 		list_add(&x->km.all, &net->xfrm.state_all);
10625447c5e4SAlexey Dobriyan 		hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
10635447c5e4SAlexey Dobriyan 		h = xfrm_src_hash(net, daddr, saddr, family);
10645447c5e4SAlexey Dobriyan 		hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1065918049f0SDavid S. Miller 
10665447c5e4SAlexey Dobriyan 		net->xfrm.state_num++;
1067918049f0SDavid S. Miller 
10685447c5e4SAlexey Dobriyan 		xfrm_hash_grow_check(net, x->bydst.next != NULL);
10692770834cSDavid S. Miller 	}
10702770834cSDavid S. Miller 
10712770834cSDavid S. Miller 	return x;
10722770834cSDavid S. Miller }
10732770834cSDavid S. Miller 
1074bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds int xfrm_state_add(struct xfrm_state *x)
10771da177e4SLinus Torvalds {
10785447c5e4SAlexey Dobriyan 	struct net *net = xs_net(x);
107937b08e34SDavid S. Miller 	struct xfrm_state *x1, *to_put;
10801da177e4SLinus Torvalds 	int family;
10811da177e4SLinus Torvalds 	int err;
1082bd55775cSJamal Hadi Salim 	u32 mark = x->mark.v & x->mark.m;
1083eb2971b6SMasahide NAKAMURA 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	family = x->props.family;
10861da177e4SLinus Torvalds 
108737b08e34SDavid S. Miller 	to_put = NULL;
108837b08e34SDavid S. Miller 
10891da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
10901da177e4SLinus Torvalds 
1091edcd5821SDavid S. Miller 	x1 = __xfrm_state_locate(x, use_spi, family);
10921da177e4SLinus Torvalds 	if (x1) {
109337b08e34SDavid S. Miller 		to_put = x1;
10941da177e4SLinus Torvalds 		x1 = NULL;
10951da177e4SLinus Torvalds 		err = -EEXIST;
10961da177e4SLinus Torvalds 		goto out;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 
1099eb2971b6SMasahide NAKAMURA 	if (use_spi && x->km.seq) {
1100bd55775cSJamal Hadi Salim 		x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
110175e252d9SJoy Latten 		if (x1 && ((x1->id.proto != x->id.proto) ||
110275e252d9SJoy Latten 		    xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
110337b08e34SDavid S. Miller 			to_put = x1;
11041da177e4SLinus Torvalds 			x1 = NULL;
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
1108eb2971b6SMasahide NAKAMURA 	if (use_spi && !x1)
1109bd55775cSJamal Hadi Salim 		x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1110bd55775cSJamal Hadi Salim 				     x->props.reqid, x->id.proto,
11111da177e4SLinus Torvalds 				     &x->id.daddr, &x->props.saddr, 0);
11121da177e4SLinus Torvalds 
1113c7f5ea3aSDavid S. Miller 	__xfrm_state_bump_genids(x);
11141da177e4SLinus Torvalds 	__xfrm_state_insert(x);
11151da177e4SLinus Torvalds 	err = 0;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds out:
11181da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	if (x1) {
11211da177e4SLinus Torvalds 		xfrm_state_delete(x1);
11221da177e4SLinus Torvalds 		xfrm_state_put(x1);
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 
112537b08e34SDavid S. Miller 	if (to_put)
112637b08e34SDavid S. Miller 		xfrm_state_put(to_put);
112737b08e34SDavid S. Miller 
11281da177e4SLinus Torvalds 	return err;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_add);
11311da177e4SLinus Torvalds 
113280c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
11336666351dSEric Dumazet static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
113480c9abaaSShinta Sugimoto {
113598806f75SAlexey Dobriyan 	struct net *net = xs_net(orig);
113680c9abaaSShinta Sugimoto 	int err = -ENOMEM;
113798806f75SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
113880c9abaaSShinta Sugimoto 	if (!x)
1139553f9118SHerbert Xu 		goto out;
114080c9abaaSShinta Sugimoto 
114180c9abaaSShinta Sugimoto 	memcpy(&x->id, &orig->id, sizeof(x->id));
114280c9abaaSShinta Sugimoto 	memcpy(&x->sel, &orig->sel, sizeof(x->sel));
114380c9abaaSShinta Sugimoto 	memcpy(&x->lft, &orig->lft, sizeof(x->lft));
114480c9abaaSShinta Sugimoto 	x->props.mode = orig->props.mode;
114580c9abaaSShinta Sugimoto 	x->props.replay_window = orig->props.replay_window;
114680c9abaaSShinta Sugimoto 	x->props.reqid = orig->props.reqid;
114780c9abaaSShinta Sugimoto 	x->props.family = orig->props.family;
114880c9abaaSShinta Sugimoto 	x->props.saddr = orig->props.saddr;
114980c9abaaSShinta Sugimoto 
115080c9abaaSShinta Sugimoto 	if (orig->aalg) {
11514447bb33SMartin Willi 		x->aalg = xfrm_algo_auth_clone(orig->aalg);
115280c9abaaSShinta Sugimoto 		if (!x->aalg)
115380c9abaaSShinta Sugimoto 			goto error;
115480c9abaaSShinta Sugimoto 	}
115580c9abaaSShinta Sugimoto 	x->props.aalgo = orig->props.aalgo;
115680c9abaaSShinta Sugimoto 
115780c9abaaSShinta Sugimoto 	if (orig->ealg) {
115880c9abaaSShinta Sugimoto 		x->ealg = xfrm_algo_clone(orig->ealg);
115980c9abaaSShinta Sugimoto 		if (!x->ealg)
116080c9abaaSShinta Sugimoto 			goto error;
116180c9abaaSShinta Sugimoto 	}
116280c9abaaSShinta Sugimoto 	x->props.ealgo = orig->props.ealgo;
116380c9abaaSShinta Sugimoto 
116480c9abaaSShinta Sugimoto 	if (orig->calg) {
116580c9abaaSShinta Sugimoto 		x->calg = xfrm_algo_clone(orig->calg);
116680c9abaaSShinta Sugimoto 		if (!x->calg)
116780c9abaaSShinta Sugimoto 			goto error;
116880c9abaaSShinta Sugimoto 	}
116980c9abaaSShinta Sugimoto 	x->props.calgo = orig->props.calgo;
117080c9abaaSShinta Sugimoto 
117180c9abaaSShinta Sugimoto 	if (orig->encap) {
117280c9abaaSShinta Sugimoto 		x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
117380c9abaaSShinta Sugimoto 		if (!x->encap)
117480c9abaaSShinta Sugimoto 			goto error;
117580c9abaaSShinta Sugimoto 	}
117680c9abaaSShinta Sugimoto 
117780c9abaaSShinta Sugimoto 	if (orig->coaddr) {
117880c9abaaSShinta Sugimoto 		x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
117980c9abaaSShinta Sugimoto 				    GFP_KERNEL);
118080c9abaaSShinta Sugimoto 		if (!x->coaddr)
118180c9abaaSShinta Sugimoto 			goto error;
118280c9abaaSShinta Sugimoto 	}
118380c9abaaSShinta Sugimoto 
1184bd55775cSJamal Hadi Salim 	memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1185bd55775cSJamal Hadi Salim 
118680c9abaaSShinta Sugimoto 	err = xfrm_init_state(x);
118780c9abaaSShinta Sugimoto 	if (err)
118880c9abaaSShinta Sugimoto 		goto error;
118980c9abaaSShinta Sugimoto 
119080c9abaaSShinta Sugimoto 	x->props.flags = orig->props.flags;
119180c9abaaSShinta Sugimoto 
119280c9abaaSShinta Sugimoto 	x->curlft.add_time = orig->curlft.add_time;
119380c9abaaSShinta Sugimoto 	x->km.state = orig->km.state;
119480c9abaaSShinta Sugimoto 	x->km.seq = orig->km.seq;
119580c9abaaSShinta Sugimoto 
119680c9abaaSShinta Sugimoto 	return x;
119780c9abaaSShinta Sugimoto 
119880c9abaaSShinta Sugimoto  error:
1199553f9118SHerbert Xu 	xfrm_state_put(x);
1200553f9118SHerbert Xu out:
120180c9abaaSShinta Sugimoto 	if (errp)
120280c9abaaSShinta Sugimoto 		*errp = err;
120380c9abaaSShinta Sugimoto 	return NULL;
120480c9abaaSShinta Sugimoto }
120580c9abaaSShinta Sugimoto 
120680c9abaaSShinta Sugimoto /* xfrm_state_lock is held */
120780c9abaaSShinta Sugimoto struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
120880c9abaaSShinta Sugimoto {
120980c9abaaSShinta Sugimoto 	unsigned int h;
121080c9abaaSShinta Sugimoto 	struct xfrm_state *x;
121180c9abaaSShinta Sugimoto 	struct hlist_node *entry;
121280c9abaaSShinta Sugimoto 
121380c9abaaSShinta Sugimoto 	if (m->reqid) {
121464d0cd00SAlexey Dobriyan 		h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
121580c9abaaSShinta Sugimoto 				  m->reqid, m->old_family);
121673d189dcSAlexey Dobriyan 		hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
121780c9abaaSShinta Sugimoto 			if (x->props.mode != m->mode ||
121880c9abaaSShinta Sugimoto 			    x->id.proto != m->proto)
121980c9abaaSShinta Sugimoto 				continue;
122080c9abaaSShinta Sugimoto 			if (m->reqid && x->props.reqid != m->reqid)
122180c9abaaSShinta Sugimoto 				continue;
122280c9abaaSShinta Sugimoto 			if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
122380c9abaaSShinta Sugimoto 					  m->old_family) ||
122480c9abaaSShinta Sugimoto 			    xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
122580c9abaaSShinta Sugimoto 					  m->old_family))
122680c9abaaSShinta Sugimoto 				continue;
122780c9abaaSShinta Sugimoto 			xfrm_state_hold(x);
122880c9abaaSShinta Sugimoto 			return x;
122980c9abaaSShinta Sugimoto 		}
123080c9abaaSShinta Sugimoto 	} else {
123164d0cd00SAlexey Dobriyan 		h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
123280c9abaaSShinta Sugimoto 				  m->old_family);
1233d320bbb3SAlexey Dobriyan 		hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
123480c9abaaSShinta Sugimoto 			if (x->props.mode != m->mode ||
123580c9abaaSShinta Sugimoto 			    x->id.proto != m->proto)
123680c9abaaSShinta Sugimoto 				continue;
123780c9abaaSShinta Sugimoto 			if (xfrm_addr_cmp(&x->id.daddr, &m->old_daddr,
123880c9abaaSShinta Sugimoto 					  m->old_family) ||
123980c9abaaSShinta Sugimoto 			    xfrm_addr_cmp(&x->props.saddr, &m->old_saddr,
124080c9abaaSShinta Sugimoto 					  m->old_family))
124180c9abaaSShinta Sugimoto 				continue;
124280c9abaaSShinta Sugimoto 			xfrm_state_hold(x);
124380c9abaaSShinta Sugimoto 			return x;
124480c9abaaSShinta Sugimoto 		}
124580c9abaaSShinta Sugimoto 	}
124680c9abaaSShinta Sugimoto 
124780c9abaaSShinta Sugimoto 	return NULL;
124880c9abaaSShinta Sugimoto }
124980c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_migrate_state_find);
125080c9abaaSShinta Sugimoto 
125180c9abaaSShinta Sugimoto struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
125280c9abaaSShinta Sugimoto 				       struct xfrm_migrate *m)
125380c9abaaSShinta Sugimoto {
125480c9abaaSShinta Sugimoto 	struct xfrm_state *xc;
125580c9abaaSShinta Sugimoto 	int err;
125680c9abaaSShinta Sugimoto 
125780c9abaaSShinta Sugimoto 	xc = xfrm_state_clone(x, &err);
125880c9abaaSShinta Sugimoto 	if (!xc)
125980c9abaaSShinta Sugimoto 		return NULL;
126080c9abaaSShinta Sugimoto 
126180c9abaaSShinta Sugimoto 	memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
126280c9abaaSShinta Sugimoto 	memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
126380c9abaaSShinta Sugimoto 
126480c9abaaSShinta Sugimoto 	/* add state */
126580c9abaaSShinta Sugimoto 	if (!xfrm_addr_cmp(&x->id.daddr, &m->new_daddr, m->new_family)) {
126680c9abaaSShinta Sugimoto 		/* a care is needed when the destination address of the
126780c9abaaSShinta Sugimoto 		   state is to be updated as it is a part of triplet */
126880c9abaaSShinta Sugimoto 		xfrm_state_insert(xc);
126980c9abaaSShinta Sugimoto 	} else {
127080c9abaaSShinta Sugimoto 		if ((err = xfrm_state_add(xc)) < 0)
127180c9abaaSShinta Sugimoto 			goto error;
127280c9abaaSShinta Sugimoto 	}
127380c9abaaSShinta Sugimoto 
127480c9abaaSShinta Sugimoto 	return xc;
127580c9abaaSShinta Sugimoto error:
127678347c8cSThomas Egerer 	xfrm_state_put(xc);
127780c9abaaSShinta Sugimoto 	return NULL;
127880c9abaaSShinta Sugimoto }
127980c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_state_migrate);
128080c9abaaSShinta Sugimoto #endif
128180c9abaaSShinta Sugimoto 
12821da177e4SLinus Torvalds int xfrm_state_update(struct xfrm_state *x)
12831da177e4SLinus Torvalds {
128437b08e34SDavid S. Miller 	struct xfrm_state *x1, *to_put;
12851da177e4SLinus Torvalds 	int err;
1286eb2971b6SMasahide NAKAMURA 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
12871da177e4SLinus Torvalds 
128837b08e34SDavid S. Miller 	to_put = NULL;
128937b08e34SDavid S. Miller 
12901da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
1291edcd5821SDavid S. Miller 	x1 = __xfrm_state_locate(x, use_spi, x->props.family);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	err = -ESRCH;
12941da177e4SLinus Torvalds 	if (!x1)
12951da177e4SLinus Torvalds 		goto out;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	if (xfrm_state_kern(x1)) {
129837b08e34SDavid S. Miller 		to_put = x1;
12991da177e4SLinus Torvalds 		err = -EEXIST;
13001da177e4SLinus Torvalds 		goto out;
13011da177e4SLinus Torvalds 	}
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	if (x1->km.state == XFRM_STATE_ACQ) {
13041da177e4SLinus Torvalds 		__xfrm_state_insert(x);
13051da177e4SLinus Torvalds 		x = NULL;
13061da177e4SLinus Torvalds 	}
13071da177e4SLinus Torvalds 	err = 0;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds out:
13101da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
13111da177e4SLinus Torvalds 
131237b08e34SDavid S. Miller 	if (to_put)
131337b08e34SDavid S. Miller 		xfrm_state_put(to_put);
131437b08e34SDavid S. Miller 
13151da177e4SLinus Torvalds 	if (err)
13161da177e4SLinus Torvalds 		return err;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	if (!x) {
13191da177e4SLinus Torvalds 		xfrm_state_delete(x1);
13201da177e4SLinus Torvalds 		xfrm_state_put(x1);
13211da177e4SLinus Torvalds 		return 0;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	err = -EINVAL;
13251da177e4SLinus Torvalds 	spin_lock_bh(&x1->lock);
13261da177e4SLinus Torvalds 	if (likely(x1->km.state == XFRM_STATE_VALID)) {
13271da177e4SLinus Torvalds 		if (x->encap && x1->encap)
13281da177e4SLinus Torvalds 			memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1329060f02a3SNoriaki TAKAMIYA 		if (x->coaddr && x1->coaddr) {
1330060f02a3SNoriaki TAKAMIYA 			memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1331060f02a3SNoriaki TAKAMIYA 		}
1332060f02a3SNoriaki TAKAMIYA 		if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1333060f02a3SNoriaki TAKAMIYA 			memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
13341da177e4SLinus Torvalds 		memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
13351da177e4SLinus Torvalds 		x1->km.dying = 0;
13361da177e4SLinus Torvalds 
13379e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
13381da177e4SLinus Torvalds 		if (x1->curlft.use_time)
13391da177e4SLinus Torvalds 			xfrm_state_check_expire(x1);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 		err = 0;
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 	spin_unlock_bh(&x1->lock);
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	xfrm_state_put(x1);
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	return err;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_update);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds int xfrm_state_check_expire(struct xfrm_state *x)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	if (!x->curlft.use_time)
13549d729f72SJames Morris 		x->curlft.use_time = get_seconds();
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	if (x->km.state != XFRM_STATE_VALID)
13571da177e4SLinus Torvalds 		return -EINVAL;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	if (x->curlft.bytes >= x->lft.hard_byte_limit ||
13601da177e4SLinus Torvalds 	    x->curlft.packets >= x->lft.hard_packet_limit) {
13614666faabSHerbert Xu 		x->km.state = XFRM_STATE_EXPIRED;
13629e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
13631da177e4SLinus Torvalds 		return -EINVAL;
13641da177e4SLinus Torvalds 	}
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	if (!x->km.dying &&
13671da177e4SLinus Torvalds 	    (x->curlft.bytes >= x->lft.soft_byte_limit ||
13684666faabSHerbert Xu 	     x->curlft.packets >= x->lft.soft_packet_limit)) {
13694666faabSHerbert Xu 		x->km.dying = 1;
137053bc6b4dSJamal Hadi Salim 		km_state_expired(x, 0, 0);
13714666faabSHerbert Xu 	}
13721da177e4SLinus Torvalds 	return 0;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_check_expire);
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds struct xfrm_state *
1377a70486f0SDavid S. Miller xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1378bd55775cSJamal Hadi Salim 		  u8 proto, unsigned short family)
13791da177e4SLinus Torvalds {
13801da177e4SLinus Torvalds 	struct xfrm_state *x;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
1383bd55775cSJamal Hadi Salim 	x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
13841da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
13851da177e4SLinus Torvalds 	return x;
13861da177e4SLinus Torvalds }
13871da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_lookup);
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds struct xfrm_state *
1390bd55775cSJamal Hadi Salim xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1391a70486f0SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1392eb2971b6SMasahide NAKAMURA 			 u8 proto, unsigned short family)
1393eb2971b6SMasahide NAKAMURA {
1394eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x;
1395eb2971b6SMasahide NAKAMURA 
1396eb2971b6SMasahide NAKAMURA 	spin_lock_bh(&xfrm_state_lock);
1397bd55775cSJamal Hadi Salim 	x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1398eb2971b6SMasahide NAKAMURA 	spin_unlock_bh(&xfrm_state_lock);
1399eb2971b6SMasahide NAKAMURA 	return x;
1400eb2971b6SMasahide NAKAMURA }
1401eb2971b6SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1402eb2971b6SMasahide NAKAMURA 
1403eb2971b6SMasahide NAKAMURA struct xfrm_state *
1404bd55775cSJamal Hadi Salim xfrm_find_acq(struct net *net, struct xfrm_mark *mark, u8 mode, u32 reqid, u8 proto,
1405a70486f0SDavid S. Miller 	      const xfrm_address_t *daddr, const xfrm_address_t *saddr,
14061da177e4SLinus Torvalds 	      int create, unsigned short family)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	struct xfrm_state *x;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
1411bd55775cSJamal Hadi Salim 	x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
14121da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
14132770834cSDavid S. Miller 
14141da177e4SLinus Torvalds 	return x;
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq);
14171da177e4SLinus Torvalds 
141841a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
141941a49cc3SMasahide NAKAMURA int
142041a49cc3SMasahide NAKAMURA xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
142141a49cc3SMasahide NAKAMURA 	       unsigned short family)
142241a49cc3SMasahide NAKAMURA {
142341a49cc3SMasahide NAKAMURA 	int err = 0;
142441a49cc3SMasahide NAKAMURA 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
142541a49cc3SMasahide NAKAMURA 	if (!afinfo)
142641a49cc3SMasahide NAKAMURA 		return -EAFNOSUPPORT;
142741a49cc3SMasahide NAKAMURA 
142841a49cc3SMasahide NAKAMURA 	spin_lock_bh(&xfrm_state_lock);
142941a49cc3SMasahide NAKAMURA 	if (afinfo->tmpl_sort)
143041a49cc3SMasahide NAKAMURA 		err = afinfo->tmpl_sort(dst, src, n);
143141a49cc3SMasahide NAKAMURA 	spin_unlock_bh(&xfrm_state_lock);
143241a49cc3SMasahide NAKAMURA 	xfrm_state_put_afinfo(afinfo);
143341a49cc3SMasahide NAKAMURA 	return err;
143441a49cc3SMasahide NAKAMURA }
143541a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_tmpl_sort);
143641a49cc3SMasahide NAKAMURA 
143741a49cc3SMasahide NAKAMURA int
143841a49cc3SMasahide NAKAMURA xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
143941a49cc3SMasahide NAKAMURA 		unsigned short family)
144041a49cc3SMasahide NAKAMURA {
144141a49cc3SMasahide NAKAMURA 	int err = 0;
144241a49cc3SMasahide NAKAMURA 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
144341a49cc3SMasahide NAKAMURA 	if (!afinfo)
144441a49cc3SMasahide NAKAMURA 		return -EAFNOSUPPORT;
144541a49cc3SMasahide NAKAMURA 
144641a49cc3SMasahide NAKAMURA 	spin_lock_bh(&xfrm_state_lock);
144741a49cc3SMasahide NAKAMURA 	if (afinfo->state_sort)
144841a49cc3SMasahide NAKAMURA 		err = afinfo->state_sort(dst, src, n);
144941a49cc3SMasahide NAKAMURA 	spin_unlock_bh(&xfrm_state_lock);
145041a49cc3SMasahide NAKAMURA 	xfrm_state_put_afinfo(afinfo);
145141a49cc3SMasahide NAKAMURA 	return err;
145241a49cc3SMasahide NAKAMURA }
145341a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_sort);
145441a49cc3SMasahide NAKAMURA #endif
145541a49cc3SMasahide NAKAMURA 
14561da177e4SLinus Torvalds /* Silly enough, but I'm lazy to build resolution list */
14571da177e4SLinus Torvalds 
1458bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
14591da177e4SLinus Torvalds {
14601da177e4SLinus Torvalds 	int i;
14611da177e4SLinus Torvalds 
14625447c5e4SAlexey Dobriyan 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
14638f126e37SDavid S. Miller 		struct hlist_node *entry;
14648f126e37SDavid S. Miller 		struct xfrm_state *x;
14658f126e37SDavid S. Miller 
14665447c5e4SAlexey Dobriyan 		hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
14678f126e37SDavid S. Miller 			if (x->km.seq == seq &&
14683d6acfa7SJamal Hadi Salim 			    (mark & x->mark.m) == x->mark.v &&
14698f126e37SDavid S. Miller 			    x->km.state == XFRM_STATE_ACQ) {
14701da177e4SLinus Torvalds 				xfrm_state_hold(x);
14711da177e4SLinus Torvalds 				return x;
14721da177e4SLinus Torvalds 			}
14731da177e4SLinus Torvalds 		}
14741da177e4SLinus Torvalds 	}
14751da177e4SLinus Torvalds 	return NULL;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
1478bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	struct xfrm_state *x;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
1483bd55775cSJamal Hadi Salim 	x = __xfrm_find_acq_byseq(net, mark, seq);
14841da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
14851da177e4SLinus Torvalds 	return x;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq_byseq);
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds u32 xfrm_get_acqseq(void)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	u32 res;
14926836b9bdSjamal 	static atomic_t acqseq;
14931da177e4SLinus Torvalds 
14946836b9bdSjamal 	do {
14956836b9bdSjamal 		res = atomic_inc_return(&acqseq);
14966836b9bdSjamal 	} while (!res);
14976836b9bdSjamal 
14981da177e4SLinus Torvalds 	return res;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_get_acqseq);
15011da177e4SLinus Torvalds 
1502658b219eSHerbert Xu int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
15031da177e4SLinus Torvalds {
1504221df1edSAlexey Dobriyan 	struct net *net = xs_net(x);
1505f034b5d4SDavid S. Miller 	unsigned int h;
15061da177e4SLinus Torvalds 	struct xfrm_state *x0;
1507658b219eSHerbert Xu 	int err = -ENOENT;
1508658b219eSHerbert Xu 	__be32 minspi = htonl(low);
1509658b219eSHerbert Xu 	__be32 maxspi = htonl(high);
1510bd55775cSJamal Hadi Salim 	u32 mark = x->mark.v & x->mark.m;
15111da177e4SLinus Torvalds 
1512658b219eSHerbert Xu 	spin_lock_bh(&x->lock);
1513658b219eSHerbert Xu 	if (x->km.state == XFRM_STATE_DEAD)
1514658b219eSHerbert Xu 		goto unlock;
1515658b219eSHerbert Xu 
1516658b219eSHerbert Xu 	err = 0;
15171da177e4SLinus Torvalds 	if (x->id.spi)
1518658b219eSHerbert Xu 		goto unlock;
1519658b219eSHerbert Xu 
1520658b219eSHerbert Xu 	err = -ENOENT;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	if (minspi == maxspi) {
1523bd55775cSJamal Hadi Salim 		x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
15241da177e4SLinus Torvalds 		if (x0) {
15251da177e4SLinus Torvalds 			xfrm_state_put(x0);
1526658b219eSHerbert Xu 			goto unlock;
15271da177e4SLinus Torvalds 		}
15281da177e4SLinus Torvalds 		x->id.spi = minspi;
15291da177e4SLinus Torvalds 	} else {
15301da177e4SLinus Torvalds 		u32 spi = 0;
153126977b4eSAl Viro 		for (h=0; h<high-low+1; h++) {
153226977b4eSAl Viro 			spi = low + net_random()%(high-low+1);
1533bd55775cSJamal Hadi Salim 			x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
15341da177e4SLinus Torvalds 			if (x0 == NULL) {
15351da177e4SLinus Torvalds 				x->id.spi = htonl(spi);
15361da177e4SLinus Torvalds 				break;
15371da177e4SLinus Torvalds 			}
15381da177e4SLinus Torvalds 			xfrm_state_put(x0);
15391da177e4SLinus Torvalds 		}
15401da177e4SLinus Torvalds 	}
15411da177e4SLinus Torvalds 	if (x->id.spi) {
15421da177e4SLinus Torvalds 		spin_lock_bh(&xfrm_state_lock);
154312604d8aSAlexey Dobriyan 		h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
154412604d8aSAlexey Dobriyan 		hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
15451da177e4SLinus Torvalds 		spin_unlock_bh(&xfrm_state_lock);
1546658b219eSHerbert Xu 
1547658b219eSHerbert Xu 		err = 0;
15481da177e4SLinus Torvalds 	}
1549658b219eSHerbert Xu 
1550658b219eSHerbert Xu unlock:
1551658b219eSHerbert Xu 	spin_unlock_bh(&x->lock);
1552658b219eSHerbert Xu 
1553658b219eSHerbert Xu 	return err;
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_alloc_spi);
15561da177e4SLinus Torvalds 
1557284fa7daSAlexey Dobriyan int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
15584c563f76STimo Teras 		    int (*func)(struct xfrm_state *, int, void*),
15591da177e4SLinus Torvalds 		    void *data)
15601da177e4SLinus Torvalds {
156112a169e7SHerbert Xu 	struct xfrm_state *state;
156212a169e7SHerbert Xu 	struct xfrm_state_walk *x;
15631da177e4SLinus Torvalds 	int err = 0;
15641da177e4SLinus Torvalds 
156512a169e7SHerbert Xu 	if (walk->seq != 0 && list_empty(&walk->all))
15664c563f76STimo Teras 		return 0;
15674c563f76STimo Teras 
15681da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_lock);
156912a169e7SHerbert Xu 	if (list_empty(&walk->all))
1570284fa7daSAlexey Dobriyan 		x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
157112a169e7SHerbert Xu 	else
157212a169e7SHerbert Xu 		x = list_entry(&walk->all, struct xfrm_state_walk, all);
1573284fa7daSAlexey Dobriyan 	list_for_each_entry_from(x, &net->xfrm.state_all, all) {
157412a169e7SHerbert Xu 		if (x->state == XFRM_STATE_DEAD)
15754c563f76STimo Teras 			continue;
157612a169e7SHerbert Xu 		state = container_of(x, struct xfrm_state, km);
157712a169e7SHerbert Xu 		if (!xfrm_id_proto_match(state->id.proto, walk->proto))
157894b9bb54SJamal Hadi Salim 			continue;
157912a169e7SHerbert Xu 		err = func(state, walk->seq, data);
15804c563f76STimo Teras 		if (err) {
158112a169e7SHerbert Xu 			list_move_tail(&walk->all, &x->all);
158294b9bb54SJamal Hadi Salim 			goto out;
158394b9bb54SJamal Hadi Salim 		}
158412a169e7SHerbert Xu 		walk->seq++;
15854c563f76STimo Teras 	}
158612a169e7SHerbert Xu 	if (walk->seq == 0) {
15871da177e4SLinus Torvalds 		err = -ENOENT;
15881da177e4SLinus Torvalds 		goto out;
15891da177e4SLinus Torvalds 	}
159012a169e7SHerbert Xu 	list_del_init(&walk->all);
15911da177e4SLinus Torvalds out:
15921da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_lock);
15931da177e4SLinus Torvalds 	return err;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_walk);
15961da177e4SLinus Torvalds 
15975c182458SHerbert Xu void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
15985c182458SHerbert Xu {
159912a169e7SHerbert Xu 	INIT_LIST_HEAD(&walk->all);
16005c182458SHerbert Xu 	walk->proto = proto;
160112a169e7SHerbert Xu 	walk->state = XFRM_STATE_DEAD;
160212a169e7SHerbert Xu 	walk->seq = 0;
16035c182458SHerbert Xu }
16045c182458SHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_init);
16055c182458SHerbert Xu 
1606abb81c4fSHerbert Xu void xfrm_state_walk_done(struct xfrm_state_walk *walk)
1607abb81c4fSHerbert Xu {
160812a169e7SHerbert Xu 	if (list_empty(&walk->all))
16095c182458SHerbert Xu 		return;
16105c182458SHerbert Xu 
161112a169e7SHerbert Xu 	spin_lock_bh(&xfrm_state_lock);
161212a169e7SHerbert Xu 	list_del(&walk->all);
16137d0b591cSChuck Ebbert 	spin_unlock_bh(&xfrm_state_lock);
1614abb81c4fSHerbert Xu }
1615abb81c4fSHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_done);
1616abb81c4fSHerbert Xu 
1617f8cd5488SJamal Hadi Salim static void xfrm_replay_timer_handler(unsigned long data)
1618f8cd5488SJamal Hadi Salim {
1619f8cd5488SJamal Hadi Salim 	struct xfrm_state *x = (struct xfrm_state*)data;
1620f8cd5488SJamal Hadi Salim 
1621f8cd5488SJamal Hadi Salim 	spin_lock(&x->lock);
1622f8cd5488SJamal Hadi Salim 
16232717096aSJamal Hadi Salim 	if (x->km.state == XFRM_STATE_VALID) {
1624a6483b79SAlexey Dobriyan 		if (xfrm_aevent_is_on(xs_net(x)))
16259fdc4883SSteffen Klassert 			x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
16262717096aSJamal Hadi Salim 		else
16272717096aSJamal Hadi Salim 			x->xflags |= XFRM_TIME_DEFER;
16282717096aSJamal Hadi Salim 	}
1629f8cd5488SJamal Hadi Salim 
1630f8cd5488SJamal Hadi Salim 	spin_unlock(&x->lock);
1631f8cd5488SJamal Hadi Salim }
1632f8cd5488SJamal Hadi Salim 
1633df01812eSDenis Cheng static LIST_HEAD(xfrm_km_list);
16341da177e4SLinus Torvalds static DEFINE_RWLOCK(xfrm_km_lock);
16351da177e4SLinus Torvalds 
1636214e005bSDavid S. Miller void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
16371da177e4SLinus Torvalds {
16381da177e4SLinus Torvalds 	struct xfrm_mgr *km;
16391da177e4SLinus Torvalds 
164026b15dadSJamal Hadi Salim 	read_lock(&xfrm_km_lock);
164126b15dadSJamal Hadi Salim 	list_for_each_entry(km, &xfrm_km_list, list)
164226b15dadSJamal Hadi Salim 		if (km->notify_policy)
164326b15dadSJamal Hadi Salim 			km->notify_policy(xp, dir, c);
164426b15dadSJamal Hadi Salim 	read_unlock(&xfrm_km_lock);
164526b15dadSJamal Hadi Salim }
164626b15dadSJamal Hadi Salim 
1647214e005bSDavid S. Miller void km_state_notify(struct xfrm_state *x, const struct km_event *c)
164826b15dadSJamal Hadi Salim {
164926b15dadSJamal Hadi Salim 	struct xfrm_mgr *km;
165026b15dadSJamal Hadi Salim 	read_lock(&xfrm_km_lock);
165126b15dadSJamal Hadi Salim 	list_for_each_entry(km, &xfrm_km_list, list)
165226b15dadSJamal Hadi Salim 		if (km->notify)
165326b15dadSJamal Hadi Salim 			km->notify(x, c);
165426b15dadSJamal Hadi Salim 	read_unlock(&xfrm_km_lock);
165526b15dadSJamal Hadi Salim }
165626b15dadSJamal Hadi Salim 
165726b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_policy_notify);
165826b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_state_notify);
165926b15dadSJamal Hadi Salim 
166053bc6b4dSJamal Hadi Salim void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
166126b15dadSJamal Hadi Salim {
166298806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
166326b15dadSJamal Hadi Salim 	struct km_event c;
166426b15dadSJamal Hadi Salim 
1665bf08867fSHerbert Xu 	c.data.hard = hard;
166653bc6b4dSJamal Hadi Salim 	c.pid = pid;
1667f60f6b8fSHerbert Xu 	c.event = XFRM_MSG_EXPIRE;
166826b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	if (hard)
167198806f75SAlexey Dobriyan 		wake_up(&net->xfrm.km_waitq);
16721da177e4SLinus Torvalds }
16731da177e4SLinus Torvalds 
167453bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(km_state_expired);
167526b15dadSJamal Hadi Salim /*
167626b15dadSJamal Hadi Salim  * We send to all registered managers regardless of failure
167726b15dadSJamal Hadi Salim  * We are happy with one success
167826b15dadSJamal Hadi Salim */
1679980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
16801da177e4SLinus Torvalds {
168126b15dadSJamal Hadi Salim 	int err = -EINVAL, acqret;
16821da177e4SLinus Torvalds 	struct xfrm_mgr *km;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 	read_lock(&xfrm_km_lock);
16851da177e4SLinus Torvalds 	list_for_each_entry(km, &xfrm_km_list, list) {
168626b15dadSJamal Hadi Salim 		acqret = km->acquire(x, t, pol, XFRM_POLICY_OUT);
168726b15dadSJamal Hadi Salim 		if (!acqret)
168826b15dadSJamal Hadi Salim 			err = acqret;
16891da177e4SLinus Torvalds 	}
16901da177e4SLinus Torvalds 	read_unlock(&xfrm_km_lock);
16911da177e4SLinus Torvalds 	return err;
16921da177e4SLinus Torvalds }
1693980ebd25SJamal Hadi Salim EXPORT_SYMBOL(km_query);
16941da177e4SLinus Torvalds 
16955d36b180SAl Viro int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
16961da177e4SLinus Torvalds {
16971da177e4SLinus Torvalds 	int err = -EINVAL;
16981da177e4SLinus Torvalds 	struct xfrm_mgr *km;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	read_lock(&xfrm_km_lock);
17011da177e4SLinus Torvalds 	list_for_each_entry(km, &xfrm_km_list, list) {
17021da177e4SLinus Torvalds 		if (km->new_mapping)
17031da177e4SLinus Torvalds 			err = km->new_mapping(x, ipaddr, sport);
17041da177e4SLinus Torvalds 		if (!err)
17051da177e4SLinus Torvalds 			break;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 	read_unlock(&xfrm_km_lock);
17081da177e4SLinus Torvalds 	return err;
17091da177e4SLinus Torvalds }
17101da177e4SLinus Torvalds EXPORT_SYMBOL(km_new_mapping);
17111da177e4SLinus Torvalds 
17126c5c8ca7SJamal Hadi Salim void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
17131da177e4SLinus Torvalds {
171498806f75SAlexey Dobriyan 	struct net *net = xp_net(pol);
171526b15dadSJamal Hadi Salim 	struct km_event c;
17161da177e4SLinus Torvalds 
1717bf08867fSHerbert Xu 	c.data.hard = hard;
17186c5c8ca7SJamal Hadi Salim 	c.pid = pid;
1719f60f6b8fSHerbert Xu 	c.event = XFRM_MSG_POLEXPIRE;
172026b15dadSJamal Hadi Salim 	km_policy_notify(pol, dir, &c);
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds 	if (hard)
172398806f75SAlexey Dobriyan 		wake_up(&net->xfrm.km_waitq);
17241da177e4SLinus Torvalds }
1725a70fcb0bSDavid S. Miller EXPORT_SYMBOL(km_policy_expired);
17261da177e4SLinus Torvalds 
17272d60abc2SEric Dumazet #ifdef CONFIG_XFRM_MIGRATE
1728183cad12SDavid S. Miller int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1729183cad12SDavid S. Miller 	       const struct xfrm_migrate *m, int num_migrate,
1730183cad12SDavid S. Miller 	       const struct xfrm_kmaddress *k)
173180c9abaaSShinta Sugimoto {
173280c9abaaSShinta Sugimoto 	int err = -EINVAL;
173380c9abaaSShinta Sugimoto 	int ret;
173480c9abaaSShinta Sugimoto 	struct xfrm_mgr *km;
173580c9abaaSShinta Sugimoto 
173680c9abaaSShinta Sugimoto 	read_lock(&xfrm_km_lock);
173780c9abaaSShinta Sugimoto 	list_for_each_entry(km, &xfrm_km_list, list) {
173880c9abaaSShinta Sugimoto 		if (km->migrate) {
173913c1d189SArnaud Ebalard 			ret = km->migrate(sel, dir, type, m, num_migrate, k);
174080c9abaaSShinta Sugimoto 			if (!ret)
174180c9abaaSShinta Sugimoto 				err = ret;
174280c9abaaSShinta Sugimoto 		}
174380c9abaaSShinta Sugimoto 	}
174480c9abaaSShinta Sugimoto 	read_unlock(&xfrm_km_lock);
174580c9abaaSShinta Sugimoto 	return err;
174680c9abaaSShinta Sugimoto }
174780c9abaaSShinta Sugimoto EXPORT_SYMBOL(km_migrate);
17482d60abc2SEric Dumazet #endif
174980c9abaaSShinta Sugimoto 
1750db983c11SAlexey Dobriyan int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
175197a64b45SMasahide NAKAMURA {
175297a64b45SMasahide NAKAMURA 	int err = -EINVAL;
175397a64b45SMasahide NAKAMURA 	int ret;
175497a64b45SMasahide NAKAMURA 	struct xfrm_mgr *km;
175597a64b45SMasahide NAKAMURA 
175697a64b45SMasahide NAKAMURA 	read_lock(&xfrm_km_lock);
175797a64b45SMasahide NAKAMURA 	list_for_each_entry(km, &xfrm_km_list, list) {
175897a64b45SMasahide NAKAMURA 		if (km->report) {
1759db983c11SAlexey Dobriyan 			ret = km->report(net, proto, sel, addr);
176097a64b45SMasahide NAKAMURA 			if (!ret)
176197a64b45SMasahide NAKAMURA 				err = ret;
176297a64b45SMasahide NAKAMURA 		}
176397a64b45SMasahide NAKAMURA 	}
176497a64b45SMasahide NAKAMURA 	read_unlock(&xfrm_km_lock);
176597a64b45SMasahide NAKAMURA 	return err;
176697a64b45SMasahide NAKAMURA }
176797a64b45SMasahide NAKAMURA EXPORT_SYMBOL(km_report);
176897a64b45SMasahide NAKAMURA 
17691da177e4SLinus Torvalds int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
17701da177e4SLinus Torvalds {
17711da177e4SLinus Torvalds 	int err;
17721da177e4SLinus Torvalds 	u8 *data;
17731da177e4SLinus Torvalds 	struct xfrm_mgr *km;
17741da177e4SLinus Torvalds 	struct xfrm_policy *pol = NULL;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	if (optlen <= 0 || optlen > PAGE_SIZE)
17771da177e4SLinus Torvalds 		return -EMSGSIZE;
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 	data = kmalloc(optlen, GFP_KERNEL);
17801da177e4SLinus Torvalds 	if (!data)
17811da177e4SLinus Torvalds 		return -ENOMEM;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	err = -EFAULT;
17841da177e4SLinus Torvalds 	if (copy_from_user(data, optval, optlen))
17851da177e4SLinus Torvalds 		goto out;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	err = -EINVAL;
17881da177e4SLinus Torvalds 	read_lock(&xfrm_km_lock);
17891da177e4SLinus Torvalds 	list_for_each_entry(km, &xfrm_km_list, list) {
1790cb969f07SVenkat Yekkirala 		pol = km->compile_policy(sk, optname, data,
17911da177e4SLinus Torvalds 					 optlen, &err);
17921da177e4SLinus Torvalds 		if (err >= 0)
17931da177e4SLinus Torvalds 			break;
17941da177e4SLinus Torvalds 	}
17951da177e4SLinus Torvalds 	read_unlock(&xfrm_km_lock);
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	if (err >= 0) {
17981da177e4SLinus Torvalds 		xfrm_sk_policy_insert(sk, err, pol);
17991da177e4SLinus Torvalds 		xfrm_pol_put(pol);
18001da177e4SLinus Torvalds 		err = 0;
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds out:
18041da177e4SLinus Torvalds 	kfree(data);
18051da177e4SLinus Torvalds 	return err;
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_user_policy);
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds int xfrm_register_km(struct xfrm_mgr *km)
18101da177e4SLinus Torvalds {
18111da177e4SLinus Torvalds 	write_lock_bh(&xfrm_km_lock);
18121da177e4SLinus Torvalds 	list_add_tail(&km->list, &xfrm_km_list);
18131da177e4SLinus Torvalds 	write_unlock_bh(&xfrm_km_lock);
18141da177e4SLinus Torvalds 	return 0;
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_register_km);
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds int xfrm_unregister_km(struct xfrm_mgr *km)
18191da177e4SLinus Torvalds {
18201da177e4SLinus Torvalds 	write_lock_bh(&xfrm_km_lock);
18211da177e4SLinus Torvalds 	list_del(&km->list);
18221da177e4SLinus Torvalds 	write_unlock_bh(&xfrm_km_lock);
18231da177e4SLinus Torvalds 	return 0;
18241da177e4SLinus Torvalds }
18251da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_unregister_km);
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds 	int err = 0;
18301da177e4SLinus Torvalds 	if (unlikely(afinfo == NULL))
18311da177e4SLinus Torvalds 		return -EINVAL;
18321da177e4SLinus Torvalds 	if (unlikely(afinfo->family >= NPROTO))
18331da177e4SLinus Torvalds 		return -EAFNOSUPPORT;
1834f3111502SIngo Molnar 	write_lock_bh(&xfrm_state_afinfo_lock);
18351da177e4SLinus Torvalds 	if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
18361da177e4SLinus Torvalds 		err = -ENOBUFS;
1837edcd5821SDavid S. Miller 	else
18381da177e4SLinus Torvalds 		xfrm_state_afinfo[afinfo->family] = afinfo;
1839f3111502SIngo Molnar 	write_unlock_bh(&xfrm_state_afinfo_lock);
18401da177e4SLinus Torvalds 	return err;
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_register_afinfo);
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
18451da177e4SLinus Torvalds {
18461da177e4SLinus Torvalds 	int err = 0;
18471da177e4SLinus Torvalds 	if (unlikely(afinfo == NULL))
18481da177e4SLinus Torvalds 		return -EINVAL;
18491da177e4SLinus Torvalds 	if (unlikely(afinfo->family >= NPROTO))
18501da177e4SLinus Torvalds 		return -EAFNOSUPPORT;
1851f3111502SIngo Molnar 	write_lock_bh(&xfrm_state_afinfo_lock);
18521da177e4SLinus Torvalds 	if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
18531da177e4SLinus Torvalds 		if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
18541da177e4SLinus Torvalds 			err = -EINVAL;
1855edcd5821SDavid S. Miller 		else
18561da177e4SLinus Torvalds 			xfrm_state_afinfo[afinfo->family] = NULL;
18571da177e4SLinus Torvalds 	}
1858f3111502SIngo Molnar 	write_unlock_bh(&xfrm_state_afinfo_lock);
18591da177e4SLinus Torvalds 	return err;
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
18621da177e4SLinus Torvalds 
186317c2a42aSHerbert Xu static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	struct xfrm_state_afinfo *afinfo;
18661da177e4SLinus Torvalds 	if (unlikely(family >= NPROTO))
18671da177e4SLinus Torvalds 		return NULL;
18681da177e4SLinus Torvalds 	read_lock(&xfrm_state_afinfo_lock);
18691da177e4SLinus Torvalds 	afinfo = xfrm_state_afinfo[family];
1870546be240SHerbert Xu 	if (unlikely(!afinfo))
18711da177e4SLinus Torvalds 		read_unlock(&xfrm_state_afinfo_lock);
18721da177e4SLinus Torvalds 	return afinfo;
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
187517c2a42aSHerbert Xu static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
18769a429c49SEric Dumazet 	__releases(xfrm_state_afinfo_lock)
18771da177e4SLinus Torvalds {
1878546be240SHerbert Xu 	read_unlock(&xfrm_state_afinfo_lock);
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
18821da177e4SLinus Torvalds void xfrm_state_delete_tunnel(struct xfrm_state *x)
18831da177e4SLinus Torvalds {
18841da177e4SLinus Torvalds 	if (x->tunnel) {
18851da177e4SLinus Torvalds 		struct xfrm_state *t = x->tunnel;
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 		if (atomic_read(&t->tunnel_users) == 2)
18881da177e4SLinus Torvalds 			xfrm_state_delete(t);
18891da177e4SLinus Torvalds 		atomic_dec(&t->tunnel_users);
18901da177e4SLinus Torvalds 		xfrm_state_put(t);
18911da177e4SLinus Torvalds 		x->tunnel = NULL;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete_tunnel);
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds int xfrm_state_mtu(struct xfrm_state *x, int mtu)
18971da177e4SLinus Torvalds {
1898c5c25238SPatrick McHardy 	int res;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	spin_lock_bh(&x->lock);
19011da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_VALID &&
1902c5c25238SPatrick McHardy 	    x->type && x->type->get_mtu)
1903c5c25238SPatrick McHardy 		res = x->type->get_mtu(x, mtu);
19041da177e4SLinus Torvalds 	else
190528121617SPatrick McHardy 		res = mtu - x->props.header_len;
19061da177e4SLinus Torvalds 	spin_unlock_bh(&x->lock);
19071da177e4SLinus Torvalds 	return res;
19081da177e4SLinus Torvalds }
19091da177e4SLinus Torvalds 
1910a454f0ccSWei Yongjun int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
191172cb6962SHerbert Xu {
1912d094cd83SHerbert Xu 	struct xfrm_state_afinfo *afinfo;
1913df9dcb45SKazunori MIYAZAWA 	struct xfrm_mode *inner_mode;
1914d094cd83SHerbert Xu 	int family = x->props.family;
191572cb6962SHerbert Xu 	int err;
191672cb6962SHerbert Xu 
1917d094cd83SHerbert Xu 	err = -EAFNOSUPPORT;
1918d094cd83SHerbert Xu 	afinfo = xfrm_state_get_afinfo(family);
1919d094cd83SHerbert Xu 	if (!afinfo)
1920d094cd83SHerbert Xu 		goto error;
1921d094cd83SHerbert Xu 
1922d094cd83SHerbert Xu 	err = 0;
1923d094cd83SHerbert Xu 	if (afinfo->init_flags)
1924d094cd83SHerbert Xu 		err = afinfo->init_flags(x);
1925d094cd83SHerbert Xu 
1926d094cd83SHerbert Xu 	xfrm_state_put_afinfo(afinfo);
1927d094cd83SHerbert Xu 
1928d094cd83SHerbert Xu 	if (err)
1929d094cd83SHerbert Xu 		goto error;
1930d094cd83SHerbert Xu 
1931d094cd83SHerbert Xu 	err = -EPROTONOSUPPORT;
1932df9dcb45SKazunori MIYAZAWA 
1933df9dcb45SKazunori MIYAZAWA 	if (x->sel.family != AF_UNSPEC) {
1934df9dcb45SKazunori MIYAZAWA 		inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1935df9dcb45SKazunori MIYAZAWA 		if (inner_mode == NULL)
193613996378SHerbert Xu 			goto error;
193713996378SHerbert Xu 
1938df9dcb45SKazunori MIYAZAWA 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1939df9dcb45SKazunori MIYAZAWA 		    family != x->sel.family) {
1940df9dcb45SKazunori MIYAZAWA 			xfrm_put_mode(inner_mode);
194113996378SHerbert Xu 			goto error;
1942df9dcb45SKazunori MIYAZAWA 		}
1943df9dcb45SKazunori MIYAZAWA 
1944df9dcb45SKazunori MIYAZAWA 		x->inner_mode = inner_mode;
1945df9dcb45SKazunori MIYAZAWA 	} else {
1946df9dcb45SKazunori MIYAZAWA 		struct xfrm_mode *inner_mode_iaf;
1947d81d2285SMartin Willi 		int iafamily = AF_INET;
1948df9dcb45SKazunori MIYAZAWA 
1949d81d2285SMartin Willi 		inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
1950df9dcb45SKazunori MIYAZAWA 		if (inner_mode == NULL)
1951df9dcb45SKazunori MIYAZAWA 			goto error;
1952df9dcb45SKazunori MIYAZAWA 
1953df9dcb45SKazunori MIYAZAWA 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1954df9dcb45SKazunori MIYAZAWA 			xfrm_put_mode(inner_mode);
1955df9dcb45SKazunori MIYAZAWA 			goto error;
1956df9dcb45SKazunori MIYAZAWA 		}
1957df9dcb45SKazunori MIYAZAWA 		x->inner_mode = inner_mode;
1958d81d2285SMartin Willi 
1959d81d2285SMartin Willi 		if (x->props.family == AF_INET)
1960d81d2285SMartin Willi 			iafamily = AF_INET6;
1961d81d2285SMartin Willi 
1962d81d2285SMartin Willi 		inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1963d81d2285SMartin Willi 		if (inner_mode_iaf) {
1964d81d2285SMartin Willi 			if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1965df9dcb45SKazunori MIYAZAWA 				x->inner_mode_iaf = inner_mode_iaf;
1966d81d2285SMartin Willi 			else
1967d81d2285SMartin Willi 				xfrm_put_mode(inner_mode_iaf);
1968df9dcb45SKazunori MIYAZAWA 		}
1969df9dcb45SKazunori MIYAZAWA 	}
197013996378SHerbert Xu 
1971d094cd83SHerbert Xu 	x->type = xfrm_get_type(x->id.proto, family);
197272cb6962SHerbert Xu 	if (x->type == NULL)
197372cb6962SHerbert Xu 		goto error;
197472cb6962SHerbert Xu 
197572cb6962SHerbert Xu 	err = x->type->init_state(x);
197672cb6962SHerbert Xu 	if (err)
197772cb6962SHerbert Xu 		goto error;
197872cb6962SHerbert Xu 
197913996378SHerbert Xu 	x->outer_mode = xfrm_get_mode(x->props.mode, family);
198013996378SHerbert Xu 	if (x->outer_mode == NULL)
1981b59f45d0SHerbert Xu 		goto error;
1982b59f45d0SHerbert Xu 
1983a454f0ccSWei Yongjun 	if (init_replay) {
1984a454f0ccSWei Yongjun 		err = xfrm_init_replay(x);
1985a454f0ccSWei Yongjun 		if (err)
1986a454f0ccSWei Yongjun 			goto error;
1987a454f0ccSWei Yongjun 	}
1988a454f0ccSWei Yongjun 
198972cb6962SHerbert Xu 	x->km.state = XFRM_STATE_VALID;
199072cb6962SHerbert Xu 
199172cb6962SHerbert Xu error:
199272cb6962SHerbert Xu 	return err;
199372cb6962SHerbert Xu }
199472cb6962SHerbert Xu 
1995a454f0ccSWei Yongjun EXPORT_SYMBOL(__xfrm_init_state);
1996a454f0ccSWei Yongjun 
1997a454f0ccSWei Yongjun int xfrm_init_state(struct xfrm_state *x)
1998a454f0ccSWei Yongjun {
1999a454f0ccSWei Yongjun 	return __xfrm_init_state(x, true);
2000a454f0ccSWei Yongjun }
2001a454f0ccSWei Yongjun 
200272cb6962SHerbert Xu EXPORT_SYMBOL(xfrm_init_state);
20031da177e4SLinus Torvalds 
2004d62ddc21SAlexey Dobriyan int __net_init xfrm_state_init(struct net *net)
20051da177e4SLinus Torvalds {
2006f034b5d4SDavid S. Miller 	unsigned int sz;
20071da177e4SLinus Torvalds 
20089d4139c7SAlexey Dobriyan 	INIT_LIST_HEAD(&net->xfrm.state_all);
20099d4139c7SAlexey Dobriyan 
2010f034b5d4SDavid S. Miller 	sz = sizeof(struct hlist_head) * 8;
2011f034b5d4SDavid S. Miller 
201273d189dcSAlexey Dobriyan 	net->xfrm.state_bydst = xfrm_hash_alloc(sz);
201373d189dcSAlexey Dobriyan 	if (!net->xfrm.state_bydst)
201473d189dcSAlexey Dobriyan 		goto out_bydst;
2015d320bbb3SAlexey Dobriyan 	net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2016d320bbb3SAlexey Dobriyan 	if (!net->xfrm.state_bysrc)
2017d320bbb3SAlexey Dobriyan 		goto out_bysrc;
2018b754a4fdSAlexey Dobriyan 	net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2019b754a4fdSAlexey Dobriyan 	if (!net->xfrm.state_byspi)
2020b754a4fdSAlexey Dobriyan 		goto out_byspi;
2021529983ecSAlexey Dobriyan 	net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2022f034b5d4SDavid S. Miller 
20230bf7c5b0SAlexey Dobriyan 	net->xfrm.state_num = 0;
202463082733SAlexey Dobriyan 	INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2025b8a0ae20SAlexey Dobriyan 	INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2026c7837144SAlexey Dobriyan 	INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
202750a30657SAlexey Dobriyan 	init_waitqueue_head(&net->xfrm.km_waitq);
2028d62ddc21SAlexey Dobriyan 	return 0;
202973d189dcSAlexey Dobriyan 
2030b754a4fdSAlexey Dobriyan out_byspi:
2031b754a4fdSAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
2032d320bbb3SAlexey Dobriyan out_bysrc:
2033d320bbb3SAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bydst, sz);
203473d189dcSAlexey Dobriyan out_bydst:
203573d189dcSAlexey Dobriyan 	return -ENOMEM;
2036d62ddc21SAlexey Dobriyan }
2037d62ddc21SAlexey Dobriyan 
2038d62ddc21SAlexey Dobriyan void xfrm_state_fini(struct net *net)
2039d62ddc21SAlexey Dobriyan {
20407c2776eeSAlexey Dobriyan 	struct xfrm_audit audit_info;
204173d189dcSAlexey Dobriyan 	unsigned int sz;
204273d189dcSAlexey Dobriyan 
20437c2776eeSAlexey Dobriyan 	flush_work(&net->xfrm.state_hash_work);
20447c2776eeSAlexey Dobriyan 	audit_info.loginuid = -1;
20457c2776eeSAlexey Dobriyan 	audit_info.sessionid = -1;
20467c2776eeSAlexey Dobriyan 	audit_info.secid = 0;
20477c2776eeSAlexey Dobriyan 	xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
20487c2776eeSAlexey Dobriyan 	flush_work(&net->xfrm.state_gc_work);
20497c2776eeSAlexey Dobriyan 
20509d4139c7SAlexey Dobriyan 	WARN_ON(!list_empty(&net->xfrm.state_all));
205173d189dcSAlexey Dobriyan 
2052529983ecSAlexey Dobriyan 	sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2053b754a4fdSAlexey Dobriyan 	WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2054b754a4fdSAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_byspi, sz);
2055d320bbb3SAlexey Dobriyan 	WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2056d320bbb3SAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
205773d189dcSAlexey Dobriyan 	WARN_ON(!hlist_empty(net->xfrm.state_bydst));
205873d189dcSAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bydst, sz);
20591da177e4SLinus Torvalds }
20601da177e4SLinus Torvalds 
2061ab5f5e8bSJoy Latten #ifdef CONFIG_AUDITSYSCALL
2062cf35f43eSIlpo Järvinen static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2063ab5f5e8bSJoy Latten 				     struct audit_buffer *audit_buf)
2064ab5f5e8bSJoy Latten {
206568277accSPaul Moore 	struct xfrm_sec_ctx *ctx = x->security;
206668277accSPaul Moore 	u32 spi = ntohl(x->id.spi);
206768277accSPaul Moore 
206868277accSPaul Moore 	if (ctx)
2069ab5f5e8bSJoy Latten 		audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
207068277accSPaul Moore 				 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2071ab5f5e8bSJoy Latten 
2072ab5f5e8bSJoy Latten 	switch(x->props.family) {
2073ab5f5e8bSJoy Latten 	case AF_INET:
207421454aaaSHarvey Harrison 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
207521454aaaSHarvey Harrison 				 &x->props.saddr.a4, &x->id.daddr.a4);
2076ab5f5e8bSJoy Latten 		break;
2077ab5f5e8bSJoy Latten 	case AF_INET6:
20785b095d98SHarvey Harrison 		audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2079fdb46ee7SHarvey Harrison 				 x->props.saddr.a6, x->id.daddr.a6);
2080ab5f5e8bSJoy Latten 		break;
2081ab5f5e8bSJoy Latten 	}
208268277accSPaul Moore 
208368277accSPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2084ab5f5e8bSJoy Latten }
2085ab5f5e8bSJoy Latten 
2086cf35f43eSIlpo Järvinen static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2087afeb14b4SPaul Moore 				      struct audit_buffer *audit_buf)
2088afeb14b4SPaul Moore {
2089afeb14b4SPaul Moore 	struct iphdr *iph4;
2090afeb14b4SPaul Moore 	struct ipv6hdr *iph6;
2091afeb14b4SPaul Moore 
2092afeb14b4SPaul Moore 	switch (family) {
2093afeb14b4SPaul Moore 	case AF_INET:
2094afeb14b4SPaul Moore 		iph4 = ip_hdr(skb);
209521454aaaSHarvey Harrison 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
209621454aaaSHarvey Harrison 				 &iph4->saddr, &iph4->daddr);
2097afeb14b4SPaul Moore 		break;
2098afeb14b4SPaul Moore 	case AF_INET6:
2099afeb14b4SPaul Moore 		iph6 = ipv6_hdr(skb);
2100afeb14b4SPaul Moore 		audit_log_format(audit_buf,
21015b095d98SHarvey Harrison 				 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2102fdb46ee7SHarvey Harrison 				 &iph6->saddr,&iph6->daddr,
2103afeb14b4SPaul Moore 				 iph6->flow_lbl[0] & 0x0f,
2104afeb14b4SPaul Moore 				 iph6->flow_lbl[1],
2105afeb14b4SPaul Moore 				 iph6->flow_lbl[2]);
2106afeb14b4SPaul Moore 		break;
2107afeb14b4SPaul Moore 	}
2108afeb14b4SPaul Moore }
2109afeb14b4SPaul Moore 
211068277accSPaul Moore void xfrm_audit_state_add(struct xfrm_state *x, int result,
21112532386fSEric Paris 			  uid_t auid, u32 sessionid, u32 secid)
2112ab5f5e8bSJoy Latten {
2113ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf;
2114ab5f5e8bSJoy Latten 
2115afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SAD-add");
2116ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
2117ab5f5e8bSJoy Latten 		return;
21182532386fSEric Paris 	xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2119afeb14b4SPaul Moore 	xfrm_audit_helper_sainfo(x, audit_buf);
2120afeb14b4SPaul Moore 	audit_log_format(audit_buf, " res=%u", result);
2121ab5f5e8bSJoy Latten 	audit_log_end(audit_buf);
2122ab5f5e8bSJoy Latten }
2123ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2124ab5f5e8bSJoy Latten 
212568277accSPaul Moore void xfrm_audit_state_delete(struct xfrm_state *x, int result,
21262532386fSEric Paris 			     uid_t auid, u32 sessionid, u32 secid)
2127ab5f5e8bSJoy Latten {
2128ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf;
2129ab5f5e8bSJoy Latten 
2130afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SAD-delete");
2131ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
2132ab5f5e8bSJoy Latten 		return;
21332532386fSEric Paris 	xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2134afeb14b4SPaul Moore 	xfrm_audit_helper_sainfo(x, audit_buf);
2135afeb14b4SPaul Moore 	audit_log_format(audit_buf, " res=%u", result);
2136ab5f5e8bSJoy Latten 	audit_log_end(audit_buf);
2137ab5f5e8bSJoy Latten }
2138ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2139afeb14b4SPaul Moore 
2140afeb14b4SPaul Moore void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2141afeb14b4SPaul Moore 				      struct sk_buff *skb)
2142afeb14b4SPaul Moore {
2143afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2144afeb14b4SPaul Moore 	u32 spi;
2145afeb14b4SPaul Moore 
2146afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-replay-overflow");
2147afeb14b4SPaul Moore 	if (audit_buf == NULL)
2148afeb14b4SPaul Moore 		return;
2149afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2150afeb14b4SPaul Moore 	/* don't record the sequence number because it's inherent in this kind
2151afeb14b4SPaul Moore 	 * of audit message */
2152afeb14b4SPaul Moore 	spi = ntohl(x->id.spi);
2153afeb14b4SPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2154afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2155afeb14b4SPaul Moore }
2156afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2157afeb14b4SPaul Moore 
21589fdc4883SSteffen Klassert void xfrm_audit_state_replay(struct xfrm_state *x,
2159afeb14b4SPaul Moore 			     struct sk_buff *skb, __be32 net_seq)
2160afeb14b4SPaul Moore {
2161afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2162afeb14b4SPaul Moore 	u32 spi;
2163afeb14b4SPaul Moore 
2164afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-replayed-pkt");
2165afeb14b4SPaul Moore 	if (audit_buf == NULL)
2166afeb14b4SPaul Moore 		return;
2167afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2168afeb14b4SPaul Moore 	spi = ntohl(x->id.spi);
2169afeb14b4SPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2170afeb14b4SPaul Moore 			 spi, spi, ntohl(net_seq));
2171afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2172afeb14b4SPaul Moore }
21739fdc4883SSteffen Klassert EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2174afeb14b4SPaul Moore 
2175afeb14b4SPaul Moore void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2176afeb14b4SPaul Moore {
2177afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2178afeb14b4SPaul Moore 
2179afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-notfound");
2180afeb14b4SPaul Moore 	if (audit_buf == NULL)
2181afeb14b4SPaul Moore 		return;
2182afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2183afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2184afeb14b4SPaul Moore }
2185afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2186afeb14b4SPaul Moore 
2187afeb14b4SPaul Moore void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2188afeb14b4SPaul Moore 			       __be32 net_spi, __be32 net_seq)
2189afeb14b4SPaul Moore {
2190afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2191afeb14b4SPaul Moore 	u32 spi;
2192afeb14b4SPaul Moore 
2193afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-notfound");
2194afeb14b4SPaul Moore 	if (audit_buf == NULL)
2195afeb14b4SPaul Moore 		return;
2196afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2197afeb14b4SPaul Moore 	spi = ntohl(net_spi);
2198afeb14b4SPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2199afeb14b4SPaul Moore 			 spi, spi, ntohl(net_seq));
2200afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2201afeb14b4SPaul Moore }
2202afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2203afeb14b4SPaul Moore 
2204afeb14b4SPaul Moore void xfrm_audit_state_icvfail(struct xfrm_state *x,
2205afeb14b4SPaul Moore 			      struct sk_buff *skb, u8 proto)
2206afeb14b4SPaul Moore {
2207afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2208afeb14b4SPaul Moore 	__be32 net_spi;
2209afeb14b4SPaul Moore 	__be32 net_seq;
2210afeb14b4SPaul Moore 
2211afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-icv-failure");
2212afeb14b4SPaul Moore 	if (audit_buf == NULL)
2213afeb14b4SPaul Moore 		return;
2214afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2215afeb14b4SPaul Moore 	if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2216afeb14b4SPaul Moore 		u32 spi = ntohl(net_spi);
2217afeb14b4SPaul Moore 		audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2218afeb14b4SPaul Moore 				 spi, spi, ntohl(net_seq));
2219afeb14b4SPaul Moore 	}
2220afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2221afeb14b4SPaul Moore }
2222afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2223ab5f5e8bSJoy Latten #endif /* CONFIG_AUDITSYSCALL */
2224