xref: /openbmc/linux/net/xfrm/xfrm_state.c (revision 776e9dd9)
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 
38f034b5d4SDavid S. Miller static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
391da177e4SLinus Torvalds 
4064d0cd00SAlexey Dobriyan static inline unsigned int xfrm_dst_hash(struct net *net,
412ab38503SDavid S. Miller 					 const xfrm_address_t *daddr,
422ab38503SDavid S. Miller 					 const xfrm_address_t *saddr,
43c1969f29SDavid S. Miller 					 u32 reqid,
44a624c108SDavid S. Miller 					 unsigned short family)
45a624c108SDavid S. Miller {
4664d0cd00SAlexey Dobriyan 	return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
47a624c108SDavid S. Miller }
48a624c108SDavid S. Miller 
4964d0cd00SAlexey Dobriyan static inline unsigned int xfrm_src_hash(struct net *net,
502ab38503SDavid S. Miller 					 const xfrm_address_t *daddr,
512ab38503SDavid S. Miller 					 const xfrm_address_t *saddr,
5244e36b42SDavid S. Miller 					 unsigned short family)
53f034b5d4SDavid S. Miller {
5464d0cd00SAlexey Dobriyan 	return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
55f034b5d4SDavid S. Miller }
56f034b5d4SDavid S. Miller 
572575b654SDavid S. Miller static inline unsigned int
582ab38503SDavid S. Miller xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
592ab38503SDavid S. Miller 	      __be32 spi, u8 proto, unsigned short family)
60f034b5d4SDavid S. Miller {
6164d0cd00SAlexey Dobriyan 	return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
62f034b5d4SDavid S. Miller }
63f034b5d4SDavid S. Miller 
64f034b5d4SDavid S. Miller static void xfrm_hash_transfer(struct hlist_head *list,
65f034b5d4SDavid S. Miller 			       struct hlist_head *ndsttable,
66f034b5d4SDavid S. Miller 			       struct hlist_head *nsrctable,
67f034b5d4SDavid S. Miller 			       struct hlist_head *nspitable,
68f034b5d4SDavid S. Miller 			       unsigned int nhashmask)
69f034b5d4SDavid S. Miller {
70b67bfe0dSSasha Levin 	struct hlist_node *tmp;
71f034b5d4SDavid S. Miller 	struct xfrm_state *x;
72f034b5d4SDavid S. Miller 
73b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(x, tmp, list, bydst) {
74f034b5d4SDavid S. Miller 		unsigned int h;
75f034b5d4SDavid S. Miller 
76c1969f29SDavid S. Miller 		h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
77c1969f29SDavid S. Miller 				    x->props.reqid, x->props.family,
78c1969f29SDavid S. Miller 				    nhashmask);
79f034b5d4SDavid S. Miller 		hlist_add_head(&x->bydst, ndsttable+h);
80f034b5d4SDavid S. Miller 
81667bbcb6SMasahide NAKAMURA 		h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
82667bbcb6SMasahide NAKAMURA 				    x->props.family,
83f034b5d4SDavid S. Miller 				    nhashmask);
84f034b5d4SDavid S. Miller 		hlist_add_head(&x->bysrc, nsrctable+h);
85f034b5d4SDavid S. Miller 
867b4dc360SMasahide NAKAMURA 		if (x->id.spi) {
877b4dc360SMasahide NAKAMURA 			h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
887b4dc360SMasahide NAKAMURA 					    x->id.proto, x->props.family,
897b4dc360SMasahide NAKAMURA 					    nhashmask);
90f034b5d4SDavid S. Miller 			hlist_add_head(&x->byspi, nspitable+h);
91f034b5d4SDavid S. Miller 		}
92f034b5d4SDavid S. Miller 	}
937b4dc360SMasahide NAKAMURA }
94f034b5d4SDavid S. Miller 
9563082733SAlexey Dobriyan static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
96f034b5d4SDavid S. Miller {
9763082733SAlexey Dobriyan 	return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
98f034b5d4SDavid S. Miller }
99f034b5d4SDavid S. Miller 
100f034b5d4SDavid S. Miller static DEFINE_MUTEX(hash_resize_mutex);
101f034b5d4SDavid S. Miller 
10263082733SAlexey Dobriyan static void xfrm_hash_resize(struct work_struct *work)
103f034b5d4SDavid S. Miller {
10463082733SAlexey Dobriyan 	struct net *net = container_of(work, struct net, xfrm.state_hash_work);
105f034b5d4SDavid S. Miller 	struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
106f034b5d4SDavid S. Miller 	unsigned long nsize, osize;
107f034b5d4SDavid S. Miller 	unsigned int nhashmask, ohashmask;
108f034b5d4SDavid S. Miller 	int i;
109f034b5d4SDavid S. Miller 
110f034b5d4SDavid S. Miller 	mutex_lock(&hash_resize_mutex);
111f034b5d4SDavid S. Miller 
11263082733SAlexey Dobriyan 	nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
11344e36b42SDavid S. Miller 	ndst = xfrm_hash_alloc(nsize);
114f034b5d4SDavid S. Miller 	if (!ndst)
115f034b5d4SDavid S. Miller 		goto out_unlock;
11644e36b42SDavid S. Miller 	nsrc = xfrm_hash_alloc(nsize);
117f034b5d4SDavid S. Miller 	if (!nsrc) {
11844e36b42SDavid S. Miller 		xfrm_hash_free(ndst, nsize);
119f034b5d4SDavid S. Miller 		goto out_unlock;
120f034b5d4SDavid S. Miller 	}
12144e36b42SDavid S. Miller 	nspi = xfrm_hash_alloc(nsize);
122f034b5d4SDavid S. Miller 	if (!nspi) {
12344e36b42SDavid S. Miller 		xfrm_hash_free(ndst, nsize);
12444e36b42SDavid S. Miller 		xfrm_hash_free(nsrc, nsize);
125f034b5d4SDavid S. Miller 		goto out_unlock;
126f034b5d4SDavid S. Miller 	}
127f034b5d4SDavid S. Miller 
128283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
129f034b5d4SDavid S. Miller 
130f034b5d4SDavid S. Miller 	nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
13163082733SAlexey Dobriyan 	for (i = net->xfrm.state_hmask; i >= 0; i--)
13263082733SAlexey Dobriyan 		xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
133f034b5d4SDavid S. Miller 				   nhashmask);
134f034b5d4SDavid S. Miller 
13563082733SAlexey Dobriyan 	odst = net->xfrm.state_bydst;
13663082733SAlexey Dobriyan 	osrc = net->xfrm.state_bysrc;
13763082733SAlexey Dobriyan 	ospi = net->xfrm.state_byspi;
13863082733SAlexey Dobriyan 	ohashmask = net->xfrm.state_hmask;
139f034b5d4SDavid S. Miller 
14063082733SAlexey Dobriyan 	net->xfrm.state_bydst = ndst;
14163082733SAlexey Dobriyan 	net->xfrm.state_bysrc = nsrc;
14263082733SAlexey Dobriyan 	net->xfrm.state_byspi = nspi;
14363082733SAlexey Dobriyan 	net->xfrm.state_hmask = nhashmask;
144f034b5d4SDavid S. Miller 
145283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
146f034b5d4SDavid S. Miller 
147f034b5d4SDavid S. Miller 	osize = (ohashmask + 1) * sizeof(struct hlist_head);
14844e36b42SDavid S. Miller 	xfrm_hash_free(odst, osize);
14944e36b42SDavid S. Miller 	xfrm_hash_free(osrc, osize);
15044e36b42SDavid S. Miller 	xfrm_hash_free(ospi, osize);
151f034b5d4SDavid S. Miller 
152f034b5d4SDavid S. Miller out_unlock:
153f034b5d4SDavid S. Miller 	mutex_unlock(&hash_resize_mutex);
154f034b5d4SDavid S. Miller }
155f034b5d4SDavid S. Miller 
15644abdc30SCong Wang static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
15744abdc30SCong Wang static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static DEFINE_SPINLOCK(xfrm_state_gc_lock);
1601da177e4SLinus Torvalds 
16153bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x);
1621da177e4SLinus Torvalds 
163980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
16415e47304SEric W. Biederman void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
1651da177e4SLinus Torvalds 
1667a9885b9SCong Wang static DEFINE_SPINLOCK(xfrm_type_lock);
167533cb5b0SEric Dumazet int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
168aa5d62ccSHerbert Xu {
1697a9885b9SCong Wang 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
170533cb5b0SEric Dumazet 	const struct xfrm_type **typemap;
171aa5d62ccSHerbert Xu 	int err = 0;
172aa5d62ccSHerbert Xu 
173aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
174aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
175aa5d62ccSHerbert Xu 	typemap = afinfo->type_map;
1767a9885b9SCong Wang 	spin_lock_bh(&xfrm_type_lock);
177aa5d62ccSHerbert Xu 
178aa5d62ccSHerbert Xu 	if (likely(typemap[type->proto] == NULL))
179aa5d62ccSHerbert Xu 		typemap[type->proto] = type;
180aa5d62ccSHerbert Xu 	else
181aa5d62ccSHerbert Xu 		err = -EEXIST;
1827a9885b9SCong Wang 	spin_unlock_bh(&xfrm_type_lock);
1837a9885b9SCong Wang 	xfrm_state_put_afinfo(afinfo);
184aa5d62ccSHerbert Xu 	return err;
185aa5d62ccSHerbert Xu }
186aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_type);
187aa5d62ccSHerbert Xu 
188533cb5b0SEric Dumazet int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
189aa5d62ccSHerbert Xu {
1907a9885b9SCong Wang 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
191533cb5b0SEric Dumazet 	const struct xfrm_type **typemap;
192aa5d62ccSHerbert Xu 	int err = 0;
193aa5d62ccSHerbert Xu 
194aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
195aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
196aa5d62ccSHerbert Xu 	typemap = afinfo->type_map;
1977a9885b9SCong Wang 	spin_lock_bh(&xfrm_type_lock);
198aa5d62ccSHerbert Xu 
199aa5d62ccSHerbert Xu 	if (unlikely(typemap[type->proto] != type))
200aa5d62ccSHerbert Xu 		err = -ENOENT;
201aa5d62ccSHerbert Xu 	else
202aa5d62ccSHerbert Xu 		typemap[type->proto] = NULL;
2037a9885b9SCong Wang 	spin_unlock_bh(&xfrm_type_lock);
2047a9885b9SCong Wang 	xfrm_state_put_afinfo(afinfo);
205aa5d62ccSHerbert Xu 	return err;
206aa5d62ccSHerbert Xu }
207aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_type);
208aa5d62ccSHerbert Xu 
209533cb5b0SEric Dumazet static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
210aa5d62ccSHerbert Xu {
211aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
212533cb5b0SEric Dumazet 	const struct xfrm_type **typemap;
213533cb5b0SEric Dumazet 	const struct xfrm_type *type;
214aa5d62ccSHerbert Xu 	int modload_attempted = 0;
215aa5d62ccSHerbert Xu 
216aa5d62ccSHerbert Xu retry:
217aa5d62ccSHerbert Xu 	afinfo = xfrm_state_get_afinfo(family);
218aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
219aa5d62ccSHerbert Xu 		return NULL;
220aa5d62ccSHerbert Xu 	typemap = afinfo->type_map;
221aa5d62ccSHerbert Xu 
222aa5d62ccSHerbert Xu 	type = typemap[proto];
223aa5d62ccSHerbert Xu 	if (unlikely(type && !try_module_get(type->owner)))
224aa5d62ccSHerbert Xu 		type = NULL;
225aa5d62ccSHerbert Xu 	if (!type && !modload_attempted) {
226aa5d62ccSHerbert Xu 		xfrm_state_put_afinfo(afinfo);
227aa5d62ccSHerbert Xu 		request_module("xfrm-type-%d-%d", family, proto);
228aa5d62ccSHerbert Xu 		modload_attempted = 1;
229aa5d62ccSHerbert Xu 		goto retry;
230aa5d62ccSHerbert Xu 	}
231aa5d62ccSHerbert Xu 
232aa5d62ccSHerbert Xu 	xfrm_state_put_afinfo(afinfo);
233aa5d62ccSHerbert Xu 	return type;
234aa5d62ccSHerbert Xu }
235aa5d62ccSHerbert Xu 
236533cb5b0SEric Dumazet static void xfrm_put_type(const struct xfrm_type *type)
237aa5d62ccSHerbert Xu {
238aa5d62ccSHerbert Xu 	module_put(type->owner);
239aa5d62ccSHerbert Xu }
240aa5d62ccSHerbert Xu 
2417a9885b9SCong Wang static DEFINE_SPINLOCK(xfrm_mode_lock);
242aa5d62ccSHerbert Xu int xfrm_register_mode(struct xfrm_mode *mode, int family)
243aa5d62ccSHerbert Xu {
244aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
245aa5d62ccSHerbert Xu 	struct xfrm_mode **modemap;
246aa5d62ccSHerbert Xu 	int err;
247aa5d62ccSHerbert Xu 
248aa5d62ccSHerbert Xu 	if (unlikely(mode->encap >= XFRM_MODE_MAX))
249aa5d62ccSHerbert Xu 		return -EINVAL;
250aa5d62ccSHerbert Xu 
2517a9885b9SCong Wang 	afinfo = xfrm_state_get_afinfo(family);
252aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
253aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
254aa5d62ccSHerbert Xu 
255aa5d62ccSHerbert Xu 	err = -EEXIST;
256aa5d62ccSHerbert Xu 	modemap = afinfo->mode_map;
2577a9885b9SCong Wang 	spin_lock_bh(&xfrm_mode_lock);
25817c2a42aSHerbert Xu 	if (modemap[mode->encap])
25917c2a42aSHerbert Xu 		goto out;
26017c2a42aSHerbert Xu 
26117c2a42aSHerbert Xu 	err = -ENOENT;
26217c2a42aSHerbert Xu 	if (!try_module_get(afinfo->owner))
26317c2a42aSHerbert Xu 		goto out;
26417c2a42aSHerbert Xu 
26517c2a42aSHerbert Xu 	mode->afinfo = afinfo;
266aa5d62ccSHerbert Xu 	modemap[mode->encap] = mode;
267aa5d62ccSHerbert Xu 	err = 0;
268aa5d62ccSHerbert Xu 
26917c2a42aSHerbert Xu out:
2707a9885b9SCong Wang 	spin_unlock_bh(&xfrm_mode_lock);
2717a9885b9SCong Wang 	xfrm_state_put_afinfo(afinfo);
272aa5d62ccSHerbert Xu 	return err;
273aa5d62ccSHerbert Xu }
274aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_register_mode);
275aa5d62ccSHerbert Xu 
276aa5d62ccSHerbert Xu int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
277aa5d62ccSHerbert Xu {
278aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
279aa5d62ccSHerbert Xu 	struct xfrm_mode **modemap;
280aa5d62ccSHerbert Xu 	int err;
281aa5d62ccSHerbert Xu 
282aa5d62ccSHerbert Xu 	if (unlikely(mode->encap >= XFRM_MODE_MAX))
283aa5d62ccSHerbert Xu 		return -EINVAL;
284aa5d62ccSHerbert Xu 
2857a9885b9SCong Wang 	afinfo = xfrm_state_get_afinfo(family);
286aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
287aa5d62ccSHerbert Xu 		return -EAFNOSUPPORT;
288aa5d62ccSHerbert Xu 
289aa5d62ccSHerbert Xu 	err = -ENOENT;
290aa5d62ccSHerbert Xu 	modemap = afinfo->mode_map;
2917a9885b9SCong Wang 	spin_lock_bh(&xfrm_mode_lock);
292aa5d62ccSHerbert Xu 	if (likely(modemap[mode->encap] == mode)) {
293aa5d62ccSHerbert Xu 		modemap[mode->encap] = NULL;
29417c2a42aSHerbert Xu 		module_put(mode->afinfo->owner);
295aa5d62ccSHerbert Xu 		err = 0;
296aa5d62ccSHerbert Xu 	}
297aa5d62ccSHerbert Xu 
2987a9885b9SCong Wang 	spin_unlock_bh(&xfrm_mode_lock);
2997a9885b9SCong Wang 	xfrm_state_put_afinfo(afinfo);
300aa5d62ccSHerbert Xu 	return err;
301aa5d62ccSHerbert Xu }
302aa5d62ccSHerbert Xu EXPORT_SYMBOL(xfrm_unregister_mode);
303aa5d62ccSHerbert Xu 
304aa5d62ccSHerbert Xu static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
305aa5d62ccSHerbert Xu {
306aa5d62ccSHerbert Xu 	struct xfrm_state_afinfo *afinfo;
307aa5d62ccSHerbert Xu 	struct xfrm_mode *mode;
308aa5d62ccSHerbert Xu 	int modload_attempted = 0;
309aa5d62ccSHerbert Xu 
310aa5d62ccSHerbert Xu 	if (unlikely(encap >= XFRM_MODE_MAX))
311aa5d62ccSHerbert Xu 		return NULL;
312aa5d62ccSHerbert Xu 
313aa5d62ccSHerbert Xu retry:
314aa5d62ccSHerbert Xu 	afinfo = xfrm_state_get_afinfo(family);
315aa5d62ccSHerbert Xu 	if (unlikely(afinfo == NULL))
316aa5d62ccSHerbert Xu 		return NULL;
317aa5d62ccSHerbert Xu 
318aa5d62ccSHerbert Xu 	mode = afinfo->mode_map[encap];
319aa5d62ccSHerbert Xu 	if (unlikely(mode && !try_module_get(mode->owner)))
320aa5d62ccSHerbert Xu 		mode = NULL;
321aa5d62ccSHerbert Xu 	if (!mode && !modload_attempted) {
322aa5d62ccSHerbert Xu 		xfrm_state_put_afinfo(afinfo);
323aa5d62ccSHerbert Xu 		request_module("xfrm-mode-%d-%d", family, encap);
324aa5d62ccSHerbert Xu 		modload_attempted = 1;
325aa5d62ccSHerbert Xu 		goto retry;
326aa5d62ccSHerbert Xu 	}
327aa5d62ccSHerbert Xu 
328aa5d62ccSHerbert Xu 	xfrm_state_put_afinfo(afinfo);
329aa5d62ccSHerbert Xu 	return mode;
330aa5d62ccSHerbert Xu }
331aa5d62ccSHerbert Xu 
332aa5d62ccSHerbert Xu static void xfrm_put_mode(struct xfrm_mode *mode)
333aa5d62ccSHerbert Xu {
334aa5d62ccSHerbert Xu 	module_put(mode->owner);
335aa5d62ccSHerbert Xu }
336aa5d62ccSHerbert Xu 
3371da177e4SLinus Torvalds static void xfrm_state_gc_destroy(struct xfrm_state *x)
3381da177e4SLinus Torvalds {
3399e0d57fdSYury Polyanskiy 	tasklet_hrtimer_cancel(&x->mtimer);
340a47f0ce0SDavid S. Miller 	del_timer_sync(&x->rtimer);
3411da177e4SLinus Torvalds 	kfree(x->aalg);
3421da177e4SLinus Torvalds 	kfree(x->ealg);
3431da177e4SLinus Torvalds 	kfree(x->calg);
3441da177e4SLinus Torvalds 	kfree(x->encap);
345060f02a3SNoriaki TAKAMIYA 	kfree(x->coaddr);
346d8647b79SSteffen Klassert 	kfree(x->replay_esn);
347d8647b79SSteffen Klassert 	kfree(x->preplay_esn);
34813996378SHerbert Xu 	if (x->inner_mode)
34913996378SHerbert Xu 		xfrm_put_mode(x->inner_mode);
350df9dcb45SKazunori MIYAZAWA 	if (x->inner_mode_iaf)
351df9dcb45SKazunori MIYAZAWA 		xfrm_put_mode(x->inner_mode_iaf);
35213996378SHerbert Xu 	if (x->outer_mode)
35313996378SHerbert Xu 		xfrm_put_mode(x->outer_mode);
3541da177e4SLinus Torvalds 	if (x->type) {
3551da177e4SLinus Torvalds 		x->type->destructor(x);
3561da177e4SLinus Torvalds 		xfrm_put_type(x->type);
3571da177e4SLinus Torvalds 	}
358df71837dSTrent Jaeger 	security_xfrm_state_free(x);
3591da177e4SLinus Torvalds 	kfree(x);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362c7837144SAlexey Dobriyan static void xfrm_state_gc_task(struct work_struct *work)
3631da177e4SLinus Torvalds {
364c7837144SAlexey Dobriyan 	struct net *net = container_of(work, struct net, xfrm.state_gc_work);
36512a169e7SHerbert Xu 	struct xfrm_state *x;
366b67bfe0dSSasha Levin 	struct hlist_node *tmp;
36712a169e7SHerbert Xu 	struct hlist_head gc_list;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_gc_lock);
370c7837144SAlexey Dobriyan 	hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
3711da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_gc_lock);
3721da177e4SLinus Torvalds 
373b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
3741da177e4SLinus Torvalds 		xfrm_state_gc_destroy(x);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds static inline unsigned long make_jiffies(long secs)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
3801da177e4SLinus Torvalds 		return MAX_SCHEDULE_TIMEOUT-1;
3811da177e4SLinus Torvalds 	else
3821da177e4SLinus Torvalds 		return secs*HZ;
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3859e0d57fdSYury Polyanskiy static enum hrtimer_restart xfrm_timer_handler(struct hrtimer * me)
3861da177e4SLinus Torvalds {
3879e0d57fdSYury Polyanskiy 	struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
3889e0d57fdSYury Polyanskiy 	struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
3899d729f72SJames Morris 	unsigned long now = get_seconds();
3901da177e4SLinus Torvalds 	long next = LONG_MAX;
3911da177e4SLinus Torvalds 	int warn = 0;
392161a09e7SJoy Latten 	int err = 0;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	spin_lock(&x->lock);
3951da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_DEAD)
3961da177e4SLinus Torvalds 		goto out;
3971da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_EXPIRED)
3981da177e4SLinus Torvalds 		goto expired;
3991da177e4SLinus Torvalds 	if (x->lft.hard_add_expires_seconds) {
4001da177e4SLinus Torvalds 		long tmo = x->lft.hard_add_expires_seconds +
4011da177e4SLinus Torvalds 			x->curlft.add_time - now;
402e3c0d047SFan Du 		if (tmo <= 0) {
403e3c0d047SFan Du 			if (x->xflags & XFRM_SOFT_EXPIRE) {
404e3c0d047SFan Du 				/* enter hard expire without soft expire first?!
405e3c0d047SFan Du 				 * setting a new date could trigger this.
406e3c0d047SFan Du 				 * workarbound: fix x->curflt.add_time by below:
407e3c0d047SFan Du 				 */
408e3c0d047SFan Du 				x->curlft.add_time = now - x->saved_tmo - 1;
409e3c0d047SFan Du 				tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
410e3c0d047SFan Du 			} else
4111da177e4SLinus Torvalds 				goto expired;
412e3c0d047SFan Du 		}
4131da177e4SLinus Torvalds 		if (tmo < next)
4141da177e4SLinus Torvalds 			next = tmo;
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 	if (x->lft.hard_use_expires_seconds) {
4171da177e4SLinus Torvalds 		long tmo = x->lft.hard_use_expires_seconds +
4181da177e4SLinus Torvalds 			(x->curlft.use_time ? : now) - now;
4191da177e4SLinus Torvalds 		if (tmo <= 0)
4201da177e4SLinus Torvalds 			goto expired;
4211da177e4SLinus Torvalds 		if (tmo < next)
4221da177e4SLinus Torvalds 			next = tmo;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 	if (x->km.dying)
4251da177e4SLinus Torvalds 		goto resched;
4261da177e4SLinus Torvalds 	if (x->lft.soft_add_expires_seconds) {
4271da177e4SLinus Torvalds 		long tmo = x->lft.soft_add_expires_seconds +
4281da177e4SLinus Torvalds 			x->curlft.add_time - now;
429e3c0d047SFan Du 		if (tmo <= 0) {
4301da177e4SLinus Torvalds 			warn = 1;
431e3c0d047SFan Du 			x->xflags &= ~XFRM_SOFT_EXPIRE;
432e3c0d047SFan Du 		} else if (tmo < next) {
4331da177e4SLinus Torvalds 			next = tmo;
434e3c0d047SFan Du 			x->xflags |= XFRM_SOFT_EXPIRE;
435e3c0d047SFan Du 			x->saved_tmo = tmo;
436e3c0d047SFan Du 		}
4371da177e4SLinus Torvalds 	}
4381da177e4SLinus Torvalds 	if (x->lft.soft_use_expires_seconds) {
4391da177e4SLinus Torvalds 		long tmo = x->lft.soft_use_expires_seconds +
4401da177e4SLinus Torvalds 			(x->curlft.use_time ? : now) - now;
4411da177e4SLinus Torvalds 		if (tmo <= 0)
4421da177e4SLinus Torvalds 			warn = 1;
4431da177e4SLinus Torvalds 		else if (tmo < next)
4441da177e4SLinus Torvalds 			next = tmo;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4474666faabSHerbert Xu 	x->km.dying = warn;
4481da177e4SLinus Torvalds 	if (warn)
44953bc6b4dSJamal Hadi Salim 		km_state_expired(x, 0, 0);
4501da177e4SLinus Torvalds resched:
4519e0d57fdSYury Polyanskiy 	if (next != LONG_MAX){
4529e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
4539e0d57fdSYury Polyanskiy 	}
454a47f0ce0SDavid S. Miller 
4551da177e4SLinus Torvalds 	goto out;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds expired:
4585b8ef341SSteffen Klassert 	if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
4591da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_EXPIRED;
460161a09e7SJoy Latten 
461161a09e7SJoy Latten 	err = __xfrm_state_delete(x);
4620806ae4cSNicolas Dichtel 	if (!err)
46353bc6b4dSJamal Hadi Salim 		km_state_expired(x, 1, 0);
4641da177e4SLinus Torvalds 
465ab5f5e8bSJoy Latten 	xfrm_audit_state_delete(x, err ? 0 : 1,
4662532386fSEric Paris 				audit_get_loginuid(current),
4672532386fSEric Paris 				audit_get_sessionid(current), 0);
468161a09e7SJoy Latten 
4691da177e4SLinus Torvalds out:
4701da177e4SLinus Torvalds 	spin_unlock(&x->lock);
4719e0d57fdSYury Polyanskiy 	return HRTIMER_NORESTART;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4740ac84752SDavid S. Miller static void xfrm_replay_timer_handler(unsigned long data);
4750ac84752SDavid S. Miller 
476673c09beSAlexey Dobriyan struct xfrm_state *xfrm_state_alloc(struct net *net)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct xfrm_state *x;
4791da177e4SLinus Torvalds 
4800da974f4SPanagiotis Issaris 	x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	if (x) {
483673c09beSAlexey Dobriyan 		write_pnet(&x->xs_net, net);
4841da177e4SLinus Torvalds 		atomic_set(&x->refcnt, 1);
4851da177e4SLinus Torvalds 		atomic_set(&x->tunnel_users, 0);
48612a169e7SHerbert Xu 		INIT_LIST_HEAD(&x->km.all);
4878f126e37SDavid S. Miller 		INIT_HLIST_NODE(&x->bydst);
4888f126e37SDavid S. Miller 		INIT_HLIST_NODE(&x->bysrc);
4898f126e37SDavid S. Miller 		INIT_HLIST_NODE(&x->byspi);
49099565a6cSFan Du 		tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
49199565a6cSFan Du 					CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
492b24b8a24SPavel Emelyanov 		setup_timer(&x->rtimer, xfrm_replay_timer_handler,
493b24b8a24SPavel Emelyanov 				(unsigned long)x);
4949d729f72SJames Morris 		x->curlft.add_time = get_seconds();
4951da177e4SLinus Torvalds 		x->lft.soft_byte_limit = XFRM_INF;
4961da177e4SLinus Torvalds 		x->lft.soft_packet_limit = XFRM_INF;
4971da177e4SLinus Torvalds 		x->lft.hard_byte_limit = XFRM_INF;
4981da177e4SLinus Torvalds 		x->lft.hard_packet_limit = XFRM_INF;
499f8cd5488SJamal Hadi Salim 		x->replay_maxage = 0;
500f8cd5488SJamal Hadi Salim 		x->replay_maxdiff = 0;
501df9dcb45SKazunori MIYAZAWA 		x->inner_mode = NULL;
502df9dcb45SKazunori MIYAZAWA 		x->inner_mode_iaf = NULL;
5031da177e4SLinus Torvalds 		spin_lock_init(&x->lock);
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	return x;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_alloc);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds void __xfrm_state_destroy(struct xfrm_state *x)
5101da177e4SLinus Torvalds {
51198806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
51298806f75SAlexey Dobriyan 
513547b792cSIlpo Järvinen 	WARN_ON(x->km.state != XFRM_STATE_DEAD);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	spin_lock_bh(&xfrm_state_gc_lock);
51698806f75SAlexey Dobriyan 	hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
5171da177e4SLinus Torvalds 	spin_unlock_bh(&xfrm_state_gc_lock);
51898806f75SAlexey Dobriyan 	schedule_work(&net->xfrm.state_gc_work);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds EXPORT_SYMBOL(__xfrm_state_destroy);
5211da177e4SLinus Torvalds 
52253bc6b4dSJamal Hadi Salim int __xfrm_state_delete(struct xfrm_state *x)
5231da177e4SLinus Torvalds {
52498806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
52526b15dadSJamal Hadi Salim 	int err = -ESRCH;
52626b15dadSJamal Hadi Salim 
5271da177e4SLinus Torvalds 	if (x->km.state != XFRM_STATE_DEAD) {
5281da177e4SLinus Torvalds 		x->km.state = XFRM_STATE_DEAD;
529283bc9f3SFan Du 		spin_lock(&net->xfrm.xfrm_state_lock);
53012a169e7SHerbert Xu 		list_del(&x->km.all);
5318f126e37SDavid S. Miller 		hlist_del(&x->bydst);
5328f126e37SDavid S. Miller 		hlist_del(&x->bysrc);
533a47f0ce0SDavid S. Miller 		if (x->id.spi)
5348f126e37SDavid S. Miller 			hlist_del(&x->byspi);
53598806f75SAlexey Dobriyan 		net->xfrm.state_num--;
536283bc9f3SFan Du 		spin_unlock(&net->xfrm.xfrm_state_lock);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		/* All xfrm_state objects are created by xfrm_state_alloc.
5391da177e4SLinus Torvalds 		 * The xfrm_state_alloc call gives a reference, and that
5401da177e4SLinus Torvalds 		 * is what we are dropping here.
5411da177e4SLinus Torvalds 		 */
5425dba4797SPatrick McHardy 		xfrm_state_put(x);
54326b15dadSJamal Hadi Salim 		err = 0;
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 
54626b15dadSJamal Hadi Salim 	return err;
54726b15dadSJamal Hadi Salim }
54853bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(__xfrm_state_delete);
54926b15dadSJamal Hadi Salim 
55026b15dadSJamal Hadi Salim int xfrm_state_delete(struct xfrm_state *x)
5511da177e4SLinus Torvalds {
55226b15dadSJamal Hadi Salim 	int err;
55326b15dadSJamal Hadi Salim 
5541da177e4SLinus Torvalds 	spin_lock_bh(&x->lock);
55526b15dadSJamal Hadi Salim 	err = __xfrm_state_delete(x);
5561da177e4SLinus Torvalds 	spin_unlock_bh(&x->lock);
55726b15dadSJamal Hadi Salim 
55826b15dadSJamal Hadi Salim 	return err;
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete);
5611da177e4SLinus Torvalds 
5624aa2e62cSJoy Latten #ifdef CONFIG_SECURITY_NETWORK_XFRM
5634aa2e62cSJoy Latten static inline int
5640e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
5651da177e4SLinus Torvalds {
5664aa2e62cSJoy Latten 	int i, err = 0;
5674aa2e62cSJoy Latten 
5680e602451SAlexey Dobriyan 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
5694aa2e62cSJoy Latten 		struct xfrm_state *x;
5704aa2e62cSJoy Latten 
571b67bfe0dSSasha Levin 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
5724aa2e62cSJoy Latten 			if (xfrm_id_proto_match(x->id.proto, proto) &&
5734aa2e62cSJoy Latten 			   (err = security_xfrm_state_delete(x)) != 0) {
574ab5f5e8bSJoy Latten 				xfrm_audit_state_delete(x, 0,
575ab5f5e8bSJoy Latten 							audit_info->loginuid,
5762532386fSEric Paris 							audit_info->sessionid,
577ab5f5e8bSJoy Latten 							audit_info->secid);
5784aa2e62cSJoy Latten 				return err;
5794aa2e62cSJoy Latten 			}
5804aa2e62cSJoy Latten 		}
5814aa2e62cSJoy Latten 	}
5824aa2e62cSJoy Latten 
5834aa2e62cSJoy Latten 	return err;
5844aa2e62cSJoy Latten }
5854aa2e62cSJoy Latten #else
5864aa2e62cSJoy Latten static inline int
5870e602451SAlexey Dobriyan xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
5884aa2e62cSJoy Latten {
5894aa2e62cSJoy Latten 	return 0;
5904aa2e62cSJoy Latten }
5914aa2e62cSJoy Latten #endif
5924aa2e62cSJoy Latten 
5930e602451SAlexey Dobriyan int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
5944aa2e62cSJoy Latten {
5959e64cc95SJamal Hadi Salim 	int i, err = 0, cnt = 0;
5961da177e4SLinus Torvalds 
597283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
5980e602451SAlexey Dobriyan 	err = xfrm_state_flush_secctx_check(net, proto, audit_info);
5994aa2e62cSJoy Latten 	if (err)
6004aa2e62cSJoy Latten 		goto out;
6014aa2e62cSJoy Latten 
6029e64cc95SJamal Hadi Salim 	err = -ESRCH;
6030e602451SAlexey Dobriyan 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
6048f126e37SDavid S. Miller 		struct xfrm_state *x;
6051da177e4SLinus Torvalds restart:
606b67bfe0dSSasha Levin 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
6071da177e4SLinus Torvalds 			if (!xfrm_state_kern(x) &&
6085794708fSMasahide NAKAMURA 			    xfrm_id_proto_match(x->id.proto, proto)) {
6091da177e4SLinus Torvalds 				xfrm_state_hold(x);
610283bc9f3SFan Du 				spin_unlock_bh(&net->xfrm.xfrm_state_lock);
6111da177e4SLinus Torvalds 
612161a09e7SJoy Latten 				err = xfrm_state_delete(x);
613ab5f5e8bSJoy Latten 				xfrm_audit_state_delete(x, err ? 0 : 1,
614ab5f5e8bSJoy Latten 							audit_info->loginuid,
6152532386fSEric Paris 							audit_info->sessionid,
616ab5f5e8bSJoy Latten 							audit_info->secid);
6171da177e4SLinus Torvalds 				xfrm_state_put(x);
6189e64cc95SJamal Hadi Salim 				if (!err)
6199e64cc95SJamal Hadi Salim 					cnt++;
6201da177e4SLinus Torvalds 
621283bc9f3SFan Du 				spin_lock_bh(&net->xfrm.xfrm_state_lock);
6221da177e4SLinus Torvalds 				goto restart;
6231da177e4SLinus Torvalds 			}
6241da177e4SLinus Torvalds 		}
6251da177e4SLinus Torvalds 	}
6269e64cc95SJamal Hadi Salim 	if (cnt)
6274aa2e62cSJoy Latten 		err = 0;
6284aa2e62cSJoy Latten 
6294aa2e62cSJoy Latten out:
630283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
6314aa2e62cSJoy Latten 	return err;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_flush);
6341da177e4SLinus Torvalds 
635e071041bSAlexey Dobriyan void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
63628d8909bSJamal Hadi Salim {
637283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
638e071041bSAlexey Dobriyan 	si->sadcnt = net->xfrm.state_num;
639e071041bSAlexey Dobriyan 	si->sadhcnt = net->xfrm.state_hmask;
64028d8909bSJamal Hadi Salim 	si->sadhmcnt = xfrm_state_hashmax;
641283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
64228d8909bSJamal Hadi Salim }
64328d8909bSJamal Hadi Salim EXPORT_SYMBOL(xfrm_sad_getinfo);
64428d8909bSJamal Hadi Salim 
6451da177e4SLinus Torvalds static int
6461a898592SDavid S. Miller xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
64704686013SDavid S. Miller 		    const struct xfrm_tmpl *tmpl,
64833765d06SDavid S. Miller 		    const xfrm_address_t *daddr, const xfrm_address_t *saddr,
6491da177e4SLinus Torvalds 		    unsigned short family)
6501da177e4SLinus Torvalds {
6511da177e4SLinus Torvalds 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
6521da177e4SLinus Torvalds 	if (!afinfo)
6531da177e4SLinus Torvalds 		return -1;
6548444cf71SThomas Egerer 	afinfo->init_tempsel(&x->sel, fl);
6558444cf71SThomas Egerer 
6568444cf71SThomas Egerer 	if (family != tmpl->encap_family) {
6578444cf71SThomas Egerer 		xfrm_state_put_afinfo(afinfo);
6588444cf71SThomas Egerer 		afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
6598444cf71SThomas Egerer 		if (!afinfo)
6608444cf71SThomas Egerer 			return -1;
6618444cf71SThomas Egerer 	}
6628444cf71SThomas Egerer 	afinfo->init_temprop(x, tmpl, daddr, saddr);
6631da177e4SLinus Torvalds 	xfrm_state_put_afinfo(afinfo);
6641da177e4SLinus Torvalds 	return 0;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6679aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
6689aa60088SDavid S. Miller 					      const xfrm_address_t *daddr,
6699aa60088SDavid S. Miller 					      __be32 spi, u8 proto,
6709aa60088SDavid S. Miller 					      unsigned short family)
671edcd5821SDavid S. Miller {
672221df1edSAlexey Dobriyan 	unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
673edcd5821SDavid S. Miller 	struct xfrm_state *x;
674edcd5821SDavid S. Miller 
675b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_byspi+h, byspi) {
676edcd5821SDavid S. Miller 		if (x->props.family != family ||
677edcd5821SDavid S. Miller 		    x->id.spi       != spi ||
6781802571bSWei Yongjun 		    x->id.proto     != proto ||
67970e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    !xfrm_addr_equal(&x->id.daddr, daddr, family))
680edcd5821SDavid S. Miller 			continue;
681edcd5821SDavid S. Miller 
6823d6acfa7SJamal Hadi Salim 		if ((mark & x->mark.m) != x->mark.v)
6833d6acfa7SJamal Hadi Salim 			continue;
684edcd5821SDavid S. Miller 		xfrm_state_hold(x);
685edcd5821SDavid S. Miller 		return x;
686edcd5821SDavid S. Miller 	}
687edcd5821SDavid S. Miller 
688edcd5821SDavid S. Miller 	return NULL;
689edcd5821SDavid S. Miller }
690edcd5821SDavid S. Miller 
6919aa60088SDavid S. Miller static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
6929aa60088SDavid S. Miller 						     const xfrm_address_t *daddr,
6939aa60088SDavid S. Miller 						     const xfrm_address_t *saddr,
6949aa60088SDavid S. Miller 						     u8 proto, unsigned short family)
695edcd5821SDavid S. Miller {
696221df1edSAlexey Dobriyan 	unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
697edcd5821SDavid S. Miller 	struct xfrm_state *x;
698edcd5821SDavid S. Miller 
699b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
700edcd5821SDavid S. Miller 		if (x->props.family != family ||
7011802571bSWei Yongjun 		    x->id.proto     != proto ||
70270e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
70370e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    !xfrm_addr_equal(&x->props.saddr, saddr, family))
704edcd5821SDavid S. Miller 			continue;
705edcd5821SDavid S. Miller 
7063d6acfa7SJamal Hadi Salim 		if ((mark & x->mark.m) != x->mark.v)
7073d6acfa7SJamal Hadi Salim 			continue;
708edcd5821SDavid S. Miller 		xfrm_state_hold(x);
709edcd5821SDavid S. Miller 		return x;
710edcd5821SDavid S. Miller 	}
711edcd5821SDavid S. Miller 
712edcd5821SDavid S. Miller 	return NULL;
713edcd5821SDavid S. Miller }
714edcd5821SDavid S. Miller 
715edcd5821SDavid S. Miller static inline struct xfrm_state *
716edcd5821SDavid S. Miller __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
717edcd5821SDavid S. Miller {
718221df1edSAlexey Dobriyan 	struct net *net = xs_net(x);
719bd55775cSJamal Hadi Salim 	u32 mark = x->mark.v & x->mark.m;
720221df1edSAlexey Dobriyan 
721edcd5821SDavid S. Miller 	if (use_spi)
722bd55775cSJamal Hadi Salim 		return __xfrm_state_lookup(net, mark, &x->id.daddr,
723bd55775cSJamal Hadi Salim 					   x->id.spi, x->id.proto, family);
724edcd5821SDavid S. Miller 	else
725bd55775cSJamal Hadi Salim 		return __xfrm_state_lookup_byaddr(net, mark,
726bd55775cSJamal Hadi Salim 						  &x->id.daddr,
727edcd5821SDavid S. Miller 						  &x->props.saddr,
728edcd5821SDavid S. Miller 						  x->id.proto, family);
729edcd5821SDavid S. Miller }
730edcd5821SDavid S. Miller 
73198806f75SAlexey Dobriyan static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
7322fab22f2SPatrick McHardy {
7332fab22f2SPatrick McHardy 	if (have_hash_collision &&
73498806f75SAlexey Dobriyan 	    (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
73598806f75SAlexey Dobriyan 	    net->xfrm.state_num > net->xfrm.state_hmask)
73698806f75SAlexey Dobriyan 		schedule_work(&net->xfrm.state_hash_work);
7372fab22f2SPatrick McHardy }
7382fab22f2SPatrick McHardy 
73908ec9af1SDavid S. Miller static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
7404a08ab0fSDavid S. Miller 			       const struct flowi *fl, unsigned short family,
74108ec9af1SDavid S. Miller 			       struct xfrm_state **best, int *acq_in_progress,
74208ec9af1SDavid S. Miller 			       int *error)
74308ec9af1SDavid S. Miller {
74408ec9af1SDavid S. Miller 	/* Resolution logic:
74508ec9af1SDavid S. Miller 	 * 1. There is a valid state with matching selector. Done.
74608ec9af1SDavid S. Miller 	 * 2. Valid state with inappropriate selector. Skip.
74708ec9af1SDavid S. Miller 	 *
74808ec9af1SDavid S. Miller 	 * Entering area of "sysdeps".
74908ec9af1SDavid S. Miller 	 *
75008ec9af1SDavid S. Miller 	 * 3. If state is not valid, selector is temporary, it selects
75108ec9af1SDavid S. Miller 	 *    only session which triggered previous resolution. Key
75208ec9af1SDavid S. Miller 	 *    manager will do something to install a state with proper
75308ec9af1SDavid S. Miller 	 *    selector.
75408ec9af1SDavid S. Miller 	 */
75508ec9af1SDavid S. Miller 	if (x->km.state == XFRM_STATE_VALID) {
75608ec9af1SDavid S. Miller 		if ((x->sel.family &&
75708ec9af1SDavid S. Miller 		     !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
75808ec9af1SDavid S. Miller 		    !security_xfrm_state_pol_flow_match(x, pol, fl))
75908ec9af1SDavid S. Miller 			return;
76008ec9af1SDavid S. Miller 
76108ec9af1SDavid S. Miller 		if (!*best ||
76208ec9af1SDavid S. Miller 		    (*best)->km.dying > x->km.dying ||
76308ec9af1SDavid S. Miller 		    ((*best)->km.dying == x->km.dying &&
76408ec9af1SDavid S. Miller 		     (*best)->curlft.add_time < x->curlft.add_time))
76508ec9af1SDavid S. Miller 			*best = x;
76608ec9af1SDavid S. Miller 	} else if (x->km.state == XFRM_STATE_ACQ) {
76708ec9af1SDavid S. Miller 		*acq_in_progress = 1;
76808ec9af1SDavid S. Miller 	} else if (x->km.state == XFRM_STATE_ERROR ||
76908ec9af1SDavid S. Miller 		   x->km.state == XFRM_STATE_EXPIRED) {
77008ec9af1SDavid S. Miller 		if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
77108ec9af1SDavid S. Miller 		    security_xfrm_state_pol_flow_match(x, pol, fl))
77208ec9af1SDavid S. Miller 			*error = -ESRCH;
77308ec9af1SDavid S. Miller 	}
77408ec9af1SDavid S. Miller }
77508ec9af1SDavid S. Miller 
7761da177e4SLinus Torvalds struct xfrm_state *
77733765d06SDavid S. Miller xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
778b520e9f6SDavid S. Miller 		const struct flowi *fl, struct xfrm_tmpl *tmpl,
7791da177e4SLinus Torvalds 		struct xfrm_policy *pol, int *err,
7801da177e4SLinus Torvalds 		unsigned short family)
7811da177e4SLinus Torvalds {
78208ec9af1SDavid S. Miller 	static xfrm_address_t saddr_wildcard = { };
7835447c5e4SAlexey Dobriyan 	struct net *net = xp_net(pol);
7846a783c90SNicolas Dichtel 	unsigned int h, h_wildcard;
78537b08e34SDavid S. Miller 	struct xfrm_state *x, *x0, *to_put;
7861da177e4SLinus Torvalds 	int acquire_in_progress = 0;
7871da177e4SLinus Torvalds 	int error = 0;
7881da177e4SLinus Torvalds 	struct xfrm_state *best = NULL;
789bd55775cSJamal Hadi Salim 	u32 mark = pol->mark.v & pol->mark.m;
7908444cf71SThomas Egerer 	unsigned short encap_family = tmpl->encap_family;
7911da177e4SLinus Torvalds 
79237b08e34SDavid S. Miller 	to_put = NULL;
79337b08e34SDavid S. Miller 
794283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
7958444cf71SThomas Egerer 	h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
796b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
7978444cf71SThomas Egerer 		if (x->props.family == encap_family &&
7981da177e4SLinus Torvalds 		    x->props.reqid == tmpl->reqid &&
7993d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
800fbd9a5b4SMasahide NAKAMURA 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
8018444cf71SThomas Egerer 		    xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
8021da177e4SLinus Torvalds 		    tmpl->mode == x->props.mode &&
8031da177e4SLinus Torvalds 		    tmpl->id.proto == x->id.proto &&
80408ec9af1SDavid S. Miller 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
8051f673c5fSDavid S. Miller 			xfrm_state_look_at(pol, x, fl, encap_family,
80608ec9af1SDavid S. Miller 					   &best, &acquire_in_progress, &error);
8071da177e4SLinus Torvalds 	}
8086f115638SFan Du 	if (best || acquire_in_progress)
80908ec9af1SDavid S. Miller 		goto found;
81008ec9af1SDavid S. Miller 
8118444cf71SThomas Egerer 	h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
812b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_bydst+h_wildcard, bydst) {
8138444cf71SThomas Egerer 		if (x->props.family == encap_family &&
81408ec9af1SDavid S. Miller 		    x->props.reqid == tmpl->reqid &&
8153d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
81608ec9af1SDavid S. Miller 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
817f59bbdfaSFan Du 		    xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
81808ec9af1SDavid S. Miller 		    tmpl->mode == x->props.mode &&
81908ec9af1SDavid S. Miller 		    tmpl->id.proto == x->id.proto &&
82008ec9af1SDavid S. Miller 		    (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
8211f673c5fSDavid S. Miller 			xfrm_state_look_at(pol, x, fl, encap_family,
82208ec9af1SDavid S. Miller 					   &best, &acquire_in_progress, &error);
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 
82508ec9af1SDavid S. Miller found:
8261da177e4SLinus Torvalds 	x = best;
8271da177e4SLinus Torvalds 	if (!x && !error && !acquire_in_progress) {
8285c5d281aSPatrick McHardy 		if (tmpl->id.spi &&
829bd55775cSJamal Hadi Salim 		    (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
8308444cf71SThomas Egerer 					      tmpl->id.proto, encap_family)) != NULL) {
83137b08e34SDavid S. Miller 			to_put = x0;
8321da177e4SLinus Torvalds 			error = -EEXIST;
8331da177e4SLinus Torvalds 			goto out;
8341da177e4SLinus Torvalds 		}
8355447c5e4SAlexey Dobriyan 		x = xfrm_state_alloc(net);
8361da177e4SLinus Torvalds 		if (x == NULL) {
8371da177e4SLinus Torvalds 			error = -ENOMEM;
8381da177e4SLinus Torvalds 			goto out;
8391da177e4SLinus Torvalds 		}
8408444cf71SThomas Egerer 		/* Initialize temporary state matching only
8411da177e4SLinus Torvalds 		 * to current session. */
8428444cf71SThomas Egerer 		xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
843bd55775cSJamal Hadi Salim 		memcpy(&x->mark, &pol->mark, sizeof(x->mark));
8441da177e4SLinus Torvalds 
8451d28f42cSDavid S. Miller 		error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
846e0d1caa7SVenkat Yekkirala 		if (error) {
847e0d1caa7SVenkat Yekkirala 			x->km.state = XFRM_STATE_DEAD;
84837b08e34SDavid S. Miller 			to_put = x;
849e0d1caa7SVenkat Yekkirala 			x = NULL;
850e0d1caa7SVenkat Yekkirala 			goto out;
851e0d1caa7SVenkat Yekkirala 		}
852e0d1caa7SVenkat Yekkirala 
8531da177e4SLinus Torvalds 		if (km_query(x, tmpl, pol) == 0) {
8541da177e4SLinus Torvalds 			x->km.state = XFRM_STATE_ACQ;
8555447c5e4SAlexey Dobriyan 			list_add(&x->km.all, &net->xfrm.state_all);
8565447c5e4SAlexey Dobriyan 			hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
8578444cf71SThomas Egerer 			h = xfrm_src_hash(net, daddr, saddr, encap_family);
8585447c5e4SAlexey Dobriyan 			hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
8591da177e4SLinus Torvalds 			if (x->id.spi) {
8608444cf71SThomas Egerer 				h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
8615447c5e4SAlexey Dobriyan 				hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
8621da177e4SLinus Torvalds 			}
863b27aeadbSAlexey Dobriyan 			x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
8649e0d57fdSYury Polyanskiy 			tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
8655447c5e4SAlexey Dobriyan 			net->xfrm.state_num++;
8665447c5e4SAlexey Dobriyan 			xfrm_hash_grow_check(net, x->bydst.next != NULL);
8671da177e4SLinus Torvalds 		} else {
8681da177e4SLinus Torvalds 			x->km.state = XFRM_STATE_DEAD;
86937b08e34SDavid S. Miller 			to_put = x;
8701da177e4SLinus Torvalds 			x = NULL;
8711da177e4SLinus Torvalds 			error = -ESRCH;
8721da177e4SLinus Torvalds 		}
8731da177e4SLinus Torvalds 	}
8741da177e4SLinus Torvalds out:
8751da177e4SLinus Torvalds 	if (x)
8761da177e4SLinus Torvalds 		xfrm_state_hold(x);
8771da177e4SLinus Torvalds 	else
8781da177e4SLinus Torvalds 		*err = acquire_in_progress ? -EAGAIN : error;
879283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
88037b08e34SDavid S. Miller 	if (to_put)
88137b08e34SDavid S. Miller 		xfrm_state_put(to_put);
8821da177e4SLinus Torvalds 	return x;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
885628529b6SJamal Hadi Salim struct xfrm_state *
886bd55775cSJamal Hadi Salim xfrm_stateonly_find(struct net *net, u32 mark,
8875447c5e4SAlexey Dobriyan 		    xfrm_address_t *daddr, xfrm_address_t *saddr,
888628529b6SJamal Hadi Salim 		    unsigned short family, u8 mode, u8 proto, u32 reqid)
889628529b6SJamal Hadi Salim {
8904bda4f25SPavel Emelyanov 	unsigned int h;
891628529b6SJamal Hadi Salim 	struct xfrm_state *rx = NULL, *x = NULL;
892628529b6SJamal Hadi Salim 
893283bc9f3SFan Du 	spin_lock(&net->xfrm.xfrm_state_lock);
8945447c5e4SAlexey Dobriyan 	h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
895b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
896628529b6SJamal Hadi Salim 		if (x->props.family == family &&
897628529b6SJamal Hadi Salim 		    x->props.reqid == reqid &&
8983d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
899628529b6SJamal Hadi Salim 		    !(x->props.flags & XFRM_STATE_WILDRECV) &&
900628529b6SJamal Hadi Salim 		    xfrm_state_addr_check(x, daddr, saddr, family) &&
901628529b6SJamal Hadi Salim 		    mode == x->props.mode &&
902628529b6SJamal Hadi Salim 		    proto == x->id.proto &&
903628529b6SJamal Hadi Salim 		    x->km.state == XFRM_STATE_VALID) {
904628529b6SJamal Hadi Salim 			rx = x;
905628529b6SJamal Hadi Salim 			break;
906628529b6SJamal Hadi Salim 		}
907628529b6SJamal Hadi Salim 	}
908628529b6SJamal Hadi Salim 
909628529b6SJamal Hadi Salim 	if (rx)
910628529b6SJamal Hadi Salim 		xfrm_state_hold(rx);
911283bc9f3SFan Du 	spin_unlock(&net->xfrm.xfrm_state_lock);
912628529b6SJamal Hadi Salim 
913628529b6SJamal Hadi Salim 
914628529b6SJamal Hadi Salim 	return rx;
915628529b6SJamal Hadi Salim }
916628529b6SJamal Hadi Salim EXPORT_SYMBOL(xfrm_stateonly_find);
917628529b6SJamal Hadi Salim 
9181da177e4SLinus Torvalds static void __xfrm_state_insert(struct xfrm_state *x)
9191da177e4SLinus Torvalds {
92098806f75SAlexey Dobriyan 	struct net *net = xs_net(x);
921a624c108SDavid S. Miller 	unsigned int h;
9221da177e4SLinus Torvalds 
92398806f75SAlexey Dobriyan 	list_add(&x->km.all, &net->xfrm.state_all);
9244c563f76STimo Teras 
92598806f75SAlexey Dobriyan 	h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
926c1969f29SDavid S. Miller 			  x->props.reqid, x->props.family);
92798806f75SAlexey Dobriyan 	hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
9281da177e4SLinus Torvalds 
92998806f75SAlexey Dobriyan 	h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
93098806f75SAlexey Dobriyan 	hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
9316c44e6b7SMasahide NAKAMURA 
9327b4dc360SMasahide NAKAMURA 	if (x->id.spi) {
93398806f75SAlexey Dobriyan 		h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
9346c44e6b7SMasahide NAKAMURA 				  x->props.family);
9351da177e4SLinus Torvalds 
93698806f75SAlexey Dobriyan 		hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
9376c44e6b7SMasahide NAKAMURA 	}
9381da177e4SLinus Torvalds 
9399e0d57fdSYury Polyanskiy 	tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
940a47f0ce0SDavid S. Miller 	if (x->replay_maxage)
941a47f0ce0SDavid S. Miller 		mod_timer(&x->rtimer, jiffies + x->replay_maxage);
942f8cd5488SJamal Hadi Salim 
94398806f75SAlexey Dobriyan 	net->xfrm.state_num++;
944f034b5d4SDavid S. Miller 
94598806f75SAlexey Dobriyan 	xfrm_hash_grow_check(net, x->bydst.next != NULL);
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948283bc9f3SFan Du /* net->xfrm.xfrm_state_lock is held */
949c7f5ea3aSDavid S. Miller static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
950c7f5ea3aSDavid S. Miller {
95198806f75SAlexey Dobriyan 	struct net *net = xs_net(xnew);
952c7f5ea3aSDavid S. Miller 	unsigned short family = xnew->props.family;
953c7f5ea3aSDavid S. Miller 	u32 reqid = xnew->props.reqid;
954c7f5ea3aSDavid S. Miller 	struct xfrm_state *x;
955c7f5ea3aSDavid S. Miller 	unsigned int h;
9563d6acfa7SJamal Hadi Salim 	u32 mark = xnew->mark.v & xnew->mark.m;
957c7f5ea3aSDavid S. Miller 
95898806f75SAlexey Dobriyan 	h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
959b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
960c7f5ea3aSDavid S. Miller 		if (x->props.family	== family &&
961c7f5ea3aSDavid S. Miller 		    x->props.reqid	== reqid &&
9623d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) == x->mark.v &&
96370e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
96470e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
96534996cb9SHerbert Xu 			x->genid++;
966c7f5ea3aSDavid S. Miller 	}
967c7f5ea3aSDavid S. Miller }
968c7f5ea3aSDavid S. Miller 
9691da177e4SLinus Torvalds void xfrm_state_insert(struct xfrm_state *x)
9701da177e4SLinus Torvalds {
971283bc9f3SFan Du 	struct net *net = xs_net(x);
972283bc9f3SFan Du 
973283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
974c7f5ea3aSDavid S. Miller 	__xfrm_state_bump_genids(x);
9751da177e4SLinus Torvalds 	__xfrm_state_insert(x);
976283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_insert);
9791da177e4SLinus Torvalds 
980283bc9f3SFan Du /* net->xfrm.xfrm_state_lock is held */
981e473fcb4SMathias Krause static struct xfrm_state *__find_acq_core(struct net *net,
982e473fcb4SMathias Krause 					  const struct xfrm_mark *m,
983a70486f0SDavid S. Miller 					  unsigned short family, u8 mode,
984a70486f0SDavid S. Miller 					  u32 reqid, u8 proto,
985a70486f0SDavid S. Miller 					  const xfrm_address_t *daddr,
986e473fcb4SMathias Krause 					  const xfrm_address_t *saddr,
987e473fcb4SMathias Krause 					  int create)
9882770834cSDavid S. Miller {
9895447c5e4SAlexey Dobriyan 	unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
9902770834cSDavid S. Miller 	struct xfrm_state *x;
9913d6acfa7SJamal Hadi Salim 	u32 mark = m->v & m->m;
9922770834cSDavid S. Miller 
993b67bfe0dSSasha Levin 	hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
9942770834cSDavid S. Miller 		if (x->props.reqid  != reqid ||
9952770834cSDavid S. Miller 		    x->props.mode   != mode ||
9962770834cSDavid S. Miller 		    x->props.family != family ||
9972770834cSDavid S. Miller 		    x->km.state     != XFRM_STATE_ACQ ||
99875e252d9SJoy Latten 		    x->id.spi       != 0 ||
9991802571bSWei Yongjun 		    x->id.proto	    != proto ||
10003d6acfa7SJamal Hadi Salim 		    (mark & x->mark.m) != x->mark.v ||
100170e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
100270e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    !xfrm_addr_equal(&x->props.saddr, saddr, family))
10032770834cSDavid S. Miller 			continue;
10042770834cSDavid S. Miller 
10052770834cSDavid S. Miller 		xfrm_state_hold(x);
10062770834cSDavid S. Miller 		return x;
10072770834cSDavid S. Miller 	}
10082770834cSDavid S. Miller 
10092770834cSDavid S. Miller 	if (!create)
10102770834cSDavid S. Miller 		return NULL;
10112770834cSDavid S. Miller 
10125447c5e4SAlexey Dobriyan 	x = xfrm_state_alloc(net);
10132770834cSDavid S. Miller 	if (likely(x)) {
10142770834cSDavid S. Miller 		switch (family) {
10152770834cSDavid S. Miller 		case AF_INET:
10162770834cSDavid S. Miller 			x->sel.daddr.a4 = daddr->a4;
10172770834cSDavid S. Miller 			x->sel.saddr.a4 = saddr->a4;
10182770834cSDavid S. Miller 			x->sel.prefixlen_d = 32;
10192770834cSDavid S. Miller 			x->sel.prefixlen_s = 32;
10202770834cSDavid S. Miller 			x->props.saddr.a4 = saddr->a4;
10212770834cSDavid S. Miller 			x->id.daddr.a4 = daddr->a4;
10222770834cSDavid S. Miller 			break;
10232770834cSDavid S. Miller 
10242770834cSDavid S. Miller 		case AF_INET6:
10254e3fd7a0SAlexey Dobriyan 			*(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr;
10264e3fd7a0SAlexey Dobriyan 			*(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr;
10272770834cSDavid S. Miller 			x->sel.prefixlen_d = 128;
10282770834cSDavid S. Miller 			x->sel.prefixlen_s = 128;
10294e3fd7a0SAlexey Dobriyan 			*(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr;
10304e3fd7a0SAlexey Dobriyan 			*(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr;
10312770834cSDavid S. Miller 			break;
10323ff50b79SStephen Hemminger 		}
10332770834cSDavid S. Miller 
10342770834cSDavid S. Miller 		x->km.state = XFRM_STATE_ACQ;
10352770834cSDavid S. Miller 		x->id.proto = proto;
10362770834cSDavid S. Miller 		x->props.family = family;
10372770834cSDavid S. Miller 		x->props.mode = mode;
10382770834cSDavid S. Miller 		x->props.reqid = reqid;
1039bd55775cSJamal Hadi Salim 		x->mark.v = m->v;
1040bd55775cSJamal Hadi Salim 		x->mark.m = m->m;
1041b27aeadbSAlexey Dobriyan 		x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
10422770834cSDavid S. Miller 		xfrm_state_hold(x);
10439e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
10445447c5e4SAlexey Dobriyan 		list_add(&x->km.all, &net->xfrm.state_all);
10455447c5e4SAlexey Dobriyan 		hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
10465447c5e4SAlexey Dobriyan 		h = xfrm_src_hash(net, daddr, saddr, family);
10475447c5e4SAlexey Dobriyan 		hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
1048918049f0SDavid S. Miller 
10495447c5e4SAlexey Dobriyan 		net->xfrm.state_num++;
1050918049f0SDavid S. Miller 
10515447c5e4SAlexey Dobriyan 		xfrm_hash_grow_check(net, x->bydst.next != NULL);
10522770834cSDavid S. Miller 	}
10532770834cSDavid S. Miller 
10542770834cSDavid S. Miller 	return x;
10552770834cSDavid S. Miller }
10562770834cSDavid S. Miller 
1057bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds int xfrm_state_add(struct xfrm_state *x)
10601da177e4SLinus Torvalds {
10615447c5e4SAlexey Dobriyan 	struct net *net = xs_net(x);
106237b08e34SDavid S. Miller 	struct xfrm_state *x1, *to_put;
10631da177e4SLinus Torvalds 	int family;
10641da177e4SLinus Torvalds 	int err;
1065bd55775cSJamal Hadi Salim 	u32 mark = x->mark.v & x->mark.m;
1066eb2971b6SMasahide NAKAMURA 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	family = x->props.family;
10691da177e4SLinus Torvalds 
107037b08e34SDavid S. Miller 	to_put = NULL;
107137b08e34SDavid S. Miller 
1072283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
10731da177e4SLinus Torvalds 
1074edcd5821SDavid S. Miller 	x1 = __xfrm_state_locate(x, use_spi, family);
10751da177e4SLinus Torvalds 	if (x1) {
107637b08e34SDavid S. Miller 		to_put = x1;
10771da177e4SLinus Torvalds 		x1 = NULL;
10781da177e4SLinus Torvalds 		err = -EEXIST;
10791da177e4SLinus Torvalds 		goto out;
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
1082eb2971b6SMasahide NAKAMURA 	if (use_spi && x->km.seq) {
1083bd55775cSJamal Hadi Salim 		x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
108475e252d9SJoy Latten 		if (x1 && ((x1->id.proto != x->id.proto) ||
108570e94e66SYOSHIFUJI Hideaki / 吉藤英明 		    !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
108637b08e34SDavid S. Miller 			to_put = x1;
10871da177e4SLinus Torvalds 			x1 = NULL;
10881da177e4SLinus Torvalds 		}
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 
1091eb2971b6SMasahide NAKAMURA 	if (use_spi && !x1)
1092bd55775cSJamal Hadi Salim 		x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1093bd55775cSJamal Hadi Salim 				     x->props.reqid, x->id.proto,
10941da177e4SLinus Torvalds 				     &x->id.daddr, &x->props.saddr, 0);
10951da177e4SLinus Torvalds 
1096c7f5ea3aSDavid S. Miller 	__xfrm_state_bump_genids(x);
10971da177e4SLinus Torvalds 	__xfrm_state_insert(x);
10981da177e4SLinus Torvalds 	err = 0;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds out:
1101283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	if (x1) {
11041da177e4SLinus Torvalds 		xfrm_state_delete(x1);
11051da177e4SLinus Torvalds 		xfrm_state_put(x1);
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
110837b08e34SDavid S. Miller 	if (to_put)
110937b08e34SDavid S. Miller 		xfrm_state_put(to_put);
111037b08e34SDavid S. Miller 
11111da177e4SLinus Torvalds 	return err;
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_add);
11141da177e4SLinus Torvalds 
111580c9abaaSShinta Sugimoto #ifdef CONFIG_XFRM_MIGRATE
11166666351dSEric Dumazet static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
111780c9abaaSShinta Sugimoto {
111898806f75SAlexey Dobriyan 	struct net *net = xs_net(orig);
111980c9abaaSShinta Sugimoto 	int err = -ENOMEM;
112098806f75SAlexey Dobriyan 	struct xfrm_state *x = xfrm_state_alloc(net);
112180c9abaaSShinta Sugimoto 	if (!x)
1122553f9118SHerbert Xu 		goto out;
112380c9abaaSShinta Sugimoto 
112480c9abaaSShinta Sugimoto 	memcpy(&x->id, &orig->id, sizeof(x->id));
112580c9abaaSShinta Sugimoto 	memcpy(&x->sel, &orig->sel, sizeof(x->sel));
112680c9abaaSShinta Sugimoto 	memcpy(&x->lft, &orig->lft, sizeof(x->lft));
112780c9abaaSShinta Sugimoto 	x->props.mode = orig->props.mode;
112880c9abaaSShinta Sugimoto 	x->props.replay_window = orig->props.replay_window;
112980c9abaaSShinta Sugimoto 	x->props.reqid = orig->props.reqid;
113080c9abaaSShinta Sugimoto 	x->props.family = orig->props.family;
113180c9abaaSShinta Sugimoto 	x->props.saddr = orig->props.saddr;
113280c9abaaSShinta Sugimoto 
113380c9abaaSShinta Sugimoto 	if (orig->aalg) {
11344447bb33SMartin Willi 		x->aalg = xfrm_algo_auth_clone(orig->aalg);
113580c9abaaSShinta Sugimoto 		if (!x->aalg)
113680c9abaaSShinta Sugimoto 			goto error;
113780c9abaaSShinta Sugimoto 	}
113880c9abaaSShinta Sugimoto 	x->props.aalgo = orig->props.aalgo;
113980c9abaaSShinta Sugimoto 
114080c9abaaSShinta Sugimoto 	if (orig->ealg) {
114180c9abaaSShinta Sugimoto 		x->ealg = xfrm_algo_clone(orig->ealg);
114280c9abaaSShinta Sugimoto 		if (!x->ealg)
114380c9abaaSShinta Sugimoto 			goto error;
114480c9abaaSShinta Sugimoto 	}
114580c9abaaSShinta Sugimoto 	x->props.ealgo = orig->props.ealgo;
114680c9abaaSShinta Sugimoto 
114780c9abaaSShinta Sugimoto 	if (orig->calg) {
114880c9abaaSShinta Sugimoto 		x->calg = xfrm_algo_clone(orig->calg);
114980c9abaaSShinta Sugimoto 		if (!x->calg)
115080c9abaaSShinta Sugimoto 			goto error;
115180c9abaaSShinta Sugimoto 	}
115280c9abaaSShinta Sugimoto 	x->props.calgo = orig->props.calgo;
115380c9abaaSShinta Sugimoto 
115480c9abaaSShinta Sugimoto 	if (orig->encap) {
115580c9abaaSShinta Sugimoto 		x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
115680c9abaaSShinta Sugimoto 		if (!x->encap)
115780c9abaaSShinta Sugimoto 			goto error;
115880c9abaaSShinta Sugimoto 	}
115980c9abaaSShinta Sugimoto 
116080c9abaaSShinta Sugimoto 	if (orig->coaddr) {
116180c9abaaSShinta Sugimoto 		x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
116280c9abaaSShinta Sugimoto 				    GFP_KERNEL);
116380c9abaaSShinta Sugimoto 		if (!x->coaddr)
116480c9abaaSShinta Sugimoto 			goto error;
116580c9abaaSShinta Sugimoto 	}
116680c9abaaSShinta Sugimoto 
1167af2f464eSSteffen Klassert 	if (orig->replay_esn) {
1168af2f464eSSteffen Klassert 		err = xfrm_replay_clone(x, orig);
1169af2f464eSSteffen Klassert 		if (err)
1170af2f464eSSteffen Klassert 			goto error;
1171af2f464eSSteffen Klassert 	}
1172af2f464eSSteffen Klassert 
1173bd55775cSJamal Hadi Salim 	memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1174bd55775cSJamal Hadi Salim 
117580c9abaaSShinta Sugimoto 	err = xfrm_init_state(x);
117680c9abaaSShinta Sugimoto 	if (err)
117780c9abaaSShinta Sugimoto 		goto error;
117880c9abaaSShinta Sugimoto 
117980c9abaaSShinta Sugimoto 	x->props.flags = orig->props.flags;
1180a947b0a9SNicolas Dichtel 	x->props.extra_flags = orig->props.extra_flags;
118180c9abaaSShinta Sugimoto 
118280c9abaaSShinta Sugimoto 	x->curlft.add_time = orig->curlft.add_time;
118380c9abaaSShinta Sugimoto 	x->km.state = orig->km.state;
118480c9abaaSShinta Sugimoto 	x->km.seq = orig->km.seq;
118580c9abaaSShinta Sugimoto 
118680c9abaaSShinta Sugimoto 	return x;
118780c9abaaSShinta Sugimoto 
118880c9abaaSShinta Sugimoto  error:
1189553f9118SHerbert Xu 	xfrm_state_put(x);
1190553f9118SHerbert Xu out:
119180c9abaaSShinta Sugimoto 	if (errp)
119280c9abaaSShinta Sugimoto 		*errp = err;
119380c9abaaSShinta Sugimoto 	return NULL;
119480c9abaaSShinta Sugimoto }
119580c9abaaSShinta Sugimoto 
1196283bc9f3SFan Du /* net->xfrm.xfrm_state_lock is held */
1197283bc9f3SFan Du struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
119880c9abaaSShinta Sugimoto {
119980c9abaaSShinta Sugimoto 	unsigned int h;
120080c9abaaSShinta Sugimoto 	struct xfrm_state *x;
120180c9abaaSShinta Sugimoto 
120280c9abaaSShinta Sugimoto 	if (m->reqid) {
1203283bc9f3SFan Du 		h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
120480c9abaaSShinta Sugimoto 				  m->reqid, m->old_family);
1205283bc9f3SFan Du 		hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
120680c9abaaSShinta Sugimoto 			if (x->props.mode != m->mode ||
120780c9abaaSShinta Sugimoto 			    x->id.proto != m->proto)
120880c9abaaSShinta Sugimoto 				continue;
120980c9abaaSShinta Sugimoto 			if (m->reqid && x->props.reqid != m->reqid)
121080c9abaaSShinta Sugimoto 				continue;
121170e94e66SYOSHIFUJI Hideaki / 吉藤英明 			if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
121280c9abaaSShinta Sugimoto 					     m->old_family) ||
121370e94e66SYOSHIFUJI Hideaki / 吉藤英明 			    !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
121480c9abaaSShinta Sugimoto 					     m->old_family))
121580c9abaaSShinta Sugimoto 				continue;
121680c9abaaSShinta Sugimoto 			xfrm_state_hold(x);
121780c9abaaSShinta Sugimoto 			return x;
121880c9abaaSShinta Sugimoto 		}
121980c9abaaSShinta Sugimoto 	} else {
1220283bc9f3SFan Du 		h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
122180c9abaaSShinta Sugimoto 				  m->old_family);
1222283bc9f3SFan Du 		hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
122380c9abaaSShinta Sugimoto 			if (x->props.mode != m->mode ||
122480c9abaaSShinta Sugimoto 			    x->id.proto != m->proto)
122580c9abaaSShinta Sugimoto 				continue;
122670e94e66SYOSHIFUJI Hideaki / 吉藤英明 			if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
122780c9abaaSShinta Sugimoto 					     m->old_family) ||
122870e94e66SYOSHIFUJI Hideaki / 吉藤英明 			    !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
122980c9abaaSShinta Sugimoto 					     m->old_family))
123080c9abaaSShinta Sugimoto 				continue;
123180c9abaaSShinta Sugimoto 			xfrm_state_hold(x);
123280c9abaaSShinta Sugimoto 			return x;
123380c9abaaSShinta Sugimoto 		}
123480c9abaaSShinta Sugimoto 	}
123580c9abaaSShinta Sugimoto 
123680c9abaaSShinta Sugimoto 	return NULL;
123780c9abaaSShinta Sugimoto }
123880c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_migrate_state_find);
123980c9abaaSShinta Sugimoto 
124080c9abaaSShinta Sugimoto struct xfrm_state * xfrm_state_migrate(struct xfrm_state *x,
124180c9abaaSShinta Sugimoto 				       struct xfrm_migrate *m)
124280c9abaaSShinta Sugimoto {
124380c9abaaSShinta Sugimoto 	struct xfrm_state *xc;
124480c9abaaSShinta Sugimoto 	int err;
124580c9abaaSShinta Sugimoto 
124680c9abaaSShinta Sugimoto 	xc = xfrm_state_clone(x, &err);
124780c9abaaSShinta Sugimoto 	if (!xc)
124880c9abaaSShinta Sugimoto 		return NULL;
124980c9abaaSShinta Sugimoto 
125080c9abaaSShinta Sugimoto 	memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
125180c9abaaSShinta Sugimoto 	memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
125280c9abaaSShinta Sugimoto 
125380c9abaaSShinta Sugimoto 	/* add state */
125470e94e66SYOSHIFUJI Hideaki / 吉藤英明 	if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
125580c9abaaSShinta Sugimoto 		/* a care is needed when the destination address of the
125680c9abaaSShinta Sugimoto 		   state is to be updated as it is a part of triplet */
125780c9abaaSShinta Sugimoto 		xfrm_state_insert(xc);
125880c9abaaSShinta Sugimoto 	} else {
125980c9abaaSShinta Sugimoto 		if ((err = xfrm_state_add(xc)) < 0)
126080c9abaaSShinta Sugimoto 			goto error;
126180c9abaaSShinta Sugimoto 	}
126280c9abaaSShinta Sugimoto 
126380c9abaaSShinta Sugimoto 	return xc;
126480c9abaaSShinta Sugimoto error:
126578347c8cSThomas Egerer 	xfrm_state_put(xc);
126680c9abaaSShinta Sugimoto 	return NULL;
126780c9abaaSShinta Sugimoto }
126880c9abaaSShinta Sugimoto EXPORT_SYMBOL(xfrm_state_migrate);
126980c9abaaSShinta Sugimoto #endif
127080c9abaaSShinta Sugimoto 
12711da177e4SLinus Torvalds int xfrm_state_update(struct xfrm_state *x)
12721da177e4SLinus Torvalds {
127337b08e34SDavid S. Miller 	struct xfrm_state *x1, *to_put;
12741da177e4SLinus Torvalds 	int err;
1275eb2971b6SMasahide NAKAMURA 	int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1276283bc9f3SFan Du 	struct net *net = xs_net(x);
12771da177e4SLinus Torvalds 
127837b08e34SDavid S. Miller 	to_put = NULL;
127937b08e34SDavid S. Miller 
1280283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1281edcd5821SDavid S. Miller 	x1 = __xfrm_state_locate(x, use_spi, x->props.family);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	err = -ESRCH;
12841da177e4SLinus Torvalds 	if (!x1)
12851da177e4SLinus Torvalds 		goto out;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 	if (xfrm_state_kern(x1)) {
128837b08e34SDavid S. Miller 		to_put = x1;
12891da177e4SLinus Torvalds 		err = -EEXIST;
12901da177e4SLinus Torvalds 		goto out;
12911da177e4SLinus Torvalds 	}
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	if (x1->km.state == XFRM_STATE_ACQ) {
12941da177e4SLinus Torvalds 		__xfrm_state_insert(x);
12951da177e4SLinus Torvalds 		x = NULL;
12961da177e4SLinus Torvalds 	}
12971da177e4SLinus Torvalds 	err = 0;
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds out:
1300283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
13011da177e4SLinus Torvalds 
130237b08e34SDavid S. Miller 	if (to_put)
130337b08e34SDavid S. Miller 		xfrm_state_put(to_put);
130437b08e34SDavid S. Miller 
13051da177e4SLinus Torvalds 	if (err)
13061da177e4SLinus Torvalds 		return err;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	if (!x) {
13091da177e4SLinus Torvalds 		xfrm_state_delete(x1);
13101da177e4SLinus Torvalds 		xfrm_state_put(x1);
13111da177e4SLinus Torvalds 		return 0;
13121da177e4SLinus Torvalds 	}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	err = -EINVAL;
13151da177e4SLinus Torvalds 	spin_lock_bh(&x1->lock);
13161da177e4SLinus Torvalds 	if (likely(x1->km.state == XFRM_STATE_VALID)) {
13171da177e4SLinus Torvalds 		if (x->encap && x1->encap)
13181da177e4SLinus Torvalds 			memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1319060f02a3SNoriaki TAKAMIYA 		if (x->coaddr && x1->coaddr) {
1320060f02a3SNoriaki TAKAMIYA 			memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1321060f02a3SNoriaki TAKAMIYA 		}
1322060f02a3SNoriaki TAKAMIYA 		if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1323060f02a3SNoriaki TAKAMIYA 			memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
13241da177e4SLinus Torvalds 		memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
13251da177e4SLinus Torvalds 		x1->km.dying = 0;
13261da177e4SLinus Torvalds 
13279e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
13281da177e4SLinus Torvalds 		if (x1->curlft.use_time)
13291da177e4SLinus Torvalds 			xfrm_state_check_expire(x1);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 		err = 0;
13328fcbc637STushar Gohad 		x->km.state = XFRM_STATE_DEAD;
13338fcbc637STushar Gohad 		__xfrm_state_put(x);
13341da177e4SLinus Torvalds 	}
13351da177e4SLinus Torvalds 	spin_unlock_bh(&x1->lock);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	xfrm_state_put(x1);
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 	return err;
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_update);
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds int xfrm_state_check_expire(struct xfrm_state *x)
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds 	if (!x->curlft.use_time)
13469d729f72SJames Morris 		x->curlft.use_time = get_seconds();
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	if (x->curlft.bytes >= x->lft.hard_byte_limit ||
13491da177e4SLinus Torvalds 	    x->curlft.packets >= x->lft.hard_packet_limit) {
13504666faabSHerbert Xu 		x->km.state = XFRM_STATE_EXPIRED;
13519e0d57fdSYury Polyanskiy 		tasklet_hrtimer_start(&x->mtimer, ktime_set(0,0), HRTIMER_MODE_REL);
13521da177e4SLinus Torvalds 		return -EINVAL;
13531da177e4SLinus Torvalds 	}
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	if (!x->km.dying &&
13561da177e4SLinus Torvalds 	    (x->curlft.bytes >= x->lft.soft_byte_limit ||
13574666faabSHerbert Xu 	     x->curlft.packets >= x->lft.soft_packet_limit)) {
13584666faabSHerbert Xu 		x->km.dying = 1;
135953bc6b4dSJamal Hadi Salim 		km_state_expired(x, 0, 0);
13604666faabSHerbert Xu 	}
13611da177e4SLinus Torvalds 	return 0;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_check_expire);
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds struct xfrm_state *
1366a70486f0SDavid S. Miller xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1367bd55775cSJamal Hadi Salim 		  u8 proto, unsigned short family)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	struct xfrm_state *x;
13701da177e4SLinus Torvalds 
1371283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1372bd55775cSJamal Hadi Salim 	x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1373283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
13741da177e4SLinus Torvalds 	return x;
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_lookup);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds struct xfrm_state *
1379bd55775cSJamal Hadi Salim xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1380a70486f0SDavid S. Miller 			 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1381eb2971b6SMasahide NAKAMURA 			 u8 proto, unsigned short family)
1382eb2971b6SMasahide NAKAMURA {
1383eb2971b6SMasahide NAKAMURA 	struct xfrm_state *x;
1384eb2971b6SMasahide NAKAMURA 
1385283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1386bd55775cSJamal Hadi Salim 	x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1387283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1388eb2971b6SMasahide NAKAMURA 	return x;
1389eb2971b6SMasahide NAKAMURA }
1390eb2971b6SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1391eb2971b6SMasahide NAKAMURA 
1392eb2971b6SMasahide NAKAMURA struct xfrm_state *
1393e473fcb4SMathias Krause xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1394e473fcb4SMathias Krause 	      u8 proto, const xfrm_address_t *daddr,
1395e473fcb4SMathias Krause 	      const xfrm_address_t *saddr, int create, unsigned short family)
13961da177e4SLinus Torvalds {
13971da177e4SLinus Torvalds 	struct xfrm_state *x;
13981da177e4SLinus Torvalds 
1399283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1400bd55775cSJamal Hadi Salim 	x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1401283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
14022770834cSDavid S. Miller 
14031da177e4SLinus Torvalds 	return x;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq);
14061da177e4SLinus Torvalds 
140741a49cc3SMasahide NAKAMURA #ifdef CONFIG_XFRM_SUB_POLICY
140841a49cc3SMasahide NAKAMURA int
140941a49cc3SMasahide NAKAMURA xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1410283bc9f3SFan Du 	       unsigned short family, struct net *net)
141141a49cc3SMasahide NAKAMURA {
141241a49cc3SMasahide NAKAMURA 	int err = 0;
141341a49cc3SMasahide NAKAMURA 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
141441a49cc3SMasahide NAKAMURA 	if (!afinfo)
141541a49cc3SMasahide NAKAMURA 		return -EAFNOSUPPORT;
141641a49cc3SMasahide NAKAMURA 
1417283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
141841a49cc3SMasahide NAKAMURA 	if (afinfo->tmpl_sort)
141941a49cc3SMasahide NAKAMURA 		err = afinfo->tmpl_sort(dst, src, n);
1420283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
142141a49cc3SMasahide NAKAMURA 	xfrm_state_put_afinfo(afinfo);
142241a49cc3SMasahide NAKAMURA 	return err;
142341a49cc3SMasahide NAKAMURA }
142441a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_tmpl_sort);
142541a49cc3SMasahide NAKAMURA 
142641a49cc3SMasahide NAKAMURA int
142741a49cc3SMasahide NAKAMURA xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
142841a49cc3SMasahide NAKAMURA 		unsigned short family)
142941a49cc3SMasahide NAKAMURA {
143041a49cc3SMasahide NAKAMURA 	int err = 0;
143141a49cc3SMasahide NAKAMURA 	struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1432283bc9f3SFan Du 	struct net *net = xs_net(*dst);
1433283bc9f3SFan Du 
143441a49cc3SMasahide NAKAMURA 	if (!afinfo)
143541a49cc3SMasahide NAKAMURA 		return -EAFNOSUPPORT;
143641a49cc3SMasahide NAKAMURA 
1437283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
143841a49cc3SMasahide NAKAMURA 	if (afinfo->state_sort)
143941a49cc3SMasahide NAKAMURA 		err = afinfo->state_sort(dst, src, n);
1440283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
144141a49cc3SMasahide NAKAMURA 	xfrm_state_put_afinfo(afinfo);
144241a49cc3SMasahide NAKAMURA 	return err;
144341a49cc3SMasahide NAKAMURA }
144441a49cc3SMasahide NAKAMURA EXPORT_SYMBOL(xfrm_state_sort);
144541a49cc3SMasahide NAKAMURA #endif
144641a49cc3SMasahide NAKAMURA 
14471da177e4SLinus Torvalds /* Silly enough, but I'm lazy to build resolution list */
14481da177e4SLinus Torvalds 
1449bd55775cSJamal Hadi Salim static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	int i;
14521da177e4SLinus Torvalds 
14535447c5e4SAlexey Dobriyan 	for (i = 0; i <= net->xfrm.state_hmask; i++) {
14548f126e37SDavid S. Miller 		struct xfrm_state *x;
14558f126e37SDavid S. Miller 
1456b67bfe0dSSasha Levin 		hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
14578f126e37SDavid S. Miller 			if (x->km.seq == seq &&
14583d6acfa7SJamal Hadi Salim 			    (mark & x->mark.m) == x->mark.v &&
14598f126e37SDavid S. Miller 			    x->km.state == XFRM_STATE_ACQ) {
14601da177e4SLinus Torvalds 				xfrm_state_hold(x);
14611da177e4SLinus Torvalds 				return x;
14621da177e4SLinus Torvalds 			}
14631da177e4SLinus Torvalds 		}
14641da177e4SLinus Torvalds 	}
14651da177e4SLinus Torvalds 	return NULL;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
1468bd55775cSJamal Hadi Salim struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
14691da177e4SLinus Torvalds {
14701da177e4SLinus Torvalds 	struct xfrm_state *x;
14711da177e4SLinus Torvalds 
1472283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
1473bd55775cSJamal Hadi Salim 	x = __xfrm_find_acq_byseq(net, mark, seq);
1474283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
14751da177e4SLinus Torvalds 	return x;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_find_acq_byseq);
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds u32 xfrm_get_acqseq(void)
14801da177e4SLinus Torvalds {
14811da177e4SLinus Torvalds 	u32 res;
14826836b9bdSjamal 	static atomic_t acqseq;
14831da177e4SLinus Torvalds 
14846836b9bdSjamal 	do {
14856836b9bdSjamal 		res = atomic_inc_return(&acqseq);
14866836b9bdSjamal 	} while (!res);
14876836b9bdSjamal 
14881da177e4SLinus Torvalds 	return res;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_get_acqseq);
14911da177e4SLinus Torvalds 
1492776e9dd9SFan Du int verify_spi_info(u8 proto, u32 min, u32 max)
1493776e9dd9SFan Du {
1494776e9dd9SFan Du 	switch (proto) {
1495776e9dd9SFan Du 	case IPPROTO_AH:
1496776e9dd9SFan Du 	case IPPROTO_ESP:
1497776e9dd9SFan Du 		break;
1498776e9dd9SFan Du 
1499776e9dd9SFan Du 	case IPPROTO_COMP:
1500776e9dd9SFan Du 		/* IPCOMP spi is 16-bits. */
1501776e9dd9SFan Du 		if (max >= 0x10000)
1502776e9dd9SFan Du 			return -EINVAL;
1503776e9dd9SFan Du 		break;
1504776e9dd9SFan Du 
1505776e9dd9SFan Du 	default:
1506776e9dd9SFan Du 		return -EINVAL;
1507776e9dd9SFan Du 	}
1508776e9dd9SFan Du 
1509776e9dd9SFan Du 	if (min > max)
1510776e9dd9SFan Du 		return -EINVAL;
1511776e9dd9SFan Du 
1512776e9dd9SFan Du 	return 0;
1513776e9dd9SFan Du }
1514776e9dd9SFan Du EXPORT_SYMBOL(verify_spi_info);
1515776e9dd9SFan Du 
1516658b219eSHerbert Xu int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
15171da177e4SLinus Torvalds {
1518221df1edSAlexey Dobriyan 	struct net *net = xs_net(x);
1519f034b5d4SDavid S. Miller 	unsigned int h;
15201da177e4SLinus Torvalds 	struct xfrm_state *x0;
1521658b219eSHerbert Xu 	int err = -ENOENT;
1522658b219eSHerbert Xu 	__be32 minspi = htonl(low);
1523658b219eSHerbert Xu 	__be32 maxspi = htonl(high);
1524bd55775cSJamal Hadi Salim 	u32 mark = x->mark.v & x->mark.m;
15251da177e4SLinus Torvalds 
1526658b219eSHerbert Xu 	spin_lock_bh(&x->lock);
1527658b219eSHerbert Xu 	if (x->km.state == XFRM_STATE_DEAD)
1528658b219eSHerbert Xu 		goto unlock;
1529658b219eSHerbert Xu 
1530658b219eSHerbert Xu 	err = 0;
15311da177e4SLinus Torvalds 	if (x->id.spi)
1532658b219eSHerbert Xu 		goto unlock;
1533658b219eSHerbert Xu 
1534658b219eSHerbert Xu 	err = -ENOENT;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 	if (minspi == maxspi) {
1537bd55775cSJamal Hadi Salim 		x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
15381da177e4SLinus Torvalds 		if (x0) {
15391da177e4SLinus Torvalds 			xfrm_state_put(x0);
1540658b219eSHerbert Xu 			goto unlock;
15411da177e4SLinus Torvalds 		}
15421da177e4SLinus Torvalds 		x->id.spi = minspi;
15431da177e4SLinus Torvalds 	} else {
15441da177e4SLinus Torvalds 		u32 spi = 0;
154526977b4eSAl Viro 		for (h=0; h<high-low+1; h++) {
154626977b4eSAl Viro 			spi = low + net_random()%(high-low+1);
1547bd55775cSJamal Hadi Salim 			x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
15481da177e4SLinus Torvalds 			if (x0 == NULL) {
15491da177e4SLinus Torvalds 				x->id.spi = htonl(spi);
15501da177e4SLinus Torvalds 				break;
15511da177e4SLinus Torvalds 			}
15521da177e4SLinus Torvalds 			xfrm_state_put(x0);
15531da177e4SLinus Torvalds 		}
15541da177e4SLinus Torvalds 	}
15551da177e4SLinus Torvalds 	if (x->id.spi) {
1556283bc9f3SFan Du 		spin_lock_bh(&net->xfrm.xfrm_state_lock);
155712604d8aSAlexey Dobriyan 		h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
155812604d8aSAlexey Dobriyan 		hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
1559283bc9f3SFan Du 		spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1560658b219eSHerbert Xu 
1561658b219eSHerbert Xu 		err = 0;
15621da177e4SLinus Torvalds 	}
1563658b219eSHerbert Xu 
1564658b219eSHerbert Xu unlock:
1565658b219eSHerbert Xu 	spin_unlock_bh(&x->lock);
1566658b219eSHerbert Xu 
1567658b219eSHerbert Xu 	return err;
15681da177e4SLinus Torvalds }
15691da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_alloc_spi);
15701da177e4SLinus Torvalds 
1571284fa7daSAlexey Dobriyan int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
15724c563f76STimo Teras 		    int (*func)(struct xfrm_state *, int, void*),
15731da177e4SLinus Torvalds 		    void *data)
15741da177e4SLinus Torvalds {
157512a169e7SHerbert Xu 	struct xfrm_state *state;
157612a169e7SHerbert Xu 	struct xfrm_state_walk *x;
15771da177e4SLinus Torvalds 	int err = 0;
15781da177e4SLinus Torvalds 
157912a169e7SHerbert Xu 	if (walk->seq != 0 && list_empty(&walk->all))
15804c563f76STimo Teras 		return 0;
15814c563f76STimo Teras 
1582283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
158312a169e7SHerbert Xu 	if (list_empty(&walk->all))
1584284fa7daSAlexey Dobriyan 		x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
158512a169e7SHerbert Xu 	else
158612a169e7SHerbert Xu 		x = list_entry(&walk->all, struct xfrm_state_walk, all);
1587284fa7daSAlexey Dobriyan 	list_for_each_entry_from(x, &net->xfrm.state_all, all) {
158812a169e7SHerbert Xu 		if (x->state == XFRM_STATE_DEAD)
15894c563f76STimo Teras 			continue;
159012a169e7SHerbert Xu 		state = container_of(x, struct xfrm_state, km);
159112a169e7SHerbert Xu 		if (!xfrm_id_proto_match(state->id.proto, walk->proto))
159294b9bb54SJamal Hadi Salim 			continue;
159312a169e7SHerbert Xu 		err = func(state, walk->seq, data);
15944c563f76STimo Teras 		if (err) {
159512a169e7SHerbert Xu 			list_move_tail(&walk->all, &x->all);
159694b9bb54SJamal Hadi Salim 			goto out;
159794b9bb54SJamal Hadi Salim 		}
159812a169e7SHerbert Xu 		walk->seq++;
15994c563f76STimo Teras 	}
160012a169e7SHerbert Xu 	if (walk->seq == 0) {
16011da177e4SLinus Torvalds 		err = -ENOENT;
16021da177e4SLinus Torvalds 		goto out;
16031da177e4SLinus Torvalds 	}
160412a169e7SHerbert Xu 	list_del_init(&walk->all);
16051da177e4SLinus Torvalds out:
1606283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
16071da177e4SLinus Torvalds 	return err;
16081da177e4SLinus Torvalds }
16091da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_walk);
16101da177e4SLinus Torvalds 
16115c182458SHerbert Xu void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto)
16125c182458SHerbert Xu {
161312a169e7SHerbert Xu 	INIT_LIST_HEAD(&walk->all);
16145c182458SHerbert Xu 	walk->proto = proto;
161512a169e7SHerbert Xu 	walk->state = XFRM_STATE_DEAD;
161612a169e7SHerbert Xu 	walk->seq = 0;
16175c182458SHerbert Xu }
16185c182458SHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_init);
16195c182458SHerbert Xu 
1620283bc9f3SFan Du void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1621abb81c4fSHerbert Xu {
162212a169e7SHerbert Xu 	if (list_empty(&walk->all))
16235c182458SHerbert Xu 		return;
16245c182458SHerbert Xu 
1625283bc9f3SFan Du 	spin_lock_bh(&net->xfrm.xfrm_state_lock);
162612a169e7SHerbert Xu 	list_del(&walk->all);
1627283bc9f3SFan Du 	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1628abb81c4fSHerbert Xu }
1629abb81c4fSHerbert Xu EXPORT_SYMBOL(xfrm_state_walk_done);
1630abb81c4fSHerbert Xu 
1631f8cd5488SJamal Hadi Salim static void xfrm_replay_timer_handler(unsigned long data)
1632f8cd5488SJamal Hadi Salim {
1633f8cd5488SJamal Hadi Salim 	struct xfrm_state *x = (struct xfrm_state*)data;
1634f8cd5488SJamal Hadi Salim 
1635f8cd5488SJamal Hadi Salim 	spin_lock(&x->lock);
1636f8cd5488SJamal Hadi Salim 
16372717096aSJamal Hadi Salim 	if (x->km.state == XFRM_STATE_VALID) {
1638a6483b79SAlexey Dobriyan 		if (xfrm_aevent_is_on(xs_net(x)))
16399fdc4883SSteffen Klassert 			x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
16402717096aSJamal Hadi Salim 		else
16412717096aSJamal Hadi Salim 			x->xflags |= XFRM_TIME_DEFER;
16422717096aSJamal Hadi Salim 	}
1643f8cd5488SJamal Hadi Salim 
1644f8cd5488SJamal Hadi Salim 	spin_unlock(&x->lock);
1645f8cd5488SJamal Hadi Salim }
1646f8cd5488SJamal Hadi Salim 
1647df01812eSDenis Cheng static LIST_HEAD(xfrm_km_list);
16481da177e4SLinus Torvalds 
1649214e005bSDavid S. Miller void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
16501da177e4SLinus Torvalds {
16511da177e4SLinus Torvalds 	struct xfrm_mgr *km;
16521da177e4SLinus Torvalds 
165385168c00SCong Wang 	rcu_read_lock();
165485168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list)
165526b15dadSJamal Hadi Salim 		if (km->notify_policy)
165626b15dadSJamal Hadi Salim 			km->notify_policy(xp, dir, c);
165785168c00SCong Wang 	rcu_read_unlock();
165826b15dadSJamal Hadi Salim }
165926b15dadSJamal Hadi Salim 
1660214e005bSDavid S. Miller void km_state_notify(struct xfrm_state *x, const struct km_event *c)
166126b15dadSJamal Hadi Salim {
166226b15dadSJamal Hadi Salim 	struct xfrm_mgr *km;
166385168c00SCong Wang 	rcu_read_lock();
166485168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list)
166526b15dadSJamal Hadi Salim 		if (km->notify)
166626b15dadSJamal Hadi Salim 			km->notify(x, c);
166785168c00SCong Wang 	rcu_read_unlock();
166826b15dadSJamal Hadi Salim }
166926b15dadSJamal Hadi Salim 
167026b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_policy_notify);
167126b15dadSJamal Hadi Salim EXPORT_SYMBOL(km_state_notify);
167226b15dadSJamal Hadi Salim 
167315e47304SEric W. Biederman void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
167426b15dadSJamal Hadi Salim {
167526b15dadSJamal Hadi Salim 	struct km_event c;
167626b15dadSJamal Hadi Salim 
1677bf08867fSHerbert Xu 	c.data.hard = hard;
167815e47304SEric W. Biederman 	c.portid = portid;
1679f60f6b8fSHerbert Xu 	c.event = XFRM_MSG_EXPIRE;
168026b15dadSJamal Hadi Salim 	km_state_notify(x, &c);
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
168353bc6b4dSJamal Hadi Salim EXPORT_SYMBOL(km_state_expired);
168426b15dadSJamal Hadi Salim /*
168526b15dadSJamal Hadi Salim  * We send to all registered managers regardless of failure
168626b15dadSJamal Hadi Salim  * We are happy with one success
168726b15dadSJamal Hadi Salim */
1688980ebd25SJamal Hadi Salim int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
16891da177e4SLinus Torvalds {
169026b15dadSJamal Hadi Salim 	int err = -EINVAL, acqret;
16911da177e4SLinus Torvalds 	struct xfrm_mgr *km;
16921da177e4SLinus Torvalds 
169385168c00SCong Wang 	rcu_read_lock();
169485168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
169565e0736bSFan Du 		acqret = km->acquire(x, t, pol);
169626b15dadSJamal Hadi Salim 		if (!acqret)
169726b15dadSJamal Hadi Salim 			err = acqret;
16981da177e4SLinus Torvalds 	}
169985168c00SCong Wang 	rcu_read_unlock();
17001da177e4SLinus Torvalds 	return err;
17011da177e4SLinus Torvalds }
1702980ebd25SJamal Hadi Salim EXPORT_SYMBOL(km_query);
17031da177e4SLinus Torvalds 
17045d36b180SAl Viro int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
17051da177e4SLinus Torvalds {
17061da177e4SLinus Torvalds 	int err = -EINVAL;
17071da177e4SLinus Torvalds 	struct xfrm_mgr *km;
17081da177e4SLinus Torvalds 
170985168c00SCong Wang 	rcu_read_lock();
171085168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
17111da177e4SLinus Torvalds 		if (km->new_mapping)
17121da177e4SLinus Torvalds 			err = km->new_mapping(x, ipaddr, sport);
17131da177e4SLinus Torvalds 		if (!err)
17141da177e4SLinus Torvalds 			break;
17151da177e4SLinus Torvalds 	}
171685168c00SCong Wang 	rcu_read_unlock();
17171da177e4SLinus Torvalds 	return err;
17181da177e4SLinus Torvalds }
17191da177e4SLinus Torvalds EXPORT_SYMBOL(km_new_mapping);
17201da177e4SLinus Torvalds 
172115e47304SEric W. Biederman void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
17221da177e4SLinus Torvalds {
172326b15dadSJamal Hadi Salim 	struct km_event c;
17241da177e4SLinus Torvalds 
1725bf08867fSHerbert Xu 	c.data.hard = hard;
172615e47304SEric W. Biederman 	c.portid = portid;
1727f60f6b8fSHerbert Xu 	c.event = XFRM_MSG_POLEXPIRE;
172826b15dadSJamal Hadi Salim 	km_policy_notify(pol, dir, &c);
17291da177e4SLinus Torvalds }
1730a70fcb0bSDavid S. Miller EXPORT_SYMBOL(km_policy_expired);
17311da177e4SLinus Torvalds 
17322d60abc2SEric Dumazet #ifdef CONFIG_XFRM_MIGRATE
1733183cad12SDavid S. Miller int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1734183cad12SDavid S. Miller 	       const struct xfrm_migrate *m, int num_migrate,
1735183cad12SDavid S. Miller 	       const struct xfrm_kmaddress *k)
173680c9abaaSShinta Sugimoto {
173780c9abaaSShinta Sugimoto 	int err = -EINVAL;
173880c9abaaSShinta Sugimoto 	int ret;
173980c9abaaSShinta Sugimoto 	struct xfrm_mgr *km;
174080c9abaaSShinta Sugimoto 
174185168c00SCong Wang 	rcu_read_lock();
174285168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
174380c9abaaSShinta Sugimoto 		if (km->migrate) {
174413c1d189SArnaud Ebalard 			ret = km->migrate(sel, dir, type, m, num_migrate, k);
174580c9abaaSShinta Sugimoto 			if (!ret)
174680c9abaaSShinta Sugimoto 				err = ret;
174780c9abaaSShinta Sugimoto 		}
174880c9abaaSShinta Sugimoto 	}
174985168c00SCong Wang 	rcu_read_unlock();
175080c9abaaSShinta Sugimoto 	return err;
175180c9abaaSShinta Sugimoto }
175280c9abaaSShinta Sugimoto EXPORT_SYMBOL(km_migrate);
17532d60abc2SEric Dumazet #endif
175480c9abaaSShinta Sugimoto 
1755db983c11SAlexey Dobriyan int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
175697a64b45SMasahide NAKAMURA {
175797a64b45SMasahide NAKAMURA 	int err = -EINVAL;
175897a64b45SMasahide NAKAMURA 	int ret;
175997a64b45SMasahide NAKAMURA 	struct xfrm_mgr *km;
176097a64b45SMasahide NAKAMURA 
176185168c00SCong Wang 	rcu_read_lock();
176285168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
176397a64b45SMasahide NAKAMURA 		if (km->report) {
1764db983c11SAlexey Dobriyan 			ret = km->report(net, proto, sel, addr);
176597a64b45SMasahide NAKAMURA 			if (!ret)
176697a64b45SMasahide NAKAMURA 				err = ret;
176797a64b45SMasahide NAKAMURA 		}
176897a64b45SMasahide NAKAMURA 	}
176985168c00SCong Wang 	rcu_read_unlock();
177097a64b45SMasahide NAKAMURA 	return err;
177197a64b45SMasahide NAKAMURA }
177297a64b45SMasahide NAKAMURA EXPORT_SYMBOL(km_report);
177397a64b45SMasahide NAKAMURA 
17741da177e4SLinus Torvalds int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
17751da177e4SLinus Torvalds {
17761da177e4SLinus Torvalds 	int err;
17771da177e4SLinus Torvalds 	u8 *data;
17781da177e4SLinus Torvalds 	struct xfrm_mgr *km;
17791da177e4SLinus Torvalds 	struct xfrm_policy *pol = NULL;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	if (optlen <= 0 || optlen > PAGE_SIZE)
17821da177e4SLinus Torvalds 		return -EMSGSIZE;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	data = kmalloc(optlen, GFP_KERNEL);
17851da177e4SLinus Torvalds 	if (!data)
17861da177e4SLinus Torvalds 		return -ENOMEM;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 	err = -EFAULT;
17891da177e4SLinus Torvalds 	if (copy_from_user(data, optval, optlen))
17901da177e4SLinus Torvalds 		goto out;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	err = -EINVAL;
179385168c00SCong Wang 	rcu_read_lock();
179485168c00SCong Wang 	list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1795cb969f07SVenkat Yekkirala 		pol = km->compile_policy(sk, optname, data,
17961da177e4SLinus Torvalds 					 optlen, &err);
17971da177e4SLinus Torvalds 		if (err >= 0)
17981da177e4SLinus Torvalds 			break;
17991da177e4SLinus Torvalds 	}
180085168c00SCong Wang 	rcu_read_unlock();
18011da177e4SLinus Torvalds 
18021da177e4SLinus Torvalds 	if (err >= 0) {
18031da177e4SLinus Torvalds 		xfrm_sk_policy_insert(sk, err, pol);
18041da177e4SLinus Torvalds 		xfrm_pol_put(pol);
18051da177e4SLinus Torvalds 		err = 0;
18061da177e4SLinus Torvalds 	}
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds out:
18091da177e4SLinus Torvalds 	kfree(data);
18101da177e4SLinus Torvalds 	return err;
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_user_policy);
18131da177e4SLinus Torvalds 
181485168c00SCong Wang static DEFINE_SPINLOCK(xfrm_km_lock);
181585168c00SCong Wang 
18161da177e4SLinus Torvalds int xfrm_register_km(struct xfrm_mgr *km)
18171da177e4SLinus Torvalds {
181885168c00SCong Wang 	spin_lock_bh(&xfrm_km_lock);
181985168c00SCong Wang 	list_add_tail_rcu(&km->list, &xfrm_km_list);
182085168c00SCong Wang 	spin_unlock_bh(&xfrm_km_lock);
18211da177e4SLinus Torvalds 	return 0;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_register_km);
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds int xfrm_unregister_km(struct xfrm_mgr *km)
18261da177e4SLinus Torvalds {
182785168c00SCong Wang 	spin_lock_bh(&xfrm_km_lock);
182885168c00SCong Wang 	list_del_rcu(&km->list);
182985168c00SCong Wang 	spin_unlock_bh(&xfrm_km_lock);
183085168c00SCong Wang 	synchronize_rcu();
18311da177e4SLinus Torvalds 	return 0;
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_unregister_km);
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
18361da177e4SLinus Torvalds {
18371da177e4SLinus Torvalds 	int err = 0;
18381da177e4SLinus Torvalds 	if (unlikely(afinfo == NULL))
18391da177e4SLinus Torvalds 		return -EINVAL;
18401da177e4SLinus Torvalds 	if (unlikely(afinfo->family >= NPROTO))
18411da177e4SLinus Torvalds 		return -EAFNOSUPPORT;
184244abdc30SCong Wang 	spin_lock_bh(&xfrm_state_afinfo_lock);
18431da177e4SLinus Torvalds 	if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
18441da177e4SLinus Torvalds 		err = -ENOBUFS;
1845edcd5821SDavid S. Miller 	else
184644abdc30SCong Wang 		rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
184744abdc30SCong Wang 	spin_unlock_bh(&xfrm_state_afinfo_lock);
18481da177e4SLinus Torvalds 	return err;
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_register_afinfo);
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
18531da177e4SLinus Torvalds {
18541da177e4SLinus Torvalds 	int err = 0;
18551da177e4SLinus Torvalds 	if (unlikely(afinfo == NULL))
18561da177e4SLinus Torvalds 		return -EINVAL;
18571da177e4SLinus Torvalds 	if (unlikely(afinfo->family >= NPROTO))
18581da177e4SLinus Torvalds 		return -EAFNOSUPPORT;
185944abdc30SCong Wang 	spin_lock_bh(&xfrm_state_afinfo_lock);
18601da177e4SLinus Torvalds 	if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
18611da177e4SLinus Torvalds 		if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
18621da177e4SLinus Torvalds 			err = -EINVAL;
1863edcd5821SDavid S. Miller 		else
186444abdc30SCong Wang 			RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
18651da177e4SLinus Torvalds 	}
186644abdc30SCong Wang 	spin_unlock_bh(&xfrm_state_afinfo_lock);
186744abdc30SCong Wang 	synchronize_rcu();
18681da177e4SLinus Torvalds 	return err;
18691da177e4SLinus Torvalds }
18701da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
18711da177e4SLinus Torvalds 
1872628e341fSHannes Frederic Sowa struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
18731da177e4SLinus Torvalds {
18741da177e4SLinus Torvalds 	struct xfrm_state_afinfo *afinfo;
18751da177e4SLinus Torvalds 	if (unlikely(family >= NPROTO))
18761da177e4SLinus Torvalds 		return NULL;
187744abdc30SCong Wang 	rcu_read_lock();
187844abdc30SCong Wang 	afinfo = rcu_dereference(xfrm_state_afinfo[family]);
1879546be240SHerbert Xu 	if (unlikely(!afinfo))
188044abdc30SCong Wang 		rcu_read_unlock();
18811da177e4SLinus Torvalds 	return afinfo;
18821da177e4SLinus Torvalds }
18831da177e4SLinus Torvalds 
1884628e341fSHannes Frederic Sowa void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
18851da177e4SLinus Torvalds {
188644abdc30SCong Wang 	rcu_read_unlock();
18871da177e4SLinus Torvalds }
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
18901da177e4SLinus Torvalds void xfrm_state_delete_tunnel(struct xfrm_state *x)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	if (x->tunnel) {
18931da177e4SLinus Torvalds 		struct xfrm_state *t = x->tunnel;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 		if (atomic_read(&t->tunnel_users) == 2)
18961da177e4SLinus Torvalds 			xfrm_state_delete(t);
18971da177e4SLinus Torvalds 		atomic_dec(&t->tunnel_users);
18981da177e4SLinus Torvalds 		xfrm_state_put(t);
18991da177e4SLinus Torvalds 		x->tunnel = NULL;
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds }
19021da177e4SLinus Torvalds EXPORT_SYMBOL(xfrm_state_delete_tunnel);
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds int xfrm_state_mtu(struct xfrm_state *x, int mtu)
19051da177e4SLinus Torvalds {
1906c5c25238SPatrick McHardy 	int res;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	spin_lock_bh(&x->lock);
19091da177e4SLinus Torvalds 	if (x->km.state == XFRM_STATE_VALID &&
1910c5c25238SPatrick McHardy 	    x->type && x->type->get_mtu)
1911c5c25238SPatrick McHardy 		res = x->type->get_mtu(x, mtu);
19121da177e4SLinus Torvalds 	else
191328121617SPatrick McHardy 		res = mtu - x->props.header_len;
19141da177e4SLinus Torvalds 	spin_unlock_bh(&x->lock);
19151da177e4SLinus Torvalds 	return res;
19161da177e4SLinus Torvalds }
19171da177e4SLinus Torvalds 
1918a454f0ccSWei Yongjun int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
191972cb6962SHerbert Xu {
1920d094cd83SHerbert Xu 	struct xfrm_state_afinfo *afinfo;
1921df9dcb45SKazunori MIYAZAWA 	struct xfrm_mode *inner_mode;
1922d094cd83SHerbert Xu 	int family = x->props.family;
192372cb6962SHerbert Xu 	int err;
192472cb6962SHerbert Xu 
1925d094cd83SHerbert Xu 	err = -EAFNOSUPPORT;
1926d094cd83SHerbert Xu 	afinfo = xfrm_state_get_afinfo(family);
1927d094cd83SHerbert Xu 	if (!afinfo)
1928d094cd83SHerbert Xu 		goto error;
1929d094cd83SHerbert Xu 
1930d094cd83SHerbert Xu 	err = 0;
1931d094cd83SHerbert Xu 	if (afinfo->init_flags)
1932d094cd83SHerbert Xu 		err = afinfo->init_flags(x);
1933d094cd83SHerbert Xu 
1934d094cd83SHerbert Xu 	xfrm_state_put_afinfo(afinfo);
1935d094cd83SHerbert Xu 
1936d094cd83SHerbert Xu 	if (err)
1937d094cd83SHerbert Xu 		goto error;
1938d094cd83SHerbert Xu 
1939d094cd83SHerbert Xu 	err = -EPROTONOSUPPORT;
1940df9dcb45SKazunori MIYAZAWA 
1941df9dcb45SKazunori MIYAZAWA 	if (x->sel.family != AF_UNSPEC) {
1942df9dcb45SKazunori MIYAZAWA 		inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
1943df9dcb45SKazunori MIYAZAWA 		if (inner_mode == NULL)
194413996378SHerbert Xu 			goto error;
194513996378SHerbert Xu 
1946df9dcb45SKazunori MIYAZAWA 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
1947df9dcb45SKazunori MIYAZAWA 		    family != x->sel.family) {
1948df9dcb45SKazunori MIYAZAWA 			xfrm_put_mode(inner_mode);
194913996378SHerbert Xu 			goto error;
1950df9dcb45SKazunori MIYAZAWA 		}
1951df9dcb45SKazunori MIYAZAWA 
1952df9dcb45SKazunori MIYAZAWA 		x->inner_mode = inner_mode;
1953df9dcb45SKazunori MIYAZAWA 	} else {
1954df9dcb45SKazunori MIYAZAWA 		struct xfrm_mode *inner_mode_iaf;
1955d81d2285SMartin Willi 		int iafamily = AF_INET;
1956df9dcb45SKazunori MIYAZAWA 
1957d81d2285SMartin Willi 		inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
1958df9dcb45SKazunori MIYAZAWA 		if (inner_mode == NULL)
1959df9dcb45SKazunori MIYAZAWA 			goto error;
1960df9dcb45SKazunori MIYAZAWA 
1961df9dcb45SKazunori MIYAZAWA 		if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
1962df9dcb45SKazunori MIYAZAWA 			xfrm_put_mode(inner_mode);
1963df9dcb45SKazunori MIYAZAWA 			goto error;
1964df9dcb45SKazunori MIYAZAWA 		}
1965df9dcb45SKazunori MIYAZAWA 		x->inner_mode = inner_mode;
1966d81d2285SMartin Willi 
1967d81d2285SMartin Willi 		if (x->props.family == AF_INET)
1968d81d2285SMartin Willi 			iafamily = AF_INET6;
1969d81d2285SMartin Willi 
1970d81d2285SMartin Willi 		inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
1971d81d2285SMartin Willi 		if (inner_mode_iaf) {
1972d81d2285SMartin Willi 			if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
1973df9dcb45SKazunori MIYAZAWA 				x->inner_mode_iaf = inner_mode_iaf;
1974d81d2285SMartin Willi 			else
1975d81d2285SMartin Willi 				xfrm_put_mode(inner_mode_iaf);
1976df9dcb45SKazunori MIYAZAWA 		}
1977df9dcb45SKazunori MIYAZAWA 	}
197813996378SHerbert Xu 
1979d094cd83SHerbert Xu 	x->type = xfrm_get_type(x->id.proto, family);
198072cb6962SHerbert Xu 	if (x->type == NULL)
198172cb6962SHerbert Xu 		goto error;
198272cb6962SHerbert Xu 
198372cb6962SHerbert Xu 	err = x->type->init_state(x);
198472cb6962SHerbert Xu 	if (err)
198572cb6962SHerbert Xu 		goto error;
198672cb6962SHerbert Xu 
198713996378SHerbert Xu 	x->outer_mode = xfrm_get_mode(x->props.mode, family);
1988599901c3SJulia Lawall 	if (x->outer_mode == NULL) {
1989599901c3SJulia Lawall 		err = -EPROTONOSUPPORT;
1990b59f45d0SHerbert Xu 		goto error;
1991599901c3SJulia Lawall 	}
1992b59f45d0SHerbert Xu 
1993a454f0ccSWei Yongjun 	if (init_replay) {
1994a454f0ccSWei Yongjun 		err = xfrm_init_replay(x);
1995a454f0ccSWei Yongjun 		if (err)
1996a454f0ccSWei Yongjun 			goto error;
1997a454f0ccSWei Yongjun 	}
1998a454f0ccSWei Yongjun 
199972cb6962SHerbert Xu 	x->km.state = XFRM_STATE_VALID;
200072cb6962SHerbert Xu 
200172cb6962SHerbert Xu error:
200272cb6962SHerbert Xu 	return err;
200372cb6962SHerbert Xu }
200472cb6962SHerbert Xu 
2005a454f0ccSWei Yongjun EXPORT_SYMBOL(__xfrm_init_state);
2006a454f0ccSWei Yongjun 
2007a454f0ccSWei Yongjun int xfrm_init_state(struct xfrm_state *x)
2008a454f0ccSWei Yongjun {
2009a454f0ccSWei Yongjun 	return __xfrm_init_state(x, true);
2010a454f0ccSWei Yongjun }
2011a454f0ccSWei Yongjun 
201272cb6962SHerbert Xu EXPORT_SYMBOL(xfrm_init_state);
20131da177e4SLinus Torvalds 
2014d62ddc21SAlexey Dobriyan int __net_init xfrm_state_init(struct net *net)
20151da177e4SLinus Torvalds {
2016f034b5d4SDavid S. Miller 	unsigned int sz;
20171da177e4SLinus Torvalds 
20189d4139c7SAlexey Dobriyan 	INIT_LIST_HEAD(&net->xfrm.state_all);
20199d4139c7SAlexey Dobriyan 
2020f034b5d4SDavid S. Miller 	sz = sizeof(struct hlist_head) * 8;
2021f034b5d4SDavid S. Miller 
202273d189dcSAlexey Dobriyan 	net->xfrm.state_bydst = xfrm_hash_alloc(sz);
202373d189dcSAlexey Dobriyan 	if (!net->xfrm.state_bydst)
202473d189dcSAlexey Dobriyan 		goto out_bydst;
2025d320bbb3SAlexey Dobriyan 	net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2026d320bbb3SAlexey Dobriyan 	if (!net->xfrm.state_bysrc)
2027d320bbb3SAlexey Dobriyan 		goto out_bysrc;
2028b754a4fdSAlexey Dobriyan 	net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2029b754a4fdSAlexey Dobriyan 	if (!net->xfrm.state_byspi)
2030b754a4fdSAlexey Dobriyan 		goto out_byspi;
2031529983ecSAlexey Dobriyan 	net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2032f034b5d4SDavid S. Miller 
20330bf7c5b0SAlexey Dobriyan 	net->xfrm.state_num = 0;
203463082733SAlexey Dobriyan 	INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2035b8a0ae20SAlexey Dobriyan 	INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
2036c7837144SAlexey Dobriyan 	INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
2037283bc9f3SFan Du 	spin_lock_init(&net->xfrm.xfrm_state_lock);
2038d62ddc21SAlexey Dobriyan 	return 0;
203973d189dcSAlexey Dobriyan 
2040b754a4fdSAlexey Dobriyan out_byspi:
2041b754a4fdSAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
2042d320bbb3SAlexey Dobriyan out_bysrc:
2043d320bbb3SAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bydst, sz);
204473d189dcSAlexey Dobriyan out_bydst:
204573d189dcSAlexey Dobriyan 	return -ENOMEM;
2046d62ddc21SAlexey Dobriyan }
2047d62ddc21SAlexey Dobriyan 
2048d62ddc21SAlexey Dobriyan void xfrm_state_fini(struct net *net)
2049d62ddc21SAlexey Dobriyan {
20507c2776eeSAlexey Dobriyan 	struct xfrm_audit audit_info;
205173d189dcSAlexey Dobriyan 	unsigned int sz;
205273d189dcSAlexey Dobriyan 
20537c2776eeSAlexey Dobriyan 	flush_work(&net->xfrm.state_hash_work);
2054e1760bd5SEric W. Biederman 	audit_info.loginuid = INVALID_UID;
20557c2776eeSAlexey Dobriyan 	audit_info.sessionid = -1;
20567c2776eeSAlexey Dobriyan 	audit_info.secid = 0;
20577c2776eeSAlexey Dobriyan 	xfrm_state_flush(net, IPSEC_PROTO_ANY, &audit_info);
20587c2776eeSAlexey Dobriyan 	flush_work(&net->xfrm.state_gc_work);
20597c2776eeSAlexey Dobriyan 
20609d4139c7SAlexey Dobriyan 	WARN_ON(!list_empty(&net->xfrm.state_all));
206173d189dcSAlexey Dobriyan 
2062529983ecSAlexey Dobriyan 	sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2063b754a4fdSAlexey Dobriyan 	WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2064b754a4fdSAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_byspi, sz);
2065d320bbb3SAlexey Dobriyan 	WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2066d320bbb3SAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
206773d189dcSAlexey Dobriyan 	WARN_ON(!hlist_empty(net->xfrm.state_bydst));
206873d189dcSAlexey Dobriyan 	xfrm_hash_free(net->xfrm.state_bydst, sz);
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
2071ab5f5e8bSJoy Latten #ifdef CONFIG_AUDITSYSCALL
2072cf35f43eSIlpo Järvinen static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2073ab5f5e8bSJoy Latten 				     struct audit_buffer *audit_buf)
2074ab5f5e8bSJoy Latten {
207568277accSPaul Moore 	struct xfrm_sec_ctx *ctx = x->security;
207668277accSPaul Moore 	u32 spi = ntohl(x->id.spi);
207768277accSPaul Moore 
207868277accSPaul Moore 	if (ctx)
2079ab5f5e8bSJoy Latten 		audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
208068277accSPaul Moore 				 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2081ab5f5e8bSJoy Latten 
2082ab5f5e8bSJoy Latten 	switch(x->props.family) {
2083ab5f5e8bSJoy Latten 	case AF_INET:
208421454aaaSHarvey Harrison 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
208521454aaaSHarvey Harrison 				 &x->props.saddr.a4, &x->id.daddr.a4);
2086ab5f5e8bSJoy Latten 		break;
2087ab5f5e8bSJoy Latten 	case AF_INET6:
20885b095d98SHarvey Harrison 		audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2089fdb46ee7SHarvey Harrison 				 x->props.saddr.a6, x->id.daddr.a6);
2090ab5f5e8bSJoy Latten 		break;
2091ab5f5e8bSJoy Latten 	}
209268277accSPaul Moore 
209368277accSPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2094ab5f5e8bSJoy Latten }
2095ab5f5e8bSJoy Latten 
2096cf35f43eSIlpo Järvinen static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2097afeb14b4SPaul Moore 				      struct audit_buffer *audit_buf)
2098afeb14b4SPaul Moore {
2099b71d1d42SEric Dumazet 	const struct iphdr *iph4;
2100b71d1d42SEric Dumazet 	const struct ipv6hdr *iph6;
2101afeb14b4SPaul Moore 
2102afeb14b4SPaul Moore 	switch (family) {
2103afeb14b4SPaul Moore 	case AF_INET:
2104afeb14b4SPaul Moore 		iph4 = ip_hdr(skb);
210521454aaaSHarvey Harrison 		audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
210621454aaaSHarvey Harrison 				 &iph4->saddr, &iph4->daddr);
2107afeb14b4SPaul Moore 		break;
2108afeb14b4SPaul Moore 	case AF_INET6:
2109afeb14b4SPaul Moore 		iph6 = ipv6_hdr(skb);
2110afeb14b4SPaul Moore 		audit_log_format(audit_buf,
21115b095d98SHarvey Harrison 				 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2112fdb46ee7SHarvey Harrison 				 &iph6->saddr,&iph6->daddr,
2113afeb14b4SPaul Moore 				 iph6->flow_lbl[0] & 0x0f,
2114afeb14b4SPaul Moore 				 iph6->flow_lbl[1],
2115afeb14b4SPaul Moore 				 iph6->flow_lbl[2]);
2116afeb14b4SPaul Moore 		break;
2117afeb14b4SPaul Moore 	}
2118afeb14b4SPaul Moore }
2119afeb14b4SPaul Moore 
212068277accSPaul Moore void xfrm_audit_state_add(struct xfrm_state *x, int result,
2121e1760bd5SEric W. Biederman 			  kuid_t auid, u32 sessionid, u32 secid)
2122ab5f5e8bSJoy Latten {
2123ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf;
2124ab5f5e8bSJoy Latten 
2125afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SAD-add");
2126ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
2127ab5f5e8bSJoy Latten 		return;
21282532386fSEric Paris 	xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2129afeb14b4SPaul Moore 	xfrm_audit_helper_sainfo(x, audit_buf);
2130afeb14b4SPaul Moore 	audit_log_format(audit_buf, " res=%u", result);
2131ab5f5e8bSJoy Latten 	audit_log_end(audit_buf);
2132ab5f5e8bSJoy Latten }
2133ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2134ab5f5e8bSJoy Latten 
213568277accSPaul Moore void xfrm_audit_state_delete(struct xfrm_state *x, int result,
2136e1760bd5SEric W. Biederman 			     kuid_t auid, u32 sessionid, u32 secid)
2137ab5f5e8bSJoy Latten {
2138ab5f5e8bSJoy Latten 	struct audit_buffer *audit_buf;
2139ab5f5e8bSJoy Latten 
2140afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SAD-delete");
2141ab5f5e8bSJoy Latten 	if (audit_buf == NULL)
2142ab5f5e8bSJoy Latten 		return;
21432532386fSEric Paris 	xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
2144afeb14b4SPaul Moore 	xfrm_audit_helper_sainfo(x, audit_buf);
2145afeb14b4SPaul Moore 	audit_log_format(audit_buf, " res=%u", result);
2146ab5f5e8bSJoy Latten 	audit_log_end(audit_buf);
2147ab5f5e8bSJoy Latten }
2148ab5f5e8bSJoy Latten EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2149afeb14b4SPaul Moore 
2150afeb14b4SPaul Moore void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2151afeb14b4SPaul Moore 				      struct sk_buff *skb)
2152afeb14b4SPaul Moore {
2153afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2154afeb14b4SPaul Moore 	u32 spi;
2155afeb14b4SPaul Moore 
2156afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-replay-overflow");
2157afeb14b4SPaul Moore 	if (audit_buf == NULL)
2158afeb14b4SPaul Moore 		return;
2159afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2160afeb14b4SPaul Moore 	/* don't record the sequence number because it's inherent in this kind
2161afeb14b4SPaul Moore 	 * of audit message */
2162afeb14b4SPaul Moore 	spi = ntohl(x->id.spi);
2163afeb14b4SPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2164afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2165afeb14b4SPaul Moore }
2166afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2167afeb14b4SPaul Moore 
21689fdc4883SSteffen Klassert void xfrm_audit_state_replay(struct xfrm_state *x,
2169afeb14b4SPaul Moore 			     struct sk_buff *skb, __be32 net_seq)
2170afeb14b4SPaul Moore {
2171afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2172afeb14b4SPaul Moore 	u32 spi;
2173afeb14b4SPaul Moore 
2174afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-replayed-pkt");
2175afeb14b4SPaul Moore 	if (audit_buf == NULL)
2176afeb14b4SPaul Moore 		return;
2177afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2178afeb14b4SPaul Moore 	spi = ntohl(x->id.spi);
2179afeb14b4SPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2180afeb14b4SPaul Moore 			 spi, spi, ntohl(net_seq));
2181afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2182afeb14b4SPaul Moore }
21839fdc4883SSteffen Klassert EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2184afeb14b4SPaul Moore 
2185afeb14b4SPaul Moore void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2186afeb14b4SPaul Moore {
2187afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2188afeb14b4SPaul Moore 
2189afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-notfound");
2190afeb14b4SPaul Moore 	if (audit_buf == NULL)
2191afeb14b4SPaul Moore 		return;
2192afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2193afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2194afeb14b4SPaul Moore }
2195afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2196afeb14b4SPaul Moore 
2197afeb14b4SPaul Moore void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2198afeb14b4SPaul Moore 			       __be32 net_spi, __be32 net_seq)
2199afeb14b4SPaul Moore {
2200afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2201afeb14b4SPaul Moore 	u32 spi;
2202afeb14b4SPaul Moore 
2203afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-notfound");
2204afeb14b4SPaul Moore 	if (audit_buf == NULL)
2205afeb14b4SPaul Moore 		return;
2206afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2207afeb14b4SPaul Moore 	spi = ntohl(net_spi);
2208afeb14b4SPaul Moore 	audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2209afeb14b4SPaul Moore 			 spi, spi, ntohl(net_seq));
2210afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2211afeb14b4SPaul Moore }
2212afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2213afeb14b4SPaul Moore 
2214afeb14b4SPaul Moore void xfrm_audit_state_icvfail(struct xfrm_state *x,
2215afeb14b4SPaul Moore 			      struct sk_buff *skb, u8 proto)
2216afeb14b4SPaul Moore {
2217afeb14b4SPaul Moore 	struct audit_buffer *audit_buf;
2218afeb14b4SPaul Moore 	__be32 net_spi;
2219afeb14b4SPaul Moore 	__be32 net_seq;
2220afeb14b4SPaul Moore 
2221afeb14b4SPaul Moore 	audit_buf = xfrm_audit_start("SA-icv-failure");
2222afeb14b4SPaul Moore 	if (audit_buf == NULL)
2223afeb14b4SPaul Moore 		return;
2224afeb14b4SPaul Moore 	xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2225afeb14b4SPaul Moore 	if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2226afeb14b4SPaul Moore 		u32 spi = ntohl(net_spi);
2227afeb14b4SPaul Moore 		audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2228afeb14b4SPaul Moore 				 spi, spi, ntohl(net_seq));
2229afeb14b4SPaul Moore 	}
2230afeb14b4SPaul Moore 	audit_log_end(audit_buf);
2231afeb14b4SPaul Moore }
2232afeb14b4SPaul Moore EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2233ab5f5e8bSJoy Latten #endif /* CONFIG_AUDITSYSCALL */
2234